From aa7ff4311f867d30c840b1e632f3c3116772c207 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Mon, 29 Apr 2024 17:07:21 +0200 Subject: [PATCH 1/3] made rules + trying to optimise countnear --- src/backend/Simulator.java | 67 +++++++++++++++++++++++--------------- src/backend/Table.java | 21 ++++++------ 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 87de99c..be30fb8 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -71,6 +71,7 @@ public class Simulator extends Thread { //Should probably stay as is public void run() { int stepCount=0; + System.out.println("Step Count: "+ stepCount); while(!stopFlag) { stepCount++; makeStep(); @@ -114,26 +115,10 @@ public class Simulator extends Thread { } //then evolution of the field // TODO-INPROGRESS : apply game rule to all cells of the field - Table tempTable = new Table(this.height, this.width, this); - for(int x=0; x3) { - tempTable.getCell(x,y).setValue(0); - } - } else { - if(table.countNear(x,y)==3) { - tempTable.getCell(x,y).setValue(1); - } - } - + this.applyRule(); - } - } - this.table = tempTable; - } + + } /* you should distribute this action in methods/classes * don't write everything here ! @@ -216,7 +201,7 @@ public class Simulator extends Thread { */ public ArrayList getAnimals(){ return agents; -} + } /** * selects Animals in a circular area of simulated world * @param x center @@ -244,7 +229,6 @@ public class Simulator extends Thread { public void setCell(int x, int y, int val) { //TODO : complete method this.table.getCell(x, y).setValue(val); - // set cell value to !currentCellValue } public void countAround(int x, int y) { @@ -372,26 +356,57 @@ public class Simulator extends Thread { System.out.println("empty rule file"); return; } - //TODO : remove previous rule (=emptying lists) - + //TODO-INPROGRESS : remove previous rule (=emptying lists) + fieldSurviveValues = new ArrayList(); + fieldBirthValues = new ArrayList(); String surviveLine = lines.get(0); String birthLine = lines.get(1); + String[] surviveElements = surviveLine.split(";"); for(int x=0; x getAgentsSave() { //TODO : Same idea as the other save method, but for agents diff --git a/src/backend/Table.java b/src/backend/Table.java index cc4463d..91f992a 100644 --- a/src/backend/Table.java +++ b/src/backend/Table.java @@ -56,24 +56,25 @@ public class Table { this.table.get(row).set(column, cell); } - //TODO-complete : count near (xy) -> return how many cells around this cell + //TODO-INPROGRESS : count near (xy) -> return how many cells around this cell + // NEED A STRONG OPTIMISATION public int countNear(int row, int column){ int cellCount =0; - // if border is true - for (int i = row-1;i<=row+1;i++){ - for (int j = column-1;j<=column+1;column++){ - if (!(i == j)){ - if (i<0 || i>=width || j<0 || j>=height){ + for (int x = row-1;x<=row+1;x++){ + for (int y = column-1;y<=column+1;column++){ + if (!(x == y)){ + try { + cellCount += this.getCell(x,y).getValue(); + } + catch(Exception e) { + cellCount +=0; //TODO what to do when out of bound (wall alive or dead/ looping border) (wall dead by default) - } else { - cellCount += this.getCell(i,j).getValue(); } } } } - return cellCount; - //if border is false + return 0; } //TODO : set agent (x y agent) load an agent to coordinates x,y From d6b70381675ba948879a3690e15c3c40508c5189 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 8 May 2024 09:52:16 +0200 Subject: [PATCH 2/3] opimised countnear (thx gpt) --- src/backend/Table.java | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/backend/Table.java b/src/backend/Table.java index 91f992a..0642adc 100644 --- a/src/backend/Table.java +++ b/src/backend/Table.java @@ -59,23 +59,27 @@ public class Table { //TODO-INPROGRESS : count near (xy) -> return how many cells around this cell // NEED A STRONG OPTIMISATION public int countNear(int row, int column){ - int cellCount =0; - for (int x = row-1;x<=row+1;x++){ - for (int y = column-1;y<=column+1;column++){ - if (!(x == y)){ - try { - cellCount += this.getCell(x,y).getValue(); - } - catch(Exception e) { - cellCount +=0; - //TODO what to do when out of bound (wall alive or dead/ looping border) (wall dead by default) - } - } - } - - } - return 0; + int numRows = this.width; + int numColumns = this.height; + // Define the relative positions of neighboring cells (assuming 8 neighbors) + int[][] neighbors = { + {-1, -1}, {-1, 0}, {-1, 1}, + {0, -1}, {0, 1}, + {1, -1}, {1, 0}, {1, 1} + }; + + int count = 0; + for (int[] neighbor : neighbors) { + int newRow = row + neighbor[0]; + int newColumn = column + neighbor[1]; + // Check if the new indices are within the table boundaries + if (newRow >= 0 && newRow < numRows && newColumn >= 0 && newColumn < numColumns) { + count+= this.getCell(newRow, newColumn).getValue(); + } + } + + return count; } //TODO : set agent (x y agent) load an agent to coordinates x,y From 83d3811a172922f1b7a39ee5a3249779a8bdc6ba Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 8 May 2024 09:54:04 +0200 Subject: [PATCH 3/3] le code de gpt mais joli --- src/backend/Table.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/backend/Table.java b/src/backend/Table.java index 0642adc..3942223 100644 --- a/src/backend/Table.java +++ b/src/backend/Table.java @@ -59,9 +59,7 @@ public class Table { //TODO-INPROGRESS : count near (xy) -> return how many cells around this cell // NEED A STRONG OPTIMISATION public int countNear(int row, int column){ - int numRows = this.width; - int numColumns = this.height; - + int count = 0; // Define the relative positions of neighboring cells (assuming 8 neighbors) int[][] neighbors = { {-1, -1}, {-1, 0}, {-1, 1}, @@ -69,13 +67,12 @@ public class Table { {1, -1}, {1, 0}, {1, 1} }; - int count = 0; for (int[] neighbor : neighbors) { - int newRow = row + neighbor[0]; - int newColumn = column + neighbor[1]; + int x = row + neighbor[0]; + int y = column + neighbor[1]; // Check if the new indices are within the table boundaries - if (newRow >= 0 && newRow < numRows && newColumn >= 0 && newColumn < numColumns) { - count+= this.getCell(newRow, newColumn).getValue(); + if (x >= 0 && x < this.width && y >= 0 && y < this.height) { + count+= this.getCell(x, y).getValue(); } }