{
return edgePosition;
}
private Position getOrigonPosition()
{
return origonPosition;
}
}[java]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* 类名称:ChessPlayer 类描述: 棋手 创建人:dobuy
*
*/
public class ChessPlayer
{
/**
* 棋子的类型:黑棋/白棋
*/
private ChessmanType chessmanType;
/**
* 已经落的棋子位置
*/
private List
/**
* 棋子连成一条线(相邻2点)的偏移量, 只列了正方向,如:向下、向右、向右下、右上, 另4个方向通过Position的reversal()方法来表达
*/
private List
new Position(1, 0), new Position(1, 1), new Position(1, -1));
public ChessPlayer(ChessmanType type)
{
this.chessmanType = type;
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;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* 类名称:ChessPlayer 类描述: 棋手 创建人:dobuy
*
*/
public class ChessPlayer
{
/**
* 棋子的类型:黑棋/白棋
*/
private ChessmanType chessmanType;
/**
* 已经落的棋子位置
*/
private List
/**
* 棋子连成一条线(相邻2点)的偏移量, 只列了正方向,如:向下、向右、向右下、右上, 另4个方向通过Position的reversal()方法来表达
*/
private List
new Position(1, 0), new Position(1, 1), new Position(1, -1));
public ChessPlayer(ChessmanType type)
{
this.chess