【发布时间】:2016-10-04 19:50:50
【问题描述】:
我正在使用下面的 Bitfield 结构来存储国际象棋游戏的棋盘状态,它是一个 32 位整数。我正在编码/解码的字段是:
epSquare(0 来自 63)
一半移动计数(0 到 100)
当前玩家(0 或 1)
4 个易位权标志
我可以创建和提取“状态”,但我无法改变给定的状态:
/*
0000 0000 0000 0000 0000 0011 1111 epSquare 0x3f
0000 0000 0000 0001 1111 1100 0000 halfMoves 0x7f >> 6
0000 0000 0000 0010 0000 0000 0000 curPlayer 0x1 >> 13;
0000 0000 0000 0100 0000 0000 0000 Kc 0x4000
0000 0000 0000 1000 0000 0000 0000 Qc 0x8000
0000 0000 0001 0000 0000 0000 0000 kc 0x10000
0000 0000 0010 0000 0000 0000 0000 qc 0x20000
*/
public class State {
public static int new_state(int epSquare, int halfMoves, int turn, int flags){
return epSquare | (halfMoves << 6) | (turn << 13) | flags;
}
public static int epSquare(int s){
return s & 0x3f;
}
public static int halfMoves(int s){
return s >> 6 & 0x7f;
}
// s state, e new epSquare
public static int setEPSquare(int s, int e){
//??
}
// s state, h halfMoves value
public static int setHalfMoves(int s, int e){
//??
}
public static void main (String[] args){
int s = BoardState.new_state(36, 84, 1, 0);
System.out.println("ep square: " + epSquare(s)); //36
System.out.println("half moves: " + halfMoves(s)); //84
s = setHalfMoves(s, 13);
System.out.println("half moves: " + halfMoves(s)); //should give 13
}
}
如何实现setHalfMoves 方法?第一个,setEPSquare,似乎工作,但我无法弄清楚前者。谢谢!
【问题讨论】:
-
参数
s和e是什么意思,这个方法应该返回什么? -
s 是状态,e 是这个参数的新值(见编辑)。
-
nm...我想我明白了。
-
请注意,调用
setHalfMoves(s, 13)没有任何效果,因为您的班级State没有任何状态。
标签: java bit-manipulation chess bitmask