分类:
2008-09-08 18:24:22
着法生成就是要产生所有有效的着法,让电脑棋手在这些着法中选择最好的着法,最后走出这一着。要生成所有着法只能用穷举了。中国象棋大约每一步可以有45个着法选择。下面是代码:
view plaincopy to clipboardprint?
/*** Generates all valid motions.
* @return all valid motion list, if no motion could be generated,
* returns
null
* @see cn.edu.ynu.sei.chinesechess.common.Motion
*/
@SuppressWarnings("unchecked")
final public List
generatePossibleMoves() { List
ret = new ArrayList ();
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 10; y++) {
int chessman = chessboard[x][y];
if (chessman != 0) {
if (!isRedGo && isRed(chessman)) {
continue;
}
if (isRedGo && !isRed(chessman)) {
continue;
}
switch (chessman) {
case 7:
//
if (isValidMove(x, y, x, y + 1)) {
ret.add(new Motion(chessman, x, y, x, y + 1, 0));
}
if (isValidMove(x, y, x, y - 1)) {
ret.add(new Motion(chessman, x, y, x, y - 1, 0));
}
if (isValidMove(x, y, x - 1, y)) {
ret.add(new Motion(chessman, x, y, x - 1, y, 0));
}
if (isValidMove(x, y, x + 1, y)) {
ret.add(new Motion(chessman, x, y, x + 1, y, 0));
}
for (int oppJiangY = 7; oppJiangY < 10; oppJiangY++) {
if (isValidMove(x, y, x, oppJiangY)) {
ret.add(new Motion(chessman, x, y, x, oppJiangY, 0));
}
}
//
break;
case 14:
//
if (isValidMove(x, y, x, y + 1)) {
ret.add(new Motion(chessman, x, y, x, y + 1, 0));
}
if (isValidMove(x, y, x, y - 1)) {
ret.add(new Motion(chessman, x, y, x, y - 1, 0));
}
if (isValidMove(x, y, x - 1, y)) {
ret.add(new Motion(chessman, x, y, x - 1, y, 0));
}
if (isValidMove(x, y, x + 1, y)) {
ret.add(new Motion(chessman, x, y, x + 1, y, 0));
}
[1]
【责编:landy】
--------------------next---------------------