【发布时间】: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 << height[column]++;
标签: java bit-manipulation artificial-intelligence bitboard