1 | package jpacman.model; |
2 | |
3 | /** |
4 | * Maintain a rectangular board of cells, potentially occupied by guests. After |
5 | * the board has been created, the dimensions cannot be modified anymore. Guests |
6 | * can move around on the board, and keep track of their position on the board. |
7 | * |
8 | * @author Arie van Deursen; Jul 27, 2003 |
9 | * @version $Id: Board.java,v 1.5 2008/02/03 19:43:38 arie Exp $ |
10 | */ |
11 | public class Board { |
12 | |
13 | /** |
14 | * Width and height of the board. |
15 | */ |
16 | private int width, height; |
17 | |
18 | /** |
19 | * The array of cells constituting the board. |
20 | */ |
21 | private Cell[][] cellAt; |
22 | |
23 | /** |
24 | * Create a new board given a width and a height. |
25 | * |
26 | * @param w |
27 | * Width of the board |
28 | * @param h |
29 | * Height of the board |
30 | */ |
31 | public Board(int w, int h) { |
32 | assert w >= 0; |
33 | assert h >= 0; |
34 | width = w; |
35 | height = h; |
36 | cellAt = new Cell[w][h]; |
37 | for (int x = 0; x < w; x++) { |
38 | for (int y = 0; y < h; y++) { |
39 | cellAt[x][y] = new Cell(x, y, this); |
40 | } |
41 | } |
42 | assert invariant(); |
43 | assert consistentBoardCellAssociation(); |
44 | } |
45 | |
46 | /** |
47 | * A board's invariant is simply that both the width and the height are not |
48 | * negative. |
49 | * |
50 | * @return True iff widht and height nonnegative. |
51 | */ |
52 | protected boolean invariant() { |
53 | return width >= 0 && height >= 0; |
54 | } |
55 | |
56 | /** |
57 | * Check that each cell has a correct link to this board. This function |
58 | * could be part of the invariant, but checking it each time is considered |
59 | * too expensive, which is why it is offered as a separate function. |
60 | * |
61 | * @return True iff the cell/board association is consistent |
62 | */ |
63 | protected boolean consistentBoardCellAssociation() { |
64 | boolean result = true; |
65 | for (Cell[] row : cellAt) { |
66 | for (Cell c : row) { |
67 | result = result && c.getBoard().equals(this); |
68 | } |
69 | } |
70 | return result; |
71 | } |
72 | |
73 | /** |
74 | * Return the cell at position (x,y). Precondition: (x,y) falls wihtin the |
75 | * borders of the board. Postcondition: returned cell exists and is not |
76 | * null. |
77 | * |
78 | * @param x |
79 | * Horizontal coordinate of the requested cell |
80 | * @param y |
81 | * Vertical coordinate of the requested cell |
82 | * @return The cell at (x,y). |
83 | */ |
84 | public Cell getCell(int x, int y) { |
85 | assert invariant(); |
86 | assert withinBorders(x, y) |
87 | : "Cell requested (" + x + "," + y + ") out of borders " |
88 | + width + " * " + height; |
89 | Cell result = cellAt[x][y]; |
90 | assert result != null; |
91 | assert invariant(); |
92 | return result; |
93 | } |
94 | |
95 | /** |
96 | * Return the guest occupying position (x,y), or null if the cell is emtpy. |
97 | * Precondition: (x,y) falls wihtin the borders of the board. |
98 | * |
99 | * @param x |
100 | * Horizontal coordinate of the requested cell |
101 | * @param y |
102 | * Vertical coordinate of the requested cell |
103 | * @return The guest at (x,y). |
104 | */ |
105 | public Guest getGuest(int x, int y) { |
106 | assert invariant(); |
107 | assert withinBorders(x, y); |
108 | return cellAt[x][y].getInhabitant(); |
109 | } |
110 | |
111 | /** |
112 | * Return the guest code of the cell at (x,y). |
113 | * |
114 | * @param x |
115 | * Horizontal position |
116 | * @param y |
117 | * Vertical position |
118 | * @return Code representing guest type |
119 | */ |
120 | public char guestCode(int x, int y) { |
121 | assert invariant(); |
122 | assert withinBorders(x, y); |
123 | char result; |
124 | Guest guest = getGuest(x, y); |
125 | if (guest != null) { |
126 | result = guest.guestType(); |
127 | } else { |
128 | result = Guest.EMPTY_TYPE; |
129 | } |
130 | assert invariant(); |
131 | return result; |
132 | } |
133 | |
134 | /** |
135 | * Return true iff (x,y) falls within the borders of the board. |
136 | * |
137 | * @param x |
138 | * Horizontal coordinate of requested position |
139 | * @param y |
140 | * Vertical coordinate of requested position. |
141 | * @return True iff (x,y) is on the board. |
142 | */ |
143 | public boolean withinBorders(int x, int y) { |
144 | // TODO actual implementation left as an exercise. |
145 | return true; |
146 | } |
147 | |
148 | /** |
149 | * Return the width of the board. |
150 | * |
151 | * @return The board's with |
152 | */ |
153 | public int getWidth() { |
154 | assert invariant(); |
155 | return width; |
156 | } |
157 | |
158 | /** |
159 | * Return the height of the board. |
160 | * |
161 | * @return The board's height. |
162 | */ |
163 | public int getHeight() { |
164 | assert invariant(); |
165 | return height; |
166 | } |
167 | } |