View Javadoc

1   /**************************************************************************
2    Copyright 2005 Webstersmalley
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   *************************************************************************/
16  /*
17   * Created on 23-Aug-2005
18   */
19  package com.webstersmalley.chessweb.model;
20  
21  import com.webstersmalley.chessweb.model.pieces.Bishop;
22  import com.webstersmalley.chessweb.model.pieces.King;
23  import com.webstersmalley.chessweb.model.pieces.Knight;
24  import com.webstersmalley.chessweb.model.pieces.Pawn;
25  import com.webstersmalley.chessweb.model.pieces.Queen;
26  import com.webstersmalley.chessweb.model.pieces.Rook;
27  
28  /***
29   * Represents an individual chess piece.
30   * 
31   * @author Matthew Smalley
32   */
33  public abstract class Piece {
34  
35      /***
36       * Constructs a new Piece.
37       * 
38       * @param team
39       *            the team.
40       */
41      public Piece(final Team team) {
42          this.team = team;
43      }
44  
45      /*** White Bishop Piece. * */
46      public static final Piece WHITE_BISHOP = new Bishop(Team.WHITE);
47  
48      /*** Black Bishop Piece. * */
49      public static final Piece BLACK_BISHOP = new Bishop(Team.BLACK);
50  
51      /*** White King Piece. * */
52      public static final Piece WHITE_KING = new King(Team.WHITE);
53  
54      /*** Black King Piece. * */
55      public static final Piece BLACK_KING = new King(Team.BLACK);
56  
57      /*** White Knight Piece. * */
58      public static final Piece WHITE_KNIGHT = new Knight(Team.WHITE);
59  
60      /*** Black Knight Piece. * */
61      public static final Piece BLACK_KNIGHT = new Knight(Team.BLACK);
62  
63      /*** White Pawn Piece. * */
64      public static final Piece WHITE_PAWN = new Pawn(Team.WHITE);
65  
66      /*** Black Pawn Piece. * */
67      public static final Piece BLACK_PAWN = new Pawn(Team.BLACK);
68  
69      /*** White Queen Piece. * */
70      public static final Piece WHITE_QUEEN = new Queen(Team.WHITE);
71  
72      /*** Black Queen Piece. * */
73      public static final Piece BLACK_QUEEN = new Queen(Team.BLACK);
74  
75      /*** White Rook Piece. * */
76      public static final Piece WHITE_ROOK = new Rook(Team.WHITE);
77  
78      /*** Black Rook Piece. * */
79      public static final Piece BLACK_ROOK = new Rook(Team.BLACK);
80  
81      /*** All the Pieces - used for iterating. * */
82      public static final Piece[] ALL_PIECES = new Piece[] {
83          WHITE_PAWN, WHITE_ROOK, WHITE_KNIGHT, WHITE_BISHOP, WHITE_KING,
84          WHITE_QUEEN, BLACK_PAWN, BLACK_ROOK, BLACK_KNIGHT, BLACK_BISHOP,
85          BLACK_KING, BLACK_QUEEN
86      };
87  
88      /*** The name of the piece. * */
89      private String name = getClass().getSimpleName();
90  
91      /*** Which team the piece is on. */
92      private Team team;
93  
94      /*** Whether the piece can jump. */
95      private boolean jumper = false;
96  
97      /*** Array of Positions representing the start positions. */
98      private String startingPositions = "";
99  
100     /***
101      * Returns the htmlName.
102      * 
103      * @return the name
104      */
105     public final String getIcon() {
106         return getName() + "-" + getTeam();
107     }
108 
109     /***
110      * Returns the starting positions.
111      * 
112      * @return the starting positions.
113      */
114     public final String getStartingPositions() {
115         return startingPositions;
116     }
117 
118     /***
119      * Sets the startign positions.
120      * 
121      * @param startingPositions
122      *            the starting positions.
123      */
124     public final void setStartingPositions(final String startingPositions) {
125         this.startingPositions = startingPositions;
126     }
127 
128     /***
129      * @return Returns the team.
130      */
131     public final Team getTeam() {
132         return team;
133     }
134 
135     /***
136      * @param team
137      *            The team to set.
138      */
139     public final void setTeam(final Team team) {
140         this.team = team;
141     }
142 
143     /***
144      * Get the name of this piece.
145      * 
146      * @return the name of the piece
147      */
148     public final String getName() {
149         return name;
150     }
151 
152     /***
153      * Validate a move.
154      * 
155      * @throws InvalidMoveException
156      *             if the move is invalid.
157      * @param move
158      *            the move
159      * @param board
160      *            the board
161      */
162     public final void validateMove(final Move move, final Board board)
163             throws InvalidMoveException {
164         if (!board.isOnBoard(move.getDestination())) {
165             throw new InvalidMoveException(
166                     "The destination is not on the board: "
167                         + move.getDestination());
168         }
169         validateDirection(move, board);
170         if (!isJumper()) {
171             validatePathIsClear(move, board);
172         }
173         validateTake(move, board);
174     }
175 
176     /***
177      * Checks whether the path is clear by "walking" the path.
178      * 
179      * @param move
180      *            the move
181      * @param board
182      *            the board
183      * @throws InvalidMoveException
184      *             if the path isn't clear.
185      */
186     private void validatePathIsClear(final Move move, final Board board)
187             throws InvalidMoveException {
188         if (move.isPathClear(board)) {
189             return;
190         }
191         throw new InvalidMoveException(
192                 "Invalid move. This piece cannot jump over others");
193     }
194 
195     /***
196      * Validate a direction.
197      * 
198      * @throws InvalidMoveException
199      *             if the move is invalid.
200      * @param board
201      *            the board
202      * @param move
203      *            the move
204      */
205     public abstract void validateDirection(Move move, final Board board)
206             throws InvalidMoveException;
207 
208     /***
209      * Validate a take.
210      * 
211      * @throws InvalidMoveException
212      *             if the move is invalid.
213      * @param move
214      *            the move
215      * @param board
216      *            the board
217      */
218     public final void validateTake(final Move move, final Board board)
219             throws InvalidMoveException {
220         if (board.getAt(move.getDestination()) != null) {
221             Team destinationTeam = board.getAt(move.getDestination()).getTeam();
222             if (destinationTeam.equals(getTeam())) {
223                 throw new InvalidMoveException(
224                         "Destination piece is on the same team");
225             }
226         }
227     }
228 
229     /***
230      * Whether the proposed move is a take. Note this doesn't <i>validate</i>
231      * the move or the take.
232      * 
233      * @param destination
234      *            the destination position.
235      * @param board
236      *            the board
237      * @return whether the move is a take.
238      */
239     public final boolean moveIsATake(final Position destination,
240             final Board board) {
241         return (board.getAt(destination) != null);
242     }
243 
244     /***
245      * @return Returns the canJump.
246      */
247     public final boolean isJumper() {
248         return jumper;
249     }
250 
251     /***
252      * @param jumper
253      *            The jumper to set.
254      */
255     public final void setJumper(final boolean jumper) {
256         this.jumper = jumper;
257     }
258 
259 }