Merge branch 'master' of gitlab.isima.fr:audorival/gomoku

This commit is contained in:
Dorian HAMDANI
2025-03-31 17:21:14 +02:00

View File

@@ -5,6 +5,9 @@ import java.util.List;
import java.util.Map;
/**
* The board of the game Gomoku.
*/
public class GomokuBoard{
/** The firstcell in the board, at the top left of the board. */
private GomokuCell firstCell;
@@ -21,6 +24,9 @@ public class GomokuBoard{
test.get(1,1).setState(Color.BLACK);
System.out.println(test);
System.out.println(test.getPlayableCells());
test.expandBoard(Cardinal.SW);
System.out.println(test);
System.out.println(test.getPlayableCells());
}
//------------------Constructors--------------------------
@@ -183,6 +189,39 @@ public class GomokuBoard{
return max;
}
/**
* This method create one line/column in a specific direction. This method is used in expandBoard, it's no recommended to
* used it in other context.
* @param act Is the cell of the start of the line/column.
* @param top Is the cell of the start of the line/column already existing.
* @param topDir The direction of the line top in relation of the line/column we create.
* @param m The direction topDir - 1.
* @param p The direction topDir + 1.
* @param dir Direction where to add the next cell.
* @param maxLen Number of cell ton gen.
*/
private void genLineOrColumn(GomokuCell act, GomokuCell top, Cardinal topDir, Cardinal m, Cardinal p, Cardinal dir, int maxLen){
for (int i = 0; i < maxLen; i++) {
if (top != null) {
GomokuCell.link(act, top.getNeighbour(topDir),m);
GomokuCell.link(act, top, topDir);
top = top.getNeighbour(dir);
GomokuCell.link(act, top , m);
}
if (i != maxLen -1 ){
GomokuCell right = new GomokuCell(Color.NIL);
GomokuCell.link(act, right, dir);
act = right;
}
}
}
/**
* This method expand the board.
* @param direction Is the direction where the method expand the board.
*/
public void expandBoard(Cardinal direction){
switch (direction) {
case Cardinal.SE:
@@ -211,8 +250,6 @@ public class GomokuBoard{
List<Cardinal> cardinals = Arrays.asList(Cardinal.values());
Cardinal topDirection = cardinals.get((cardinals.indexOf(direction) + 4)%8);
Cardinal topMinus = cardinals.get((cardinals.indexOf(topDirection) + 2)%8);
Cardinal topPlus = cardinals.get((cardinals.indexOf(topDirection) - 2)%8);
GomokuCell act = new GomokuCell(Color.NIL);
int maxLen = 0;
@@ -223,43 +260,38 @@ public class GomokuBoard{
top = this.get(0, 0);
maxLen = this.boardWidth;
newOrigin = act;
this.genLineOrColumn(act, top, topDirection, Cardinal.SW, Cardinal.SE, Cardinal.E, maxLen);
this.boardHeight += 1;
break;
case Cardinal.W:
top = this.get(0,0);
maxLen = this.boardHeight;
newOrigin = act;
this.genLineOrColumn(act, top, topDirection, Cardinal.NE, Cardinal.SE, Cardinal.S, maxLen);
this.boardWidth += 1;
break;
case Cardinal.S:
top = this.get(this.boardHeight-1, 0);
maxLen = this.boardWidth;
newOrigin = this.get(0, 0);
this.genLineOrColumn(act, top, topDirection, Cardinal.NW, Cardinal.NE, Cardinal.E, maxLen);
this.boardHeight += 1;
break;
case Cardinal.E:
top = this.get(0, this.boardWidth-1);
maxLen = this.boardHeight;
newOrigin = this.get(0, 0);
this.genLineOrColumn(act, top, topDirection, Cardinal.NW, Cardinal.SW, Cardinal.S, maxLen);
this.boardWidth += 1;
break;
default:
throw new IllegalStateException("Why are you here ?");
}
for (int i = 0; i < maxLen; i++) {
if (top != null) {
GomokuCell.link(act, top.getNeighbour(Cardinal.W), Cardinal.NW);
GomokuCell.link(act, top, Cardinal.N);
top = top.getNeighbour(Cardinal.E);
GomokuCell.link(act, top , Cardinal.NE);
}
}
this.firstCell = newOrigin;
}
//------------------Overides--------------------------