1 | package jpacman.model; |
2 | |
3 | /** |
4 | * Class to represent the effects of moving the player. |
5 | * |
6 | * @author Arie van Deursen; Aug 18, 2003 |
7 | * @version $Id: PlayerMove.java,v 1.6 2008/02/11 13:05:20 arie Exp $ |
8 | */ |
9 | public class PlayerMove extends Move { |
10 | |
11 | /** |
12 | * The player wishing to move. |
13 | */ |
14 | private Player thePlayer; |
15 | |
16 | /** |
17 | * The amount of food that will be eaten if this move is |
18 | * successful. |
19 | */ |
20 | private int foodEaten = 0; |
21 | |
22 | /** |
23 | * Create a move for the given player to a given target cell. |
24 | * |
25 | * @param player |
26 | * the Player to be moved |
27 | * @param newCell |
28 | * the target location. |
29 | * @see jpacman.model.Move |
30 | */ |
31 | public PlayerMove(Player player, Cell newCell) { |
32 | // preconditions checked in super method, |
33 | // and cannot be repeated here ("super(...)" must be 1st stat.). |
34 | super(player, newCell); |
35 | thePlayer = player; |
36 | precomputeEffects(); |
37 | assert invariant(); |
38 | } |
39 | |
40 | /** |
41 | * Verify that the food eaten remains non negative, the player/mover equal |
42 | * and non-null. |
43 | * |
44 | * @return true iff the invariant holds. |
45 | */ |
46 | public boolean invariant() { |
47 | return moveInvariant() && foodEaten >= 0 && thePlayer != null |
48 | && getMovingGuest().equals(thePlayer); |
49 | } |
50 | |
51 | /** |
52 | * Attempt to move the player towards a target guest. |
53 | * @param targetGuest The guest that the player will meet. |
54 | * @return true if the move is possible, false otherwise. |
55 | * @see jpacman.model.Move#tryMoveToGuest(jpacman.model.Guest) |
56 | */ |
57 | @Override |
58 | protected boolean tryMoveToGuest(Guest targetGuest) { |
59 | assert tryMoveToGuestPrecondition(targetGuest) |
60 | : "percolated precondition"; |
61 | return targetGuest.meetPlayer(this); |
62 | } |
63 | |
64 | /** |
65 | * Return the player initiating this move. |
66 | * |
67 | * @return The moving player. |
68 | */ |
69 | public Player getPlayer() { |
70 | assert invariant(); |
71 | return thePlayer; |
72 | } |
73 | |
74 | /** |
75 | * Return the food that would be eaten if this move is applied. |
76 | * Precondition: the move is initialized. |
77 | * |
78 | * @return The food that would be eaten. |
79 | */ |
80 | public int getFoodEaten() { |
81 | assert invariant(); |
82 | return foodEaten; |
83 | } |
84 | |
85 | /** |
86 | * Set the amount of food eaten to a given value. |
87 | * |
88 | * @param food |
89 | * the amount of food. |
90 | */ |
91 | protected void setFoodEaten(int food) { |
92 | assert invariant() : "Invariant before set food."; |
93 | foodEaten = food; |
94 | assert invariant() : "Invariant in set Food Eaten"; |
95 | } |
96 | |
97 | /** |
98 | * Actually apply the move, assuming it is possible. |
99 | */ |
100 | @Override |
101 | public void apply() { |
102 | assert invariant(); |
103 | assert movePossible(); |
104 | super.apply(); |
105 | int oldFood = getPlayer().getPointsEaten(); |
106 | getPlayer().eat(foodEaten); |
107 | assert getPlayer().getPointsEaten() == oldFood + foodEaten; |
108 | assert invariant(); |
109 | } |
110 | |
111 | } |