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.pieces;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import com.webstersmalley.chessweb.model.Board;
25  import com.webstersmalley.chessweb.model.InvalidMoveException;
26  import com.webstersmalley.chessweb.model.Move;
27  import com.webstersmalley.chessweb.model.Piece;
28  import com.webstersmalley.chessweb.model.Position;
29  import com.webstersmalley.chessweb.model.Team;
30  
31  /***
32   * Pawn class.
33   * 
34   * @author Matthew Smalley
35   */
36  public class Pawn extends Piece {
37      /*** Logger for this class and subclasses. */
38      private final Log logger = LogFactory.getLog(getClass());
39  
40      /*** White starting positions. * */
41      public static final String WHITE_STARTING_POSITIONS = 
42          "a2:b2:c2:d2:e2:f2:g2:h2";
43  
44      /*** Black starting positions. * */
45      public static final String BLACK_STARTING_POSITIONS = 
46          "a7:b7:c7:d7:e7:f7:g7:h7";
47  
48      /***
49       * Constructs the Pawn.
50       * 
51       * @param team
52       *            the team.
53       */
54      public Pawn(final Team team) {
55          super(team);
56          if (team == Team.WHITE) {
57              setStartingPositions(WHITE_STARTING_POSITIONS);
58          } else {
59              setStartingPositions(BLACK_STARTING_POSITIONS);
60          }
61      }
62  
63      /***
64       * Whether the pawn is in the starting position. This is used to determine
65       * whether the pawn can move 1 or 2 squares.
66       * 
67       * @param position
68       *            the position to check.
69       * @param team
70       *            the team
71       * @return boolean representing whether it is in the starting position.
72       */
73      private static boolean isStartingPosition(final Position position,
74              final Team team) {
75          return position.getRank() == team.getPawnStartingRank();
76      }
77  
78      /***
79       * Validates the direction.
80       * 
81       * @param move
82       *            the move
83       * @param board
84       *            the board
85       * @throws InvalidMoveException
86       *             if the move is invalid.
87       */
88      public final void validateDirection(final Move move, final Board board)
89              throws InvalidMoveException {
90  
91          logger.debug("Attempting to validate: " + move);
92  
93          if (move.getYDirection() != getTeam().getPawnIncrement()) {
94              throw new InvalidMoveException("Invalid move. "
95                  + "Pawns must move towards the opponent!");
96          }
97  
98          if (moveIsATake(move.getDestination(), board)) {
99              logger.debug("Moving " + getTeam() + "'s pawn");
100             if (move.getYDifference() != getTeam().getPawnIncrement()) {
101                 throw new InvalidMoveException("Invalid move. "
102                     + "Pawns must take diagonally towards the opponent!");
103             }
104             if (move.getXMagnitude() != 1) {
105                 throw new InvalidMoveException("Invalid move. "
106                     + "Pawns must take diagonally towards the opponent!");
107             }
108         } else {
109             if (move.getXDifference() != 0) {
110                 throw new InvalidMoveException(
111                         "Invalid move. Pawns cannot move from side to side!");
112             }
113 
114             int maxMove = 1;
115             if (Pawn.isStartingPosition(move.getSource(), getTeam())) {
116                 maxMove = 2;
117             }
118             if (move.getYMagnitude() > maxMove) {
119                 throw new InvalidMoveException(
120                         "Invalid move. This pawn cannot move more than "
121                             + maxMove + " squares!");
122             }
123 
124         }
125     }
126 }