genCells no finished

This commit is contained in:
Aubin DORIVAL
2025-03-31 11:03:40 +02:00
parent 0140f1ed93
commit f7825248bc
3 changed files with 98 additions and 17 deletions

View File

@@ -5,10 +5,20 @@ public class GomokuBoard {
/** The firstcell in the board, at the top left of the board. */ /** The firstcell in the board, at the top left of the board. */
private GomokuCell firstCell; private GomokuCell firstCell;
/** The width of the GomokuBoard.*/ /** The width of the GomokuBoard.*/
private int boardWith; private int boardWidth;
/** The height of the GomokuBoard.*/ /** The height of the GomokuBoard.*/
private int boardHeight; private int boardHeight;
public static void main(String[] args) {
Color[][] colors = {{Color.BLACK,Color.BLACK,Color.BLACK},
{Color.BLACK,Color.BLACK,Color.BLACK},
{Color.WHITE,Color.WHITE,Color.WHITE}};
GomokuBoard test = new GomokuBoard(3,3, colors);
test.get(1,1).setState(Color.BLACK);
System.out.println(test);
System.out.println(test.getPlayableCells());
}
//------------------Constructors-------------------------- //------------------Constructors--------------------------
/** /**
@@ -17,9 +27,47 @@ public class GomokuBoard {
* @param height Size of height. * @param height Size of height.
*/ */
public GomokuBoard(int width, int height){ public GomokuBoard(int width, int height){
this(width, height, null);
}
public GomokuBoard(int width, int height, Color[][] colors){
this.firstCell = new GomokuCell(Color.NIL); this.firstCell = new GomokuCell(Color.NIL);
this.boardWith = width; this.boardWidth = width;
this.boardHeight = height; this.boardHeight = height;
this.genCells(width, height, colors);
}
private void genCells(int width, int height, Color[][] colors){
this.firstCell = new GomokuCell(colors == null ? Color.NIL : colors[0][0]);
GomokuCell act = this.firstCell;
GomokuCell top = null;
for (int i = 0; i < height; i++){
GomokuCell nextLine = null;
if (i != height-1){
nextLine = new GomokuCell(colors == null ? Color.NIL: colors[i+1][0]);
GomokuCell.link(act, nextLine, Cardinal.S);
}
for (int j = 1; j < width; j++) {
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);
}
if (j != width -1 ){
GomokuCell right = new GomokuCell(colors == null ? Color.NIL : colors[i][j+1] );
GomokuCell.link(act, right, Cardinal.E);
act = right;
}
}
act = nextLine;
if (act == null) break;
top = act.getNeighbour(Cardinal.N);
}
} }
//------------------Gets-------------------------- //------------------Gets--------------------------
@@ -45,10 +93,24 @@ public class GomokuBoard {
} }
/**
* This method return all cells in the board in the order.
* @return All cells with a array 2D.
*/
private GomokuCell[][] getAllsCells(){ private GomokuCell[][] getAllsCells(){
GomokuCell[][] cells = new GomokuCell[this.boardWith][this.boardHeight]; GomokuCell[][] cells = new GomokuCell[this.boardHeight][this.boardWidth];
int i = 0, j = 0; GomokuCell act = this.firstCell;
GomokuCell nextLine = this.firstCell.getNeighbour(Cardinal.S); GomokuCell nextLine = this.firstCell.getNeighbour(Cardinal.S);
for (int i = 0; i < this.boardHeight; i++) {
for (int j = 0; j < this.boardWidth; j++) {
cells[i][j] = act;
act = act.getNeighbour(Cardinal.E);
}
act = nextLine;
if (act != null){
nextLine = act.getNeighbour(Cardinal.S);
}
}
return cells; return cells;
} }
@@ -59,13 +121,10 @@ public class GomokuBoard {
public List<GomokuCell> getPlayableCells(){ public List<GomokuCell> getPlayableCells(){
List<GomokuCell> output = new ArrayList<>(); List<GomokuCell> output = new ArrayList<>();
GomokuCell act = null; for (GomokuCell[] line : this.getAllsCells()) {
for (int i = 0; i < this.boardWith; i++) { for (GomokuCell c : line) {
for (int j = 0; j < this.boardHeight; j++) { if (c.isPlayable())
act = this.get(i,j); output.add(c);
if (act.isPlayable()){
output.add(act);
}
} }
} }
@@ -75,8 +134,20 @@ public class GomokuBoard {
//------------------Overides-------------------------- //------------------Overides--------------------------
/**
* This method print the board cell by cell with change line when
* the line is finished.
*/
@Override @Override
public String toString() { public String toString() {
return ""; StringBuilder out = new StringBuilder();
GomokuCell[][] cells = this.getAllsCells();
for (GomokuCell[] line : cells) {
for (GomokuCell c : line) {
out.append(c.toString());
}
out.append("\n");
}
return out.toString();
} }
} }

View File

@@ -1,6 +1,8 @@
import java.util.EnumMap; import java.util.EnumMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
/** /**
* This class is cell of the board of the Gomoku game. * This class is cell of the board of the Gomoku game.
*/ */
@@ -56,8 +58,6 @@ public class GomokuCell{
* @throws IllegalStateException If the cell is not playable. * @throws IllegalStateException If the cell is not playable.
*/ */
public void setState(Color c){ public void setState(Color c){
if (!this.isPlayable())
throw new IllegalStateException("This cell is not playable !" + this.toString());
this.state = c; this.state = c;
} }
@@ -104,12 +104,22 @@ public class GomokuCell{
this.neighbour.put(car, cell); this.neighbour.put(car, cell);
} }
//------------------Statics--------------------------
public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2){
if (c1 == null || c2 == null) return ;
List<Cardinal> cardinals = Arrays.asList(Cardinal.values());
c1.linkCell(c1Toc2, c2);
c2.linkCell(cardinals.get((cardinals.indexOf(c1Toc2) + 4) % 8), c1);
}
//------------------Overides-------------------------- //------------------Overides--------------------------
@Override @Override
public String toString() { public String toString() {
switch (this.getSate()) { switch (this.getSate()) {
case Color.NIL: case Color.NIL:
return " "; return ".";
case Color.BLACK: case Color.BLACK:
return "X"; return "X";
case Color.WHITE: case Color.WHITE:

View File

@@ -33,6 +33,6 @@ public class GomokuGame {
} }
public GomokuBoard getBoard() { public GomokuBoard getBoard() {
return new GomokuBoard(0,0);
} }
} }