diff --git a/src/GomokuBoard.java b/src/GomokuBoard.java index 879539b..ceaf7e3 100644 --- a/src/GomokuBoard.java +++ b/src/GomokuBoard.java @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - +import java.util.EnumMap; /** * The board of the game Gomoku. @@ -221,6 +221,35 @@ public class GomokuBoard{ return map; } + public EnumMap countAlignedCellsv2(GomokuCell cell) { + + EnumMap map = new EnumMap<>(Cardinal.class); + + // Iterate over all different axes (4 directions) + for (int i = 0; i < 4; i++) { + Cardinal direction = Cardinal.fromInt(i); + int count = 1; // Start with the current cell + + // Check in the positive direction + GomokuCell nextCell = cell.getNeighbour(direction); + while (nextCell != null && nextCell.getState() == cell.getState()) { + count++; + nextCell = nextCell.getNeighbour(direction); + } + + // Check in the negative direction + nextCell = cell.getNeighbour(direction.inverse()); + while (nextCell != null && nextCell.getState() == cell.getState()) { + count++; + nextCell = nextCell.getNeighbour(direction.inverse()); + } + + map.put(direction, count); + } + + return map; + } + /** * This method return the number max of the aligned Cells. * @param mapColor A map of number aligned cells. @@ -228,9 +257,8 @@ public class GomokuBoard{ */ public int countMax(Map mapColor){ - Map map = new HashMap<>(); int max = 0; - for (Map.Entry entry : map.entrySet()) { + for (Map.Entry entry : mapColor.entrySet()) { if(entry.getValue() > max){ max = entry.getValue(); }