From f6c231d8a1c71790f6247cab649bdf5a0b662636 Mon Sep 17 00:00:00 2001 From: Balthazar SQUINABOL Date: Wed, 22 May 2024 13:50:12 +0200 Subject: [PATCH 1/3] Actualiser README.md --- README.md | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 112 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6c3fd2e..f0f7270 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,124 @@ -When starting a task "TODO" edit it to "TODO-INPROGRESS" -When task is done edit it to "TODO-COMPLETE" -If there is an error in the code edit it to "TODO-ERROR" -Link Canva Whiteboard: -https://www.canva.com/design/DAGCBGF5b4c/4cNmhoS6lSC8Once9r_Tlg/edit?utm_content=DAGCBGF5b4c&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton +# Game Of Life +The Game of Life (an example of a cellular automaton) is played on an infinite two-dimensional rectangular grid of cells. Each cell can be either alive or dead. The status of each cell changes each turn of the game (also called a generation) depending on the statuses of that cell's 8 neighbors. Neighbors of a cell are cells that touch that cell, either horizontal, vertical, or diagonal from that cell. -Reminder of game of life rules: +### Reminder of basic game of life rules 1. Any living cell with strictly fewer than two living neighbors dies (referred to as underpopulation or exposure). + 2. Any living cell with strictly more than three living neighbors dies (referred to as overpopulation or overcrowding). + 3. Any dead cell with exactly three living neighbors will come to life. (Referred to as spreading or growth) With the implied additional rules: + 4. Any living cell with two or three living neighbors continues to live, unchanged. -5. Any dead cell who doesn’t have exactly 3 living neighbors stays dead, -unchanged. + +5. Any dead cell who doesn’t have exactly 3 living neighbors stays dead, unchanged. -TEST \ No newline at end of file + +## Features + +- Classic Game of Life Rules: Implements the traditional rules of Conway's Game of Life. +- Customizable Rulesets: Load and apply custom rulesets from JSON files. +- Agent Behavior: Simulate agents with specific behaviors based on their surroundings. +- Interactive Grid: Click on cells to change their states with two modes: cell state change and agent placement. +- Looping Borders: Option to enable or disable looping borders for the grid. (Opposit sides are connected) +- Adjustable Simulation Speed: Change the speed of the simulation using a slider. +- Random Cell Generation: Generate a random initial state for the grid, the density slider allows to adjust the probability for a cell to become alive. +- Save and Load States: Save/Load current state of the simulation. +- Pause and Resume. +- Visual Representation: Display the grid with different colors representing different cell states. + + + +## Run Locally + +Clone the project + +```bash + git clone https://gitarero.ecam.fr/guillaume.bonabau/OOP_F1_Project.git +``` + +Go to the project directory + +```bash + cd OOP_F1_Project +``` + +Install dependencies + +```bash + TODO How to install the json-simple-1.1.1.jar +``` + +Start the program + +```bash + TODO ???? => javac .\src\Main.java +``` + + +## Basic How to + +- Load Rules +By default, Coneways rules are loaded. You can load other set of rules by clicking the "Load Rule" button. + +- Create Field +Create the fild either by generating a random field, or by clicking yourself on cells (Don't forget to Toggle click). + +- Toggle Border +There is 2 different iterations methodes. + +1. Closed +The sides of the gride will count as dead. + +2. Loop +The gride technicaly has no sides. The left handside is connected to the right one, the top to the bottom. +All 4 corners cells are nearby cells of each others. +## Color Reference + +| Color | HEX | RGB | +| ----------------- | ------------------------------------------------------------------ |------| +| Dead | ![#25341F](https://via.placeholder.com/10/25341F?text=+) #25341F | [37,52,31] | +| Alived | ![#a7ed8b](https://via.placeholder.com/10/A7ED8B?text=+) #f8f8f8 | [167,237,139] | + + +## FAQ + +#### Question 1 + +Answer 1 + +#### Question 2 + +Answer 2 + + +## Acknowledgements + + - [OOP Project](https://gitarero.ecam.fr/francois.neron/OOP_H03_test_Project) + - [Francois NERON](https://gitarero.ecam.fr/francois.neron) + +## Appendix + +To download the json library: +https://code.google.com/archive/p/json-simple/downloads + +Workflow : +When starting a task "TODO" edit it to "TODO-INPROGRESS" + +When task is done edit it to "TODO-COMPLETE" + +If there is an error in the code edit it to "TODO-ERROR" + + +Link Canva Whiteboard: +https://www.canva.com/design/DAGCBGF5b4c/4cNmhoS6lSC8Once9r_Tlg/edit?utm_content=DAGCBGF5b4c&utm_campaign=designshare&utm_medium=link2&utm_source=sharebutton +## Authors + +- [@guillaume.bonabau](https://gitarero.ecam.fr/guillaume.bonabau) +- [@g.le-chartier](https://gitarero.ecam.fr/g.le-chartier) +- [@balthazar.squinabol](https://gitarero.ecam.fr/balthazar.squinabol) From 818e8d2e980e34d0b87717c4274f78015d13ab53 Mon Sep 17 00:00:00 2001 From: Balthazar Squinabol Date: Wed, 29 May 2024 17:02:41 +0200 Subject: [PATCH 2/3] SaveState --- src/backend/Sheep.java | 22 ++++++++++++++++---- src/backend/Simulator.java | 30 +++++++++++++++++++++++----- src/windowInterface/MyInterface.java | 7 ++++--- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 9420f05..c3a8250 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -12,6 +12,7 @@ public class Sheep extends Agent { int hunger; Random rand; + Simulator simulator; Sheep(int x,int y){ //first we call the constructor of the superClass(Animal) @@ -24,16 +25,24 @@ public class Sheep extends Agent { rand = new Random(); } - + boolean loopingBorder = simulator.isLoopingBorder(); + int width = simulator.getWidth(); + int height = simulator.getHeight(); /** * action of the animal * it can interact with the cells or with other animals * as you wish */ - public boolean liveTurn(ArrayList neighbors, Simulator world) { - if(world.getCell(x, y)==1) { - world.setCell(x, y, 0); + public boolean liveTurn(ArrayList neighbors, Simulator word) { + + //we check if the sheep is on the border of the world + //If loopingBorder == true, the world is a torus + //If loopingBorder == false, the world is a square and the sheep can't go out of the world + + if(simulator.getCell(x, y)==1) { + simulator.setCell(x, y, 0); + hunger = hunger--; } else { hunger++; } @@ -42,6 +51,11 @@ public class Sheep extends Agent { } private void moveRandom() { + //check if the sheep is on the border of the world + //If loopingBorder == true, the world is a torus + + + int direction = rand.nextInt(4); if(direction == 0) { x+=1; diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 256a03a..e3ed745 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -18,8 +18,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 100; - private final int LINE_NUM = 100; + private final int COL_NUM = 10; + private final int LINE_NUM = 10; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -70,7 +70,8 @@ public class Simulator extends Thread { //Default rule : Survive always, birth never - loadRule("ressources/Rule/conwayRule.json"); + //loadRule("ressources/Rule/conwayRule.json"); + loadRule("OOP_F1_Project/ressources/Rule/conwayRule.json"); } @@ -352,9 +353,28 @@ public class Simulator extends Thread { * the simulated world in its present state */ public ArrayList getSaveState() { - //TODO : complete method with proper return - return null; + ArrayList saveState = new ArrayList<>(); + + // Ensure height and width are properly initialized + int height = getHeight(); // Replace getHeight() with your method to get the height + int width = getWidth(); // Replace getWidth() with your method to get the width + + for (int y = 0; y < height; y++) { + StringBuilder lineBuilder = new StringBuilder(); + for (int x = 0; x < width; x++) { + lineBuilder.append(getCell(x, y)); + if (x < width - 1) { + lineBuilder.append(";"); + } + } + saveState.add(lineBuilder.toString()); + } + return saveState; } + + + + /** * * @param lines of file representing saved world state diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index d33f149..59a8c24 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -351,12 +351,13 @@ public class MyInterface extends JFrame { public void clicSaveToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { + String fileName = SelectFile(); + if (fileName.length() > 0) { ArrayList content = mySimu.getSaveState(); - writeFile(fileName, (String[]) content.toArray()); + writeFile(fileName, content.toArray(new String[0])); } } + public void clicSaveAgentsToFileButton() { String fileName=SelectFile(); From 1b432953a195c607a735416ffbe16fba9c705440 Mon Sep 17 00:00:00 2001 From: Balthazar Squinabol Date: Wed, 29 May 2024 17:07:30 +0200 Subject: [PATCH 3/3] Fix --- src/backend/Sheep.java | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index f6fb5bb..3ff1d63 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -12,7 +12,6 @@ public class Sheep extends Agent { int hunger; Random rand; - Simulator simulator; Sheep(int x,int y){ //first we call the constructor of the superClass(Animal) @@ -25,37 +24,24 @@ public class Sheep extends Agent { rand = new Random(); } - boolean loopingBorder = simulator.isLoopingBorder(); - int width = simulator.getWidth(); - int height = simulator.getHeight(); + /** * action of the animal * it can interact with the cells or with other animals * as you wish */ - public boolean liveTurn(ArrayList neighbors, Simulator word) { - - //we check if the sheep is on the border of the world - //If loopingBorder == true, the world is a torus - //If loopingBorder == false, the world is a square and the sheep can't go out of the world - - if(simulator.getCell(x, y)==1) { - simulator.setCell(x, y, 0); - hunger = hunger--; + public boolean liveTurn(ArrayList neighbors, Simulator world) { + if(world.getCell(x, y)==1) { + world.setCell(x, y, 0); } else { hunger++; } - this.moveRandom(world); + this.moveRandom(); return hunger<10; //condition to be alive } private void moveRandom() { - //check if the sheep is on the border of the world - //If loopingBorder == true, the world is a torus - - - int direction = rand.nextInt(4); if(direction == 0) { x+=1; @@ -70,4 +56,6 @@ public class Sheep extends Agent { y-=1; } } -} + + +} \ No newline at end of file