位板很有趣:-)
我正在为这个问题编写一些代码,但这里有一些可以让你开始的东西:
public class BitBoard {
public static final int White = 0;
public static final int Black = 1;
public long[] board = { 0, 0 };
public int Player = 0;
public int[] left = { 9, 9 };
public int Opponent()
{
return Player == White ? Black : White;
}
public void makemove(int from, int to, int capture)
{
if (from == 0) {
assert left[Player] > 0 : "makemove: no left";
left[Player]--;
}
if (from != 0)
{
assert (board[Player] & from) != 0 : "makemove: source empty";
board[Player] &= ~from;
}
assert (board[Player] & to) == 0 : "makemove: target must be empty";
board[Player] |= to;
if (capture != 0)
{
assert (board[Opponent()] & capture) != 0 : "makemove: capture empty";
board[Opponent()] &= ~capture;
}
}
public void unmakemove(int from, int to, int capture)
{
if (capture != 0)
{
assert (board[Opponent()] & capture) == 0 : "unmakemove: capture empty";
board[Opponent()] |= capture;
}
assert (board[Player] & to) != 0 : "unmakemove: target empty";
board[Player] &= ~to;
if (from != 0)
{
assert (board[Opponent()] & capture) != 0 : "unmakemove: source must be empty empty";
board[Player] |= from;
}
if (from == 0) {
assert left[Player] < 9 : "unmakemove: too many left";
left[Player]++;
}
}
public void generatemoves()
{
// determine phase
//
}
}
没有对此进行全面测试,但以下代码显示了如何使用:
import java.math.*;
public class NineMenBitboard {
static int tst;
// A B C D E F G
// 1 0 1 2
// 2 3 4 5
// 3 6 7 8
// 4 9 10 11 12 13 14
// 5 15 16 17
// 6 18 19 20
// 7 21 22 23
// positions
static int A1 = 1 << 0;
static int D1 = 1 << 1;
static int G1 = 1 << 2;
static int B2 = 1 << 3;
static int D2 = 1 << 4;
static int F2 = 1 << 5;
static int C3 = 1 << 6;
static int D3 = 1 << 7;
static int E3 = 1 << 8;
static int A4 = 1 << 9;
static int B4 = 1 << 10;
static int C4 = 1 << 11;
static int E4 = 1 << 12;
static int F4 = 1 << 13;
static int G4 = 1 << 14;
static int C5 = 1 << 15;
static int D5 = 1 << 16;
static int E5 = 1 << 17;
static int B6 = 1 << 18;
static int D6 = 1 << 19;
static int F6 = 1 << 20;
static int A7 = 1 << 21;
static int D7 = 1 << 22;
static int G7 = 1 << 23;
// mills
static int hor1 = A1 | D1 | G1;
static int hor2 = B2 | D2 | F2;
static int hor3 = C3 | D3 | E3;
static int hor4_1 = A4 | B4 | C4;
static int hor4_2 = E4 | F4 | G4;
static int hor5 = C5 | D5 | E5;
static int hor6 = B6 | D6 | F6;
static int hor7 = A7 | D7 | G7;
static int ver1 = A1 | A4 | A7;
static int ver2 = B2 | B4 | B6;
static int ver3 = C3 | C4 | C5;
static int ver4_1 = D1 | D2 | D3;
static int ver4_2 = D5 | D6 | D7;
static int ver5 = E3 | E4 | E5;
static int ver6 = F2 | F4 | F6;
static int ver7 = G1 | G4 | G7;
public static void main(String[] args) {
// sample on how to loop bits
BitBoard game = new BitBoard();
game.makemove(0, A1, 0);
game.makemove(A1, A4, 0);
System.out.println();
tst = 255;
for(int looper = tst, i = Integer.highestOneBit(looper); looper != 0; looper &= ~i, i = Integer.highestOneBit(looper))
{
System.out.println(i);
}
System.out.println(tst);
}
}
还添加了一个循环来显示如何循环遍历位置。
玩得开心。我也将编写这个游戏,因为我想刷新我的 AB 修剪 :-)