1 | package jpacman.model; |
2 | |
3 | |
4 | /** |
5 | * A Cell keeps track of its (x,y) position on the board, and the potential |
6 | * Guest occupying the Cell. It's responsibilities include identifying |
7 | * neighbouring cells that fall within the Board's borders (to support moving |
8 | * guests on the board), and keeping the Cell-Guest association consistent. |
9 | * |
10 | * @author Arie van Deursen; Jul 27, 2003 |
11 | * @version $Id: Cell.java,v 1.9 2008/02/03 19:43:38 arie Exp $ |
12 | */ |
13 | public class Cell { |
14 | |
15 | /** |
16 | * The x and y coordinates of the cell. |
17 | */ |
18 | private int x, y; |
19 | |
20 | /** |
21 | * The board the cell lives on. |
22 | */ |
23 | private Board board; |
24 | |
25 | /** |
26 | * The guest occupying the cell, null if not occupied. |
27 | */ |
28 | private Guest inhabitant; |
29 | |
30 | /** |
31 | * Create a new cell at a given position on the board. |
32 | * |
33 | * @param xCoordinate |
34 | * The X coordinate |
35 | * @param yCoordinate |
36 | * The Y coordinate |
37 | * @param b |
38 | * The board |
39 | */ |
40 | public Cell(int xCoordinate, int yCoordinate, Board b) { |
41 | x = xCoordinate; |
42 | y = yCoordinate; |
43 | this.board = b; |
44 | this.inhabitant = null; |
45 | assert invariant(); |
46 | } |
47 | |
48 | /** |
49 | * Conjunction of all invariants. |
50 | * |
51 | * @return true iff all invariants hold. |
52 | */ |
53 | protected boolean invariant() { |
54 | return guestInvariant() && boardInvariant(); |
55 | } |
56 | |
57 | /** |
58 | * A Cell should always be part of the Board given at construction. |
59 | * |
60 | * @return true iff this is the case. |
61 | */ |
62 | protected boolean boardInvariant() { |
63 | return board != null && board.withinBorders(x, y); |
64 | } |
65 | |
66 | /** |
67 | * TODO Invent invariant for the guest association? |
68 | * |
69 | * @return true for the time being. |
70 | */ |
71 | public boolean guestInvariant() { |
72 | return true; |
73 | } |
74 | |
75 | |
76 | |
77 | /** |
78 | * Return the inhabitant of this cell. |
79 | * |
80 | * @return The Guest hosted by this Cell, or null if the Cell is free. |
81 | */ |
82 | public Guest getInhabitant() { |
83 | return inhabitant; |
84 | } |
85 | |
86 | |
87 | /** |
88 | * Modify the guest of this cell. This method is needed by the Guest's |
89 | * occupy method which keeps track of the links in the Cell-Guest |
90 | * association. |
91 | * Precondition: the guest's location should be set at this Cell, |
92 | * and the current cell should not be occupied already |
93 | * by some other guest. |
94 | * <p> |
95 | * Observe that the |
96 | * class invariant doesn't hold at method entry -- therefore it's not a |
97 | * public method. On method exit, however, it is valid again. |
98 | * |
99 | * @param aGuest |
100 | * The new guest of this cell. |
101 | */ |
102 | void setGuest(Guest aGuest) { |
103 | inhabitant = aGuest; |
104 | } |
105 | |
106 | |
107 | /** |
108 | * Remove the inhabitant from this Cell. This method assumes (precondition) |
109 | * that the inhabitant (if any) has already removed it's link to this cell. |
110 | * (Only) to be used by Guest.deoccupy(). |
111 | * <p> |
112 | * Upon method entry, the class invariant doesn't hold, but on |
113 | * method exit it does. |
114 | */ |
115 | void free() { |
116 | inhabitant = null; |
117 | } |
118 | |
119 | /** |
120 | * Determine iff this cell is occupied. |
121 | * |
122 | * @return true iff the cell is occupied. |
123 | */ |
124 | public boolean isOccupied() { |
125 | return inhabitant != null; |
126 | } |
127 | |
128 | |
129 | /** |
130 | * Get the horizontal position of this cell. |
131 | * |
132 | * @return the cell's X coordinate |
133 | */ |
134 | public int getX() { |
135 | assert invariant(); |
136 | return x; |
137 | } |
138 | |
139 | /** |
140 | * Get the vertical position of this cell. |
141 | * |
142 | * @return the cell's Y coordinate. |
143 | */ |
144 | public int getY() { |
145 | assert invariant(); |
146 | return y; |
147 | } |
148 | |
149 | /** |
150 | * Return the cell located at position (x + dx, y + dy), or null if this |
151 | * cell would fall beyond the borders of the Board. |
152 | * |
153 | * @param dx |
154 | * The x offset |
155 | * @param dy |
156 | * The y offset |
157 | * @return null or the cell at (x+dx,y+dy). |
158 | */ |
159 | public Cell cellAtOffset(int dx, int dy) { |
160 | assert invariant(); |
161 | Cell result = null; |
162 | int newx = x + dx; |
163 | int newy = y + dy; |
164 | if (getBoard().withinBorders(newx, newy)) { |
165 | result = getBoard().getCell(newx, newy); |
166 | } |
167 | assert invariant(); |
168 | return result; |
169 | } |
170 | |
171 | /** |
172 | * Return the board this cell is part of. |
173 | * |
174 | * @return This cell's board. |
175 | */ |
176 | public Board getBoard() { |
177 | assert invariant(); |
178 | return board; |
179 | } |
180 | } |