if (currentPlayer.isWin())
{
reset();
return playerNo == ChessConstants.BLACK ChessConstants.BLACK_WIN
: ChessConstants.WHITE_WIN;
}
return ChessConstants.SUCCESS;
}
private void reset()
{
getPlayer1().clear();
getPlayer2().clear();
setTurnNo(ChessConstants.DEFAULT_TURN);
}
private ChessPlayer getCurrentChessPlayer(ChessmanType chessmanType)
{
if (getPlayer1().getChessmanType() == chessmanType)
{
return getPlayer1();
}
else
{
return getPlayer2();
}
}
/**
* 判断当前棋子是否合法:包括参数不合法、超越棋盘边界、重复落子
*
*/
private boolean isIllegalPosition(int[] position)
{
if (null == position || position.length != 2)
{
return true;
}
Position pos = new Position(position[0], position[1]);
return isIllegalPosition(pos);
}
private boolean isIllegalPosition(Position position)
{
// 越界
if (getChessboard().isOverEdge(position))
{
return true;
}
// 重复落子
if (getPlayer1().contains(position) || getPlayer2().contains(position))
{
return true;
}
return false;
}
private Chessboard getChessboard()
{
return chessboard;
}
private ChessPlayer getPlayer1()
{
return player1;
}
private ChessPlayer getPlayer2()
{
return player2;
}
private int getTurnNo()
{
return turnNo;
}
private void setTurnNo(int turnNo)
{
this.turnNo = turnNo;
}
}
测试类:
[java]
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
*
* 类名称:ChessGameTest 类描述: 创建人:dobuy
*
*/
public class ChessGameTest
{
private static ChessGame game = new ChessGame();
/**
* 落子成功
*/
int SUCCESS = 1;
/**
* 落子失败:顺序不对、位置非法、指定棋手非法
*/
int FAIL = -1;
/**
* 黑子赢
*/
int BLACK_WIN = 2;
/**
* 白子赢
*/
int WHITE_WIN = 3;
/**
* 黑子
*/
int BLACK = 0;
/**
* 白子
*/
int WHITE = 1;
/**
* CASE1:正常流程
*/
@Test
public void testStartGame1()
{
// 2条直线
assertEquals(game.startGame(BLACK, new int[] { 4, 4 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 5, 3 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 5, 4 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 7, 6 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 6, 4 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 9, 4 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 7, 4 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 2, 4 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 8, 4 }), BLACK_WIN);
assertEquals(game.startGame(BLACK, new int[] { 4, 4 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 5, 3 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 4, 5 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 7, 6 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 4, 6 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 9, 4 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 4, 7 }), SUCCESS);
assertEquals(game.startGame(WHITE, new int[] { 2, 4 }), SUCCESS);
assertEquals(game.startGame(BLACK, new int[] { 4, 8 }), BLACK_WIN);
// 2条对角线
assertEquals(game.startGame(BLACK, new in