From aa7ff4311f867d30c840b1e632f3c3116772c207 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Mon, 29 Apr 2024 17:07:21 +0200 Subject: [PATCH] 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