This commit is contained in:
Aubin DORIVAL
2025-04-14 09:24:15 +02:00
2 changed files with 46 additions and 13 deletions

View File

@@ -2,6 +2,10 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
<<<<<<< HEAD
=======
import java.util.EnumMap;
>>>>>>> 20e2f2eaec0f2988bef55ac30d3f0b3f123b171d
/** /**
* The board of the game Gomoku. * The board of the game Gomoku.
@@ -247,6 +251,35 @@ public class GomokuBoard {
return map; 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. * This method return the number max of the aligned Cells.
* *
@@ -255,10 +288,9 @@ public class GomokuBoard {
*/ */
public int countMax(Map<Cardinal, Integer> mapColor) { public int countMax(Map<Cardinal, Integer> mapColor) {
Map<Cardinal, Integer> map = new HashMap<>();
int max = 0; int max = 0;
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) { for (Map.Entry<Cardinal, Integer> entry : mapColor.entrySet()) {
if (entry.getValue() > max) { if(entry.getValue() > max){
max = entry.getValue(); max = entry.getValue();
} }
} }

View File

@@ -29,18 +29,14 @@ public class GomokuGame {
GomokuGame(boolean renderer) { GomokuGame(boolean renderer) {
this.playRenderer = renderer; this.playRenderer = renderer;
this.board = new GomokuBoard(15, 15);
this.board.get(7, 7).setState(Color.BLACK);
this.player1 = new Human("un", Color.WHITE, 60);
this.player2 = new GomokuAI("deux", Color.BLACK, 60);
} }
public static void main(String[] args) { public static void main(String[] args) {
int sizeX = 0; int sizeX = 15;
int sizeY = 0; int sizeY = 15;
int nbToken = 0; int nbToken = 50;
int nbJetonsAligne = 0; int nbJetonsAligne = 5;
boolean renderer = false; boolean renderer = false;
String path_a_load = ""; String path_a_load = "";
@@ -88,6 +84,10 @@ public class GomokuGame {
} }
} }
GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non
g.board = new GomokuBoard(sizeX, sizeY);
g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK);
g.player1 = new Human("un", Color.WHITE, nbToken);
g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1);
System.out.println(g.board); System.out.println(g.board);
// g.renderer.update(); // g.renderer.update();
g.startGame(); g.startGame();
@@ -146,7 +146,7 @@ public class GomokuGame {
} }
System.out.println(board.countMax(board.countAlignedCells(currentPlay))); System.out.println(board.countMax(board.countAlignedCells(currentPlay)));
if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) { if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) {
this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !"); // this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !");
return; return;
} }
this.currentPlayer.tokens -= 1; this.currentPlayer.tokens -= 1;
@@ -154,7 +154,8 @@ public class GomokuGame {
System.out.println(this.board); System.out.println(this.board);
} }
this.renderer.updateStatus("Match nul, il ne reste plus de jeton."); // this.renderer.updateStatus("Match nul, il ne reste plus de jeton.");
System.out.println("Match nul, il ne reste plus de jeton");
return; return;
} }