【问题标题】:Java connect four bit wise functions and bit board errorsJava连接四个位功能和位板错误
【发布时间】:2020-01-24 17:34:38
【问题描述】:

基本上我试图创建一个连接四个 Ai,我遇到了一个 article,它使用位板来优化移动和检查胜利。本质上,我从 git hub readme 中采用了一些方法,这些方法应该在您的位板上进行移动并撤消移动,不幸的是,这些方法似乎无法正常工作,因为它会将这些方法放在位字符串的末尾将它们间隔 7。我认为这可能是一些阻止这些正常工作的 java 东西,我已经发布了一个示例程序,我认为它可以准确地演示这个问题。例如,如果我设置一个长变量,使其在第 5 行有一行四个 1 并显示它。它可以正确显示而没有开头的零,但另一方面,如果我在第一列中添加三个标记。然后是第三列的三个标记。它显示为 111 和 111。什么时候应该是

000000000000000000000000011100000000000000
000000000000000000000000000000000000000111

并且没有前导零,所以

11100000000000000
111

然后,如果我用这些列进行另一个测试,下降 1,3,1,3,2,4 这应该会导致这种董事会状态。

|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
| X |   | O |   |   |   |   |
| X | X | O | O |   |   |   |
-----------------------------

应该显示10、10

000000000000000000001000001100000000000000
000000000000000000000000000000000010000011

或

1000001100000000000000
10000011

这里有一些测试代码演示了第二种情况。在这一点上,我不知所措,因为这些方法的操作非常优雅和复杂,尽管它们只有 3 行代码,如果有人能告诉我我在做什么有什么问题,我将不胜感激。干杯!

public class EConnectFour {
    private static int[] height = {0, 0, 0, 0, 0, 0, 0};
    private static int counter = 0;
    private static int[] moves = new int[42];
    private static long[] bitBoard = new long[2];



    public static void main(String[] args) {
          long TOP = 0b0000000001000000100000010000001000000000000000000L;
          System.out.println(Long.toBinaryString(TOP));
          makeMove(1);
          makeMove(3);
          makeMove(1);
          makeMove(3);
          makeMove(2);
          makeMove(4);
          System.out.println(Long.toBinaryString(bitBoard[0]));
          System.out.println(Long.toBinaryString(bitBoard[1]));
    }

    private static boolean isWin(long board) {
        int[] directions = {1, 7, 6, 8};
        long bb;
        for(int direction : directions) {
            bb = board & (board >> direction);
            if ((bb & (bb >> (2 * direction))) != 0) return true;
        }
        return false;
    }

    private static void makeMove(int column) {
        long move = 1L << height[column]++;
        bitBoard[counter & 1] ^= move; 
        moves[counter++] = column;  
    }

    private static void undoMove() {
        int column = moves[--counter];
        long move = 1L << --height[column];
        bitBoard[counter & 1] ^= move;
    }
}

【问题讨论】:

  • 这条线应该做什么? long move = 1L &lt;&lt; height[column]++;

标签: java bit-manipulation artificial-intelligence bitboard


【解决方案1】:

您无意中一直将令牌放入同一个插槽中。

替换:

// long move = 1L << height[column]++;
long move = 1L << (height[column]++ + ((column-1) * height.length));

您对column 的引用减一(Java 中的数组索引为0)。跑步结束时,高度如下所示:

height = [0, 2, 1, 2, 1, 0, 0]

您可以通过以下方式解决此问题:

long move = 1L << (height[column-1]++ + ((column-1) * height.length));

makeMove 的最终版本:

private static void makeMove(int column) {
    long move = 1L << (height[column-1]++ + ((column-1) * height.length));
    bitBoard[counter & 1] ^= move; 
    moves[counter++] = column;  
}

undoMove 的版本:

private static void undoMove() {
    int column = moves[--counter];
    moves[counter] = 0;  
    long move = 1L << (--height[column-1] + ((column-1) * height.length));
    bitBoard[counter & 1] ^= move;
}

【讨论】:

  • 我该如何修改 undoMove 方法来扭转这种情况?
  • @JulianCarrier 您能否在最初的问题下方提供移动和结果作为您问题的补充?我很可能可以修复它,但我认为它与 makeMove 中的相同。
  • 对,它与 makeMove 的问题相同,但相反。如果我们有相同的示例程序具有相同的列删除,则 undoMoves 应该将位板恢复到更早的一圈,因此假设我们将这些作为我们的位板。 span >>>>>>>>>>> 跨>
  • @JulianCarrier 主体是一样的。我只找到了答案,因为我用 System.out.println 调试了整个事情:)
  • @JulianCarrier 新版undoMove()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
相关资源
最近更新 更多