我正在创建一个由JButtons组成的二维单元格数组(如果有意义的话)来创建一个类似于井字游戏的游戏。 Example of what im trying to accomplish 忘了添加,如果一个小区被包围,则赢得游戏
我目前在编辑for循环时失去了考虑从当前单元格中分配一个或零,并将其分配给局部变量。
当我说topNum = board [row-1] [col]; ,我将如何检索分配给该单元格的一个或零。 (我使用setText属性将数字分配给单元格。)
感谢您的帮助
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;
}
答案 0 :(得分:2)
从简单的事情开始,确定一个单元格当前是否被玩家占用
protected boolean isOffBoard(int x, int y) {
return x < 0 || y < 0 || y >= board.length || x >= board[y].length;
}
protected boolean isOccupied(int x, int y) {
if (isOffBoard(x, y)) {
return true;
}
return board[y][x] != null;
}
protected boolean isOccupiedByPlayer(int player, int x, int y) {
if (isOffBoard(x, y)) {
return true;
}
if (!isOccupied(x, y)) {
return false;
}
return board[y][x] == player;
}
这三种方法做得很简单
这很方便,因为现在我们可以简单地对特定点进行循环检查,并确定玩家是否被包围,这样的事情......
protected boolean isSurroundedBy(int player, int x, int y) {
return isOccupiedByPlayer(player, x - 1, y - 1)
&& isOccupiedByPlayer(player, x, y - 1)
&& isOccupiedByPlayer(player, x + 1, y - 1)
&& isOccupiedByPlayer(player, x - 1, y)
&& isOccupiedByPlayer(player, x + 1, y)
&& isOccupiedByPlayer(player, x - 1, y + 1)
&& isOccupiedByPlayer(player, x, y + 1)
&& isOccupiedByPlayer(player, x + 1, y + 1);
}
这只是检查给定点周围的单元格是否被特定玩家占用
我使用以下方法测试它......
board[1][1] = 0;
System.out.println("");
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 1, 1));
board = new Integer[3][3];
board[0][0] = 0;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));
board[1][0] = 1;
board[1][1] = 1;
board[0][1] = 1;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));
board = new Integer[3][3];
board[0][0] = 0;
board[1][0] = 1;
board[1][1] = 0;
board[0][1] = 1;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));
哪些输出......
1x1 isOccupiedByPlayer 0 = true
isSurrounded = false
0x0 isOccupiedByPlayer 0 = true
isSurrounded = false
0x0 isOccupiedByPlayer 0 = true
1x1 isOccupiedByPlayer 0 = false
isSurrounded = true
0x0 isOccupiedByPlayer 0 = true
1x1 isOccupiedByPlayer 0 = true
isSurrounded = false
现在,这可能无法满足您的绝对需求,但应该给您一个跳跃点
答案 1 :(得分:0)
如果您使用setText()将一个或零分配给单元格。我会使用getText()来检索指定的值。