Helenr
因此,浏览完您的代码后,似乎对角线检查只能在一个方向上获胜(如果我在最低行和最低列中添加标记,会发生什么情况?)而是,基本检查算法始终是相同的过程,而不管您要检查的方向是什么。您需要一个起点(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");哪个输出...VerticalWinWinLoseHorizontalWinWinLoseDiagWinWinLose现在,您可以将其总结为...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");哪个打印出类似...VerticalWinWinHorizontalWinWinDiagWinWin我要补充一点,这种方法仅在您连续提供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));并能够使其正常工作。有时答案不是一个完整的解决方案,而是将某人带到新地方的想法的种子;)我将进一步增强功能,包括提供预期的连接件数量,但是我很确定这是我真的不需要演示的增强功能;)