试图弄清楚如何比较4x4矩阵的次对角线上的所有元素是否彼此相等。我怎么能这样做? 如何为主对角线
矩阵看起来像这样
00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33
我在问,因为我正试图在java中实现可调整大小的tic tac toe游戏。我已经做了3x3板,它工作得很好,但我现在想要它4x4,希望n x n,但不知道如何为大于3x3的数组实现endGame()方法。
想法会大大贬值
示例endGame()for 3x3:
/*horizontal game end, dr and dc are just a row and column that is kept constant while changing the second parameter for comparison*/
if(gameBoard[dr][0] == gameBoard[dr][2] && gameBoard[dr][0] == gameBoard[dr][1]) return true;
/*check for vertical game end*/
if(gameBoard[0][dc] == gameBoard[1][dc] && gameBoard[0][dc] == gameBoard[2][dc]) return true;
/*check for diagonal game end*/
if(gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[2][0] && gameBoard[0][2]!='_') return true;
if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard[2][2] && gameBoard[0][0]!='_') return true;
return false;
}