V1 Updated
This commit is contained in:
parent
e6265238d9
commit
42e349e8a0
|
|
@ -0,0 +1,25 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class BasicAgent extends Agent {
|
||||||
|
|
||||||
|
public BasicAgent(int x, int y, Color color) {
|
||||||
|
super(x, y, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
|
// Example implementation: Move to a random neighboring position
|
||||||
|
int dx = (int) (Math.random() * 3) - 1; // random value -1, 0, or 1
|
||||||
|
int dy = (int) (Math.random() * 3) - 1; // random value -1, 0, or 1
|
||||||
|
|
||||||
|
// Update position
|
||||||
|
this.x += dx;
|
||||||
|
this.y += dy;
|
||||||
|
|
||||||
|
// Example condition for survival: always return true (agent never dies)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,56 +4,109 @@ import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
// example of basic animal.
|
|
||||||
// do not hesitate to make it more complex
|
|
||||||
// and DO add at least another species that interact with it
|
|
||||||
// for example wolves that eat Sheep
|
|
||||||
public class Sheep extends Agent {
|
public class Sheep extends Agent {
|
||||||
|
|
||||||
int hunger;
|
int hunger;
|
||||||
Random rand;
|
Random rand;
|
||||||
|
|
||||||
Sheep(int x,int y){
|
Sheep(int x, int y) {
|
||||||
//first we call the constructor of the superClass(Animal)
|
// Call the constructor of the superclass(Agent) with the specified values.
|
||||||
//with the values we want.
|
// Here we decide that a Sheep is initially white using this constructor.
|
||||||
// here we decide that a Sheep is initially white using this constructor
|
super(x, y, Color.white);
|
||||||
super(x,y,Color.white);
|
// Initialize the hunger value of the sheep to zero at birth.
|
||||||
// we give our sheep a hunger value of zero at birth
|
|
||||||
hunger = 0;
|
hunger = 0;
|
||||||
//we initialize the random number generator we will use to move randomly
|
// Initialize the random number generator we will use to move randomly.
|
||||||
rand = new Random();
|
rand = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* action of the animal
|
* Action of the sheep.
|
||||||
* it can interact with the cells or with other animals
|
* It can interact with the cells or with other animals.
|
||||||
* as you wish
|
|
||||||
*/
|
*/
|
||||||
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
if(world.getCell(x, y)==1) {
|
// If the cell where the sheep is located is grass, eat it and reset hunger to zero.
|
||||||
|
if (world.getCell(x, y) == 1) {
|
||||||
world.setCell(x, y, 0);
|
world.setCell(x, y, 0);
|
||||||
|
hunger = 0;
|
||||||
} else {
|
} else {
|
||||||
|
// Increment hunger if no grass is found.
|
||||||
hunger++;
|
hunger++;
|
||||||
}
|
}
|
||||||
this.moveRandom();
|
// Move the sheep randomly.
|
||||||
return hunger>10;
|
this.moveRandom(world);
|
||||||
|
// Return true if hunger is greater than 10, indicating the sheep dies.
|
||||||
|
return hunger > 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveRandom() {
|
private void moveRandom(Simulator world) {
|
||||||
int direction = rand.nextInt(4);
|
int direction = rand.nextInt(4);
|
||||||
if(direction == 0) {
|
// Move the sheep based on the random direction.
|
||||||
x+=1;
|
switch (direction) {
|
||||||
|
case 0:
|
||||||
|
x += 1; // Move right
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
y += 1; // Move down
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
x -= 1; // Move left
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
y -= 1; // Move up
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if(direction == 1) {
|
// Ensure the sheep stays within the bounds of the grid.
|
||||||
y+=1;
|
x = Math.max(0, Math.min(world.getWidth() - 1, x));
|
||||||
|
y = Math.max(0, Math.min(world.getHeight() - 1, y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Wolf extends Agent {
|
||||||
|
|
||||||
|
Random rand;
|
||||||
|
|
||||||
|
Wolf(int x, int y) {
|
||||||
|
// Call the constructor of the superclass(Agent) with the specified values.
|
||||||
|
// Here we decide that a Wolf is initially gray using this constructor.
|
||||||
|
super(x, y, Color.gray);
|
||||||
|
// Initialize the random number generator we will use to move randomly.
|
||||||
|
rand = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action of the wolf.
|
||||||
|
* It can interact with the cells or with other animals.
|
||||||
|
*/
|
||||||
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
|
// Move the wolf randomly.
|
||||||
|
this.moveRandom(world);
|
||||||
|
// Return true if the wolf always survives (example implementation).
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveRandom(Simulator world) {
|
||||||
|
int direction = rand.nextInt(4);
|
||||||
|
// Move the wolf based on the random direction.
|
||||||
|
switch (direction) {
|
||||||
|
case 0:
|
||||||
|
x += 1; // Move right
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
y += 1; // Move down
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
x -= 1; // Move left
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
y -= 1; // Move up
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Ensure the wolf stays within the bounds of the grid.
|
||||||
|
x = Math.max(0, Math.min(world.getWidth() - 1, x));
|
||||||
|
y = Math.max(0, Math.min(world.getHeight() - 1, y));
|
||||||
}
|
}
|
||||||
if(direction == 2) {
|
|
||||||
x-=1;
|
|
||||||
}
|
|
||||||
if(direction == 3) {
|
|
||||||
y-=1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
|
@ -11,9 +14,9 @@ public class Simulator extends Thread {
|
||||||
private final int COL_NUM = 100;
|
private final int COL_NUM = 100;
|
||||||
private final int LINE_NUM = 100;
|
private final int LINE_NUM = 100;
|
||||||
private final int LIFE_TYPE_NUM = 4;
|
private final int LIFE_TYPE_NUM = 4;
|
||||||
//Conway Radius : 1
|
// Conway Radius : 1
|
||||||
private final int LIFE_AREA_RADIUS = 1;
|
private final int LIFE_AREA_RADIUS = 1;
|
||||||
//Animal Neighborhood Radius : 5
|
// Animal Neighborhood Radius : 5
|
||||||
private final int ANIMAL_AREA_RADIUS = 2;
|
private final int ANIMAL_AREA_RADIUS = 2;
|
||||||
private ArrayList<Integer> fieldSurviveValues;
|
private ArrayList<Integer> fieldSurviveValues;
|
||||||
private ArrayList<Integer> fieldBirthValues;
|
private ArrayList<Integer> fieldBirthValues;
|
||||||
|
|
@ -26,44 +29,39 @@ public class Simulator extends Thread {
|
||||||
private boolean clickActionFlag;
|
private boolean clickActionFlag;
|
||||||
private int loopDelay = 150;
|
private int loopDelay = 150;
|
||||||
|
|
||||||
//TODO : add missing attribute(s)
|
private int[][] grid; // Adding the grid to represent cells in the world
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
mjf = mjfParam;
|
||||||
stopFlag=false;
|
stopFlag = false;
|
||||||
pauseFlag=false;
|
pauseFlag = false;
|
||||||
loopingBorder=false;
|
loopingBorder = false;
|
||||||
clickActionFlag=false;
|
clickActionFlag = false;
|
||||||
|
|
||||||
agents = new ArrayList<Agent>();
|
agents = new ArrayList<Agent>();
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
fieldSurviveValues = new ArrayList<Integer>();
|
||||||
|
|
||||||
//TODO : add missing attribute initialization
|
grid = new int[LINE_NUM][COL_NUM]; // Initializing the grid
|
||||||
|
|
||||||
|
// Default rule : Survive always, birth never
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
//Default rule : Survive always, birth never
|
|
||||||
for(int i =0; i<9; i++) {
|
|
||||||
fieldSurviveValues.add(i);
|
fieldSurviveValues.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
//TODO : replace with proper return
|
return COL_NUM;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
//TODO : replace with proper return
|
return LINE_NUM;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Should probably stay as is
|
// Should probably stay as is
|
||||||
public void run() {
|
public void run() {
|
||||||
int stepCount=0;
|
int stepCount = 0;
|
||||||
while(!stopFlag) {
|
while (!stopFlag) {
|
||||||
stepCount++;
|
stepCount++;
|
||||||
makeStep();
|
makeStep();
|
||||||
mjf.update(stepCount);
|
mjf.update(stepCount);
|
||||||
|
|
@ -72,7 +70,7 @@ public class Simulator extends Thread {
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
while(pauseFlag && !stopFlag) {
|
while (pauseFlag && !stopFlag) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(loopDelay);
|
Thread.sleep(loopDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
@ -80,7 +78,6 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -88,248 +85,196 @@ public class Simulator extends Thread {
|
||||||
* makes all the actions to go from one step to the other
|
* makes all the actions to go from one step to the other
|
||||||
*/
|
*/
|
||||||
public void makeStep() {
|
public void makeStep() {
|
||||||
// agent behaviors first
|
// Agent behaviors first
|
||||||
// only modify if sure of what you do
|
|
||||||
// to modify agent behavior, see liveTurn method
|
|
||||||
// in agent classes
|
|
||||||
Iterator<Agent> agentIterator = agents.iterator();
|
Iterator<Agent> agentIterator = agents.iterator();
|
||||||
while(agentIterator.hasNext()) {
|
while (agentIterator.hasNext()) {
|
||||||
Agent agent = agentIterator.next();
|
Agent agent = agentIterator.next();
|
||||||
ArrayList<Agent> neighbors =
|
ArrayList<Agent> neighbors = this.getNeighboringAnimals(
|
||||||
this.getNeighboringAnimals(
|
|
||||||
agent.getX(),
|
agent.getX(),
|
||||||
agent.getY(),
|
agent.getY(),
|
||||||
ANIMAL_AREA_RADIUS);
|
ANIMAL_AREA_RADIUS);
|
||||||
if(!agent.liveTurn(
|
if (!agent.liveTurn(neighbors, this)) {
|
||||||
neighbors,
|
|
||||||
this)) {
|
|
||||||
agentIterator.remove();
|
agentIterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//then evolution of the field
|
|
||||||
// TODO : apply game rule to all cells of the field
|
|
||||||
|
|
||||||
/* you should distribute this action in methods/classes
|
|
||||||
* don't write everything here !
|
|
||||||
*
|
|
||||||
* the idea is first to get the surrounding values
|
|
||||||
* then count how many are alive
|
|
||||||
* then check if that number is in the lists of rules
|
|
||||||
* if the cell is alive
|
|
||||||
* and the count is in the survive list,
|
|
||||||
* then the cell stays alive
|
|
||||||
* if the cell is not alive
|
|
||||||
* and the count is in the birth list,
|
|
||||||
* then the cell becomes alive
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Then evolution of the field
|
||||||
|
int[][] newGrid = new int[LINE_NUM][COL_NUM];
|
||||||
|
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
int aliveNeighbors = countAliveNeighbors(x, y);
|
||||||
|
if (grid[y][x] == 1) {
|
||||||
|
newGrid[y][x] = fieldSurviveValues.contains(aliveNeighbors) ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
newGrid[y][x] = fieldBirthValues.contains(aliveNeighbors) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
grid = newGrid; // Update the grid with new state
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countAliveNeighbors(int x, int y) {
|
||||||
|
int count = 0;
|
||||||
|
for (int dy = -1; dy <= 1; dy++) {
|
||||||
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
|
if (dx == 0 && dy == 0) continue;
|
||||||
|
int nx = x + dx;
|
||||||
|
int ny = y + dy;
|
||||||
|
if (nx >= 0 && nx < COL_NUM && ny >= 0 && ny < LINE_NUM) {
|
||||||
|
count += grid[ny][nx];
|
||||||
|
} else if (loopingBorder) {
|
||||||
|
nx = (nx + COL_NUM) % COL_NUM;
|
||||||
|
ny = (ny + LINE_NUM) % LINE_NUM;
|
||||||
|
count += grid[ny][nx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* leave this as is
|
|
||||||
*/
|
|
||||||
public void stopSimu() {
|
public void stopSimu() {
|
||||||
stopFlag=true;
|
stopFlag = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* method called when clicking pause button
|
|
||||||
*/
|
|
||||||
public void togglePause() {
|
public void togglePause() {
|
||||||
// TODO : actually toggle the corresponding flag
|
pauseFlag = !pauseFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* method called when clicking on a cell in the interface
|
|
||||||
*/
|
|
||||||
public void clickCell(int x, int y) {
|
public void clickCell(int x, int y) {
|
||||||
//TODO : complete method
|
grid[y][x] = (grid[y][x] == 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) {
|
public int getCell(int x, int y) {
|
||||||
//TODO : complete method with proper return
|
return grid[y][x];
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
*
|
public ArrayList<Agent> getAnimals() {
|
||||||
* @return list of Animals in simulated world
|
|
||||||
*/
|
|
||||||
public ArrayList<Agent> getAnimals(){
|
|
||||||
return agents;
|
return agents;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* selects Animals in a circular area of simulated world
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius) {
|
||||||
* @param x center
|
|
||||||
* @param y center
|
|
||||||
* @param radius
|
|
||||||
* @return list of agents in area
|
|
||||||
*/
|
|
||||||
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
|
|
||||||
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
||||||
for(int i=0;i<agents.size();i++) {
|
for (int i = 0; i < agents.size(); i++) {
|
||||||
Agent agent = agents.get(i);
|
Agent agent = agents.get(i);
|
||||||
if(agent.isInArea(x,y,radius)) {
|
if (agent.isInArea(x, y, radius)) {
|
||||||
inArea.add(agent);
|
inArea.add(agent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return inArea;
|
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) {
|
public void setCell(int x, int y, int val) {
|
||||||
//TODO : complete method
|
grid[y][x] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return lines of file representing
|
|
||||||
* the simulated world in its present state
|
|
||||||
*/
|
|
||||||
public ArrayList<String> getSaveState() {
|
public ArrayList<String> getSaveState() {
|
||||||
//TODO : complete method with proper return
|
ArrayList<String> state = new ArrayList<String>();
|
||||||
return null;
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
}
|
StringBuilder line = new StringBuilder();
|
||||||
/**
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
*
|
line.append(grid[y][x]).append(";");
|
||||||
* @param lines of file representing saved world state
|
|
||||||
*/
|
|
||||||
public void loadSaveState(ArrayList<String> lines) {
|
|
||||||
/*
|
|
||||||
* 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<lines.size();y++) {
|
|
||||||
String line = lines.get(y);
|
|
||||||
String[] lineElements = line.split(";");
|
|
||||||
for(int x=0; x<lineElements.length;x++) {
|
|
||||||
String elem = lineElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
|
||||||
setCell(x, y, value);
|
|
||||||
}
|
}
|
||||||
|
state.add(line.toString());
|
||||||
}
|
}
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* called by button, with slider providing the argument
|
|
||||||
* makes a new world state with random cell states
|
|
||||||
* @param chanceOfLife the chance for each cell
|
|
||||||
* to be alive in new state
|
|
||||||
*/
|
|
||||||
public void generateRandom(float chanceOfLife) {
|
public void generateRandom(float chanceOfLife) {
|
||||||
//TODO : complete method
|
Random rand = new Random();
|
||||||
/*
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
* Advice :
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
* as you should probably have a separate class
|
grid[y][x] = rand.nextFloat() < chanceOfLife ? 1 : 0;
|
||||||
* representing the field of cells...
|
}
|
||||||
* maybe just make a constructor in there
|
}
|
||||||
* and use it here
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
public boolean isLoopingBorder() {
|
||||||
//TODO : complete method with proper return
|
return loopingBorder;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleLoopingBorder() {
|
public void toggleLoopingBorder() {
|
||||||
//TODO : complete method
|
loopingBorder = !loopingBorder;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
public void setLoopDelay(int delay) {
|
||||||
//TODO : complete method
|
loopDelay = delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleClickAction() {
|
public void toggleClickAction() {
|
||||||
//TODO : complete method
|
clickActionFlag = !clickActionFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* prepare the content of a file saving present ruleSet
|
|
||||||
* as you might want to save a state,
|
|
||||||
* initialy written in this class constructor
|
|
||||||
* as a file for future use
|
|
||||||
* @return File content as an ArrayList of Lines (String)
|
|
||||||
* @see loadRule for inverse process
|
|
||||||
*/
|
|
||||||
public ArrayList<String> getRule() {
|
public ArrayList<String> getRule() {
|
||||||
//TODO : complete method with proper return
|
ArrayList<String> rule = new ArrayList<String>();
|
||||||
|
StringBuilder surviveLine = new StringBuilder();
|
||||||
|
for (int value : fieldSurviveValues) {
|
||||||
|
surviveLine.append(value).append(";");
|
||||||
|
}
|
||||||
|
rule.add(surviveLine.toString());
|
||||||
|
|
||||||
return null;
|
StringBuilder birthLine = new StringBuilder();
|
||||||
|
for (int value : fieldBirthValues) {
|
||||||
|
birthLine.append(value).append(";");
|
||||||
|
}
|
||||||
|
rule.add(birthLine.toString());
|
||||||
|
return rule;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
public void loadRule(ArrayList<String> lines) {
|
||||||
if(lines.size()<=0) {
|
if (lines.size() <= 0) {
|
||||||
System.out.println("empty rule file");
|
System.out.println("empty rule file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//TODO : remove previous rule (=emptying lists)
|
|
||||||
|
|
||||||
|
fieldSurviveValues.clear();
|
||||||
|
fieldBirthValues.clear();
|
||||||
|
|
||||||
String surviveLine = lines.get(0);
|
String surviveLine = lines.get(0);
|
||||||
String birthLine = lines.get(1);
|
String birthLine = lines.get(1);
|
||||||
String[] surviveElements = surviveLine.split(";");
|
String[] surviveElements = surviveLine.split(";");
|
||||||
for(int x=0; x<surviveElements.length;x++) {
|
for (String elem : surviveElements) {
|
||||||
String elem = surviveElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible survive values
|
fieldSurviveValues.add(value);
|
||||||
|
|
||||||
}
|
}
|
||||||
String[] birthElements = birthLine.split(";");
|
String[] birthElements = birthLine.split(";");
|
||||||
for(int x=0; x<birthElements.length;x++) {
|
for (String elem : birthElements) {
|
||||||
String elem = birthElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible birth values
|
fieldBirthValues.add(value);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getAgentsSave() {
|
public ArrayList<String> getAgentsSave() {
|
||||||
//TODO : Same idea as the other save method, but for agents
|
ArrayList<String> agentState = new ArrayList<String>();
|
||||||
return null;
|
for (Agent agent : agents) {
|
||||||
|
String state = agent.getX() + ";" + agent.getY() + ";" + agent.getDisplayColor().getRGB();
|
||||||
|
agentState.add(state);
|
||||||
|
}
|
||||||
|
return agentState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
public void loadAgents(ArrayList<String> stringArray) {
|
||||||
//TODO : Same idea as other load methods, but for agent list
|
agents.clear();
|
||||||
|
for (String line : stringArray) {
|
||||||
|
String[] elements = line.split(";");
|
||||||
|
int x = Integer.parseInt(elements[0]);
|
||||||
|
int y = Integer.parseInt(elements[1]);
|
||||||
|
Color color = new Color(Integer.parseInt(elements[2]));
|
||||||
|
agents.add(new BasicAgent(x, y, color));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadSaveState(ArrayList<String> stringArray) {
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
String[] line = stringArray.get(y).split(";");
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
grid[y][x] = Integer.parseInt(line[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* used by label in interface to show the active click action
|
|
||||||
* @return String representation of click action
|
|
||||||
*/
|
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
// TODO : initially return "sheep" or "cell"
|
return clickActionFlag ? "cell" : "sheep";
|
||||||
// depending on clickActionFlag
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue