From d6b70381675ba948879a3690e15c3c40508c5189 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 8 May 2024 09:52:16 +0200 Subject: [PATCH] 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