-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserGrid.java
More file actions
192 lines (174 loc) · 4.66 KB
/
UserGrid.java
File metadata and controls
192 lines (174 loc) · 4.66 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author AM
*
*/
public class UserGrid {
private int GRID_ROW_SIZE = 80; // setting a xed size
private int GRID_COL_SIZE = 80; //
private char[][] grid;
private int numGoals;
private int numBoxes;
private Vector2[][] userDefinedGrid;
private FileInput f = new FileInput();
public UserGrid() {
grid = new char[GRID_ROW_SIZE][GRID_COL_SIZE];
}
public void readInputGrid() {
System.out.println("Starting to read Input");
Scanner sc = null;
try {
Stage stage = null;
f.start(stage);
sc = new Scanner(new FileReader(f.getFile()));
sc.useDelimiter("\\n");
int i = 0;
String str = "";
while (sc.hasNext()) {
str = sc.next();
if (i == 0) {
GRID_COL_SIZE = str.toCharArray().length;
// System.out.println("length of COL: " + GRID_COL_SIZE );
}
grid[i] = str.toCharArray();
i++;
}
GRID_ROW_SIZE = i;
// System.out.println("length of ROW: " + GRID_ROW_SIZE );
} catch (FileNotFoundException e) {
System.out.println("File Not Found: " + e.getMessage());
} finally {
if (sc != null) {
sc.close();
}
}
}
public void createUserDefinedGrid() {
numGoals = 0;
numBoxes = 0;
readInputGrid();
userDefinedGrid = new Vector2[GRID_ROW_SIZE][GRID_COL_SIZE];
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
Vector2 mazeblock = new Vector2(r, c);
userDefinedGrid[r][c] = mazeblock;
switch (grid[r][c]) {
case '#':
userDefinedGrid[r][c].setBlockType(BlockType.wall);
break;
case ' ':
userDefinedGrid[r][c].setBlockType(BlockType.floor);
break;
case 'G':
userDefinedGrid[r][c].setBlockType(BlockType.goal);
numGoals++;
break;
case 'B':
userDefinedGrid[r][c].setBlockType(BlockType.box);
numBoxes++;
break;
case 'P':
userDefinedGrid[r][c].setBlockType(BlockType.player);
break;
}
}
}
}
public void printGrid() {
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (userDefinedGrid[r][c].getBlockType().equals(BlockType.wall)) {
System.out.print("#");
} else if (userDefinedGrid[r][c].getBlockType().equals(BlockType.floor)) {
System.out.print(" ");
} else if (userDefinedGrid[r][c].getBlockType().equals(BlockType.goal)) {
System.out.print("G");
} else if (userDefinedGrid[r][c].getBlockType().equals(BlockType.box)) {
System.out.print("B");
} else if (userDefinedGrid[r][c].getBlockType().equals(BlockType.player)) {
System.out.print("P");
}
}
System.out.println();
}
}
public int getGridRawSize() {
return GRID_ROW_SIZE;
}
public int getGridColSize() {
return GRID_COL_SIZE;
}
public Vector2[][] getUserDefinedGrid() {
return userDefinedGrid;
}
public Vector2 getPlayerLocation() {
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (userDefinedGrid[r][c].getBlockType().equals(BlockType.player))
return userDefinedGrid[r][c];
}
}
return null;
}
public Set<Vector2> getFloorSet() {
Set<Vector2> floorVector = new HashSet<Vector2>();
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (!userDefinedGrid[r][c].getBlockType().equals(BlockType.wall))
floorVector.add(userDefinedGrid[r][c]);
}
}
return floorVector;
}
public Set<Vector2> getEdgesSet() {
Set<Vector2> wallVector = new HashSet<Vector2>();
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (userDefinedGrid[r][c].getBlockType().equals(BlockType.wall))
wallVector.add(userDefinedGrid[r][c]);
}
}
return wallVector;
}
public Vector2[] getGoals() {
Vector2[] goals = new Vector2[numGoals];
int i = 0;
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (userDefinedGrid[r][c].getBlockType().equals(BlockType.goal)) {
goals[i] = userDefinedGrid[r][c];
i++;
}
}
}
return goals;
}
public Vector2[] getBoxLoctions() {
Vector2[] boxes = new Vector2[numBoxes]; // numGoals must be equal to
// num ofBoxes
int i = 0;
for (int r = 0; r < GRID_ROW_SIZE; r++) {
for (int c = 0; c < GRID_COL_SIZE; c++) {
if (userDefinedGrid[r][c].getBlockType().equals(BlockType.box)) {
boxes[i] = userDefinedGrid[r][c];
i++;
}
}
}
return boxes;
}
public int getNumGoals() {
return numGoals;
}
public int getNumBoxes() {
return numBoxes;
}
}