【问题标题】:Connect 4, check for winner algorithm连接4,检查获胜者算法
【发布时间】:2013-11-25 19:10:02
【问题描述】:

我正在编写一些 Java 代码来实现 Connect 4 游戏。当玩家在水平、垂直或对角线上连续放置四个筹码时,即为获胜者。显然,我可以编写一些 for 循环并每次检查获胜者,但希望获得一些关于更优雅地执行此操作的建议。我正在考虑将所有获胜组合添加到某些数据结构中,并且只是检查最后一次移动所涉及的组合,但我不确定这是否可能或如何实现它。我是 Java 新手,因此对于使用什么数据结构或如何实现的任何提示或建议将不胜感激。谢谢

编辑:好的,有人可以告诉我从哪里开始实施这些家伙在这里回答:algorithm to check a connect four field

【问题讨论】:

  • 循环优雅的。
  • 我不想每次都检查整个板子
  • 为什么不呢?这不像你每秒检查 1000 次。
  • 是的,谢谢 Ahmet 我已经看过了,这是我有兴趣尝试做的答案,但是我不知道从哪里开始

标签: java algorithm


【解决方案1】:

其他答案声称您无法在不循环的情况下检查胜利。今天我将扮演魔鬼的拥护者:你可以做到(尽管你仍然不应该这样做)!对于一个典型的 7*6 连接四板,每种颜色只有 69 个可能的获胜位置,即使您是一个打字慢的人,也可以在一两个下午轻松编码。

此代码假设您有一个名为 matrix 的 7x6 二维字符数组,其中包含三个值之一:'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;
}

【讨论】:

  • 我撤回我的声明,它可以做到。没有人喜欢聪明的a**:P
  • 是的,我想您可以列举所有可能的获胜组合。但是话又说回来,具有 69 个可能条件的条件块真的不是条件的循环吗?当然,您将它们全部作为单独的条件。另一种选择是将它们放在要评估的条件列表中,然后遍历该列表(更优雅)。但在本质上,我认为这个想法是相同的。当然,随着您的规格发生变化,您可以生成一个新的获胜组合列表 - 但要做到这一点,您将 cough 需要一个循环 cough。 :) 无论如何+1:P
  • 我的问题的意图或意思是我是否可以将获胜的组合存储在诸如哈希表之类的数据结构中。并在其中放置芯片时查找该位置的密钥。我并没有试图避免做任何循环,即使这听起来像我的意思
  • 嘿..您只需要检查插入最后一个硬币的列!不是整个网格!
【解决方案2】:

无论如何,为了检查胜利条件,您需要在整个棋盘上进行某种循环或递归。只要您的循环在停止成为获胜条件后立即停止检查任何方向(例如,如果您从左到右检查并在 2 次迭代后发现不同的颜色),那么它应该没问题。

优化这一点的一种方法是仅在下新棋子时检查胜利条件,然后您只需要检查该棋子周围的棋子而不是整个棋盘。 如果您需要检查一个完整的棋盘而不是逐个回合,那么进一步的步骤是保留已播放的动作列表并从第一个动作向前进行检查,然后您可以在下完获胜动作后立即停止.

【讨论】:

  • 我对循环做同样的事情,如果连拍被打断,我会检查以终止它们,但是你的优化听起来很有趣,它是如何实现的,特别是如果每​​个玩家都把他们的棋子放在相反的位置棋盘的两端?
  • Connect 4 是回合制的,如果玩家一是黄色并丢下一个标记,那么您只需检查该标记即可获胜。玩家 2 放入他的红色,你检查它是否获胜。很简单。
  • 无需全面循环。只需检查最后插入的硬币 :-)
  • 第二段是这么说的。
【解决方案3】:

我假设您的董事会是 char[][]int[][](矩阵),您可能有:

if (char[][]) -> 'B' for black, 'R' for red

if (int[][]) -> 1 for black, 0 for red

在我看来,只有二维数组(矩阵)才能解决这类问题。无论如何,检查获胜者的算法不仅应该,而且必须在棋盘上循环,正如其他人所说的那样。这样做的原因是因为它此类问题的最优雅的解决方案。

你应该基本上做到以下几点:

一个嵌套的 for 循环:一个用于迭代行,一个用于迭代列。

for (int i = 0; i < matrix.length; i ++) {
  for (int j = 0; j < matrix.length; j ++) {
    // Check for stuff in here
  }
}

您可以通过以下方式检查 verticalhorizo​​ntal 和对角线:

垂直(向下)

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]))执行此操作,或者从“丢弃”部分的位置开始。

【讨论】:

  • 是的,这就是我所拥有的,一个包含红色或黑色状态的二维数组。只是认为可能有更好的方法来解决它。谢谢
  • 好吧,我的意思是,既然您使用的是 Java,那么您可以构建自己的矩阵 iterator,它可以使该过程“功能化”。但是我已经为不同的游戏做了类似的“获胜者检查”算法,如果棋盘类似于矩阵,最好只做一个嵌套的for 循环。就像我在回答中所说的那样,正如其他人所说的那样,你可以让它“更智能”,方法是让它只在移动之后在那个特定位置检查获胜者(这将使最有意义)。否则,您可以每轮检查整个棋盘。任何一个都可以!
  • 实际上它会更有意义,我这样做是为了让 0 成为一个空白空间,因为这是默认的 int[][] 值并且红色 = 1,黑色 = 2。我创建了 RoundButtons ,字面意思是圆形按钮,当单击更改颜色并制作一个二维数组时。我的代码与您所拥有的代码几乎相同,除了我的代码总是从左下角开始,检查该部分的所有方向@ [5] [0],然后将一列移过等等,但我遇到了问题。当我手动运行我的代码时,它可以工作,但是有些东西搞砸了
【解决方案4】:

如果搜索空间很小,完全可以使用嵌套循环来检查四人连线游戏中的胜利条件。

如果您存储获胜配置,则必须将您的竞争环境与它们进行比较 - 最有可能通过嵌套循环或散列。 (直接比较或矩阵乘法,无所谓)

我建议使用嵌套循环并使用不同的方法检查水平、垂直和对角线获胜条件。是的,有可能使它更高效 - 主要用于更大的电路板 - 但对于 4x4 电路板来说,这真的不值得麻烦,因为即使存在速度提升也是微不足道的(它可能更慢)并且代码变得更加复杂。

您可以找到一些优化,例如here。但如您所见,代码变得不直观。

【讨论】:

    【解决方案5】:

    我最近在构建 Connect4 的 NodeJS 版本时这样做了。每走一步,你检查一个赢家。我这样做的方式是有 3 个方法,checkForWinnerHorizontal()checkForWinnerVertical()checkForWinnerDiagonal()

    每个方法都会连接一行(或列或对角线)中的所有字符,然后检查连接的字符串是否有 4 个 R 或 4 个 B。

    • 水平很容易。只需加入行中的字符即可。
    • 垂直有点困难,因为您需要获取列。您可以通过硬编码每一列或旋转数组来构建它,然后使用 Horizo​​ntal 方法。
    • 对角线是最难的。你需要得到所有的前向对角线和后向对角线。您还必须检查超出范围的条件(取决于您的算法)。我刚刚构建了一个getDiagonal(start, direction) 方法来返回每个对角线。

    最后,您只需检查每个连接的字符串,看看其中是否有 4 个相同的字符。

    【讨论】:

      猜你喜欢
      • 2015-12-22
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      相关资源
      最近更新 更多