Repository navigation
Expand file tree
/
Copy pathSoldierPiece.java
More file actions
66 lines (64 loc) · 2.49 KB
/
Copy pathSoldierPiece.java
File metadata and controls
66 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
*
* A class which represents a Xiangqi Soldier Piece
* @author Peter Schlueter
*
*/
public class SoldierPiece extends ChessPiece implements OneSpaceHorizontalMove
{
/**
* Creates a Xiangqi king chess piece with the given user specifications
* @param side the side in which the chess piece is on
* @param row the row the chess piece is in
* @param column the column the chess piece is in
* @param board the board the chess piece is on
*/
public SoldierPiece(ChessGame.Side side, int row, int column, ChessBoard board)
{
super(side, "S", null, row, column, board);
}
/**
* Checks if the inputted move is legal for a soldier
* @param toRow stores the row which the rook is intended to be moved to
* @param toColumn stores the column which the rook is intended to be moved to
* @return true if the move is legal for a king and not going off the board or in the same location as before, false otherwise
*/
@Override
public boolean isLegalMove(int toRow, int toColumn)
{
if (super.isLegalMove(toRow, toColumn))
{
//If this piece is on the NORTH side, do the following checks
if (getSide() == ChessGame.Side.NORTH)
{
//If this piece is moving up one space, return true
if ((getColumn() == toColumn) && (getRow() - toRow == -1))
{
return true;
}
//If this piece is of the NORTH side and has crossed the middle of the board, it can also make a one space horizontal move
//Integer division erros are avoided, since 10 (the number of rows of the board) divides evenly into 2
if ((toRow >= (getChessBoard().getGameRules().getNumRows() / 2)) && (isOneSpaceHorizontalMove(toRow, toColumn, this)))
{
return true;
}
}
//If this piece is on the SOUTH side, do the following checks
if (getSide() == ChessGame.Side.SOUTH)
{
//If this piece is moving up one space, return true
if ((getColumn() == toColumn) && (getRow() - toRow == 1))
{
return true;
}
//If this piece is of the SOUTH side and has crossed the middle of the board, it can also make a one space horizontal move
//Integer division errors are avoided since 10 (the number of rows of the board) divides evenly into 2
if ((toRow < (getChessBoard().getGameRules().getNumRows() / 2)) && (isOneSpaceHorizontalMove(toRow, toColumn, this)))
{
return true;
}
}
}
return false;
}
}