-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
47 lines (39 loc) · 1.12 KB
/
Grid.java
File metadata and controls
47 lines (39 loc) · 1.12 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
import java.util.ArrayList;
public class Grid {
private Cell[][] grid;
private ArrayList<Cell> arr;
private int numRows;
private int numCols;
public Grid(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
this.grid = new Cell[numRows][numCols];
this.arr = new ArrayList<Cell>();
this.instantiateCells();
}
private void instantiateCells() {
for (int r = 0; r < this.numRows; r++) {
for (int c = 0; c < this.numCols; c++) {
this.grid[r][c] = new Cell(r, c);
this.arr.add(this.grid[r][c]);
}
}
for (int r = 0; r < this.numRows; r++) {
for (int c = 0; c < this.numCols; c++) {
this.grid[r][c].addNeighbors(this);
}
}
}
public ArrayList<Cell> asArray() {
return (new ArrayList<Cell>(this.arr));
}
public Cell getCellAt(int row, int col) {
return grid[row][col];
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
}