From a16b1691d3bf5d69a45a01f573040ea479a612bd Mon Sep 17 00:00:00 2001 From: timeo Date: Tue, 28 May 2024 23:18:44 +0200 Subject: [PATCH] created classes rules and world and moved things there, and resolved pb with rand and density rand --- src/backend/Agent.java | 2 +- src/backend/Rules.java | 69 +++++++++++ src/backend/Sheep.java | 2 +- src/backend/Simulator.java | 172 +++++++++++---------------- src/backend/World.java | 64 ++++++++++ src/windowInterface/MyInterface.java | 37 ++++-- 6 files changed, 234 insertions(+), 112 deletions(-) create mode 100644 src/backend/Rules.java create mode 100644 src/backend/World.java diff --git a/src/backend/Agent.java b/src/backend/Agent.java index bc71b36..00d199f 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -34,7 +34,7 @@ public abstract class Agent { // then returns a boolean // if false, agent dies at end of turn // see step function in Simulator - public abstract boolean liveTurn(ArrayList neighbors, Simulator world); + public abstract boolean liveTurn(ArrayList neighbors, World world); } diff --git a/src/backend/Rules.java b/src/backend/Rules.java new file mode 100644 index 0000000..a979f9b --- /dev/null +++ b/src/backend/Rules.java @@ -0,0 +1,69 @@ +package backend; +import java.util.ArrayList; + +public class Rules { + + + private int [] survivalRulesArray; + private int[] birthRulesArray; + + + public Rules() { + + survivalRulesArray = new int[0]; + birthRulesArray = new int[0]; + } +//initialize my arrays to empty + + public int[] getBirthRulesArray() { + return this.birthRulesArray; + + } + + public int[] getSurvivalRulesArray() { + return this.survivalRulesArray; + + } + + public void loadRule(ArrayList row) { + if (row.size() <= 0) { + System.out.println("wrong file buddy, this one's empty"); + return; + + } + + + String surviveRulesRow = row.get(0); + String[] surviveCells = surviveRulesRow.split(";"); + if(surviveCells.length<=0) { + System.out.println("wrong file buddy, this one's does not have survival rules, won't work"); + return; + } + + String birthRulesRow = row.get(1); + String[] birthCells = birthRulesRow.split(";"); + +//places the values of our rules CSV file in the right category, as survival or birth rules + + + + survivalRulesArray = new int[surviveCells.length]; + birthRulesArray = new int[birthCells.length]; +//initialize my arrays with the correct length + + for (int x = 0; x < birthCells.length; x++) { + String elem = surviveCells[x]; + int value = Integer.parseInt(elem); + } +//determines the number of alive neighboring cells needed to birth, and places them in the neededNb list + + + for (int x = 0; x < surviveCells.length; x++) { + String elem = surviveCells[x]; + int value = Integer.parseInt(elem); + } +//determines the number of alive neighboring cells needed to survive, and places them in the neededNb list + + } + +} diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index b62af02..3c0f302 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -29,7 +29,7 @@ public class Sheep extends Agent { * it can interact with the cells or with other animals * as you wish */ - public boolean liveTurn(ArrayList neighbors, Simulator world) { + public boolean liveTurn(ArrayList neighbors, World world) { if(world.getCell(x, y)==1) { world.setCell(x, y, 0); } else { diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 2ff6ec6..d50f06a 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -5,14 +5,18 @@ import java.util.ArrayList; import java.util.Random; + import windowInterface.MyInterface; +import windowInterface.JPanelDraw; + public class Simulator extends Thread { private MyInterface mjf; + public Rules rules; - private final int COL_NUM = 100; - private final int LINE_NUM = 100; + //private final int COL_NUM = 100; + //private final int LINE_NUM = 100; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -22,19 +26,21 @@ public class Simulator extends Thread { private ArrayList fieldBirthValues; private ArrayList agents; + private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - private int[][] world; + private int[][] worldGrid; + private World world; //get the World instance //TODO : add missing attribute(s) private int stepCount; - public Simulator(MyInterface mjfParam) { + public Simulator(MyInterface mjfParam, int worldWidth, int worldHeight) { mjf = mjfParam; //stopFlag=false; //not necessary since i set the state when pressing the button start pauseFlag=false; @@ -44,8 +50,10 @@ public class Simulator extends Thread { agents = new ArrayList(); fieldBirthValues = new ArrayList(); fieldSurviveValues = new ArrayList(); - world =new int[getWidth()][getHeight()]; - //TODO : add missing attribute initialization + world =new World(worldWidth, worldHeight); //to initialize the world instance + worldGrid = new int[world.getWidth()][world.getHeight()]; + this.rules = new Rules(); + //TODO : add missing attribute initialization @@ -55,15 +63,28 @@ public class Simulator extends Thread { } } - + public int getWidth() { - return COL_NUM; + return world.getWidth(); } - + public int getHeight() { - return LINE_NUM; + return world.getHeight(); } - + + public World getActualWorld(){ + return world; + } + + public int getCell(int x, int y) { + return world.getCell(x, y); + } + + public void setWorld(World world) { + this.world = world; + } + + //Should probably stay as is public void run() { stepCount=0; @@ -101,11 +122,11 @@ public class Simulator extends Thread { int nx = x + dir[0]; int ny = y + dir[1]; if (nx >= 0 && nx < getWidth() && ny >= 0 && ny < getHeight()) { - count += world[nx][ny]; + count += worldGrid[nx][ny]; } else if (loopingBorder) { nx = (nx + getWidth()) % getWidth(); ny = (ny + getHeight()) % getHeight(); - count += world[nx][ny]; + count += worldGrid[nx][ny]; } } return count; @@ -122,18 +143,18 @@ public class Simulator extends Thread { int[][] newWorld = new int[getWidth()][getHeight()]; - ArrayList newAgents = new ArrayList<>(); + /*ArrayList newAgents = new ArrayList<>(); for(Agent agent : agents) { ArrayList neighbors = this.getNeighboringAnimals( agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);} - //if(!agent.liveTurn( - // neighbors, - // this)) { - // agents.remove(agent); - //{ + if(!agent.liveTurn( + neighbors, + this)) { + agents.remove(agent); + }*/ // Apply Game of Life rules @@ -150,18 +171,14 @@ public class Simulator extends Thread { - - //world = newWorld; - - - } - //then evolution of the field + //then evolution of the field + // TODO : apply game rule to all cells of the field /* you should distribute this action in methods/classes @@ -177,6 +194,9 @@ public class Simulator extends Thread { * and the count is in the birth list, * then the cell becomes alive */ + + } + @@ -204,20 +224,11 @@ public class Simulator extends Thread { * method called when clicking on a cell in the interface */ public void clickCell(int x, int y) { - setCell(x, y, getCell(x, y) == 1 ? 0 : 1); + world.setCell(x, y, getCell(x, y) == 1 ? 0 : 1); } - /** - * get cell value in simulated world - * @param x coordinate of cell - * @param y coordinate of cell - * @return value of cell - */ - public int getCell(int x, int y) { - return world[x][y]; - //get the value (dead or alive) of my cell at x y - } + /** * * @return list of Animals in simulated world @@ -243,16 +254,6 @@ public class Simulator extends Thread { return inArea; } - /** - * set value of cell - * @param x coord of cell - * @param y coord of cell - * @param val to set in cell - */ - public void setCell(int x, int y, int val) { - world [x][y] = val; - - } /** * @@ -304,7 +305,7 @@ public class Simulator extends Thread { for(int x=0; x lines) { //to check - /* - * First some checks that the file is usable - * We call early returns in conditions like this - * "Guard clauses", as they guard the method - * against unwanted inputs - */ - if(lines.size()<=0) { - return; - } - String firstLine = lines.get(0); - String[] firstLineElements = firstLine.split(";"); - if(firstLineElements.length<=0) { - return; - } - /* - * now we fill in the world - * with the content of the file - */ - for(int y =0; y stringArray = new ArrayList(); if (fileName.length()>0) { @@ -309,7 +326,7 @@ public class MyInterface extends JFrame { //to integrate the baseworld public void clicLoadFileButtonCSV(String fileName) { - Simulator loadedSim = new Simulator(this); + Simulator loadedSim = new Simulator(this, 100, 100); //String fileName="baseworld.csv"; ArrayList stringArray = new ArrayList(); if (fileName.length()>0) { @@ -354,7 +371,7 @@ public class MyInterface extends JFrame { } catch (Exception e) { e.printStackTrace(); } - mySimu.loadRule(stringArray); + mySimu.rules.loadRule(stringArray); this.repaint(); } } @@ -453,5 +470,11 @@ public class MyInterface extends JFrame { + + + + + + }