Final version code with comment
This commit is contained in:
parent
83fa6ae949
commit
1d9a1357c4
|
|
@ -1,2 +1,2 @@
|
||||||
2;3
|
[1, 3, 5, 8]
|
||||||
3
|
[3, 5, 7]
|
||||||
|
|
|
||||||
|
|
|
@ -7,34 +7,31 @@ public abstract class Agent {
|
||||||
protected int x;
|
protected int x;
|
||||||
protected int y;
|
protected int y;
|
||||||
protected Color color;
|
protected Color color;
|
||||||
|
|
||||||
protected Agent(int x, int y, Color color) {
|
protected Agent(int x, int y, Color color) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color getDisplayColor() {
|
public Color getDisplayColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getY() {
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInArea(int x, int y, int radius) {
|
public boolean isInArea(int x, int y, int radius) {
|
||||||
int diffX = this.x-x;
|
int diffX = this.x - x;
|
||||||
int diffY = this.y-y;
|
int diffY = this.y - y;
|
||||||
int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY));
|
int dist = (int) Math.floor(Math.sqrt(diffX * diffX + diffY * diffY));
|
||||||
return dist<radius;
|
return dist < radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does whatever the agent does during a step
|
|
||||||
// then returns a boolean
|
|
||||||
// if false, agent dies at end of turn
|
|
||||||
// see step function in Simulator
|
|
||||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
|
||||||
|
package backend;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Grid {
|
||||||
|
// Private fields to store the grid data and dimensions
|
||||||
|
private int[][] cells;
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
|
||||||
|
//Constructor to initialize the grid with given the width and height
|
||||||
|
public Grid(int width, int height) {
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.cells = new int[width][height];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Getter methods to retrieve the grid dimensions
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getter and setter methods for individual cells
|
||||||
|
public int getCell(int x, int y) {
|
||||||
|
return cells[x][y];
|
||||||
|
}
|
||||||
|
public void setCell(int x, int y, int val) {
|
||||||
|
cells[x][y] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Method to change the state of a cell (0=not colored, 1=colored)
|
||||||
|
public void toggleCell(int x, int y) {
|
||||||
|
cells[x][y] = (cells[x][y] == 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Method to save the current state of the grid to an ArrayList of strings
|
||||||
|
public ArrayList<String> saveState() {
|
||||||
|
ArrayList<String> state = new ArrayList<>();
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
StringBuilder row = new StringBuilder();
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
row.append(cells[x][y]).append(";");
|
||||||
|
}
|
||||||
|
state.add(row.toString());
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to load the state of the grid from an ArrayList of strings
|
||||||
|
public void loadState(ArrayList<String> lines) {
|
||||||
|
for (int x = 0; x < lines.size(); x++) {
|
||||||
|
String[] values = lines.get(x).split(";");
|
||||||
|
for (int y = 0; y < values.length; y++) {
|
||||||
|
cells[x][y] = Integer.parseInt(values[y]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to update the cells of the grid based on game of life rules
|
||||||
|
public void updateCells(ArrayList<Integer> surviveRules, ArrayList<Integer> birthRules, boolean loopingBorder) {
|
||||||
|
int[][] newCells = new int[width][height];
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
int neighbors = countNeighbors(x, y, loopingBorder);
|
||||||
|
if (cells[x][y] == 1) {
|
||||||
|
newCells[x][y] = (surviveRules.contains(neighbors)) ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
newCells[x][y] = (birthRules.contains(neighbors)) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cells = newCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Method to count the number of live neighbors of a cell
|
||||||
|
private int countNeighbors(int x, int y, boolean loopingBorder) {
|
||||||
|
int count = 0;
|
||||||
|
for (int i = -1; i <= 1; i++) {
|
||||||
|
for (int j = -1; j <= 1; j++) {
|
||||||
|
if (i == 0 && j == 0)
|
||||||
|
continue;
|
||||||
|
int nx = x + i;
|
||||||
|
int ny = y + j;
|
||||||
|
if (loopingBorder) {
|
||||||
|
nx = (nx + width) % width;
|
||||||
|
ny = (ny + height) % height;
|
||||||
|
}
|
||||||
|
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
|
||||||
|
count += cells[nx][ny];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,56 +4,22 @@ 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){
|
public Sheep(int x, int y) {
|
||||||
//first we call the constructor of the superClass(Animal)
|
super(x, y, Color.white);
|
||||||
//with the values we want.
|
|
||||||
// here we decide that a Sheep is initially white using this constructor
|
|
||||||
super(x,y,Color.white);
|
|
||||||
// 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
|
|
||||||
rand = new Random();
|
rand = new Random();
|
||||||
}
|
}
|
||||||
|
//The method replaces the superclass method animal
|
||||||
/**
|
@Override
|
||||||
* action of the animal
|
|
||||||
* 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) {
|
// To make the sheep moving randomly
|
||||||
world.setCell(x, y, 0);
|
x += rand.nextInt(3) - 1;
|
||||||
} else {
|
y += rand.nextInt(3) - 1;
|
||||||
hunger++;
|
hunger++;
|
||||||
}
|
return hunger < 20; // The sheep dies if it reaches maximum hunger
|
||||||
this.moveRandom();
|
|
||||||
return hunger>10;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveRandom() {
|
|
||||||
int direction = rand.nextInt(4);
|
|
||||||
if(direction == 0) {
|
|
||||||
x+=1;
|
|
||||||
}
|
|
||||||
if(direction == 1) {
|
|
||||||
y+=1;
|
|
||||||
}
|
|
||||||
if(direction == 2) {
|
|
||||||
x-=1;
|
|
||||||
}
|
|
||||||
if(direction == 3) {
|
|
||||||
y-=1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,68 +1,66 @@
|
||||||
package backend;
|
package backend;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.ArrayList;
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
// Simulator class that controls the lifecycle and rules of the simulation
|
||||||
public class Simulator extends Thread {
|
public class Simulator extends Thread {
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
|
private World world;
|
||||||
private final int COL_NUM = 100;
|
private ArrayList<Integer> surviveRules;
|
||||||
private final int LINE_NUM = 100;
|
private ArrayList<Integer> birthRules;
|
||||||
private final int LIFE_TYPE_NUM = 4;
|
|
||||||
//Conway Radius : 1
|
|
||||||
private final int LIFE_AREA_RADIUS = 1;
|
|
||||||
//Animal Neighborhood Radius : 5
|
|
||||||
private final int ANIMAL_AREA_RADIUS = 2;
|
|
||||||
private ArrayList<Integer> fieldSurviveValues;
|
|
||||||
private ArrayList<Integer> fieldBirthValues;
|
|
||||||
|
|
||||||
private ArrayList<Agent> agents;
|
|
||||||
|
|
||||||
private boolean stopFlag;
|
private boolean stopFlag;
|
||||||
private boolean pauseFlag;
|
private boolean pauseFlag;
|
||||||
private boolean loopingBorder;
|
private boolean loopingBorder;
|
||||||
private boolean clickActionFlag;
|
|
||||||
private int loopDelay = 150;
|
private int loopDelay = 150;
|
||||||
|
private int clickActionFlag;
|
||||||
|
|
||||||
//TODO : add missing attribute(s)
|
//The constructor initializes the simulator with a user interface parameter
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
this.mjf = mjfParam;
|
||||||
stopFlag=false;
|
this.world = new World(100, 100); // Set world size to 100x100
|
||||||
pauseFlag=false;
|
this.stopFlag = false;
|
||||||
loopingBorder=false;
|
this.pauseFlag = false;
|
||||||
clickActionFlag=false;
|
this.loopingBorder = false;
|
||||||
|
this.clickActionFlag = 0; // 0 for cell, 1 for agent
|
||||||
|
this.surviveRules = new ArrayList<>();
|
||||||
|
this.birthRules = new ArrayList<>();
|
||||||
|
initializeDefaultRules();
|
||||||
|
}
|
||||||
|
|
||||||
agents = new ArrayList<Agent>();
|
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
// Initialization of the default survival rules for the simulation
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
private void initializeDefaultRules() {
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
//TODO : add missing attribute initialization
|
surviveRules.add(i);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Default rule : Survive always, birth never
|
|
||||||
for(int i =0; i<9; i++) {
|
|
||||||
fieldSurviveValues.add(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// To retrieves neighbors of a specified agent in a certain radius
|
||||||
|
public ArrayList<Agent> getNeighbors(Agent agent) {
|
||||||
|
ArrayList<Agent> neighbors = new ArrayList<>();
|
||||||
|
for (Agent other : world.getAgents()) {
|
||||||
|
if (other != agent && agent.isInArea(other.getX(), other.getY(), 1)) { // Assuming radius of 1
|
||||||
|
neighbors.add(other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return neighbors;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getter methods for world dimensions
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
//TODO : replace with proper return
|
return world.getGrid().getWidth();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
//TODO : replace with proper return
|
return world.getGrid().getHeight();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Should probably stay as is
|
// The main run method for the thread that handles simulation steps
|
||||||
|
@Override
|
||||||
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);
|
||||||
|
|
@ -71,7 +69,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) {
|
||||||
|
|
@ -79,254 +77,159 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* method called at each step of the simulation
|
|
||||||
* makes all the actions to go from one step to the other
|
|
||||||
*/
|
|
||||||
public void makeStep() {
|
public void makeStep() {
|
||||||
// agent behaviors first
|
world.processAgentTurns(this);
|
||||||
// only modify if sure of what you do
|
world.updateGrid(surviveRules, birthRules, loopingBorder);
|
||||||
// to modify agent behavior, see liveTurn method
|
|
||||||
// in agent classes
|
|
||||||
for(Agent agent : agents) {
|
|
||||||
ArrayList<Agent> neighbors =
|
|
||||||
this.getNeighboringAnimals(
|
|
||||||
agent.getX(),
|
|
||||||
agent.getY(),
|
|
||||||
ANIMAL_AREA_RADIUS);
|
|
||||||
if(!agent.liveTurn(
|
|
||||||
neighbors,
|
|
||||||
this)) {
|
|
||||||
agents.remove(agent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//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
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* leave this as is
|
|
||||||
*/
|
// Stop the simulation
|
||||||
public void stopSimu() {
|
public void stopSimu() {
|
||||||
stopFlag=true;
|
stopFlag = true;
|
||||||
}
|
}
|
||||||
|
// Pause the simulation
|
||||||
/*
|
|
||||||
* method called when clicking pause button
|
|
||||||
*/
|
|
||||||
public void togglePause() {
|
public void togglePause() {
|
||||||
// TODO : actually toggle the corresponding flag
|
pauseFlag = !pauseFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
// Handles cell click actions based on the current clickActionFlag
|
||||||
* 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
|
if (clickActionFlag == 0) {
|
||||||
|
world.getGrid().toggleCell(x, y);
|
||||||
|
} else {
|
||||||
|
world.addAgent(new Sheep(x, y));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
// To returns the state of a cell at a given coordinate
|
||||||
* 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 world.getGrid().getCell(x, y);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
*
|
// To retrieves all animals in the simulation
|
||||||
* @return list of Animals in simulated world
|
public ArrayList<Agent> getAnimals() {
|
||||||
*/
|
return new ArrayList<>(world.getAgents());
|
||||||
public ArrayList<Agent> getAnimals(){
|
|
||||||
return agents;
|
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* selects Animals in a circular area of simulated world
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius) {
|
||||||
* @param x center
|
ArrayList<Agent> inArea = new ArrayList<>();
|
||||||
* @param y center
|
for (Agent agent : world.getAgents()) {
|
||||||
* @param radius
|
if (agent.isInArea(x, y, radius)) {
|
||||||
* @return list of agents in area
|
|
||||||
*/
|
|
||||||
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
|
|
||||||
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
|
||||||
for(int i=0;i<agents.size();i++) {
|
|
||||||
Agent agent = agents.get(i);
|
|
||||||
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
|
world.getGrid().setCell(x, y, 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
|
return world.getGrid().saveState();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param lines of file representing saved world state
|
|
||||||
*/
|
|
||||||
public void loadSaveState(ArrayList<String> lines) {
|
public void loadSaveState(ArrayList<String> lines) {
|
||||||
/*
|
world.getGrid().loadState(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
|
// Generates random initial states based on a given life chance
|
||||||
*/
|
public void generateRandom(float chanceOfLife) {
|
||||||
if(lines.size()<=0) {
|
Random rand = new Random();
|
||||||
return;
|
for (int x = 0; x < getWidth(); x++) {
|
||||||
|
for (int y = 0; y < getHeight(); y++) {
|
||||||
|
world.getGrid().setCell(x, y, (rand.nextFloat() < chanceOfLife) ? 1 : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
String firstLine = lines.get(0);
|
}
|
||||||
String[] firstLineElements = firstLine.split(";");
|
public boolean isLoopingBorder() {
|
||||||
if(firstLineElements.length<=0) {
|
return loopingBorder;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
/*
|
|
||||||
* now we fill in the world
|
// Switch between using or not using looping borders
|
||||||
* with the content of the file
|
public void toggleLoopingBorder() {
|
||||||
*/
|
loopingBorder = !loopingBorder;
|
||||||
for(int y =0; y<lines.size();y++) {
|
}
|
||||||
String line = lines.get(y);
|
|
||||||
String[] lineElements = line.split(";");
|
//Set the delay between the simulation loops
|
||||||
for(int x=0; x<lineElements.length;x++) {
|
public void setLoopDelay(int delay) {
|
||||||
String elem = lineElements[x];
|
loopDelay = delay;
|
||||||
int value = Integer.parseInt(elem);
|
}
|
||||||
setCell(x, y, value);
|
|
||||||
|
// Change the action performed when clicking in the world grid
|
||||||
|
public void toggleClickAction() {
|
||||||
|
clickActionFlag = (clickActionFlag == 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ArrayList<String> getRule() {
|
||||||
|
ArrayList<String> rules = new ArrayList<>();
|
||||||
|
rules.add(String.join(";", surviveRules.toString()));
|
||||||
|
rules.add(String.join(";", birthRules.toString()));
|
||||||
|
return rules;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void loadRule(ArrayList<String> lines) {
|
||||||
|
surviveRules.clear();
|
||||||
|
birthRules.clear();
|
||||||
|
if (!lines.isEmpty()) {
|
||||||
|
// Handle the survival rules from the first line
|
||||||
|
String[] survivalNumbers = extractNumbers(lines.get(0));
|
||||||
|
for (String num : survivalNumbers) {
|
||||||
|
if (!num.isEmpty()) {
|
||||||
|
surviveRules.add(Integer.parseInt(num.trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle the birth rules from the second line (if it exists)
|
||||||
|
if (lines.size() > 1) {
|
||||||
|
String[] birthNumbers = extractNumbers(lines.get(1));
|
||||||
|
for (String num : birthNumbers) {
|
||||||
|
if (!num.isEmpty()) {
|
||||||
|
birthRules.add(Integer.parseInt(num.trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
//TODO : complete method
|
|
||||||
/*
|
|
||||||
* Advice :
|
|
||||||
* as you should probably have a separate class
|
|
||||||
* representing the field of cells...
|
|
||||||
* maybe just make a constructor in there
|
|
||||||
* and use it here
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
|
||||||
//TODO : complete method with proper return
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleLoopingBorder() {
|
private String[] extractNumbers(String line) {
|
||||||
//TODO : complete method
|
line = line.trim().replaceAll("[\\[\\]]", ""); // Remove brackets if present
|
||||||
|
return line.split(",\\s*|;\\s*"); // Split by comma or semicolon with optional whitespace
|
||||||
}
|
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
|
||||||
//TODO : complete method
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleClickAction() {
|
|
||||||
//TODO : complete method
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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() {
|
|
||||||
//TODO : complete method with proper return
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
|
||||||
if(lines.size()<=0) {
|
|
||||||
System.out.println("empty rule file");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//TODO : remove previous rule (=emptying lists)
|
|
||||||
|
|
||||||
|
|
||||||
String surviveLine = lines.get(0);
|
|
||||||
String birthLine = lines.get(1);
|
|
||||||
String[] surviveElements = surviveLine.split(";");
|
|
||||||
for(int x=0; x<surviveElements.length;x++) {
|
|
||||||
String elem = surviveElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
|
||||||
//TODO : add value to possible survive values
|
|
||||||
|
|
||||||
}
|
|
||||||
String[] birthElements = birthLine.split(";");
|
|
||||||
for(int x=0; x<birthElements.length;x++) {
|
|
||||||
String elem = birthElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
|
||||||
//TODO : add value to possible birth values
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<String> getAgentsSave() {
|
public ArrayList<String> getAgentsSave() {
|
||||||
//TODO : Same idea as the other save method, but for agents
|
ArrayList<String> agentsList = new ArrayList<>();
|
||||||
return null;
|
for (Agent agent : world.getAgents()) {
|
||||||
|
agentsList.add(agent.getClass().getSimpleName() + "," + agent.getX() + "," + agent.getY());
|
||||||
|
}
|
||||||
|
return agentsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
|
||||||
//TODO : Same idea as other load methods, but for agent list
|
public void loadAgents(ArrayList<String> lines) {
|
||||||
|
world.getAgents().clear();
|
||||||
|
for (String line : lines) {
|
||||||
|
String[] data = line.split(";"); // Split using semicolons
|
||||||
|
String type = data[0].trim(); // Trim whitespace
|
||||||
|
int x = Integer.parseInt(data[1].trim().replace(",", "")); // Remove commas and trim spaces
|
||||||
|
int y = Integer.parseInt(data[2].trim().replace(",", "")); // Remove commas and trim spaces
|
||||||
|
if (type.equals("Sheep")) {
|
||||||
|
world.addAgent(new Sheep(x, y));
|
||||||
|
} else if (type.equals("Wolf")) {
|
||||||
|
world.addAgent(new Wolf(x, y));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 == 0 ? "cell" : "sheep";
|
||||||
// depending on clickActionFlag
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
// We created a class for another animal : wolves
|
||||||
|
// They eat sheeps
|
||||||
|
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Wolf extends Agent {
|
||||||
|
int hunger;
|
||||||
|
Random rand;
|
||||||
|
|
||||||
|
|
||||||
|
public Wolf(int x, int y) {
|
||||||
|
// We call the constructor of the superClass animal
|
||||||
|
// Ande we decided that the wolves are gray
|
||||||
|
super(x, y, Color.gray);
|
||||||
|
// At birth, their hunger value is zero
|
||||||
|
hunger = 0;
|
||||||
|
//And we initialize the random number generator for their random movement
|
||||||
|
rand = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
//The method replaces the superclass method animal
|
||||||
|
@Override
|
||||||
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
|
for (Agent neighbor : neighbors) {
|
||||||
|
if (neighbor instanceof Sheep) {
|
||||||
|
// To reset their hunger after eating
|
||||||
|
hunger = 0;
|
||||||
|
// And wolf survives by eating sheep
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// To make them move randomly
|
||||||
|
x += rand.nextInt(3) - 1;
|
||||||
|
y += rand.nextInt(3) - 1;
|
||||||
|
hunger++;
|
||||||
|
//The wolve dies if it starves
|
||||||
|
return hunger < 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package backend;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
|
public class World {
|
||||||
|
// Private fields to store the grid and the agents
|
||||||
|
private Grid grid;
|
||||||
|
private Set<Agent> agents;
|
||||||
|
|
||||||
|
|
||||||
|
// Constructor to initialize the world with the grid and an empty set of agents
|
||||||
|
public World(int width, int height) {
|
||||||
|
this.grid = new Grid(width, height);
|
||||||
|
this.agents = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Getter method for the grid
|
||||||
|
public Grid getGrid() {
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
//Getter method for the set of agents
|
||||||
|
public Set<Agent> getAgents() {
|
||||||
|
return agents;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Method to add an agent to the world
|
||||||
|
public void addAgent(Agent agent) {
|
||||||
|
agents.add(agent);
|
||||||
|
}
|
||||||
|
// Method to remove it
|
||||||
|
public void removeAgent(Agent agent) {
|
||||||
|
agents.remove(agent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Method to update the grid based on game of life rules
|
||||||
|
public void updateGrid(ArrayList<Integer> surviveRules, ArrayList<Integer> birthRules, boolean loopingBorder) {
|
||||||
|
grid.updateCells(surviveRules, birthRules, loopingBorder);
|
||||||
|
}
|
||||||
|
// Method to process turns for all agents and return the surviving ones
|
||||||
|
public Set<Agent> processAgentTurns(Simulator simulator) {
|
||||||
|
Set<Agent> survivingAgents = new HashSet<>();
|
||||||
|
for (Agent agent : agents) {
|
||||||
|
|
||||||
|
// Call liveTurn method for each agent and add surviving ones to the set
|
||||||
|
if (agent.liveTurn(simulator.getNeighbors(agent), simulator)) {
|
||||||
|
survivingAgents.add(agent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return survivingAgents;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue