1 | package jpacman.model; |
2 | |
3 | /** |
4 | * Class containing responsibilities shared by all Guests. |
5 | * |
6 | * @author Arie van Deursen; Jul 28, 2003 |
7 | * @version $Id: Guest.java,v 1.9 2008/02/11 13:05:20 arie Exp $ |
8 | */ |
9 | public abstract class Guest { |
10 | |
11 | /** |
12 | * The cell the guest is occupying. |
13 | */ |
14 | private Cell location; |
15 | |
16 | /** |
17 | * The character code representing the player guest type. |
18 | */ |
19 | public static final char PLAYER_TYPE = 'P'; |
20 | |
21 | /** |
22 | * The character code representing the monster guest type. |
23 | */ |
24 | public static final char MONSTER_TYPE = 'M'; |
25 | |
26 | /** |
27 | * The character code representing the food guest type. |
28 | */ |
29 | public static final char FOOD_TYPE = 'F'; |
30 | |
31 | /** |
32 | * The character code representing the wall guest type. |
33 | */ |
34 | public static final char WALL_TYPE = 'W'; |
35 | |
36 | /** |
37 | * The character code representing an empty cell. |
38 | */ |
39 | public static final char EMPTY_TYPE = '0'; |
40 | |
41 | /** |
42 | * Create a new Guest satisfying the class invariant. |
43 | */ |
44 | public Guest() { |
45 | location = null; |
46 | assert guestInvariant(); |
47 | } |
48 | |
49 | /** |
50 | * Actual invariant left as an exercise. |
51 | * @return true iff invariant holds. |
52 | */ |
53 | protected boolean guestInvariant() { |
54 | return true; |
55 | } |
56 | |
57 | |
58 | /** |
59 | * @return The location of this guest. |
60 | */ |
61 | public Cell getLocation() { |
62 | return location; |
63 | } |
64 | |
65 | /** |
66 | * Occupy a non-null, empty cell. |
67 | * Precondition: the current Guest must not |
68 | * have occupied another cell, and the target cell should be empty. |
69 | * Postcondition: both the cell and the guest |
70 | * have changed their pointers to reflect the occupation. |
71 | * |
72 | * @param cell |
73 | * New location for this guest. |
74 | */ |
75 | public void occupy(Cell cell) { |
76 | assert guestInvariant(); |
77 | |
78 | location = cell; |
79 | cell.setGuest(this); |
80 | |
81 | assert guestInvariant(); |
82 | } |
83 | |
84 | /** |
85 | * Remove the occupation link with this Guest's Cell association. |
86 | * |
87 | * @return The location that is now free, or null if the Guest didn't occupy |
88 | * a cell. |
89 | */ |
90 | public Cell deoccupy() { |
91 | assert guestInvariant(); |
92 | Cell oldLocation = location; |
93 | location = null; |
94 | if (oldLocation != null) { |
95 | oldLocation.free(); |
96 | } |
97 | assert guestInvariant(); |
98 | return oldLocation; |
99 | } |
100 | |
101 | /** |
102 | * The player would like to visit the cell occupied by this guest. Indicate |
103 | * whether this is possible, and modify the move's state to indicate what |
104 | * the effect of such a move would be. Precondition: the move object is non |
105 | * null and initializing. |
106 | * <p> |
107 | * |
108 | * @param aMove |
109 | * theMove move object representing intended move and its |
110 | * effects. |
111 | * |
112 | * @return True iff this guest has no objection to the player taking his |
113 | * place. |
114 | */ |
115 | protected abstract boolean meetPlayer(PlayerMove aMove); |
116 | |
117 | |
118 | /** |
119 | * Return a character code representing the type of guest. |
120 | * |
121 | * @return Type code for this guest. |
122 | */ |
123 | public abstract char guestType(); |
124 | |
125 | } |