Session 1 completing simulator
This commit is contained in:
parent
399172d6c8
commit
fd4d680a22
|
|
@ -0,0 +1,17 @@
|
|||
package backend;
|
||||
|
||||
public class Cell {
|
||||
private int value;
|
||||
|
||||
public Cell(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Grid {
|
||||
private int height;
|
||||
private int width;
|
||||
private ArrayList<ArrayList<Cell>> Grid;
|
||||
private Simulator simulator;
|
||||
|
||||
public Grid(int height, int width, Simulator tempSimulator) {
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
this.simulator = tempSimulator;
|
||||
|
||||
Grid = new ArrayList<>(height);
|
||||
|
||||
for (int i = 0; i < height; i++) {
|
||||
this.Grid.add(i, new ArrayList<Cell>());
|
||||
for (int j = 0; j < width; j++) {
|
||||
this.Grid.get(i).add(new Cell(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getheight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public int getwidth() {
|
||||
return this.width;
|
||||
}
|
||||
public boolean isLoopingBorder() {
|
||||
return simulator.isLoopingBorder();
|
||||
}
|
||||
|
||||
public Cell getCell(int row,int column) {
|
||||
return Grid.get(row).get(column);
|
||||
}
|
||||
|
||||
public void setCell(int row, int column, Cell cell){
|
||||
this.Grid.get(row).set(column, cell);
|
||||
}
|
||||
|
||||
public int countAround(int row, int column) {
|
||||
int count = 0;
|
||||
boolean loopingBorder = isLoopingBorder();
|
||||
|
||||
for (int i = row - 1; i <= row + 1; i++) {
|
||||
for (int j = column - 1; j <= column + 1; j++) {
|
||||
if (i == row && j == column) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int x = i;
|
||||
int y = j;
|
||||
|
||||
if (loopingBorder) {
|
||||
x = (x + width) % width;
|
||||
y = (y + height) % height;
|
||||
} else {
|
||||
if (x < 0 || x >= width || y < 0 || y >= height) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
count += this.getCell(x, y).getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//TODO : set agent (x y agent) load an agent to coordinates x,y
|
||||
|
||||
//TODO : set random (density) create a random Grid of determined density
|
||||
public void setRandom(double density) {
|
||||
for (int i = 0; i < height; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
double random = Math.random();
|
||||
if (random < density) {
|
||||
Grid.get(i).get(j).setValue(1);
|
||||
} else {
|
||||
Grid.get(i).get(j).setValue(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Created a random field");
|
||||
}
|
||||
|
||||
public void serialPrint(){
|
||||
for (int i = 0; i < height; i++) {
|
||||
System.out.print("\n");
|
||||
for (int j = 0; j < width; j++) {
|
||||
System.out.print(this.getCell(i, j).getValue() +" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,8 +7,8 @@ public class Simulator extends Thread {
|
|||
|
||||
private MyInterface mjf;
|
||||
|
||||
private final int COL_NUM = 100;
|
||||
private final int LINE_NUM = 100;
|
||||
private final int COL_NUM = 50;
|
||||
private final int LINE_NUM = 50;
|
||||
private final int LIFE_TYPE_NUM = 4;
|
||||
//Conway Radius : 1
|
||||
private final int LIFE_AREA_RADIUS = 1;
|
||||
|
|
@ -27,6 +27,11 @@ public class Simulator extends Thread {
|
|||
|
||||
//TODO : add missing attribute(s)
|
||||
|
||||
private double randomDansitySlider = 0.5;
|
||||
private int width;
|
||||
private int height;
|
||||
private Grid grid;
|
||||
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
stopFlag=false;
|
||||
|
|
@ -40,7 +45,9 @@ public class Simulator extends Thread {
|
|||
|
||||
//TODO : add missing attribute initialization
|
||||
|
||||
|
||||
this.width=COL_NUM;
|
||||
this.height=LINE_NUM;
|
||||
grid = new Grid(height, width, this);
|
||||
|
||||
//Default rule : Survive always, birth never
|
||||
for(int i =0; i<9; i++) {
|
||||
|
|
@ -51,12 +58,12 @@ public class Simulator extends Thread {
|
|||
|
||||
public int getWidth() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
return this.height;
|
||||
}
|
||||
|
||||
//Should probably stay as is
|
||||
|
|
@ -81,7 +88,7 @@ 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
|
||||
|
|
@ -103,6 +110,7 @@ public class Simulator extends Thread {
|
|||
agents.remove(agent);
|
||||
}
|
||||
}
|
||||
this.applyStep();
|
||||
//then evolution of the field
|
||||
// TODO : apply game rule to all cells of the field
|
||||
|
||||
|
|
@ -137,6 +145,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public void togglePause() {
|
||||
// TODO : actually toggle the corresponding flag
|
||||
pauseFlag = !pauseFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -144,6 +153,19 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
//TODO : complete method
|
||||
if (clickActionFlag == true) {
|
||||
int currentCellValue = getCell(x, y);
|
||||
int newCellValue = 0;
|
||||
|
||||
if (currentCellValue == 0) {
|
||||
newCellValue = 1;
|
||||
} else {
|
||||
newCellValue = 0;
|
||||
}
|
||||
this.setCell(x, y, newCellValue);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -154,7 +176,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public int getCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
return 0;
|
||||
return this.grid.getCell(x,y).getValue();
|
||||
}
|
||||
/**
|
||||
*
|
||||
|
|
@ -188,7 +210,7 @@ public class Simulator extends Thread {
|
|||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
//TODO : complete method
|
||||
this.grid.getCell(x, y).setValue(val);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -196,6 +218,10 @@ public class Simulator extends Thread {
|
|||
* @return lines of file representing
|
||||
* the simulated world in its present state
|
||||
*/
|
||||
public void countAround(int x, int y) {
|
||||
this.grid.countAround(x, y);
|
||||
}
|
||||
|
||||
public ArrayList<String> getSaveState() {
|
||||
//TODO : complete method with proper return
|
||||
return null;
|
||||
|
|
@ -249,24 +275,30 @@ public class Simulator extends Thread {
|
|||
* maybe just make a constructor in there
|
||||
* and use it here
|
||||
*/
|
||||
this.grid.setRandom(chanceOfLife);
|
||||
|
||||
}
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
//TODO : complete method with proper return
|
||||
return false;
|
||||
return loopingBorder;
|
||||
}
|
||||
|
||||
public void toggleLoopingBorder() {
|
||||
//TODO : complete method
|
||||
|
||||
loopingBorder = !loopingBorder;
|
||||
|
||||
}
|
||||
|
||||
public void setLoopDelay(int delay) {
|
||||
//TODO : complete method
|
||||
loopDelay = delay;
|
||||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
//TODO : complete method
|
||||
clickActionFlag = !clickActionFlag;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -309,6 +341,36 @@ public class Simulator extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void applyStep() {
|
||||
Grid newGridUpdated = new Grid(this.height, this.width, this);
|
||||
System.out.println("New Iteration :");
|
||||
for (int i = 0; i < this.height; i++) {
|
||||
for (int j = 0; j < this.width; j++) {
|
||||
int cellValue = this.grid.getCell(i, j).getValue();
|
||||
|
||||
int newCellValue = cellValue;
|
||||
int count = this.grid.countAround(i, j);
|
||||
|
||||
if (cellValue == 1) {
|
||||
if (count == 2 || count == 3) {
|
||||
newCellValue = 1;
|
||||
} else {
|
||||
newCellValue = 0;
|
||||
}
|
||||
} else {
|
||||
if (count == 3) {
|
||||
newCellValue = 1;
|
||||
}
|
||||
}
|
||||
newGridUpdated.setCell(i, j, new Cell(newCellValue));
|
||||
System.out.println("applyStep called : " + newGridUpdated.getCell(i, j).getValue() + " at " + i + " " + j);
|
||||
}
|
||||
}
|
||||
grid = newGridUpdated;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
//TODO : Same idea as the other save method, but for agents
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
public class test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue