【发布时间】:2014-03-01 17:51:06
【问题描述】:
我有一个类似下面的数组。
int[][] myArray =
{{1, 2, 3, 0, 0, 1}
{1, 0, 4, 4, 0, 1}
{1, 2, 4, 3, 4, 0}
{2, 2, 0, 0, 2, 2}
{3, 0, 0, 3, 0, 0}
{4, 2, 3, 0, 0, 0}}
它会说一个赢了,因为第一列的三个 1 中的 1。两个人不会赢,因为他们不在一个“行”中。
我想进行某种获胜检查,以便在行、对角线或列中找到三个相同的数字。有点像井字游戏,但网格更大。在我使用一组凌乱的 if 语句和 goto 语句之前。 (它是用 Basic 编写的。)我尝试使用一个系统,它从最后放置的一块中找到方向,其中有许多相同的,但它不能正常工作。我怎样才能以一种简单且可维护的方式做到这一点?
尝试过的代码:
private static boolean checkBoardCombinations(int[][] board, int inputX, int inputY) {
int currentPlayer = board[inputX-1][inputY-1];
boolean[][] directions = new boolean[3][3];
for(int y = 0; y >= -2; y--){
for(int x = 0; x >= -2; x--){
if(inputX+x >= 0 && inputX+x <= 7 && inputY+y >= 0 && inputY+y <= 7
&& (board[inputX+x][inputY+y] == currentPlayer)){
//System.out.printf("X: %s Y: %s", inputX+x, inputY+y);
directions[x+2][y+2] = true;
}
else{
directions[x+2][y+2] = false;
}
//System.out.printf("X: %s Y: %s B: %s,", inputX+x, inputY+y, directions[x+2][y+2]);
}
//System.out.println();
}
/*
for(int x = 0; x <= 2; x++){
for(int y = 0; y <= 2; y++){
System.out.print(directions[x][y] + " ");
}
System.out.println();
}
*/
return false;
}
【问题讨论】:
-
应用什么逻辑或者粘贴代码会更好
-
3个相同的值必须一个接一个,还是可以分散在行、对角线或列中?
-
这些数字的范围是多少?它们只是个位数吗?或者它们的范围可以是任何值吗?还是它们只是 0、1、2、3 和 4?
-
@NiksTyagi 查看底部的代码。我正在考虑创建一个系统,它可以扫描最后放置的相同编号的部件。它将存储最接近的数字与最后放置的棋子相同的方向。从那里它将继续向那个方向扫描。最后它会查看一行、一列或对角线中是否存在三个真值。
-
@halex 数字必须是感人的,有点,所以是的,它们必须一个接一个。