ver 1.0.1
This commit is contained in:
parent
a29159b365
commit
c79074bcc2
|
|
@ -1,2 +1,2 @@
|
||||||
[.ShellClassInfo]
|
[.ShellClassInfo]
|
||||||
IconResource=C:\Program Files\Google\Drive File Stream\74.0.3.0\GoogleDriveFS.exe,23
|
IconResource=C:\Program Files\Google\Drive File Stream\75.0.2.0\GoogleDriveFS.exe,23
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ public class Simulator extends Thread {
|
||||||
private boolean pauseFlag;
|
private boolean pauseFlag;
|
||||||
private int loopDelay;
|
private int loopDelay;
|
||||||
//TODO : add declaration of additional attributes here
|
//TODO : add declaration of additional attributes here
|
||||||
|
private int[][] cells;
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
mjf = mjfParam;
|
||||||
|
|
@ -34,8 +35,8 @@ public class Simulator extends Thread {
|
||||||
* @return the number of columns in the grid composing the simulated world
|
* @return the number of columns in the grid composing the simulated world
|
||||||
*/
|
*/
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
//TODO : correct return
|
//TODO : correct return (DONE MAYBE)
|
||||||
return 0;
|
return mjf.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -43,8 +44,8 @@ public class Simulator extends Thread {
|
||||||
* @return the number of rows in the grid composing the simulated world
|
* @return the number of rows in the grid composing the simulated world
|
||||||
*/
|
*/
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
//TODO : correct return
|
//TODO : correct return (DONE MAYBE)
|
||||||
return 0;
|
return mjf.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
@ -90,14 +91,69 @@ public class Simulator extends Thread {
|
||||||
* be it as variables or as attributes you may add to the class Simulator,
|
* be it as variables or as attributes you may add to the class Simulator,
|
||||||
* by using their (public) methods.
|
* by using their (public) methods.
|
||||||
*/
|
*/
|
||||||
|
int simRow = this.getWidth();
|
||||||
|
int simCol = this.getHeight();
|
||||||
|
|
||||||
|
cells = new int[simRow][simCol];
|
||||||
|
|
||||||
|
for (int row=0;row<simRow; row++) {
|
||||||
|
for (int col=0; col<simCol; col++) {
|
||||||
|
//Count the number of live neighbor
|
||||||
|
int liveNeighbors = countLiveNeighbors(row,col);
|
||||||
|
|
||||||
|
//Apply Conway's rules
|
||||||
|
if (cells[row][col]==1) { //cell us allive
|
||||||
|
if(liveNeighbors <2 || liveNeighbors >3) {
|
||||||
|
cells[row][col]=0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cells[row][col]=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { //cell is dead
|
||||||
|
if(liveNeighbors == 3) {
|
||||||
|
cells[row][col]=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countLiveNeighbors(int row, int col) {
|
||||||
|
int liveNeighbors =0;
|
||||||
|
|
||||||
|
// Check the eight neighboring cells
|
||||||
|
for (int i = -1; i <= 1; i++) {
|
||||||
|
for (int j = -1; j <= 1; j++) {
|
||||||
|
if (i == 0 && j == 0) {
|
||||||
|
continue; // Skip the current cell
|
||||||
|
}
|
||||||
|
|
||||||
|
int neighborRow = row + i;
|
||||||
|
int neighborCol = col + j;
|
||||||
|
|
||||||
|
// Check if the neighboring cell is within the board bounds
|
||||||
|
if (neighborRow >= 0 && neighborRow < this.getWidth() && neighborCol >= 0 && neighborCol < this.getHeight()) {
|
||||||
|
// Check if the neighboring cell is alive
|
||||||
|
if (cells[neighborRow][neighborCol] == 1) {
|
||||||
|
liveNeighbors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return liveNeighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops simulation by raising the stop flag used in the run method
|
* Stops simulation by raising the stop flag used in the run method
|
||||||
*/
|
*/
|
||||||
public void stopSimu() {
|
public boolean stopSimu() {
|
||||||
//TODO : set stopFlag to true
|
//TODO : set stopFlag to true (DONE)
|
||||||
|
return stopFlag = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,9 +161,16 @@ public class Simulator extends Thread {
|
||||||
* Toggles Pause of simulation
|
* Toggles Pause of simulation
|
||||||
* by raising or lowering the pause flag used in the run method
|
* by raising or lowering the pause flag used in the run method
|
||||||
*/
|
*/
|
||||||
public void togglePause() {
|
public boolean togglePause() {
|
||||||
//TODO : change value of boolean attribute pauseFlag
|
//TODO : change value of boolean attribute pauseFlag (DONE)
|
||||||
// from false to true, or from true to false
|
// from false to true, or from true to false
|
||||||
|
if (pauseFlag == true) {
|
||||||
|
pauseFlag = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pauseFlag = true;
|
||||||
|
}
|
||||||
|
return pauseFlag;
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -125,6 +188,7 @@ public class Simulator extends Thread {
|
||||||
* But the GUI can also print properly more values than that.
|
* But the GUI can also print properly more values than that.
|
||||||
* You might want to use this for the going further section.
|
* You might want to use this for the going further section.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* get the value of a cell at coordinates
|
* get the value of a cell at coordinates
|
||||||
|
|
@ -145,6 +209,7 @@ public class Simulator extends Thread {
|
||||||
*/
|
*/
|
||||||
public void setCell(int x, int y, int val) {
|
public void setCell(int x, int y, int val) {
|
||||||
//TODO implement
|
//TODO implement
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -166,6 +231,7 @@ public class Simulator extends Thread {
|
||||||
public void populateLine(int coord, String fileLine) {
|
public void populateLine(int coord, String fileLine) {
|
||||||
//TODO : implement and correct the comment
|
//TODO : implement and correct the comment
|
||||||
// As you have to choose row OR column depending on your implementation
|
// As you have to choose row OR column depending on your implementation
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
[.ShellClassInfo]
|
[.ShellClassInfo]
|
||||||
IconResource=C:\Program Files\Google\Drive File Stream\74.0.3.0\GoogleDriveFS.exe,23
|
IconResource=C:\Program Files\Google\Drive File Stream\75.0.2.0\GoogleDriveFS.exe,23
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
[.ShellClassInfo]
|
[.ShellClassInfo]
|
||||||
IconResource=C:\Program Files\Google\Drive File Stream\74.0.3.0\GoogleDriveFS.exe,23
|
IconResource=C:\Program Files\Google\Drive File Stream\75.0.2.0\GoogleDriveFS.exe,23
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
[.ShellClassInfo]
|
[.ShellClassInfo]
|
||||||
IconResource=C:\Program Files\Google\Drive File Stream\74.0.3.0\GoogleDriveFS.exe,23
|
IconResource=C:\Program Files\Google\Drive File Stream\75.0.2.0\GoogleDriveFS.exe,23
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue