-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAmazing.cs
320 lines (276 loc) · 8.95 KB
/
Amazing.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
namespace Amazing
{
class AmazingGame
{
private const int FIRST_COL = 0;
private const int FIRST_ROW = 0;
private const int EXIT_UNSET = 0;
private const int EXIT_DOWN = 1;
private const int EXIT_RIGHT = 2;
private static int GetDelimitedValue(String text, int pos)
{
String[] tokens = text.Split(",");
int val;
if (Int32.TryParse(tokens[pos], out val))
{
return val;
}
return 0;
}
private static String Tab(int spaces)
{
return new String(' ', spaces);
}
public static int Random(int min, int max)
{
Random random = new Random();
return random.Next(max - min) + min;
}
public void Play()
{
Console.WriteLine(Tab(28) + "AMAZING PROGRAM");
Console.WriteLine(Tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
Console.WriteLine();
int width = 0;
int length = 0;
do
{
String range = DisplayTextAndGetInput("WHAT ARE YOUR WIDTH AND LENGTH");
if (range.IndexOf(",") > 0)
{
width = GetDelimitedValue(range, 0);
length = GetDelimitedValue(range, 1);
}
}
while (width < 1 || length < 1);
Grid grid = new Grid(length, width);
int enterCol = grid.SetupEntrance();
int totalWalls = width * length + 1;
int count = 2;
Cell cell = grid.StartingCell();
while (count != totalWalls)
{
List<Direction> possibleDirs = GetPossibleDirs(grid, cell);
if (possibleDirs.Count != 0)
{
cell = SetCellExit(grid, cell, possibleDirs);
cell.Count = count++;
}
else
{
cell = grid.GetFirstUnset(cell);
}
}
grid.SetupExit();
WriteMaze(width, grid, enterCol);
}
private Cell SetCellExit(Grid grid, Cell cell, List<Direction> possibleDirs)
{
Direction direction = possibleDirs[Random(0, possibleDirs.Count)];
if (direction == Direction.GO_LEFT)
{
cell = grid.GetPrevCol(cell);
cell.ExitType = EXIT_RIGHT;
}
else if (direction == Direction.GO_UP)
{
cell = grid.GetPrevRow(cell);
cell.ExitType = EXIT_DOWN;
}
else if (direction == Direction.GO_RIGHT)
{
cell.ExitType = cell.ExitType + EXIT_RIGHT;
cell = grid.GetNextCol(cell);
}
else if (direction == Direction.GO_DOWN)
{
cell.ExitType = cell.ExitType + EXIT_DOWN;
cell = grid.GetNextRow(cell);
}
return cell;
}
private void WriteMaze(int width, Grid grid, int enterCol)
{
// top line
for (int i = 0; i < width; i++)
{
if (i == enterCol) Console.Write(". ");
else Console.Write(".--");
}
Console.WriteLine(".");
for (int i = 0; i < grid.Length; i++)
{
Console.Write("I");
for (int j = 0; j < grid.Width; j++)
{
if (grid.Cells[i,j].ExitType == EXIT_UNSET || grid.Cells[i, j].ExitType == EXIT_DOWN)
Console.Write(" I");
else Console.Write(" ");
}
Console.WriteLine();
for (int j = 0; j < grid.Width; j++)
{
if (grid.Cells[i,j].ExitType == EXIT_UNSET || grid.Cells[i, j].ExitType == EXIT_RIGHT)
Console.Write(":--");
else Console.Write(": ");
}
Console.WriteLine(".");
}
}
private List<Direction> GetPossibleDirs(Grid grid, Cell cell)
{
var possibleDirs = new List<Direction>();
foreach (var val in Enum.GetValues(typeof(Direction)))
{
possibleDirs.Add((Direction)val);
}
if (cell.Col == FIRST_COL || grid.IsPrevColSet(cell))
{
possibleDirs.Remove(Direction.GO_LEFT);
}
if (cell.Row == FIRST_ROW || grid.IsPrevRowSet(cell))
{
possibleDirs.Remove(Direction.GO_UP);
}
if (cell.Col == grid.LastCol || grid.IsNextColSet(cell))
{
possibleDirs.Remove(Direction.GO_RIGHT);
}
if (cell.Row == grid.LastRow || grid.IsNextRowSet(cell))
{
possibleDirs.Remove(Direction.GO_DOWN);
}
return possibleDirs;
}
private String DisplayTextAndGetInput(String text)
{
Console.WriteLine(text);
return Console.ReadLine();
}
private enum Direction
{
GO_LEFT,
GO_UP,
GO_RIGHT,
GO_DOWN,
}
public class Cell
{
public int ExitType { get; set; }
public int Count { get; set; }
public int Col { get; set; }
public int Row { get; set; }
public Cell(int row, int col)
{
ExitType = EXIT_UNSET;
Row = row;
Col = col;
}
}
public class Grid
{
public Cell[,] Cells { get; private set; }
public int LastCol { get; set; }
public int LastRow { get; set; }
public int Width { get; private set; }
public int Length { get; private set; }
private int enterCol;
public Grid(int length, int width)
{
LastCol = width - 1;
LastRow = length - 1;
Width = width;
Length = length;
Cells = new Cell[length,width];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
this.Cells[i,j] = new Cell(i, j);
}
}
}
public int SetupEntrance()
{
this.enterCol = Random(0, Width);
Cells[0, enterCol].Count = 1;
return this.enterCol;
}
public void SetupExit()
{
int exit = Random(0, Width - 1);
Cells[LastRow, exit].ExitType += 1;
}
public Cell StartingCell()
{
return Cells[0, enterCol];
}
public bool IsPrevColSet(Cell cell)
{
return 0 != Cells[cell.Row, cell.Col - 1].Count;
}
public bool IsPrevRowSet(Cell cell)
{
return 0 != Cells[cell.Row - 1, cell.Col].Count;
}
public bool IsNextColSet(Cell cell)
{
return 0 != Cells[cell.Row, cell.Col + 1].Count;
}
public bool IsNextRowSet(Cell cell)
{
return 0 != Cells[cell.Row + 1, cell.Col].Count;
}
public Cell GetPrevCol(Cell cell)
{
return Cells[cell.Row, cell.Col - 1];
}
public Cell GetPrevRow(Cell cell)
{
return Cells[cell.Row - 1, cell.Col];
}
public Cell GetNextCol(Cell cell)
{
return Cells[cell.Row, cell.Col + 1];
}
public Cell GetNextRow(Cell cell)
{
return Cells[cell.Row + 1, cell.Col];
}
public Cell GetFirstUnset(Cell cell)
{
int col = cell.Col;
int row = cell.Row;
Cell newCell;
do
{
if (col != this.LastCol)
{
col++;
}
else if (row != this.LastRow)
{
row++;
col = 0;
}
else
{
row = 0;
col = 0;
}
}
while ((newCell = Cells[row, col]).Count == 0);
return newCell;
}
}
}
class Program
{
static void Main(string[] args)
{
new AmazingGame().Play();
}
}
}