expand + doc
This commit is contained in:
@@ -5,6 +5,9 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The board of the game Gomoku.
|
||||||
|
*/
|
||||||
public class GomokuBoard{
|
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;
|
||||||
@@ -21,6 +24,9 @@ public class GomokuBoard{
|
|||||||
test.get(1,1).setState(Color.BLACK);
|
test.get(1,1).setState(Color.BLACK);
|
||||||
System.out.println(test);
|
System.out.println(test);
|
||||||
System.out.println(test.getPlayableCells());
|
System.out.println(test.getPlayableCells());
|
||||||
|
test.expandBoard(Cardinal.SW);
|
||||||
|
System.out.println(test);
|
||||||
|
System.out.println(test.getPlayableCells());
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------Constructors--------------------------
|
//------------------Constructors--------------------------
|
||||||
@@ -185,6 +191,39 @@ public class GomokuBoard{
|
|||||||
return max;
|
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){
|
public void expandBoard(Cardinal direction){
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case Cardinal.SE:
|
case Cardinal.SE:
|
||||||
@@ -213,8 +252,6 @@ public class GomokuBoard{
|
|||||||
|
|
||||||
List<Cardinal> cardinals = Arrays.asList(Cardinal.values());
|
List<Cardinal> cardinals = Arrays.asList(Cardinal.values());
|
||||||
Cardinal topDirection = cardinals.get((cardinals.indexOf(direction) + 4)%8);
|
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);
|
GomokuCell act = new GomokuCell(Color.NIL);
|
||||||
int maxLen = 0;
|
int maxLen = 0;
|
||||||
@@ -225,43 +262,38 @@ public class GomokuBoard{
|
|||||||
top = this.get(0, 0);
|
top = this.get(0, 0);
|
||||||
maxLen = this.boardWidth;
|
maxLen = this.boardWidth;
|
||||||
newOrigin = act;
|
newOrigin = act;
|
||||||
|
this.genLineOrColumn(act, top, topDirection, Cardinal.SW, Cardinal.SE, Cardinal.E, maxLen);
|
||||||
|
this.boardHeight += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Cardinal.W:
|
case Cardinal.W:
|
||||||
top = this.get(0,0);
|
top = this.get(0,0);
|
||||||
maxLen = this.boardHeight;
|
maxLen = this.boardHeight;
|
||||||
newOrigin = act;
|
newOrigin = act;
|
||||||
|
this.genLineOrColumn(act, top, topDirection, Cardinal.NE, Cardinal.SE, Cardinal.S, maxLen);
|
||||||
|
this.boardWidth += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Cardinal.S:
|
case Cardinal.S:
|
||||||
top = this.get(this.boardHeight-1, 0);
|
top = this.get(this.boardHeight-1, 0);
|
||||||
maxLen = this.boardWidth;
|
maxLen = this.boardWidth;
|
||||||
newOrigin = this.get(0, 0);
|
newOrigin = this.get(0, 0);
|
||||||
|
this.genLineOrColumn(act, top, topDirection, Cardinal.NW, Cardinal.NE, Cardinal.E, maxLen);
|
||||||
|
this.boardHeight += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Cardinal.E:
|
case Cardinal.E:
|
||||||
top = this.get(0, this.boardWidth-1);
|
top = this.get(0, this.boardWidth-1);
|
||||||
maxLen = this.boardHeight;
|
maxLen = this.boardHeight;
|
||||||
newOrigin = this.get(0, 0);
|
newOrigin = this.get(0, 0);
|
||||||
|
this.genLineOrColumn(act, top, topDirection, Cardinal.NW, Cardinal.SW, Cardinal.S, maxLen);
|
||||||
|
this.boardWidth += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("Why are you here ?");
|
throw new IllegalStateException("Why are you here ?");
|
||||||
}
|
}
|
||||||
|
this.firstCell = newOrigin;
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------Overides--------------------------
|
//------------------Overides--------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user