Redo countAlignedCells in GomokuBoard
This commit is contained in:
@@ -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<Cardinal, Integer> countAlignedCellsv2(GomokuCell cell) {
|
||||
|
||||
EnumMap<Cardinal, Integer> 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<Cardinal, Integer> mapColor){
|
||||
|
||||
Map<Cardinal, Integer> map = new HashMap<>();
|
||||
int max = 0;
|
||||
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) {
|
||||
for (Map.Entry<Cardinal, Integer> entry : mapColor.entrySet()) {
|
||||
if(entry.getValue() > max){
|
||||
max = entry.getValue();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user