-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
126 lines (90 loc) · 3.5 KB
/
Grid.java
File metadata and controls
126 lines (90 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// =============================================================================
/**
* The <code>Grid</code> class. Manage a two-dimentional collection of
* <code>Cell</code>s.
**/
// =============================================================================
// =============================================================================
public class Grid {
// =============================================================================
// =========================================================================
/**
* The constructor. Create a grid of <code>Cell</code>s, in their default
* initial states.
*
* @param rows The number of rows in the known region.
* @param columns The number of columns in the known region.
**/
public Grid (int rows, int columns) {
// Ensure that the sizes are valid.
if ((rows <= 0) || (columns <= 0)) {
Support.abort("Cannot construct a grid of size " +
rows +
", " +
columns);
}
// Create the underlying 2-D array that will track the Cells.
_array = new Cell[rows][columns];
// Create the Cells, all in their default states.
for (int row = 0; row < rows; row += 1) {
for (int col = 0; col < columns; col += 1) {
_array[row][col] = new Cell(this, row, col);
}
}
} // Grid()
// =========================================================================
// =========================================================================
/**
* Provide the number of rows in the known region of the grid.
*
* @return The number of rows in the known region.
**/
public int getRows () {
return _array.length;
} // getRows ()
// =========================================================================
// =========================================================================
/**
* Provide the number of columns in the known region of the grid.
*
* @return The number of rows in the known region.
**/
public int getColumns () {
return _array[0].length;
} // getColumns ()
// =========================================================================
// =========================================================================
/**
* Obtain the <code>Cell</code> at given coordinates.
*
* @param row The row of the requested <code>Cell</code>.
* @param column The column of the requested <code>Cell</code>.
* @return If the coordinates are within the grid, the <code>Cell</code> at
* that location; otherwise, <code>null</code>.
**/
public Cell getCell (int row, int column) {
// Is this cell within the known region?
if ((row >= 0) && (row < getRows()) &&
(column >= 0) && (column < getColumns())) {
// Yes, so return that cell.
return _array[row][column];
} else {
// No.
return null;
}
} // getCell()
// =========================================================================
// =========================================================================
// DATA MEMBERS
/**
* The actual 2-D collection of Cells.
**/
private Cell[][] _array;
/**
* Whether to provide debugging information.
**/
private final static boolean _debug = false;
// =========================================================================
// =============================================================================
} // class Grid
// =============================================================================