1 | package jpacman.model; |
2 | |
3 | /** |
4 | * Food having a number of calories, which can occupy a cell on the board. |
5 | * |
6 | * @author Arie van Deursen; Jul 28, 2003 |
7 | * @version $Id: Food.java,v 1.9 2008/02/07 12:46:27 arie Exp $ |
8 | */ |
9 | public class Food extends Guest { |
10 | |
11 | /** |
12 | * Number of points this food element represents. |
13 | */ |
14 | private int points; |
15 | |
16 | /** |
17 | * Create a new Food element. |
18 | * |
19 | * @param p |
20 | * Calories of this piece of food. |
21 | */ |
22 | public Food(int p) { |
23 | points = p; |
24 | assert foodInvariant(); |
25 | } |
26 | |
27 | /** |
28 | * Create a simple piece of food of just one point. |
29 | */ |
30 | public Food() { |
31 | this(1); |
32 | assert foodInvariant(); |
33 | } |
34 | |
35 | /** |
36 | * Whatever happens, the amount of food is non-negative. |
37 | * |
38 | * @return True iff the food is non-negative and the guest's super invariant |
39 | * holds as well. |
40 | */ |
41 | public boolean foodInvariant() { |
42 | return guestInvariant() && points >= 0; |
43 | } |
44 | |
45 | /** |
46 | * Provide the number of calories for this piece of food. |
47 | * |
48 | * @return Points of this food element. |
49 | */ |
50 | public int getPoints() { |
51 | return points; |
52 | } |
53 | |
54 | /** |
55 | * The player wants to eat this food cell. Modify the move's state to |
56 | * reflect the effect this would have. Precondition: the move is in its |
57 | * initialization stage. |
58 | * |
59 | * @see jpacman.model.Guest#meetPlayer(jpacman.model.PlayerMove) |
60 | * @param theMove the move the player intends to do. |
61 | * @return True, since such a move is possible. |
62 | */ |
63 | @Override |
64 | protected boolean meetPlayer(PlayerMove theMove) { |
65 | assert foodInvariant(); |
66 | assert theMove != null; |
67 | assert !theMove.initialized(); |
68 | theMove.setFoodEaten(points); |
69 | return true; |
70 | } |
71 | |
72 | |
73 | /** |
74 | * @see jpacman.model.Guest#guestType() |
75 | * @return character encoding for food guests. |
76 | */ |
77 | @Override |
78 | public char guestType() { |
79 | return Guest.FOOD_TYPE; |
80 | } |
81 | |
82 | } |