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;
|
package backend;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
public class Simulator extends Thread {
|
public class Simulator extends Thread {
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
|
|
||||||
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> fieldBirthValues;
|
|
||||||
|
|
||||||
private ArrayList<Agent> agents;
|
private List<List<Integer>> survivalRules;
|
||||||
|
private List<List<Integer>> birthRules;
|
||||||
|
|
||||||
private boolean stopFlag;
|
private ArrayList<Agent> agents;
|
||||||
private boolean pauseFlag;
|
|
||||||
private boolean loopingBorder;
|
|
||||||
private boolean clickActionFlag;
|
|
||||||
private int loopDelay = 150;
|
|
||||||
|
|
||||||
//TODO : add missing attribute(s)
|
private boolean stopFlag;
|
||||||
|
private boolean pauseFlag;
|
||||||
|
private boolean loopingBorder;
|
||||||
|
private String clickActionFlag;
|
||||||
|
private int loopDelay = 150;
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
private Grid grid;
|
||||||
mjf = mjfParam;
|
|
||||||
stopFlag=false;
|
|
||||||
pauseFlag=false;
|
|
||||||
loopingBorder=false;
|
|
||||||
clickActionFlag=false;
|
|
||||||
|
|
||||||
agents = new ArrayList<Agent>();
|
//TODO : add missing attribute(s)
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
|
||||||
|
|
||||||
//TODO : add missing attribute initialization
|
public Simulator(MyInterface mjfParam) {
|
||||||
|
mjf = mjfParam;
|
||||||
|
stopFlag=false;
|
||||||
|
pauseFlag=false;
|
||||||
|
loopingBorder=false;
|
||||||
|
clickActionFlag="Cells";
|
||||||
|
|
||||||
|
agents = new ArrayList<Agent>();
|
||||||
|
|
||||||
|
// Initialize rules
|
||||||
|
survivalRules = new ArrayList<>();
|
||||||
|
birthRules = new ArrayList<>();
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
grid = new Grid(LINE_NUM, COL_NUM);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return LINE_NUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return COL_NUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then evolution of the field
|
||||||
|
Grid nextGrid = new Grid(LINE_NUM, COL_NUM);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Default rule : Survive always, birth never
|
/*
|
||||||
for(int i =0; i<9; i++) {
|
* leave this as is
|
||||||
fieldSurviveValues.add(i);
|
*/
|
||||||
}
|
public void stopSimu() {
|
||||||
|
stopFlag=true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/*
|
||||||
|
* method called when clicking pause button
|
||||||
|
*/
|
||||||
|
public void togglePause() {
|
||||||
|
if (pauseFlag == true ) {
|
||||||
|
pauseFlag=false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pauseFlag=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
/**
|
||||||
//TODO : replace with proper return
|
* method called when clicking on a cell in the interface
|
||||||
return 0;
|
* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
/**
|
||||||
//TODO : replace with proper return
|
* get cell value in simulated world
|
||||||
return 0;
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
//Should probably stay as is
|
/**
|
||||||
public void run() {
|
* set value of cell
|
||||||
int stepCount=0;
|
* @param x coord of cell
|
||||||
while(!stopFlag) {
|
* @param y coord of cell
|
||||||
stepCount++;
|
* @param val to set in cell
|
||||||
makeStep();
|
*/
|
||||||
mjf.update(stepCount);
|
public void setCell(int x, int y, int val) {
|
||||||
try {
|
grid.setValue(x, y, val);
|
||||||
Thread.sleep(loopDelay);
|
}
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
while(pauseFlag && !stopFlag) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(loopDelay);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
*
|
||||||
|
* @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++) {
|
||||||
* method called at each step of the simulation
|
StringBuilder line = new StringBuilder();
|
||||||
* makes all the actions to go from one step to the other
|
for (int j = 0; j < COL_NUM; j++) {
|
||||||
*/
|
line.append(grid.getValue(i, j));
|
||||||
public void makeStep() {
|
if (j < COL_NUM - 1) {
|
||||||
// agent behaviors first
|
line.append(";");
|
||||||
// only modify if sure of what you do
|
}
|
||||||
// to modify agent behavior, see liveTurn method
|
}
|
||||||
// in agent classes
|
saveState.add(line.toString());
|
||||||
for(Agent agent : agents) {
|
}
|
||||||
ArrayList<Agent> neighbors =
|
return saveState;
|
||||||
this.getNeighboringAnimals(
|
}
|
||||||
agent.getX(),
|
/**
|
||||||
agent.getY(),
|
*
|
||||||
ANIMAL_AREA_RADIUS);
|
* @param lines of file representing saved world state
|
||||||
if(!agent.liveTurn(
|
*/
|
||||||
neighbors,
|
public void loadSaveState(ArrayList<String> lines) {
|
||||||
this)) {
|
/*
|
||||||
agents.remove(agent);
|
* First some checks that the file is usable
|
||||||
}
|
* We call early returns in conditions like this
|
||||||
}
|
* "Guard clauses", as they guard the method
|
||||||
//then evolution of the field
|
* against unwanted inputs
|
||||||
// TODO : apply game rule to all cells of the field
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* you should distribute this action in methods/classes
|
/**
|
||||||
* don't write everything here !
|
* called by button, with slider providing the argument
|
||||||
*
|
* makes a new world state with random cell states
|
||||||
* the idea is first to get the surrounding values
|
* @param chanceOfLife the chance for each cell
|
||||||
* then count how many are alive
|
* to be alive in new state
|
||||||
* then check if that number is in the lists of rules
|
*/
|
||||||
* if the cell is alive
|
public void generateRandom(float chanceOfLife) {
|
||||||
* and the count is in the survive list,
|
for (int i = 0; i < LINE_NUM; i++) {
|
||||||
* then the cell stays alive
|
for (int j = 0; j < COL_NUM; j++) {
|
||||||
* if the cell is not alive
|
float randomValue = (float) Math.random();
|
||||||
* and the count is in the birth list,
|
if (randomValue <= chanceOfLife) {
|
||||||
* then the cell becomes alive
|
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
|
||||||
* leave this as is
|
*/
|
||||||
*/
|
public String clickActionName() {
|
||||||
public void stopSimu() {
|
if (clickActionFlag.equals("Cells")) {
|
||||||
stopFlag=true;
|
return "Cells";
|
||||||
}
|
} else {
|
||||||
|
return "Virus";
|
||||||
/*
|
}
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
|
||||||
//TODO : Same idea as other load methods, but for agent list
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 "";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
public class JPanelDraw extends JPanel {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Simulator mySimu;
|
private Simulator mySimu;
|
||||||
private MyInterface interfaceGlobal;
|
private MyInterface interfaceGlobal;
|
||||||
|
|
||||||
public JPanelDraw(MyInterface itf) {
|
public JPanelDraw(MyInterface itf) {
|
||||||
super();
|
super();
|
||||||
mySimu = null;
|
mySimu = null;
|
||||||
interfaceGlobal = itf;
|
interfaceGlobal = itf;
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent me) {
|
public void mousePressed(MouseEvent me) {
|
||||||
// System.out.println(me);
|
// System.out.println(me);
|
||||||
if(mySimu == null) {
|
if(mySimu == null) {
|
||||||
interfaceGlobal.instantiateSimu();
|
interfaceGlobal.instantiateSimu();
|
||||||
}
|
}
|
||||||
int x = (me.getX()*mySimu.getWidth())/getWidth();
|
int x = (me.getX()*mySimu.getWidth())/getWidth();
|
||||||
int y = (me.getY()*mySimu.getHeight())/getHeight();
|
int y = (me.getY()*mySimu.getHeight())/getHeight();
|
||||||
mySimu.clickCell(x,y);
|
mySimu.clickCell(x,y);
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setSimu(Simulator simu) {
|
public void setSimu(Simulator simu) {
|
||||||
mySimu = simu;
|
mySimu = simu;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
this.setBackground(Color.black);
|
this.setBackground(Color.black);
|
||||||
if (mySimu != null) {
|
if (mySimu != null) {
|
||||||
// Draw Interface from state of simulator
|
// Draw Interface from state of simulator
|
||||||
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
|
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
|
||||||
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
|
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
|
||||||
g.setColor(Color.gray);
|
g.setColor(Color.gray);
|
||||||
for(int x=0; x<mySimu.getWidth();x++) {
|
for(int x=0; x<mySimu.getWidth();x++) {
|
||||||
int graphX = Math.round(x*cellWidth);
|
int graphX = Math.round(x*cellWidth);
|
||||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||||
}
|
}
|
||||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||||
int graphY = Math.round(y*cellHeight);
|
int graphY = Math.round(y*cellHeight);
|
||||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||||
}
|
}
|
||||||
for(int x=0; x<mySimu.getWidth();x++) {
|
for(int x=0; x<mySimu.getWidth();x++) {
|
||||||
for (int y=0; y<mySimu.getHeight(); y++) {
|
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||||
int cellContent = mySimu.getCell(x,y);
|
int cellContent = mySimu.getCell(x,y);
|
||||||
if(cellContent == 0) {
|
if(cellContent == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(cellContent == 1) {
|
if(cellContent == 1) {
|
||||||
g.setColor(Color.green);
|
g.setColor(Color.green);
|
||||||
}
|
}
|
||||||
if(cellContent == 2) {
|
if(cellContent == 2) {
|
||||||
g.setColor(Color.yellow);
|
g.setColor(Color.yellow);
|
||||||
}
|
}
|
||||||
if(cellContent == 3) {
|
if(cellContent == 3) {
|
||||||
g.setColor(Color.cyan);
|
g.setColor(Color.cyan);
|
||||||
}
|
}
|
||||||
g.fillRect(
|
if (cellContent == 4) {
|
||||||
(int) Math.round(x*cellWidth),
|
g.setColor(Color.red);
|
||||||
(int) Math.round(y*cellHeight),
|
}
|
||||||
(int) Math.round(cellWidth),
|
if (cellContent >= -3 && cellContent < 0) {
|
||||||
(int) Math.round(cellHeight)
|
g.setColor(Color.red);
|
||||||
);
|
}
|
||||||
}
|
g.fillRect(
|
||||||
}
|
(int) Math.round(x*cellWidth),
|
||||||
for (Agent animal:mySimu.getAnimals()){
|
(int) Math.round(y*cellHeight),
|
||||||
g.setColor(animal.getDisplayColor());
|
(int) Math.round(cellWidth),
|
||||||
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
(int) Math.round(cellHeight)
|
||||||
(int)Math.round(animal.getY()*cellHeight),
|
);
|
||||||
(int)Math.round(cellWidth/2),
|
}
|
||||||
(int)Math.round(cellHeight/2));
|
}
|
||||||
}
|
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