Almost the end
This commit is contained in:
parent
5e45df9159
commit
21708034c9
|
|
@ -1,17 +1,35 @@
|
||||||
package backend;
|
package backend;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Grid class represents a 2D grid of integers.
|
||||||
|
*/
|
||||||
public class Grid {
|
public class Grid {
|
||||||
private int width;
|
private final int width;
|
||||||
private int height;
|
private final int height;
|
||||||
private int[][] grid;
|
private final int[][] grid;
|
||||||
private int rando;
|
private final Random rand = new Random();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a Grid with the specified width and height.
|
||||||
|
*
|
||||||
|
* @param width the width of the grid
|
||||||
|
* @param height the height of the grid
|
||||||
|
*/
|
||||||
public Grid(int width, int height) {
|
public Grid(int width, int height) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.grid = new int[height][width];
|
this.grid = new int[height][width];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value at the specified position in the grid.
|
||||||
|
*
|
||||||
|
* @param x the x-coordinate
|
||||||
|
* @param y the y-coordinate
|
||||||
|
* @param value the value to set
|
||||||
|
* @throws IndexOutOfBoundsException if the position is out of range
|
||||||
|
*/
|
||||||
public void setValue(int x, int y, int value) {
|
public void setValue(int x, int y, int value) {
|
||||||
if (x >= 0 && x < width && y >= 0 && y < height) {
|
if (x >= 0 && x < width && y >= 0 && y < height) {
|
||||||
grid[y][x] = value;
|
grid[y][x] = value;
|
||||||
|
|
@ -20,6 +38,14 @@ public class Grid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value at the specified position in the grid.
|
||||||
|
*
|
||||||
|
* @param x the x-coordinate
|
||||||
|
* @param y the y-coordinate
|
||||||
|
* @return the value at the specified position
|
||||||
|
* @throws IndexOutOfBoundsException if the position is out of range
|
||||||
|
*/
|
||||||
public int getValue(int x, int y) {
|
public int getValue(int x, int y) {
|
||||||
if (x >= 0 && x < width && y >= 0 && y < height) {
|
if (x >= 0 && x < width && y >= 0 && y < height) {
|
||||||
return grid[y][x];
|
return grid[y][x];
|
||||||
|
|
@ -27,15 +53,35 @@ public class Grid {
|
||||||
throw new IndexOutOfBoundsException("Grid position out of range");
|
throw new IndexOutOfBoundsException("Grid position out of range");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fills the grid with random values based on the given randomness parameter.
|
||||||
|
*
|
||||||
|
* @param randomness a float value between 0 and 1 indicating the probability
|
||||||
|
* of a cell being set to 1
|
||||||
|
*/
|
||||||
public void fillRandom(float randomness) {
|
public void fillRandom(float randomness) {
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
int rando = Random.nextInt(100);
|
int rando = rand.nextInt(100);
|
||||||
int r= (int) randomness*100;
|
int r = (int) (randomness * 100);
|
||||||
if(rando<r) {
|
if (rando < r) {
|
||||||
grid[y][x] = 1;}
|
grid[y][x] = 1;
|
||||||
|
} else {
|
||||||
|
grid[y][x] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Counts the number of neighbors with a value of 1 for the cell at the
|
||||||
|
* specified position.
|
||||||
|
*
|
||||||
|
* @param x the x-coordinate of the cell
|
||||||
|
* @param y the y-coordinate of the cell
|
||||||
|
* @return the number of neighbors with a value of 1
|
||||||
|
* @throws IndexOutOfBoundsException if the position is out of range
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,14 +39,13 @@ public class Simulator extends Thread {
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
fieldSurviveValues = new ArrayList<Integer>();
|
||||||
|
|
||||||
//TODO : add missing attribute initialization
|
// Conway's Game of Life rules:
|
||||||
|
// Survive with 2 or 3 neighbors
|
||||||
|
fieldSurviveValues.add(2);
|
||||||
|
fieldSurviveValues.add(3);
|
||||||
//Default rule : Survive always, birth never
|
|
||||||
for(int i =0; i<9; i++) {
|
// Birth with exactly 3 neighbors
|
||||||
fieldSurviveValues.add(i);
|
fieldBirthValues.add(3);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,7 +117,61 @@ public class Simulator extends Thread {
|
||||||
* and the count is in the birth list,
|
* and the count is in the birth list,
|
||||||
* then the cell becomes alive
|
* then the cell becomes alive
|
||||||
*/
|
*/
|
||||||
|
Grid nextGrid = new Grid(LINE_NUM,COL_NUM);
|
||||||
|
for (int x = 0; x < getWidth(); x++) {
|
||||||
|
for (int y = 0; y < getHeight(); y++) {
|
||||||
|
int actualState = getCell(x,y);
|
||||||
|
int futureState =0;
|
||||||
|
// high life
|
||||||
|
if(actualState==0) {
|
||||||
|
for (int i = 1; i<=5; i++) {
|
||||||
|
if(fieldBirthValues.contains(countNeighbors(x,y,i))) {
|
||||||
|
futureState = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(fieldSurviveValues.contains(countNeighbors(x,y,actualState))) {
|
||||||
|
futureState = actualState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextGrid.setValue(x,y,futureState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maingrid = nextGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countNeighbors(int x, int y, int state) {
|
||||||
|
int nbNeighbors = 0;
|
||||||
|
|
||||||
|
for (int i = x - 1; i <= x + 1; i++) {
|
||||||
|
for (int j = y - 1; j <= y + 1; j++) {
|
||||||
|
int x_looped = i;
|
||||||
|
int y_looped = j;
|
||||||
|
|
||||||
|
if (loopingBorder) {
|
||||||
|
if (x_looped < 0) {
|
||||||
|
x_looped = getWidth() - 1;
|
||||||
|
} else if (x_looped >= getWidth()) {
|
||||||
|
x_looped = 0;
|
||||||
|
}
|
||||||
|
if (y_looped < 0) {
|
||||||
|
y_looped = getHeight() - 1;
|
||||||
|
} else if (y_looped >= getHeight()) {
|
||||||
|
y_looped = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) {
|
||||||
|
if (state == maingrid.getValue(x_looped, y_looped)) {
|
||||||
|
nbNeighbors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(nbNeighbors);
|
||||||
|
return nbNeighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -205,7 +258,18 @@ public class Simulator extends Thread {
|
||||||
*/
|
*/
|
||||||
public ArrayList<String> getSaveState() {
|
public ArrayList<String> getSaveState() {
|
||||||
//TODO : complete method with proper return
|
//TODO : complete method with proper return
|
||||||
return null;
|
ArrayList<String> saveState = new ArrayList<>();
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
StringBuilder rowBuilder = new StringBuilder();
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
if (x > 0) {
|
||||||
|
rowBuilder.append(";");
|
||||||
|
}
|
||||||
|
rowBuilder.append(maingrid.getValue(x, y));
|
||||||
|
}
|
||||||
|
saveState.add(rowBuilder.toString());
|
||||||
|
}
|
||||||
|
return saveState;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -261,16 +325,17 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
public boolean isLoopingBorder() {
|
||||||
//TODO : complete method with proper return
|
//TODO : complete method with proper return
|
||||||
return false;
|
return loopingBorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleLoopingBorder() {
|
public void toggleLoopingBorder() {
|
||||||
//TODO : complete method
|
//TODO : complete method
|
||||||
|
loopingBorder = !loopingBorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
public void setLoopDelay(int delay) {
|
||||||
//TODO : complete method
|
//TODO : complete method
|
||||||
|
loopDelay = delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleClickAction() {
|
public void toggleClickAction() {
|
||||||
|
|
@ -292,8 +357,29 @@ public class Simulator extends Thread {
|
||||||
*/
|
*/
|
||||||
public ArrayList<String> getRule() {
|
public ArrayList<String> getRule() {
|
||||||
//TODO : complete method with proper return
|
//TODO : complete method with proper return
|
||||||
|
|
||||||
return null;
|
StringBuilder birthValues = new StringBuilder();
|
||||||
|
StringBuilder surviveValues = new StringBuilder();
|
||||||
|
|
||||||
|
for (int value : fieldBirthValues) {
|
||||||
|
if (birthValues.length() > 0) {
|
||||||
|
birthValues.append(";");
|
||||||
|
}
|
||||||
|
birthValues.append(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int value : fieldSurviveValues) {
|
||||||
|
if (surviveValues.length() > 0) {
|
||||||
|
surviveValues.append(";");
|
||||||
|
}
|
||||||
|
surviveValues.append(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
result.add(birthValues.toString());
|
||||||
|
result.add(surviveValues.toString());
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
public void loadRule(ArrayList<String> lines) {
|
||||||
|
|
@ -302,7 +388,8 @@ public class Simulator extends Thread {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//TODO : remove previous rule (=emptying lists)
|
//TODO : remove previous rule (=emptying lists)
|
||||||
|
fieldSurviveValues.clear();
|
||||||
|
fieldBirthValues.clear();
|
||||||
|
|
||||||
String surviveLine = lines.get(0);
|
String surviveLine = lines.get(0);
|
||||||
String birthLine = lines.get(1);
|
String birthLine = lines.get(1);
|
||||||
|
|
@ -311,20 +398,22 @@ public class Simulator extends Thread {
|
||||||
String elem = surviveElements[x];
|
String elem = surviveElements[x];
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible survive values
|
//TODO : add value to possible survive values
|
||||||
|
fieldSurviveValues.add(value);
|
||||||
}
|
}
|
||||||
String[] birthElements = birthLine.split(";");
|
String[] birthElements = birthLine.split(";");
|
||||||
for(int x=0; x<birthElements.length;x++) {
|
for(int x=0; x<birthElements.length;x++) {
|
||||||
String elem = birthElements[x];
|
String elem = birthElements[x];
|
||||||
int value = Integer.parseInt(elem);
|
int value = Integer.parseInt(elem);
|
||||||
//TODO : add value to possible birth values
|
//TODO : add value to possible birth values
|
||||||
|
fieldBirthValues.add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getAgentsSave() {
|
public ArrayList<String> getAgentsSave() {
|
||||||
//TODO : Same idea as the other save method, but for agents
|
//TODO : Same idea as the other save method, but for agents
|
||||||
return null;
|
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
public void loadAgents(ArrayList<String> stringArray) {
|
||||||
|
|
@ -339,7 +428,7 @@ public class Simulator extends Thread {
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
// TODO : initially return "sheep" or "cell"
|
// TODO : initially return "sheep" or "cell"
|
||||||
// depending on clickActionFlag
|
// depending on clickActionFlag
|
||||||
return "";
|
if (clickActionFlag) return "cell"; else return "sheep";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue