Compare commits

...

6 Commits

Author SHA1 Message Date
sebas df9cec1421 Merge branch 'master' of
https://gitarero.ecam.fr/maxime.lobietti/OOP_F2_Project.git

Conflicts:
	src/backend/Grid.java
2024-05-31 16:39:01 +02:00
sebas 056568a855 almost the end 2024-05-31 16:36:59 +02:00
sebas a8498b4afa Almost the end4 2024-05-31 16:21:51 +02:00
sebas 3e6d38aa6a Almost the end 2024-05-31 16:07:57 +02:00
sebas 503ccd708b almost the end 2024-05-31 16:03:17 +02:00
sebas 21708034c9 Almost the end 2024-05-30 11:42:09 +02:00
2 changed files with 160 additions and 29 deletions

View File

@ -1,18 +1,35 @@
package backend; package backend;
import java.util.Random; import java.util.Random;
public class Grid { /**
private int width; * The Grid class represents a 2D grid of integers.
private int height; */
private int[][] grid; public class Grid {
private int rando; private final int width;
private Random rand = new Random(); private final int height;
private final int[][] grid;
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;
@ -21,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];
@ -28,12 +53,18 @@ 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 = rand.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 { } else {
@ -42,4 +73,15 @@ public class Grid {
} }
} }
} }
}
/**
* 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
*/
}

View File

@ -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";
} }
} }