View Javadoc

1   package net.sourceforge.simplegamenet.chess;
2   
3   public class ChessRook extends ChessPiece {
4   
5       private boolean unmoved = true;
6   
7       public ChessRook(int participantOwnerIndex, int x, int y) {
8           super(participantOwnerIndex, x, y);
9       }
10  
11      public boolean isMoveAllowed(ChessPiece[] pieceGrid,
12                                   int destinationX, int destinationY) {
13          if (pieceGrid[destinationX * GRID_HEIGHT + destinationY] != null
14                  && pieceGrid[destinationX * GRID_HEIGHT + destinationY]
15                  .getParticipantsOwnerIndex() == participantOwnerIndex) {
16              return false;
17          } else if (x != destinationX && y != destinationY) {
18              return false;
19          } else {
20              if (x == destinationX) {
21                  for (int i = 1; i < Math.abs(y - destinationY); i++) {
22                      if (pieceGrid[x * GRID_HEIGHT
23                              + (y + (y < destinationY ? i : -i))] != null) {
24                          return false;
25                      }
26                  }
27              } else {
28                  for (int i = 1; i < Math.abs(x - destinationX); i++) {
29                      if (pieceGrid[(x + (x < destinationX ? i : -i)) * GRID_HEIGHT
30                              + y] != null) {
31                          return false;
32                      }
33                  }
34              }
35              return true;
36          }
37      }
38  
39      public void doMove(ChessPiece[] pieceGrid, int destinationX, int destinationY) {
40          super.doMove(pieceGrid, destinationX, destinationY);
41          unmoved = false;
42      }
43  
44      public Object clone() {
45          // Returns a shallow copy
46          return super.clone();
47      }
48  
49      public boolean isUnmoved() {
50          return unmoved;
51      }
52  
53      public int getPieceType() {
54          return ChessPieceType.ROOK;
55      }
56  
57      public int getPieceValue() {
58          return 4;
59      }
60  
61  }