-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
146 lines (134 loc) · 3.89 KB
/
Grid.java
File metadata and controls
146 lines (134 loc) · 3.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/***************************************************************************************
*
* NAME: Alyssa Higuchi
*
* HOMEWORK: 6
*
* CLASS: ICS 211
*
* INSTRUCTOR: Scott Robertson
*
* DATE: February 25, 2016
*
* FILE: Grid.java
*
* DESCRIPTION: This file contains the Grid class for homework 6. This Grid is a 2-D
* array which you will use to store your maze.
*
***************************************************************************************/
public class Grid {
private Cell[][] grid;
private int rows;
private int cols;
/********************************************************************
*
* Method: Grid
*
* Description: Constructs a grid with the given dimensions
*
* @param numRows Contains the number of rows the grid should have
* numCols Contains the number of columns the grid should have
*
* @return None
*
********************************************************************/
public Grid(int numRows, int numCols) {
grid = new Cell[numRows][numCols];
rows = numRows;
cols = numCols;
for(int i = 0; i < numRows; i++) {
for(int j = 0; j < numCols; j++) {
grid[i][j] = new Cell();
grid[i][j].setData("-");
}
}
for(int i = 0; i < numRows; i++) {
for(int j = 0; j < numCols; j++) {
if(i == 0) {
if(i + 1 < numRows) {
grid[i][j].setSouth(grid[i+1][j]);
}
}
else if(i == numRows - 1) {
if(i - 1 >= 0) {
grid[i][j].setNorth(grid[i-1][j]);
}
}
else {
grid[i][j].setNorth(grid[i-1][j]);
grid[i][j].setSouth(grid[i+1][j]);
}
if(j == 0) {
if(j + 1 < numCols) {
grid[i][j].setEast(grid[i][j+1]);
}
}
else if(j == numCols - 1) {
if(j - 1 >= 0) {
grid[i][j].setWest(grid[i][j-1]);
}
}
else {
grid[i][j].setEast(grid[i][j+1]);
grid[i][j].setWest(grid[i][j-1]);
}
}
}
}
/********************************************************************
*
* Method: setData
*
* Description: Sets the data for a given cell, if it exists
*
* @param row The row that the cell is in
* col The column that the cell is in
* data The data that will go in the given cell
*
* @return None
*
********************************************************************/
public void setData(int row, int col, String data) {
if(row >= 0 && row < rows && col >= 0 && col < cols) {
grid[row][col].setData(data);
}
}
/********************************************************************
*
* Method: getData
*
* Description: Gets the data of a given cell
*
* @param row The row that the cell is in
* col The column that the cell is in
*
* @return str The data that was in the given cell
*
********************************************************************/
public String getData(int row, int col) {
String str = null;
if(row >= 0 && row < rows && col >= 0 && col < cols) {
str = grid[row][col].getData();
}
return str;
}
/********************************************************************
*
* Method: getCell
*
* Description: Gets a given cell in the grid
*
* @param row The row that the cell is in
* col The column that the cell is in
*
* @return cell The cell in the given row and column
*
********************************************************************/
public Cell getCell(int row, int col) {
Cell cell = null;
if(row >= 0 && row < rows && col >= 0 && col < cols) {
cell = grid[row][col];
}
return cell;
}
}