我正在编写一些Java代码来实现Connect 4游戏。当玩家在水平,垂直或对角线上连续放置四个筹码时,会宣布获胜者。显然我可以写一些for循环并每次检查一个胜利者,但是想要更优雅地做一些建议。我正在考虑将所有获胜组合添加到某些数据结构中,只是检查最后一步所涉及的组合,但我不确定这是否可能或如何实现它。我是Java的新手,所以对于使用什么数据结构或如何实现的任何提示或建议将非常感激。感谢
编辑:好的,有人可以告诉我在哪里开始实施这些家伙的回答:algorithm to check a connect four field
答案 0 :(得分:5)
其他答案声称你不能在没有循环的情况下检查胜利。今天我将扮演魔鬼的拥护者:你可以做到这一点(尽管你仍然不应该这样做)!对于一个典型的7 * 6 Connnect Four电路板,每种颜色只有69个可能的获胜位置,即使你是一个慢打字员,也可以在一两个下午轻松编码。
此代码假设您有一个名为matrix
的7x6 2D字符数组,其中包含三个值'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;
}
答案 1 :(得分:3)
无论如何,为了检查胜利条件,你需要在整个板上进行某种循环或递归。只要你的循环一旦停止成为获胜条件就停止向任何方向检查(例如,如果你从左到右检查,并且在2次迭代后你找到不同的颜色)那么它应该没问题。
优化这种方法的方法是只在新的动作播放时检查胜利条件,然后你只需要检查那些移动周围的那些而不是整个棋盘。 如果你需要检查一个完整的棋盘而不是转弯,那么进一步的步骤是保持一个动作列表,并从第一个前进进行检查,然后你可以在获胜的动作一旦完成就停止
答案 2 :(得分:2)
我假设你把你的董事会作为char[][]
或int[][]
(矩阵),你可能有:
if (char[][]) -> 'B' for black, 'R' for red
或
if (int[][]) -> 1 for black, 0 for red
在我看来,只有这种问题的二维数组(矩阵)才有意义。无论如何,检查胜利者的算法不仅仅是 ,而且必须循环在董事会上,正如其他人所说的那样。这是因为 是解决此类问题的最佳解决方案。
您基本上应该执行以下操作:
用于循环的嵌套:一个用于迭代行,一个用于迭代列。
for (int i = 0; i < matrix.length; i ++) {
for (int j = 0; j < matrix.length; j ++) {
// Check for stuff in here
}
}
您可以通过以下内容检查垂直,水平和对角线:
垂直(向下):
if (colorOfPieceFound) {
// check j-1 (move down one row, same column);
// check j-2, etc.
}
横向(左):
if (colorOfPieceFound) {
// check i-1 (move left one column, same row);
// check i-2, etc.
}
对角线(左上角):
if (colorOfPieceFound) {
// check [i-1][j+1] (move down one row, same column);
// repeat with +/- 2
}
基本上,当您找到一件时,您需要检查8个方向。您可以为矩阵的每个元素(即checkAllDirections(matrix[i][j])
)执行此操作,也可以从作品“删除”的位置开始。
答案 3 :(得分:1)
使用较小的搜索空间,在连接四游戏中使用嵌套循环检查胜利条件是完全可以的。
如果你存储获胜的配置,你必须将你的比赛场地与他们进行比较 - 很可能是通过嵌套循环或哈希。 (直接比较或矩阵乘法,无所谓)
我建议使用嵌套循环并使用不同的方法来检查水平,垂直和对角线获胜条件。是的,有可能提高它的效率 - 主要是对于更大的电路板 - 但对于4x4电路板来说真的不值得,因为即使存在(速度可能更慢)并且代码变得更复杂,速度提升也是微不足道的。
您可以找到一些优化,例如here。但正如您所见,代码变得不直观。
答案 4 :(得分:1)
我最近在构建Node4的NodeJS版本时这样做了。每次搬家后,您都会检查获胜者。我这样做的方法是使用3种方法checkForWinnerHorizontal()
,checkForWinnerVertical()
和checkForWinnerDiagonal()
。
每个方法将连接一行(或列或对角线)中的所有字符,然后检查串联字符串中的4个R或4个B.
getDiagonal(start, direction)
方法来返回每个对角线。最后,您只需检查每个连接的字符串,看看其中是否有4个相同的字符。