Version finale
This commit is contained in:
parent
160aea46c4
commit
ec97401b75
|
|
@ -0,0 +1,29 @@
|
|||
package backend;
|
||||
|
||||
|
||||
public class Cell {
|
||||
private int state;
|
||||
|
||||
public Cell(int state) {
|
||||
this.setState(state);
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
if (state == 4) {
|
||||
this.state = -3;
|
||||
}
|
||||
}
|
||||
|
||||
public void age() {
|
||||
if (state > 0 && state < 4) {
|
||||
state = Math.min(state + 1, 3);
|
||||
} else if (state < 0) {
|
||||
this.state++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package backend;
|
||||
|
||||
|
||||
public class Grid {
|
||||
private int[][] grid;
|
||||
|
||||
// Constructor to initialize the grid with given dimensions
|
||||
public Grid(int rows, int columns) {
|
||||
grid = new int[rows][columns];
|
||||
}
|
||||
|
||||
// Method to set the value of a cell at specified coordinates
|
||||
public void setValue(int x, int y, int value) {
|
||||
grid[x][y] = value;
|
||||
}
|
||||
|
||||
// Method to get the value of a cell at specified coordinates
|
||||
public int getValue(int x, int y) {
|
||||
return grid[x][y];
|
||||
}
|
||||
|
||||
// Method to get the number of rows in the grid
|
||||
public int getRowCount() {
|
||||
return grid.length;
|
||||
}
|
||||
|
||||
// Method to get the number of columns in the grid
|
||||
public int getColumnCount() {
|
||||
return grid[0].length;
|
||||
}
|
||||
|
||||
// Additional method to get the Cell object for logic purposes
|
||||
public Cell getCell(int x, int y) {
|
||||
return new Cell(grid[x][y]);
|
||||
}
|
||||
|
||||
// Additional method to set the Cell object state
|
||||
public void setCell(int x, int y, Cell cell) {
|
||||
grid[x][y] = cell.getState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,332 +1,458 @@
|
|||
package backend;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
public class Simulator extends Thread {
|
||||
|
||||
private MyInterface mjf;
|
||||
|
||||
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;
|
||||
//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 pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
private boolean clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
private MyInterface mjf;
|
||||
|
||||
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;
|
||||
//Animal Neighborhood Radius : 5
|
||||
private final int ANIMAL_AREA_RADIUS = 2;
|
||||
|
||||
private List<List<Integer>> survivalRules;
|
||||
private List<List<Integer>> birthRules;
|
||||
|
||||
private ArrayList<Agent> agents;
|
||||
|
||||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
private String clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
|
||||
private Grid grid;
|
||||
|
||||
//TODO : add missing attribute(s)
|
||||
//TODO : add missing attribute(s)
|
||||
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
stopFlag=false;
|
||||
pauseFlag=false;
|
||||
loopingBorder=false;
|
||||
clickActionFlag=false;
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
stopFlag=false;
|
||||
pauseFlag=false;
|
||||
loopingBorder=false;
|
||||
clickActionFlag="Cells";
|
||||
|
||||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
agents = new ArrayList<Agent>();
|
||||
|
||||
//TODO : add missing attribute initialization
|
||||
|
||||
|
||||
|
||||
//Default rule : Survive always, birth never
|
||||
for(int i =0; i<9; i++) {
|
||||
fieldSurviveValues.add(i);
|
||||
}
|
||||
|
||||
}
|
||||
// Initialize rules
|
||||
survivalRules = new ArrayList<>();
|
||||
birthRules = new ArrayList<>();
|
||||
|
||||
public int getWidth() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
}
|
||||
// Define survival rules for each cell state
|
||||
survivalRules.add(Arrays.asList()); // State 0 (Dead cells don't survive)
|
||||
survivalRules.add(Arrays.asList(2, 3)); // State 1 (Young cells)
|
||||
survivalRules.add(Arrays.asList(2, 3, 4)); // State 2 (Middle-aged cells)
|
||||
survivalRules.add(Arrays.asList(1, 2, 3)); // State 3 (Old cells)
|
||||
|
||||
public int getHeight() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
}
|
||||
// Define birth rules for each cell state
|
||||
birthRules.add(Arrays.asList(3)); // State 0 (Dead cells birth to young cells)
|
||||
birthRules.add(Arrays.asList()); // State 1 (Young cells don't give birth)
|
||||
birthRules.add(Arrays.asList()); // State 2 (Middle-aged cells don't give birth)
|
||||
birthRules.add(Arrays.asList(4)); // State 3 (Old cells give birth to young cells)
|
||||
|
||||
//Should probably stay as is
|
||||
public void run() {
|
||||
int stepCount=0;
|
||||
while(!stopFlag) {
|
||||
stepCount++;
|
||||
makeStep();
|
||||
mjf.update(stepCount);
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while(pauseFlag && !stopFlag) {
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
grid = new Grid(LINE_NUM, COL_NUM);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public int getWidth() {
|
||||
return LINE_NUM;
|
||||
}
|
||||
|
||||
/**
|
||||
* method called at each step of the simulation
|
||||
* makes all the actions to go from one step to the other
|
||||
*/
|
||||
public void makeStep() {
|
||||
// agent behaviors first
|
||||
// only modify if sure of what you do
|
||||
// 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
|
||||
*/
|
||||
public void stopSimu() {
|
||||
stopFlag=true;
|
||||
}
|
||||
|
||||
/*
|
||||
* method called when clicking pause button
|
||||
*/
|
||||
public void togglePause() {
|
||||
// TODO : actually toggle the corresponding flag
|
||||
}
|
||||
|
||||
/**
|
||||
* method called when clicking on a cell in the interface
|
||||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
//TODO : complete method
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
//TODO : complete method with proper return
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return list of Animals in simulated world
|
||||
*/
|
||||
public ArrayList<Agent> getAnimals(){
|
||||
return agents;
|
||||
}
|
||||
/**
|
||||
* selects Animals in a circular area of simulated world
|
||||
* @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>();
|
||||
for(int i=0;i<agents.size();i++) {
|
||||
Agent agent = agents.get(i);
|
||||
if(agent.isInArea(x,y,radius)) {
|
||||
inArea.add(agent);
|
||||
}
|
||||
}
|
||||
return inArea;
|
||||
}
|
||||
public int getHeight() {
|
||||
return COL_NUM;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
//TODO : complete method
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return lines of file representing
|
||||
* the simulated world in its present state
|
||||
*/
|
||||
public ArrayList<String> getSaveState() {
|
||||
//TODO : complete method with proper return
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Should probably stay as is
|
||||
public void run() {
|
||||
int stepCount=0;
|
||||
while(!stopFlag) {
|
||||
stepCount++;
|
||||
makeStep();
|
||||
mjf.update(stepCount);
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while(pauseFlag && !stopFlag) {
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
//TODO : complete method
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* method called at each step of the simulation
|
||||
* makes all the actions to go from one step to the other
|
||||
*/
|
||||
public void makeStep() {
|
||||
// Agent behaviors first
|
||||
for(Agent agent : agents) {
|
||||
ArrayList<Agent> neighbors =
|
||||
this.getNeighboringAnimals(
|
||||
agent.getX(),
|
||||
agent.getY(),
|
||||
ANIMAL_AREA_RADIUS);
|
||||
if(!agent.liveTurn(
|
||||
neighbors,
|
||||
this)) {
|
||||
agents.remove(agent);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
//TODO : Same idea as the other save method, but for agents
|
||||
return null;
|
||||
}
|
||||
// Then evolution of the field
|
||||
Grid nextGrid = new Grid(LINE_NUM, COL_NUM);
|
||||
|
||||
public void loadAgents(ArrayList<String> stringArray) {
|
||||
//TODO : Same idea as other load methods, but for agent list
|
||||
|
||||
}
|
||||
for (int i = 0; i < LINE_NUM; i++) {
|
||||
for (int j = 0; j < COL_NUM; j++) {
|
||||
Cell cell = grid.getCell(i, j);
|
||||
int liveNeighbors = countLiveNeighbors(i, j);
|
||||
int cellState = cell.getState();
|
||||
|
||||
/**
|
||||
* used by label in interface to show the active click action
|
||||
* @return String representation of click action
|
||||
*/
|
||||
public String clickActionName() {
|
||||
// TODO : initially return "sheep" or "cell"
|
||||
// depending on clickActionFlag
|
||||
return "";
|
||||
}
|
||||
if (cellState > 0 && cellState < 4) { // For living cells
|
||||
if (survivalRules.get(cellState).contains(liveNeighbors)) {
|
||||
cell.age();
|
||||
nextGrid.setCell(i, j, cell);
|
||||
} else {
|
||||
nextGrid.setValue(i, j, 0);
|
||||
}
|
||||
} else if (cellState < 0) {
|
||||
cell.age();
|
||||
|
||||
if (cell.getState() == 0) {
|
||||
nextGrid.setValue(i, j, 0);
|
||||
} else {
|
||||
nextGrid.setCell(i, j, cell);
|
||||
}
|
||||
} else {
|
||||
if (birthRules.get(cellState).contains(liveNeighbors)) {
|
||||
nextGrid.setValue(i, j, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
grid = nextGrid;
|
||||
}
|
||||
|
||||
/*
|
||||
* method called to count living cells around one
|
||||
*/
|
||||
public int countLiveNeighbors(int x, int y) {
|
||||
int liveNeighbors = 0;
|
||||
|
||||
for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) {
|
||||
for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) {
|
||||
if (i == 0 && j == 0) continue;
|
||||
|
||||
int neighborX = x + i;
|
||||
int neighborY = y + j;
|
||||
|
||||
if (loopingBorder) {
|
||||
if (neighborX < 0) neighborX = LINE_NUM - 1;
|
||||
else if (neighborX >= LINE_NUM) neighborX = 0;
|
||||
|
||||
if (neighborY < 0) neighborY = COL_NUM - 1;
|
||||
else if (neighborY >= COL_NUM) neighborY = 0;
|
||||
}
|
||||
|
||||
if (neighborX >= 0 && neighborX < LINE_NUM && neighborY >= 0 && neighborY < COL_NUM) {
|
||||
if (grid.getValue(neighborX, neighborY) > 0) {
|
||||
liveNeighbors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return liveNeighbors;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* leave this as is
|
||||
*/
|
||||
public void stopSimu() {
|
||||
stopFlag=true;
|
||||
}
|
||||
|
||||
/*
|
||||
* method called when clicking pause button
|
||||
*/
|
||||
public void togglePause() {
|
||||
if (pauseFlag == true ) {
|
||||
pauseFlag=false;
|
||||
}
|
||||
else {
|
||||
pauseFlag=true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* method called when clicking on a cell in the interface
|
||||
* Cycles through cell states: 0 (dead), 1 (young), 2 (middle-aged), 3 (old).
|
||||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
if (clickActionFlag.equals("Cells")) {
|
||||
int currentValue = grid.getValue(x, y);
|
||||
int nextValue = (currentValue + 1) % 5; // Change to 5 to include infected state
|
||||
this.setCell(x, y, nextValue);
|
||||
} else if (clickActionFlag.equals("Virus")) {
|
||||
agents.add(new Virus(x, y)); // Add a new Virus agent at the clicked location
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 grid.getValue(x, y);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return list of Animals in simulated world
|
||||
*/
|
||||
public ArrayList<Agent> getAnimals(){
|
||||
return agents;
|
||||
}
|
||||
/**
|
||||
* selects Animals in a circular area of simulated world
|
||||
* @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>();
|
||||
for(int i=0;i<agents.size();i++) {
|
||||
Agent agent = agents.get(i);
|
||||
if(agent.isInArea(x,y,radius)) {
|
||||
inArea.add(agent);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
grid.setValue(x, y, val);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return lines of file representing
|
||||
* the simulated world in its present state
|
||||
*/
|
||||
public ArrayList<String> getSaveState() {
|
||||
ArrayList<String> saveState = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < LINE_NUM; i++) {
|
||||
StringBuilder line = new StringBuilder();
|
||||
for (int j = 0; j < COL_NUM; j++) {
|
||||
line.append(grid.getValue(i, j));
|
||||
if (j < COL_NUM - 1) {
|
||||
line.append(";");
|
||||
}
|
||||
}
|
||||
saveState.add(line.toString());
|
||||
}
|
||||
return saveState;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
for (int i = 0; i < LINE_NUM; i++) {
|
||||
for (int j = 0; j < COL_NUM; j++) {
|
||||
float randomValue = (float) Math.random();
|
||||
if (randomValue <= chanceOfLife) {
|
||||
grid.setValue(i, j, 1);
|
||||
} else {
|
||||
grid.setValue(i, j, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
return loopingBorder;
|
||||
}
|
||||
|
||||
public void toggleLoopingBorder() {
|
||||
if (isLoopingBorder() == true) {
|
||||
loopingBorder=false;
|
||||
}else {
|
||||
loopingBorder=true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setLoopDelay(int delay) {
|
||||
loopDelay=delay;
|
||||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
if (clickActionFlag.equals("Cells")) {
|
||||
clickActionFlag = "Virus";
|
||||
} else {
|
||||
clickActionFlag = "Cells";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
ArrayList<String> rule = new ArrayList<>();
|
||||
|
||||
// Add survival rules for each cell state
|
||||
for (List<Integer> stateRules : survivalRules) {
|
||||
rule.add(String.join(";", stateRules.stream().map(Object::toString).toArray(String[]::new)));
|
||||
}
|
||||
|
||||
// Add birth rules for each cell state
|
||||
for (List<Integer> stateRules : birthRules) {
|
||||
rule.add(String.join(";", stateRules.stream().map(Object::toString).toArray(String[]::new)));
|
||||
}
|
||||
|
||||
return rule;
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
if (lines.size() != LIFE_TYPE_NUM * 2) {
|
||||
System.out.println("Invalid rule file format");
|
||||
return;
|
||||
}
|
||||
|
||||
survivalRules.clear();
|
||||
birthRules.clear();
|
||||
|
||||
for (int i = 0; i < LIFE_TYPE_NUM; i++) {
|
||||
String survivalLine = lines.get(i);
|
||||
String[] survivalParts = survivalLine.split(";");
|
||||
ArrayList<Integer> survivalValues = new ArrayList<>();
|
||||
for (String part : survivalParts) {
|
||||
String[] survivalElements = part.split(",");
|
||||
for (String elem : survivalElements) {
|
||||
if (!elem.isEmpty()) { // Skip empty elements
|
||||
int value = Integer.parseInt(elem);
|
||||
survivalValues.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
survivalRules.add(survivalValues);
|
||||
}
|
||||
|
||||
for (int i = LIFE_TYPE_NUM; i < LIFE_TYPE_NUM * 2; i++) {
|
||||
String birthLine = lines.get(i);
|
||||
String[] birthParts = birthLine.split(";");
|
||||
ArrayList<Integer> birthValues = new ArrayList<>();
|
||||
for (String part : birthParts) {
|
||||
String[] birthElements = part.split(",");
|
||||
for (String elem : birthElements) {
|
||||
if (!elem.isEmpty()) { // Skip empty elements
|
||||
int value = Integer.parseInt(elem);
|
||||
birthValues.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
birthRules.add(birthValues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
ArrayList<String> saveData = new ArrayList<>();
|
||||
for (Agent agent : agents) {
|
||||
saveData.add(agent.getClass().getSimpleName() + "," + agent.getX() + "," + agent.getY());
|
||||
}
|
||||
return saveData;
|
||||
}
|
||||
|
||||
public void loadAgents(ArrayList<String> stringArray) {
|
||||
agents.clear();
|
||||
for (String line : stringArray) {
|
||||
String[] parts = line.split(",");
|
||||
String type = parts[0];
|
||||
int x = Integer.parseInt(parts[1]);
|
||||
int y = Integer.parseInt(parts[2]);
|
||||
if (type.equals("Virus")) {
|
||||
agents.add(new Virus(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* used by label in interface to show the active click action
|
||||
* @return String representation of click action
|
||||
*/
|
||||
public String clickActionName() {
|
||||
if (clickActionFlag.equals("Cells")) {
|
||||
return "Cells";
|
||||
} else {
|
||||
return "Virus";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class Virus extends Agent {
|
||||
Random rand;
|
||||
|
||||
Virus(int x, int y){
|
||||
super(x, y, Color.red);
|
||||
rand = new Random();
|
||||
}
|
||||
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||
if(world.getCell(x, y) > 0 && world.getCell(x, y) < 4) {
|
||||
world.setCell(x, y, 4); // Infect cell
|
||||
}
|
||||
this.moveRandom();
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,82 +13,88 @@ import backend.Simulator;
|
|||
|
||||
public class JPanelDraw extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX()*mySimu.getWidth())/getWidth();
|
||||
int y = (me.getY()*mySimu.getHeight())/getHeight();
|
||||
mySimu.clickCell(x,y);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX()*mySimu.getWidth())/getWidth();
|
||||
int y = (me.getY()*mySimu.getHeight())/getHeight();
|
||||
mySimu.clickCell(x,y);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
|
||||
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<mySimu.getWidth();x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for(int x=0; x<mySimu.getWidth();x++) {
|
||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||
int cellContent = mySimu.getCell(x,y);
|
||||
if(cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if(cellContent == 1) {
|
||||
g.setColor(Color.green);
|
||||
}
|
||||
if(cellContent == 2) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
if(cellContent == 3) {
|
||||
g.setColor(Color.cyan);
|
||||
}
|
||||
g.fillRect(
|
||||
(int) Math.round(x*cellWidth),
|
||||
(int) Math.round(y*cellHeight),
|
||||
(int) Math.round(cellWidth),
|
||||
(int) Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (Agent animal:mySimu.getAnimals()){
|
||||
g.setColor(animal.getDisplayColor());
|
||||
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
||||
(int)Math.round(animal.getY()*cellHeight),
|
||||
(int)Math.round(cellWidth/2),
|
||||
(int)Math.round(cellHeight/2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
|
||||
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<mySimu.getWidth();x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for(int x=0; x<mySimu.getWidth();x++) {
|
||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||
int cellContent = mySimu.getCell(x,y);
|
||||
if(cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if(cellContent == 1) {
|
||||
g.setColor(Color.green);
|
||||
}
|
||||
if(cellContent == 2) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
if(cellContent == 3) {
|
||||
g.setColor(Color.cyan);
|
||||
}
|
||||
if (cellContent == 4) {
|
||||
g.setColor(Color.red);
|
||||
}
|
||||
if (cellContent >= -3 && cellContent < 0) {
|
||||
g.setColor(Color.red);
|
||||
}
|
||||
g.fillRect(
|
||||
(int) Math.round(x*cellWidth),
|
||||
(int) Math.round(y*cellHeight),
|
||||
(int) Math.round(cellWidth),
|
||||
(int) Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (Agent animal:mySimu.getAnimals()){
|
||||
g.setColor(animal.getDisplayColor());
|
||||
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
||||
(int)Math.round(animal.getY()*cellHeight),
|
||||
(int)Math.round(cellWidth/2),
|
||||
(int)Math.round(cellHeight/2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue