From 414219a89a28f455823895f20acfde3e16f0ab62 Mon Sep 17 00:00:00 2001 From: mathisdebard Date: Tue, 14 May 2024 15:03:36 +0200 Subject: [PATCH] Add the Method concerning the cell --- src/Simulator.java | 11 ------ src/backend/Simulator.java | 70 +++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 16 deletions(-) delete mode 100644 src/Simulator.java diff --git a/src/Simulator.java b/src/Simulator.java deleted file mode 100644 index 8a3f297..0000000 --- a/src/Simulator.java +++ /dev/null @@ -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; - } -} diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index aba4fc5..11a78b6 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -24,6 +24,11 @@ public class Simulator extends Thread { private boolean loopingBorder; private boolean clickActionFlag; 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) @@ -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() { //TODO : replace with proper return return width; @@ -144,20 +157,60 @@ public class Simulator extends Thread { /** * 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) { //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 * @param x coordinate of cell * @param y coordinate of cell * @return value of cell */ + + // Method to get the value of cell at coordinates x, y public int getCell(int x, int y) { //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 @@ -191,6 +244,13 @@ public class Simulator extends Thread { */ public void setCell(int x, int y, int val) { //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 + ")"); + + } } /**