Java 连接4,检查获胜算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20201216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect 4, check for winner algorithm
提问by PhilipGough
I'm writing some Java code to implement the Connect 4 game. A winner is declared when a player places four chips in a row, either horizontally, vertically or diagonally. Obviously I could write some for loops and check for a winner each time but would like some advice on doing it more elegantly. I was thinking about adding all winning combinations to some data structure and just checking the combinations the last move made is involved in but I'm not sure if that is possible or how to implement it. I am new to Java so any tips or advice on what data structure to use or how to implement would be much appreciated. Thanks
我正在编写一些 Java 代码来实现 Connect 4 游戏。当玩家在水平、垂直或对角线上连续放置四个筹码时,将宣布获胜者。显然,我可以编写一些 for 循环并每次检查获胜者,但想要一些关于更优雅地执行此操作的建议。我正在考虑将所有获胜组合添加到某些数据结构中,并只检查最后一步所涉及的组合,但我不确定这是否可能或如何实现。我是 Java 新手,所以任何关于使用什么数据结构或如何实现的提示或建议将不胜感激。谢谢
Edit: Ok, could someone please advise me on where to start to implement the guys answer here: algorithm to check a connect four field
编辑:好的,有人可以告诉我从哪里开始实施这些人的答案:检查连接四个字段的算法
采纳答案by Ross Drew
No matter what, in order to check for victory condition, you'll need to do some sort of looping or recursion on the whole board. As long as your loop stops checking in any direction as soon as it stops being a winning condition (e.g. if you check left to right and after 2 iterations you find a different color) then it should be fine.
无论如何,为了检查胜利条件,您需要在整个板上进行某种循环或递归。只要您的循环停止成为获胜条件后立即停止检查任何方向(例如,如果您从左到右检查并且在 2 次迭代后发现不同的颜色),那么它应该没问题。
A way of optimizing this would be to only check for victory conditions when new moves are played, then you only need to check those around that move and not the whole board. If you need to check a complete board and not turn-by-turn then a further step would be to keep a list of moves played and do your checking from the first move forward, then you can stop as soon as the winning move was played.
优化这一点的一种方法是仅在进行新动作时检查胜利条件,然后您只需要检查该动作周围的条件,而不是整个棋盘。如果您需要检查完整的棋盘而不是逐轮检查,那么进一步的步骤是保留已玩的棋步列表并从第一步开始进行检查,然后您可以在获胜棋步完成后立即停止.
回答by TheMorph
With a small search space it is totally okay to use nested loops for checking victory conditions in a connect four game.
在搜索空间很小的情况下,使用嵌套循环来检查连接四游戏中的胜利条件是完全可以的。
If you store winning configurations you have to compare your playing field with them - most likely via nested loops or hashes. (Direct comparison or matrix multiplication, doesn't matter)
如果您存储获胜配置,则必须将您的比赛场地与它们进行比较 - 最有可能是通过嵌套循环或散列。(直接比较或矩阵乘法,无所谓)
I would advice using nested loops and using different methods for checking for horizontal, vertical and diagonal winning conditions. Yes there are possibilities to make it more efficient - mostly for bigger boards - but it is really not worth the hassle for a 4x4 board as the speed improvement is marginal if even existent (it could be even slower) and the code gets more complicated.
我建议使用嵌套循环并使用不同的方法来检查水平、垂直和对角线获胜条件。是的,有可能使其更高效 - 主要是对于更大的板 - 但是对于 4x4 板来说真的不值得麻烦,因为即使存在速度提升也是微不足道的(它可能会更慢)并且代码变得更加复杂。
There is some optimization you can find for example here. But as you see, the code gets non-intuitive.
例如,您可以在此处找到一些优化。但是如您所见,代码变得不直观。
回答by Chris Cirefice
I'm going to assume you have your board as a char[][]
or int[][]
(matrix), where you might have:
我假设你有你的董事会作为一个char[][]
或int[][]
(矩阵),你可能有:
if (char[][]) -> 'B' for black, 'R' for red
if (char[][]) -> 'B' for black, 'R' for red
or
或者
if (int[][]) -> 1 for black, 0 for red
if (int[][]) -> 1 for black, 0 for red
In my mind it would only make sense to have a 2-d array (matrix) for this kind of problem. Anyway, the algorithm to check the winner not just should, but mustloop over the board, as others have said. The reason for this is because it isthe most elegant solution to this type of problem.
在我看来,对于这种问题,只有一个二维数组(矩阵)才有意义。无论如何,检查获胜者的算法不仅应该,而且必须像其他人所说的那样在棋盘上循环。这样做的原因是因为它是此类问题的最优雅的解决方案。
You should basically do the following:
您基本上应该执行以下操作:
A nested forloop: one to iterate rows, one to iterate columns.
嵌套for循环:一个迭代行,一个迭代列。
for (int i = 0; i < matrix.length; i ++) {
for (int j = 0; j < matrix.length; j ++) {
// Check for stuff in here
}
}
You can check vertical, horizontaland diagonal through something like the following:
您可以通过以下方式检查垂直、水平和对角线:
vertical (down):
垂直(向下):
if (colorOfPieceFound) {
// check j-1 (move down one row, same column);
// check j-2, etc.
}
horizontal (left):
水平(左):
if (colorOfPieceFound) {
// check i-1 (move left one column, same row);
// check i-2, etc.
}
diagonal (up-left):
对角线(左上):
if (colorOfPieceFound) {
// check [i-1][j+1] (move down one row, same column);
// repeat with +/- 2
}
Basically you have 8 directions you need to check when you find a piece. You do this for each element of the matrix (i.e. checkAllDirections(matrix[i][j])
) or starting at the place where the piece was 'dropped'.
基本上,当您找到一件作品时,您需要检查 8 个方向。您对矩阵的每个元素(即checkAllDirections(matrix[i][j])
)执行此操作,或者从“丢弃”块的位置开始执行此操作。
回答by Kevin
Other answers claim you can't check for a win without looping. Today I will play the Devil's advocate: you cando it (although you still shouldn'tdo it)! For a typical 7*6 Connnect Four board, there are only 69 possible winning positions per color, which can easily be coded in an afternoon or two, even if you're a slow typist.
其他答案声称您无法在不循环的情况下检查胜利。今天我要扮演魔鬼的代言人:你可以做到(虽然你仍然不应该这样做)!对于典型的 7*6 Connnect Four 板,每种颜色只有 69 个可能的获胜位置,即使您是一个缓慢的打字员,也可以在一两个下午轻松编码。
This code supposes that you have a 7x6 2D array of characters called matrix
, which contains one of the three values 'B'
, 'R'
, or ' '
, corresponding to a black, red, or empty tile respectively. It either returns the character of the winner, or null
if there is no winner yet.
此代码假设你有一个7x6 2D阵列的字符称为matrix
,其中包含了三个值之一'B'
,'R'
或者' '
,分别对应于黑色,红色,或空的瓦片。它要么返回获胜者的角色,要么返回null
尚无获胜者的角色。
char getWinner(){
if (matrix[0][0] == 'B' and matrix[1][0] == 'B' and matrix[2][0] == 'B' and matrix[3][0] == 'B'){return 'B';}
if (matrix[0][0] == 'R' and matrix[1][0] == 'R' and matrix[2][0] == 'R' and matrix[3][0] == 'R'){return 'R';}
if (matrix[1][0] == 'B' and matrix[2][0] == 'B' and matrix[3][0] == 'B' and matrix[4][0] == 'B'){return 'B';}
if (matrix[1][0] == 'R' and matrix[2][0] == 'R' and matrix[3][0] == 'R' and matrix[4][0] == 'R'){return 'R';}
if (matrix[2][0] == 'B' and matrix[3][0] == 'B' and matrix[4][0] == 'B' and matrix[5][0] == 'B'){return 'B';}
if (matrix[2][0] == 'R' and matrix[3][0] == 'R' and matrix[4][0] == 'R' and matrix[5][0] == 'R'){return 'R';}
if (matrix[3][0] == 'B' and matrix[4][0] == 'B' and matrix[5][0] == 'B' and matrix[6][0] == 'B'){return 'B';}
if (matrix[3][0] == 'R' and matrix[4][0] == 'R' and matrix[5][0] == 'R' and matrix[6][0] == 'R'){return 'R';}
if (matrix[0][1] == 'B' and matrix[1][1] == 'B' and matrix[2][1] == 'B' and matrix[3][1] == 'B'){return 'B';}
if (matrix[0][1] == 'R' and matrix[1][1] == 'R' and matrix[2][1] == 'R' and matrix[3][1] == 'R'){return 'R';}
if (matrix[1][1] == 'B' and matrix[2][1] == 'B' and matrix[3][1] == 'B' and matrix[4][1] == 'B'){return 'B';}
if (matrix[1][1] == 'R' and matrix[2][1] == 'R' and matrix[3][1] == 'R' and matrix[4][1] == 'R'){return 'R';}
if (matrix[2][1] == 'B' and matrix[3][1] == 'B' and matrix[4][1] == 'B' and matrix[5][1] == 'B'){return 'B';}
if (matrix[2][1] == 'R' and matrix[3][1] == 'R' and matrix[4][1] == 'R' and matrix[5][1] == 'R'){return 'R';}
if (matrix[3][1] == 'B' and matrix[4][1] == 'B' and matrix[5][1] == 'B' and matrix[6][1] == 'B'){return 'B';}
if (matrix[3][1] == 'R' and matrix[4][1] == 'R' and matrix[5][1] == 'R' and matrix[6][1] == 'R'){return 'R';}
if (matrix[0][2] == 'B' and matrix[1][2] == 'B' and matrix[2][2] == 'B' and matrix[3][2] == 'B'){return 'B';}
if (matrix[0][2] == 'R' and matrix[1][2] == 'R' and matrix[2][2] == 'R' and matrix[3][2] == 'R'){return 'R';}
if (matrix[1][2] == 'B' and matrix[2][2] == 'B' and matrix[3][2] == 'B' and matrix[4][2] == 'B'){return 'B';}
if (matrix[1][2] == 'R' and matrix[2][2] == 'R' and matrix[3][2] == 'R' and matrix[4][2] == 'R'){return 'R';}
if (matrix[2][2] == 'B' and matrix[3][2] == 'B' and matrix[4][2] == 'B' and matrix[5][2] == 'B'){return 'B';}
if (matrix[2][2] == 'R' and matrix[3][2] == 'R' and matrix[4][2] == 'R' and matrix[5][2] == 'R'){return 'R';}
if (matrix[3][2] == 'B' and matrix[4][2] == 'B' and matrix[5][2] == 'B' and matrix[6][2] == 'B'){return 'B';}
if (matrix[3][2] == 'R' and matrix[4][2] == 'R' and matrix[5][2] == 'R' and matrix[6][2] == 'R'){return 'R';}
if (matrix[0][3] == 'B' and matrix[1][3] == 'B' and matrix[2][3] == 'B' and matrix[3][3] == 'B'){return 'B';}
if (matrix[0][3] == 'R' and matrix[1][3] == 'R' and matrix[2][3] == 'R' and matrix[3][3] == 'R'){return 'R';}
if (matrix[1][3] == 'B' and matrix[2][3] == 'B' and matrix[3][3] == 'B' and matrix[4][3] == 'B'){return 'B';}
if (matrix[1][3] == 'R' and matrix[2][3] == 'R' and matrix[3][3] == 'R' and matrix[4][3] == 'R'){return 'R';}
if (matrix[2][3] == 'B' and matrix[3][3] == 'B' and matrix[4][3] == 'B' and matrix[5][3] == 'B'){return 'B';}
if (matrix[2][3] == 'R' and matrix[3][3] == 'R' and matrix[4][3] == 'R' and matrix[5][3] == 'R'){return 'R';}
if (matrix[3][3] == 'B' and matrix[4][3] == 'B' and matrix[5][3] == 'B' and matrix[6][3] == 'B'){return 'B';}
if (matrix[3][3] == 'R' and matrix[4][3] == 'R' and matrix[5][3] == 'R' and matrix[6][3] == 'R'){return 'R';}
if (matrix[0][4] == 'B' and matrix[1][4] == 'B' and matrix[2][4] == 'B' and matrix[3][4] == 'B'){return 'B';}
if (matrix[0][4] == 'R' and matrix[1][4] == 'R' and matrix[2][4] == 'R' and matrix[3][4] == 'R'){return 'R';}
if (matrix[1][4] == 'B' and matrix[2][4] == 'B' and matrix[3][4] == 'B' and matrix[4][4] == 'B'){return 'B';}
if (matrix[1][4] == 'R' and matrix[2][4] == 'R' and matrix[3][4] == 'R' and matrix[4][4] == 'R'){return 'R';}
if (matrix[2][4] == 'B' and matrix[3][4] == 'B' and matrix[4][4] == 'B' and matrix[5][4] == 'B'){return 'B';}
if (matrix[2][4] == 'R' and matrix[3][4] == 'R' and matrix[4][4] == 'R' and matrix[5][4] == 'R'){return 'R';}
if (matrix[3][4] == 'B' and matrix[4][4] == 'B' and matrix[5][4] == 'B' and matrix[6][4] == 'B'){return 'B';}
if (matrix[3][4] == 'R' and matrix[4][4] == 'R' and matrix[5][4] == 'R' and matrix[6][4] == 'R'){return 'R';}
if (matrix[0][5] == 'B' and matrix[1][5] == 'B' and matrix[2][5] == 'B' and matrix[3][5] == 'B'){return 'B';}
if (matrix[0][5] == 'R' and matrix[1][5] == 'R' and matrix[2][5] == 'R' and matrix[3][5] == 'R'){return 'R';}
if (matrix[1][5] == 'B' and matrix[2][5] == 'B' and matrix[3][5] == 'B' and matrix[4][5] == 'B'){return 'B';}
if (matrix[1][5] == 'R' and matrix[2][5] == 'R' and matrix[3][5] == 'R' and matrix[4][5] == 'R'){return 'R';}
if (matrix[2][5] == 'B' and matrix[3][5] == 'B' and matrix[4][5] == 'B' and matrix[5][5] == 'B'){return 'B';}
if (matrix[2][5] == 'R' and matrix[3][5] == 'R' and matrix[4][5] == 'R' and matrix[5][5] == 'R'){return 'R';}
if (matrix[3][5] == 'B' and matrix[4][5] == 'B' and matrix[5][5] == 'B' and matrix[6][5] == 'B'){return 'B';}
if (matrix[3][5] == 'R' and matrix[4][5] == 'R' and matrix[5][5] == 'R' and matrix[6][5] == 'R'){return 'R';}
if (matrix[0][0] == 'B' and matrix[0][1] == 'B' and matrix[0][2] == 'B' and matrix[0][3] == 'B'){return 'B';}
if (matrix[0][0] == 'R' and matrix[0][1] == 'R' and matrix[0][2] == 'R' and matrix[0][3] == 'R'){return 'R';}
if (matrix[0][1] == 'B' and matrix[0][2] == 'B' and matrix[0][3] == 'B' and matrix[0][4] == 'B'){return 'B';}
if (matrix[0][1] == 'R' and matrix[0][2] == 'R' and matrix[0][3] == 'R' and matrix[0][4] == 'R'){return 'R';}
if (matrix[0][2] == 'B' and matrix[0][3] == 'B' and matrix[0][4] == 'B' and matrix[0][5] == 'B'){return 'B';}
if (matrix[0][2] == 'R' and matrix[0][3] == 'R' and matrix[0][4] == 'R' and matrix[0][5] == 'R'){return 'R';}
if (matrix[1][0] == 'B' and matrix[1][1] == 'B' and matrix[1][2] == 'B' and matrix[1][3] == 'B'){return 'B';}
if (matrix[1][0] == 'R' and matrix[1][1] == 'R' and matrix[1][2] == 'R' and matrix[1][3] == 'R'){return 'R';}
if (matrix[1][1] == 'B' and matrix[1][2] == 'B' and matrix[1][3] == 'B' and matrix[1][4] == 'B'){return 'B';}
if (matrix[1][1] == 'R' and matrix[1][2] == 'R' and matrix[1][3] == 'R' and matrix[1][4] == 'R'){return 'R';}
if (matrix[1][2] == 'B' and matrix[1][3] == 'B' and matrix[1][4] == 'B' and matrix[1][5] == 'B'){return 'B';}
if (matrix[1][2] == 'R' and matrix[1][3] == 'R' and matrix[1][4] == 'R' and matrix[1][5] == 'R'){return 'R';}
if (matrix[2][0] == 'B' and matrix[2][1] == 'B' and matrix[2][2] == 'B' and matrix[2][3] == 'B'){return 'B';}
if (matrix[2][0] == 'R' and matrix[2][1] == 'R' and matrix[2][2] == 'R' and matrix[2][3] == 'R'){return 'R';}
if (matrix[2][1] == 'B' and matrix[2][2] == 'B' and matrix[2][3] == 'B' and matrix[2][4] == 'B'){return 'B';}
if (matrix[2][1] == 'R' and matrix[2][2] == 'R' and matrix[2][3] == 'R' and matrix[2][4] == 'R'){return 'R';}
if (matrix[2][2] == 'B' and matrix[2][3] == 'B' and matrix[2][4] == 'B' and matrix[2][5] == 'B'){return 'B';}
if (matrix[2][2] == 'R' and matrix[2][3] == 'R' and matrix[2][4] == 'R' and matrix[2][5] == 'R'){return 'R';}
if (matrix[3][0] == 'B' and matrix[3][1] == 'B' and matrix[3][2] == 'B' and matrix[3][3] == 'B'){return 'B';}
if (matrix[3][0] == 'R' and matrix[3][1] == 'R' and matrix[3][2] == 'R' and matrix[3][3] == 'R'){return 'R';}
if (matrix[3][1] == 'B' and matrix[3][2] == 'B' and matrix[3][3] == 'B' and matrix[3][4] == 'B'){return 'B';}
if (matrix[3][1] == 'R' and matrix[3][2] == 'R' and matrix[3][3] == 'R' and matrix[3][4] == 'R'){return 'R';}
if (matrix[3][2] == 'B' and matrix[3][3] == 'B' and matrix[3][4] == 'B' and matrix[3][5] == 'B'){return 'B';}
if (matrix[3][2] == 'R' and matrix[3][3] == 'R' and matrix[3][4] == 'R' and matrix[3][5] == 'R'){return 'R';}
if (matrix[4][0] == 'B' and matrix[4][1] == 'B' and matrix[4][2] == 'B' and matrix[4][3] == 'B'){return 'B';}
if (matrix[4][0] == 'R' and matrix[4][1] == 'R' and matrix[4][2] == 'R' and matrix[4][3] == 'R'){return 'R';}
if (matrix[4][1] == 'B' and matrix[4][2] == 'B' and matrix[4][3] == 'B' and matrix[4][4] == 'B'){return 'B';}
if (matrix[4][1] == 'R' and matrix[4][2] == 'R' and matrix[4][3] == 'R' and matrix[4][4] == 'R'){return 'R';}
if (matrix[4][2] == 'B' and matrix[4][3] == 'B' and matrix[4][4] == 'B' and matrix[4][5] == 'B'){return 'B';}
if (matrix[4][2] == 'R' and matrix[4][3] == 'R' and matrix[4][4] == 'R' and matrix[4][5] == 'R'){return 'R';}
if (matrix[5][0] == 'B' and matrix[5][1] == 'B' and matrix[5][2] == 'B' and matrix[5][3] == 'B'){return 'B';}
if (matrix[5][0] == 'R' and matrix[5][1] == 'R' and matrix[5][2] == 'R' and matrix[5][3] == 'R'){return 'R';}
if (matrix[5][1] == 'B' and matrix[5][2] == 'B' and matrix[5][3] == 'B' and matrix[5][4] == 'B'){return 'B';}
if (matrix[5][1] == 'R' and matrix[5][2] == 'R' and matrix[5][3] == 'R' and matrix[5][4] == 'R'){return 'R';}
if (matrix[5][2] == 'B' and matrix[5][3] == 'B' and matrix[5][4] == 'B' and matrix[5][5] == 'B'){return 'B';}
if (matrix[5][2] == 'R' and matrix[5][3] == 'R' and matrix[5][4] == 'R' and matrix[5][5] == 'R'){return 'R';}
if (matrix[6][0] == 'B' and matrix[6][1] == 'B' and matrix[6][2] == 'B' and matrix[6][3] == 'B'){return 'B';}
if (matrix[6][0] == 'R' and matrix[6][1] == 'R' and matrix[6][2] == 'R' and matrix[6][3] == 'R'){return 'R';}
if (matrix[6][1] == 'B' and matrix[6][2] == 'B' and matrix[6][3] == 'B' and matrix[6][4] == 'B'){return 'B';}
if (matrix[6][1] == 'R' and matrix[6][2] == 'R' and matrix[6][3] == 'R' and matrix[6][4] == 'R'){return 'R';}
if (matrix[6][2] == 'B' and matrix[6][3] == 'B' and matrix[6][4] == 'B' and matrix[6][5] == 'B'){return 'B';}
if (matrix[6][2] == 'R' and matrix[6][3] == 'R' and matrix[6][4] == 'R' and matrix[6][5] == 'R'){return 'R';}
if (matrix[0][3] == 'B' and matrix[1][2] == 'B' and matrix[2][1] == 'B' and matrix[3][0] == 'B'){return 'B';}
if (matrix[0][3] == 'R' and matrix[1][2] == 'R' and matrix[2][1] == 'R' and matrix[3][0] == 'R'){return 'R';}
if (matrix[3][3] == 'B' and matrix[2][2] == 'B' and matrix[1][1] == 'B' and matrix[0][0] == 'B'){return 'B';}
if (matrix[3][3] == 'R' and matrix[2][2] == 'R' and matrix[1][1] == 'R' and matrix[0][0] == 'R'){return 'R';}
if (matrix[0][4] == 'B' and matrix[1][3] == 'B' and matrix[2][2] == 'B' and matrix[3][1] == 'B'){return 'B';}
if (matrix[0][4] == 'R' and matrix[1][3] == 'R' and matrix[2][2] == 'R' and matrix[3][1] == 'R'){return 'R';}
if (matrix[3][4] == 'B' and matrix[2][3] == 'B' and matrix[1][2] == 'B' and matrix[0][1] == 'B'){return 'B';}
if (matrix[3][4] == 'R' and matrix[2][3] == 'R' and matrix[1][2] == 'R' and matrix[0][1] == 'R'){return 'R';}
if (matrix[0][5] == 'B' and matrix[1][4] == 'B' and matrix[2][3] == 'B' and matrix[3][2] == 'B'){return 'B';}
if (matrix[0][5] == 'R' and matrix[1][4] == 'R' and matrix[2][3] == 'R' and matrix[3][2] == 'R'){return 'R';}
if (matrix[3][5] == 'B' and matrix[2][4] == 'B' and matrix[1][3] == 'B' and matrix[0][2] == 'B'){return 'B';}
if (matrix[3][5] == 'R' and matrix[2][4] == 'R' and matrix[1][3] == 'R' and matrix[0][2] == 'R'){return 'R';}
if (matrix[1][3] == 'B' and matrix[2][2] == 'B' and matrix[3][1] == 'B' and matrix[4][0] == 'B'){return 'B';}
if (matrix[1][3] == 'R' and matrix[2][2] == 'R' and matrix[3][1] == 'R' and matrix[4][0] == 'R'){return 'R';}
if (matrix[4][3] == 'B' and matrix[3][2] == 'B' and matrix[2][1] == 'B' and matrix[1][0] == 'B'){return 'B';}
if (matrix[4][3] == 'R' and matrix[3][2] == 'R' and matrix[2][1] == 'R' and matrix[1][0] == 'R'){return 'R';}
if (matrix[1][4] == 'B' and matrix[2][3] == 'B' and matrix[3][2] == 'B' and matrix[4][1] == 'B'){return 'B';}
if (matrix[1][4] == 'R' and matrix[2][3] == 'R' and matrix[3][2] == 'R' and matrix[4][1] == 'R'){return 'R';}
if (matrix[4][4] == 'B' and matrix[3][3] == 'B' and matrix[2][2] == 'B' and matrix[1][1] == 'B'){return 'B';}
if (matrix[4][4] == 'R' and matrix[3][3] == 'R' and matrix[2][2] == 'R' and matrix[1][1] == 'R'){return 'R';}
if (matrix[1][5] == 'B' and matrix[2][4] == 'B' and matrix[3][3] == 'B' and matrix[4][2] == 'B'){return 'B';}
if (matrix[1][5] == 'R' and matrix[2][4] == 'R' and matrix[3][3] == 'R' and matrix[4][2] == 'R'){return 'R';}
if (matrix[4][5] == 'B' and matrix[3][4] == 'B' and matrix[2][3] == 'B' and matrix[1][2] == 'B'){return 'B';}
if (matrix[4][5] == 'R' and matrix[3][4] == 'R' and matrix[2][3] == 'R' and matrix[1][2] == 'R'){return 'R';}
if (matrix[2][3] == 'B' and matrix[3][2] == 'B' and matrix[4][1] == 'B' and matrix[5][0] == 'B'){return 'B';}
if (matrix[2][3] == 'R' and matrix[3][2] == 'R' and matrix[4][1] == 'R' and matrix[5][0] == 'R'){return 'R';}
if (matrix[5][3] == 'B' and matrix[4][2] == 'B' and matrix[3][1] == 'B' and matrix[2][0] == 'B'){return 'B';}
if (matrix[5][3] == 'R' and matrix[4][2] == 'R' and matrix[3][1] == 'R' and matrix[2][0] == 'R'){return 'R';}
if (matrix[2][4] == 'B' and matrix[3][3] == 'B' and matrix[4][2] == 'B' and matrix[5][1] == 'B'){return 'B';}
if (matrix[2][4] == 'R' and matrix[3][3] == 'R' and matrix[4][2] == 'R' and matrix[5][1] == 'R'){return 'R';}
if (matrix[5][4] == 'B' and matrix[4][3] == 'B' and matrix[3][2] == 'B' and matrix[2][1] == 'B'){return 'B';}
if (matrix[5][4] == 'R' and matrix[4][3] == 'R' and matrix[3][2] == 'R' and matrix[2][1] == 'R'){return 'R';}
if (matrix[2][5] == 'B' and matrix[3][4] == 'B' and matrix[4][3] == 'B' and matrix[5][2] == 'B'){return 'B';}
if (matrix[2][5] == 'R' and matrix[3][4] == 'R' and matrix[4][3] == 'R' and matrix[5][2] == 'R'){return 'R';}
if (matrix[5][5] == 'B' and matrix[4][4] == 'B' and matrix[3][3] == 'B' and matrix[2][2] == 'B'){return 'B';}
if (matrix[5][5] == 'R' and matrix[4][4] == 'R' and matrix[3][3] == 'R' and matrix[2][2] == 'R'){return 'R';}
if (matrix[3][3] == 'B' and matrix[4][2] == 'B' and matrix[5][1] == 'B' and matrix[6][0] == 'B'){return 'B';}
if (matrix[3][3] == 'R' and matrix[4][2] == 'R' and matrix[5][1] == 'R' and matrix[6][0] == 'R'){return 'R';}
if (matrix[6][3] == 'B' and matrix[5][2] == 'B' and matrix[4][1] == 'B' and matrix[3][0] == 'B'){return 'B';}
if (matrix[6][3] == 'R' and matrix[5][2] == 'R' and matrix[4][1] == 'R' and matrix[3][0] == 'R'){return 'R';}
if (matrix[3][4] == 'B' and matrix[4][3] == 'B' and matrix[5][2] == 'B' and matrix[6][1] == 'B'){return 'B';}
if (matrix[3][4] == 'R' and matrix[4][3] == 'R' and matrix[5][2] == 'R' and matrix[6][1] == 'R'){return 'R';}
if (matrix[6][4] == 'B' and matrix[5][3] == 'B' and matrix[4][2] == 'B' and matrix[3][1] == 'B'){return 'B';}
if (matrix[6][4] == 'R' and matrix[5][3] == 'R' and matrix[4][2] == 'R' and matrix[3][1] == 'R'){return 'R';}
if (matrix[3][5] == 'B' and matrix[4][4] == 'B' and matrix[5][3] == 'B' and matrix[6][2] == 'B'){return 'B';}
if (matrix[3][5] == 'R' and matrix[4][4] == 'R' and matrix[5][3] == 'R' and matrix[6][2] == 'R'){return 'R';}
if (matrix[6][5] == 'B' and matrix[5][4] == 'B' and matrix[4][3] == 'B' and matrix[3][2] == 'B'){return 'B';}
if (matrix[6][5] == 'R' and matrix[5][4] == 'R' and matrix[4][3] == 'R' and matrix[3][2] == 'R'){return 'R';}
return null;
}
回答by L_7337
I did this recently while building a NodeJS version of Connect4. After every move, you check for a winner. The way I did it was to have 3 methods, checkForWinnerHorizontal()
, checkForWinnerVertical()
, and checkForWinnerDiagonal()
.
我最近在构建 NodeJS 版本的 Connect4 时这样做了。每次移动后,您都会检查获胜者。我这样做的方法是使用 3 种方法checkForWinnerHorizontal()
,checkForWinnerVertical()
, 和checkForWinnerDiagonal()
.
Each method would concatenate all the characters in a row(or column or diagonal) and then check the concatenated string for either 4 R's or 4 B's.
每种方法都会连接一行(或列或对角线)中的所有字符,然后检查连接的字符串是否有 4 个 R 或 4 个 B。
- Horizontal was easy. Just join the characters in the row.
- Vertical was a little harder because you need to get columns. You can build it by hardcoding each column or pivot the array and then use the Horizontal method.
- Diagonal was the hardest. You need to get all the forward diagonals and backward diagonals. You also must check for out of bounds conditions (depending on you algorithm). I just built a
getDiagonal(start, direction)
method to return each diagonal.
- 水平很容易。只需加入行中的字符即可。
- 垂直有点难,因为你需要得到列。您可以通过对每一列进行硬编码或旋转数组来构建它,然后使用 Horizontal 方法。
- 对角线是最难的。你需要得到所有的前对角线和后对角线。您还必须检查越界条件(取决于您的算法)。我刚刚构建了一个
getDiagonal(start, direction)
方法来返回每个对角线。
In the end, you just check each of the concatenated strings to see if there are 4 of the same character in it.
最后,您只需检查每个连接的字符串,看看其中是否有 4 个相同的字符。