着法生成就是要产生所有有效的着法让电脑棋手在这些着法中选择最好的着法最后走出这一着要生成所有着法只能用穷举了中国象棋大约每一步可以有个着法选择下面是代码
view plaincopy to clipboardprint?
<PRE class=java name=code> /**
* Generates all valid motions
* @return all valid motion list if no motion could be generated
* returns <code>null</code>
* @see cneduynumonMotion
*/
@SuppressWarnings(unchecked)
final public List<Motion> generatePossibleMoves() {
List<Motion> ret = new ArrayList<Motion>();
for (int x = ; x < ; x++) {
for (int y = ; y < ; y++) {
int chessman = chessboard[x][y];
if (chessman != ) {
if (!isRedGo && isRed(chessman)) {
continue;
}
if (isRedGo && !isRed(chessman)) {
continue;
}
switch (chessman) {
case :
//<editorfold defaultstate=collapsed desc=Hong Jiang>
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x y)) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y)) {
retadd(new Motion(chessman x y x + y ));
}
for (int oppJiangY = ; oppJiangY < ; oppJiangY++) {
if (isValidMove(x y x oppJiangY)) {
retadd(new Motion(chessman x y x oppJiangY ));
}
}
//</editorfold>
break;
case :
//<editorfold defaultstate=collapsed desc=Hei Jiang>
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x y)) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y)) {
retadd(new Motion(chessman x y x + y ));
}
for (int oppJiangY = ; oppJiangY < ; oppJiangY++) {
if (isValidMove(x y x oppJiangY)) {
retadd(new Motion(chessman x y x oppJiangY ));
}
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Shi>
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y + )) {
retadd(new Motion(chessman x y x + y + ));
}
if (isValidMove(x y x + y )) {
retadd(new Motion(chessman x y x + y ));
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Xiang>
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y + )) {
retadd(new Motion(chessman x y x + y + ));
}
if (isValidMove(x y x + y )) {
retadd(new Motion(chessman x y x + y ));
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Ma>
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x y + )) {
retadd(new Motion(chessman x y x y + ));
}
if (isValidMove(x y x y )) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y + )) {
retadd(new Motion(chessman x y x + y + ));
}
if (isValidMove(x y x + y )) {
retadd(new Motion(chessman x y x + y ));
}
if (isValidMove(x y x + y + )) {
retadd(new Motion(chessman x y x + y + ));
}
if (isValidMove(x y x + y )) {
retadd(new Motion(chessman x y x + y ));
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Che>
// up
for (int i = y + ; i < ; i++) {
if (isValidMove(x y x i)) {
retadd(new Motion(chessman x y x i ));
} else {
// one chessman block its ways
break;
}
}
// down
for (int i = y ; i > ; i) {
if (isValidMove(x y x i)) {
retadd(new Motion(chessman x y x i ));
} else {
// one chessman block its ways
break;
}
}
// left
for (int j = x ; j > ; j) {
if (isValidMove(x y j y)) {
retadd(new Motion(chessman x y j y ));
} else {
// one chessman block its ways
break;
}
}
// right
for (int j = x + ; j < ; j++) {
if (isValidMove(x y j y)) {
retadd(new Motion(chessman x y j y ));
} else {
// one chessman block its ways
break;
}
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Pao>
// up
for (int i = y + ; i < ; i++) {
if (isValidMove(x y x i)) {
retadd(new Motion(chessman x y x i ));
}
}
// down
for (int i = y ; i > ; i) {
if (isValidMove(x y x i)) {
retadd(new Motion(chessman x y x i ));
}
}
// left
for (int j = x ; j > ; j) {
if (isValidMove(x y j y)) {
retadd(new Motion(chessman x y j y ));
}
}
// right
for (int j = x + ; j < ; j++) {
if (isValidMove(x y j y)) {
retadd(new Motion(chessman x y j y ));
}
}
//</editorfold>
break;
case :
case :
//<editorfold defaultstate=collapsed desc=Bing>
if (isRed(chessman)) {
if (isValidMove(x y x y + )) {
// I can see one point at my front
retadd(new Motion(chessman x y x y + ));
}
if (y >= ) {
// passed the He Jie I can see the point at
// my left and right
if (isValidMove(x y x y)) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y)) {
retadd(new Motion(chessman x y x + y ));
}
}
} else {
if (isValidMove(x y x y )) {
// I can see one point at my front
retadd(new Motion(chessman x y x y ));
}
if (y <= ) {
// passed the He Jie I can see the point at
// my left and right
if (isValidMove(x y x y)) {
retadd(new Motion(chessman x y x y ));
}
if (isValidMove(x y x + y)) {
retadd(new Motion(chessman x y x + y ));
}
}
}
//</editorfold>
break;
}
}
}
}
// alphabeta or its variations are all moveordering related!
Collectionssort(ret);
return retisEmpty() ? null : ret;
}
</PRE>
转载请保留作者信息
作者
Blog
MSN & Gmail & QQ