This commit is contained in:
Guillaume LE CHARTIER 2024-04-10 16:51:30 +02:00
commit d349fb5567
3 changed files with 66 additions and 14 deletions

View File

@ -1,10 +1,10 @@
package backend;
public class Cell {
protected int value;
protected int density;
private int value;
private int density;
protected Cell(int value, int density) {
public Cell(int value, int density) {
this.value = value;
this.density = density;
}
@ -15,10 +15,10 @@ public class Cell {
public int getDensity() {
return density;
}
public int setValue(int value) {
public void setValue(int value) {
this.value = value;
}
public int setDensity(int density){
public void setDensity(int density){
this.density = density;
}
}

View File

@ -26,9 +26,11 @@ public class Simulator extends Thread {
private int loopDelay = 150;
//TODO : add missing attribute(s)
private double randomDansitySlider = 0.5;
private int width;
private int height;
private boolean enableLogs;
private Table table;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
@ -46,6 +48,7 @@ public class Simulator extends Thread {
this.width=COL_NUM;
this.height=LINE_NUM;
enableLogs = true; // for debugging purposes
table = new Table(height, width);
//Default rule : Survive always, birth never
@ -110,7 +113,27 @@ public class Simulator extends Thread {
}
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
// TODO-INPROGRESS : apply game rule to all cells of the field
Table tempTable = new Table(this.height, this.width);
for(int x=0; x<width; x++) {
for(int y=0; y<height; y++) {
if (this.table.getCell(x, y).getValue()=1) {
if (table.countNear(x,y)<2) {
tempTable.getCell(x,y).setValue(0);
} else if(table.countNear(x,y)>3) {
tempTable.getCell(x,y).setValue(0);
}
} else {
if(table.countNear(x,y)==3) {
tempTable.getCell(x,y).setValue(1);
}
}
}
}
this.table = tempTable;
}
/* you should distribute this action in methods/classes
* don't write everything here !
@ -129,8 +152,6 @@ public class Simulator extends Thread {
}
/*
* leave this as is
*/
@ -185,7 +206,8 @@ public class Simulator extends Thread {
* @return value of cell
*/
public int getCell(int x, int y) {
//TODO : complete method with proper return
//TODO-ERROR : WHY THE FUCK DOES IT WORK AT 0 BUT NOT WITH table.getcell.getvalue ????
//complete method with proper return
return 0;
}
/**
@ -300,12 +322,20 @@ public class Simulator extends Thread {
}
public boolean isLoopingBorder() {
//TODO : complete method with proper return
return false;
//ODO-COMPLETE : complete method with proper return
return loopingBorder;
}
public void toggleLoopingBorder() {
//TODO : complete method
//ODO-COMPLETE : complete method
loopingBorder = !loopingBorder;
if (enableLogs) {
if (loopingBorder) {
System.out.println("toggleLoopingBorder called, set loopingBorder to true");
} else {
System.out.println("toggleLoopingBorder called, set loopingBorder to false");
}
}
}
@ -317,6 +347,13 @@ public class Simulator extends Thread {
}
}
public void setDansity(double density) {
randomDansitySlider = density;
if (enableLogs) {
System.out.println("Density set to " + density);
}
}
public void toggleClickAction() {
//TODO-COMPLETE : complete method
clickActionFlag = !clickActionFlag;

View File

@ -165,6 +165,11 @@ public class MyInterface extends JFrame {
randSlider.setMinimum(0);
randSlider.setMaximum(100);
randSlider.setPreferredSize(new Dimension(30,200));
randSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
changeDansity();
}
});
panelRight.add(randSlider);
@ -254,6 +259,15 @@ public class MyInterface extends JFrame {
}
}
public void changeDansity() {
if(mySimu != null) {
double density = ((double)randSlider.getValue())/((double)randSlider.getMaximum());
mySimu.setDansity(density);
} else {
randSlider.setValue(50);
}
}
public void clicLoadFileButton() {
Simulator loadedSim = new Simulator(this);
String fileName=SelectFile();
@ -379,6 +393,7 @@ public class MyInterface extends JFrame {
}
public void update (int stepCount) {
System.out.println("update called");
this.setStepBanner("Step : "+ stepCount);
this.repaint();
}