所以,经过查看您的代码,似乎对角线检查只能在单个方向上获胜(如果我将一个令牌添加到最低行和最低列会发生什么?)
相反,基本的检查算法始终是相同的过程,无论您要检查哪个方向。
您需要一个起点(x / y)和x / y增量(移动方向)。 您可以将其总结为单个方法...
public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
int test = grid[row][col];
if (test != check) {
win = false;
break;
}
}
row += rowDelta;
col += colDelta;
}
return win;
}
这将基本上允许您检查四个方向,但也可以向后检查。
因此,如果我们使用类似于...
int[][] gridTable = new int[ROWS][COLUMNS];
gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;
System.out.println("Vertical");
System.out.println(didWin(gridTable, 1, ROWS - 4, 3, 1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 1, 3, -1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 0, 3, 1, 0) ? "Win" : "Lose");
gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;
System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, 0, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 0, 0, 1) ? "Win" : "Lose");
gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;
System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1, 1, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, -1, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 1, 2, 1, 1) ? "Win" : "Lose");
这个输出指的是什么?
Vertical
Win
Win
Lose
Horizontal
Win
Win
Lose
Diag
Win
Win
Lose
现在,你可以将其概括为...
public boolean didWin(int[][] grid, int check, int row, int col) {
return didWin(grid, check, row, col, 1, 0) ||
didWin(grid, check, row, col, -1, 0) ||
didWin(grid, check, row, col, 0, 1) ||
didWin(grid, check, row, col, 0, -1) ||
didWin(grid, check, row, col, 1, 1) ||
didWin(grid, check, row, col, -1, -1) ||
didWin(grid, check, row, col, -1, 1) ||
didWin(grid, check, row, col, 1, -1);
}
因此,使用类似于...的东西......
int[][] gridTable = new int[ROWS][COLUMNS];
gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;
System.out.println("Vertical");
System.out.println(didWin(gridTable, 1, ROWS - 1, 3) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 4, 3) ? "Win" : "Lose");
gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;
System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");
gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;
System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");
打印出类似于以下内容...
Vertical
Win
Win
Horizontal
Win
Win
Diag
Win
Win
我想补充一点,这种方法只有在您提供了一行中4个芯片的正确起始位置时才有效。例如,对于您的水平检查,didWin(gridTable,1,3,3)将提供false而不是true,因为循环只能检查一个方向。
我的意图并不是提供一个“开箱即用”的解决方案,而是提供一个概念,可以从中开发出更广泛的解决方案(我的意思是,我讨厌人们实际上必须去思考;))。我还根据OP知道最后一块放置的位置,即起点的想法设计了解决方案;)
通过略微修改didWin方法,可以从任何点检查n乘n网格...
public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
boolean match = false;
int matches = 0;
while (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
int test = grid[row][col];
if (test != check && match) {
break;
} else if (test == check) {
match = true;
matches++;
}
row += rowDelta;
col += colDelta;
}
return matches == 4;
}
所以,我使用了...
public static final int ROWS = 8;
public static final int COLUMNS = 8;
int[][] gridTable = new int[ROWS][COLUMNS];
gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;
for (int[] row : gridTable) {
StringJoiner sj = new StringJoiner("|", "|", "|");
for (int col : row) {
sj.add(Integer.toString(col));
}
System.out.println(sj);
}
System.out.println(didWin(gridTable, 1, 3, 3));
我成功地让它工作了。有时候一个答案并不是完整的解决方案,而是一个种子,可以激发灵感带领某人走向新的领域 ;)
更进一步的改进将包括提供预期联接部件的数量,但我相信这是一个我真的不需要演示的改进 ;)