Day01
开篇
自从爱恩斯坦棋列入中国大学生计算机博弈大赛项目之后,爱恩斯坦棋逐渐被人们熟知,对爱恩斯坦棋估值、搜索和系统的研究也逐年增加。本文主要研究利用java开发爱恩斯坦棋的对战系统,设计对战界面,实现人-人、人-机和机-机自动对弈,以及后期研究内部对战算法。
需求分析
开发前进行简要的需求分析
首先主要是设计棋盘、棋子
定义枚举PieceType,里面有三种状态 BLUE表示蓝方、RED表示红方、NULL则表示该位置没有棋子。
package Enstein.board;
/**
* 棋子类型,BLUE表示蓝方;RED表示红方;NULL表示无棋子
*
* @author 敷衍zgf
*
*/
public enum PieceType{
/**
* 蓝色
*/
BLUE,
/**
* 红色
*/
RED,
/**
* 空白
*/
NULL;
}
设计将棋子进行类型和编号的定义,并转换成棋谱输出在文本框上。
package Enstein.board;
/**
* 棋子 每个棋子用独有的编码id表示,id的取值范围:{0,11~16,21~26}
* 0表示无棋子;
* 11~16表示蓝方的1~6号棋子;
* 21~26表示红方的1~6号棋子.
*
* @author 敷衍zgf
*
*/
public class Piece{
/**
* 创建一个棋子,根据棋子类型和编号转换成编码id
*
* @param type 棋子类型
* @param number 棋子编号
*
* @return 棋子ID
*/
public static byte create(PieceType type, byte number) {
byte id = 0;
if (type == PieceType.RED) //红方21~26
id = (byte) (20 + number);
else if (type == PieceType.BLUE) //蓝方11~16
id = (byte) (10 + number);
else id=0;//没有棋子
return id; //返回给piece
}
/**
* 获取棋子类型
*
* @param piece 棋子ID
* @return PieceType 棋子类型
*/
public static final PieceType getPieceType(byte piece){ //piece值为返回的id
if (piece / 10 == 2) // (21~26)/10 ==2
return PieceType.RED; //则这个棋子是红方
else if (piece / 10 == 1) // (11~16)/10 ==1
return PieceType.BLUE; //则这个棋子是蓝方
else
return PieceType.NULL; //没有棋子
}
/**
* 获取棋子编号
*
* @param piece 棋子id
* @return 编号
*/
public static final byte getNumber(byte piece) { //piece值为返回的id
return (byte) (piece % 10); //(piece%10)余数为1~6,可得棋子编号
}
/**
* 获取ID号
*
* @param type 棋子类型
* @param number 棋子编号
* @return id
*/
public static final byte getID(PieceType type, byte number) {
byte id=0;
if (type == PieceType.RED)
id = (byte) (20 + number);
if (type == PieceType.BLUE)
id = (byte) (10 + number);
else
id = 0;
return id;
}
/**
* 将棋子转换成文本,使棋子可直接打印至控制台
*
* 例:
* System.out.println(12) 输出结果: B2
* System.out.println(22) 输出结果: R2
*
* @param piece 棋子
* @return String
*/
public static String toString(byte piece){ //piece值为返回的id,利用getPieceType()方法,根据piece的值判断是否为
if(Piece.getPieceType(piece) == PieceType.BLUE){ //若是蓝方
return new String("B"+String.valueOf(Piece.getNumber(piece))+"\t");
}//通过getNumber()方法获得棋子编号 打印输出B(_)
else if(Piece.getPieceType(piece) == PieceType.RED){ //若是红方
return new String("R"+String.valueOf(Piece.getNumber(piece))+"\t");
}//通过getNumber()方法获得棋子编号 打印输出R(_)
else return new String("-" + "\t"); //空棋子 输出-
}
}
设计棋子移动方向,移动方向是以棋子为本体,以敌方的阵营方向为正前方而定。
package Enstein.game;
/**
* 移动方向:左方,右方,前方。为了统一方向,移动方向是以棋子为本体,以敌方的阵营方向为正前方而定的
*
* 对于红方棋子:
* 左方:向右移动一格
* 前方:向右下移动一格
* 右方:向下移动一格
*
* 对于蓝方棋子:
* 左方:向左移动一格
* 前方:向左上移动一格
* 右方:向上移动一格
*
* @author 敷衍zgf
*
*/
public enum MoveDirection {
/**
* 向左方移动 ,即红方棋子右移/蓝方棋子左移
*/
LEFT,
/**
* 向右方移动,即红方棋子下移/蓝方棋子上移
*/
RIGHT,
/**
* 向前方移动,即红方棋子右下移动/蓝方棋子左上移动
*/
FORWARD
}
package Enstein.game;
import Enstein.board.Piece;
/**
* 下棋动作 由棋子编码ID和移动方向决定
*
* @author 郑国风
*
*/
public class Move {
/**
* 棋子
*/
private byte piece;
/**
* 移动方向
*/
private MoveDirection direction;
/**
* 构造函数
*
* @param piece 棋子ID
* @param direction 移动方向
*/
public Move(byte piece, MoveDirection direction) {
this.piece = piece;
this.direction = direction;
}
/**
* 获取棋子
*
* @return Piece 棋子
*/
public byte getPiece() {
return piece;
}
/**
* 设置棋子
*
* @param piece 棋子
*/
public void setPiece(byte piece) {
this.piece = piece;
}
/**
* 获取移动方向
*
* @return MoveDirection 移动方向
*/
public MoveDirection getDirection() {
return direction;
}
/**
* 设置移动方向
*
* @param direction 移动方向
*/
public void setDirection(MoveDirection direction) {
this.direction = direction;
}
/**
* 比较当前动作与指定动作是否相同
*
* @param move 被比较的动作
* @return true 相同
* false 不相同
*/
public boolean compareTo(Move move) {
// TODO 自动生成的方法存根
if(piece == move.piece && direction==move.direction) return true;
else return false;
}
@Override
public Move clone(){
Move move=new Move(this.piece,this.direction);
return move;
}
/**
* 输出至控制台
*
* @param tabNum tab键数量
*/
public void printMove(int tabNum) {
// TODO 自动生成的方法存根
for(int i=0;i<tabNum;i++) System.out.print(" ");
System.out.println("Move Info:");
for(int i=0;i<tabNum;i++) System.out.print(" ");
System.out.println("Piece:"+Piece.toString(piece));
for(int i=0;i<tabNum;i++) System.out.print(" ");
System.out.println("MoveDirection:"+direction);
}
/*
* 转化为文本格式
*/
@Override
public String toString(){
return Piece.toString(piece).trim()+" "+direction;
}//trim()取出字符串前后空格
/*
* 用途在于使Move类可以作为Map对象的Key值
*/
@Override
public boolean equals(Object object){
Move move=(Move) object;
return compareTo(move);
}
/*
*
* 哈希编码,使Move类可以作为Map对象的Key值
*/
public int hashCode() {
return this.piece * 10 + this.direction.hashCode();
}
}