I am creating a Two Dimensional Array of Cells consisting of JButtons (If that makes sense) to create a game similar to tic-tac-toe. Example of what im trying to accomplish Forgot to add that if a cell is surrounded, the game is won
I am currently lost at editing the for loop to account for assigning either the one or zero from that current cell, and assigning it to a local variable.
When I say topNum = board[row-1][col]; , how will I go about retrieving either the one or zero assigned to that cell. (I am using a setText property to assign the number to the cell.)
Thanks for your help
public boolean isWinner() {
for (int row = 0; row < this.sizeRow; row++) {
for (int col = 0; col < this.sizeCol; col++) {
Cell num = board[row][col];
// not a winner if this cell is null
if (num != null) {
// Look at above cell
Integer topNum = -1;
if (row > 0) {
topNum = board[row-1][col];
if (topNum == null) break;
}
// Look at right cell
Integer rightNum = -1;
if (col == this.sizeCol-1) {
rightNum = board[row][col+];
if (rightNum == null) break;
}
// Look at left cell
Integer leftNum = -1;
if (col == this.sizeCol-1) {
rightNum = board[row][col-];
if (rightNum == null) break;
}
// Look at bottom cell
Integer bottomNum = -1;
if (row > 0) {
topNum = board[row+1][col];
if (topNum == null) break;
}
// do similar for left cell and bottom cell
// Check that topNum is not our current cell
// then check that topNum is the same number as right left and bottom OR those cells are -1, which
// means that side was on an edge.
if (!num.equals(topNum)) {
if ((topNum.equals(rightNum) || rightNum.equals(-1))
&& (topNum.equals(leftNum) || leftNum.equals(-1))
&& (topNum.equals(bottomNum) || bottomNum.equals(-1)) {
return true;
}
}
}
}
}
return false;
}