this.chessmans = new ArrayList
}
public ChessmanType getChessmanType()
{
return chessmanType;
}
public void addChessman(Position chessman)
{
getChessmans().add(chessman);
}
public boolean contains(Position chessman)
{
return getChessmans().contains(chessman);
}
public void clear()
{
getChessmans().clear();
}
/**
* 判断是否有五子连成线,只需判断以最后下的棋子为中心的2条对接线和2条直线即可
*
*/
public boolean isWin()
{
if (getChessmans().isEmpty())
{
return false;
}
Position currentPosition = getLastChessman();
int lineOfChesses = 0;
Position nextPosition = null;
boolean hasReversal = false;
// 依次遍历2条直线上和2条对角线上的点,遍历每条直线时,当正向找不到时,开始反向找
for (Position offset : getOffsets())
{
lineOfChesses = 1;
nextPosition = currentPosition.offset(offset);
// 是否反向找过了
hasReversal = false;
while ((contains(nextPosition) || !hasReversal))
{
// 不包含这个点时,说明正向直线上已经找不到下一个点了,开始反向找,下一个指向正向的第一个点
if (!contains(nextPosition))
{
hasReversal = true;
nextPosition = currentPosition;
offset = offset.reversal();
}
else
{
lineOfChesses++;
}
if (ChessConstants.WIN_OF_LINE_CHESSES == lineOfChesses)
{
return true;
}
nextPosition = nextPosition.offset(offset);
}
}
return false;
}
/**
* 获取最近下的棋子位置
*
*/
private Position getLastChessman()
{
int size = getChessmans().size();
return getChessmans().get(size - 1);
}
private List
{
return chessmans;
}
private List
{
return offsets;
}
}
[java]
public enum ChessmanType
{
BLACK(ChessConstants.BLACK), WHITE(ChessConstants.WHITE), ERROR(
ChessConstants.ERROR);
private int type;
private ChessmanType(int type)
{
this.type = type;
}
public int getChessmanType()
{
return this.type;
}
/**
* 查找指定的棋手,找不到返回错误信息
*
*/
public static ChessmanType getChessmanTypeByNo(int playerNo)
{
ChessmanType[] types = ChessmanType.values();
for (ChessmanType chessmanType : types)
{
if (chessmanType.getChessmanType() == playerNo)
{
return chessmanType;
}
}
return ERROR;
}
}
public enum ChessmanType
{
BLACK(ChessConstants.BLACK), WHITE(ChessConstants.WHITE), ERROR(
ChessConstants.ERROR);
private int type;
private ChessmanType(int type)
{
this.type = type;
}
public int getChessmanType()
{
return this.type;
}
/**
* 查找指定的棋手,找不到返回错误信息
*
*/
public static ChessmanType getChessmanTypeByNo(int playerNo)
{
ChessmanType[] types = ChessmanType.values();
for (ChessmanType chessmanType : types)
{
if (chessmanType.getChessmanType() == playerNo)
{
return chessmanType;
}
}
return ERROR;
}
}
[java]
/**
* 坐标位置对象
*
* @author dobuy
* @time 2013-5-12
*/
public class Position
{
private int x;
private int y;
public Position(int x, int y)
{
super();
this.x = x;
this.y = y;
}
/**
* 获取偏移后的位置
*
* @param offset 偏移量
* @return
*/
public Position offset(Position offset)
{
return new Position(getX() + offset.getX(), getY() + offset.getY());
}
/**
* 偏移量的X,Y坐标取反
*
* @return
*/
public Position reversal()
{