Add the Method concerning the cell

This commit is contained in:
mathisdebard 2024-05-14 15:03:36 +02:00
parent b136a1a634
commit 414219a89a
2 changed files with 65 additions and 16 deletions

View File

@ -1,11 +0,0 @@
public class Simulator {
private int width;
private int height;
public Simulator(int numRows, int numColumns) {
// Calculate the width and height based on the number of rows and columns
this.width = numColumns;
this.height = numRows;
}
}

View File

@ -24,6 +24,11 @@ public class Simulator extends Thread {
private boolean loopingBorder; private boolean loopingBorder;
private boolean clickActionFlag; private boolean clickActionFlag;
private int loopDelay = 150; private int loopDelay = 150;
private int[][] board;
private int width;
private int height;
private int clickActionFlagValue; // Flag to determine the behavior of clickCell method
//TODO : add missing attribute(s) //TODO : add missing attribute(s)
@ -49,7 +54,15 @@ public class Simulator extends Thread {
} }
} }
public void setSize(int numRows, int numColumns) {
// Calculate the width and height based on the number of rows and columns
this.width = numColumns;
this.height = numRows;
this.board = new int[numRows][numColumns];
}
public int getWidth() { public int getWidth() {
//TODO : replace with proper return //TODO : replace with proper return
return width; return width;
@ -144,20 +157,60 @@ public class Simulator extends Thread {
/** /**
* method called when clicking on a cell in the interface * method called when clicking on a cell in the interface
*/ */
// Method to handle different behaviors based on the clickActionFlagValue
public void clickCell(int x, int y) { public void clickCell(int x, int y) {
//TODO : complete method //TODO : complete method
} switch (clickActionFlagValue) {
case 1: // Cell case (default): toggles the state of the cell at coordinates x,y
toggleCellState(x, y);
break;
case 2: // Agent case: create an Agent (Sheep provided for example) at provided coordinates
createAgent(x, y);
break;
// Add more cases as needed for different flag values
default:
// Default behavior
System.out.println("Invalid click action flag value.");
break;
}
}
// Method to toggle the state of the cell at coordinates x, y
private void toggleCellState(int x, int y) {
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
// Toggle the state of the cell
board[x][y] = (board[x][y] == 0) ? 1 : 0; // Assuming 0 represents dead cell and 1 represents alive cell
} else {
// Handle out of bounds condition or throw an exception
System.out.println("Invalid coordinates: (" + x + ", " + y + ")");
// You can choose to throw an exception here instead
}
}
// Method to create an agent at the provided coordinates (example: Sheep)
private void createAgent(int x, int y) {
Agent agent = new Sheep(x, y);
}
/** /**
* get cell value in simulated world * get cell value in simulated world
* @param x coordinate of cell * @param x coordinate of cell
* @param y coordinate of cell * @param y coordinate of cell
* @return value of cell * @return value of cell
*/ */
// Method to get the value of cell at coordinates x, y
public int getCell(int x, int y) { public int getCell(int x, int y) {
//TODO : complete method with proper return //TODO : complete method with proper return
return 0; if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
} return board[x][y];
} else {
// Handle out of bounds condition or return default value
return -1; // We can choose any default value here
}
}
/** /**
* *
* @return list of Animals in simulated world * @return list of Animals in simulated world
@ -191,6 +244,13 @@ public class Simulator extends Thread {
*/ */
public void setCell(int x, int y, int val) { public void setCell(int x, int y, int val) {
//TODO : complete method //TODO : complete method
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
board[x][y] = val;
} else {
// Handle out of bounds condition or throw an exception
System.out.println("Invalid coordinates: (" + x + ", " + y + ")");
}
} }
/** /**