五子棋落子游戏 (四)

2014-11-24 11:10:37 · 作者: · 浏览: 3
return new Position(-getX(), -getY());
}

public int getX()
{
return x;
}

public void setX(int x)
{
this.x = x;
}

public int getY()
{
return y;
}

public void setY(int y)
{
this.y = y;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Position other = (Position) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

@Override
public String toString()
{
return "Position [x=" + x + ", y=" + y + "]";
}
}

/**
* 坐标位置对象
*
* @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()
{
return new Position(-getX(), -getY());
}

public int getX()
{
return x;
}

public void setX(int x)
{
this.x = x;
}

public int getY()
{
return y;
}

public void setY(int y)
{
this.y = y;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Position other = (Position) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

@Override
public String toString()
{
return "Position [x=" + x + ", y=" + y + "]";
}
}[java]
/**
*
* 类名称:ChessGame 类描述: 创建人:dobuy
*
*/
public class ChessGame
{
private Chessboard chessboard;

private ChessPlayer player1;

private ChessPlayer player2;

private int turnNo = ChessConstants.DEFAULT_TURN;

public ChessGame()
{
chessboard = new Chessboard();
player1 = new ChessPlayer(ChessmanType.BLACK);
player2 = new ChessPlayer(ChessmanType.WHITE);
}

/**
* 启动游戏: 每次落一子,如果棋子位置在棋盘上,且落子顺序正常(黑子白子交替下), 则添加棋子到棋盘上,如果没人赢,返回落子成功;
* 如果当前黑子/白子赢了,则返回黑子/白子赢的编号(非棋手的编号)
*
* @param playerNo:玩家序号:黑子棋手/白子棋手
* @param position 落子位置
*
*/
public int startGame(int playerNo, int[] position)
{
ChessmanType chessmanType = ChessmanType.getChessmanTypeByNo(playerNo);

if (ChessmanType.ERROR == chessmanType)
{
return ChessConstants.FAIL;
}

// 位置非法:参数非法、重复落子、棋子位置越界
if (isIllegalPosition(position))
{
return ChessConstants.FAIL;
}

if (getTurnNo() == ChessConstants.DEFAULT_TURN)
{
setTurnNo(chessmanType.getChessmanType());
}
// 不是第一次落子时,判断上一次落子的下一个选手的序号是否和输入相同
else
{
if