-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabyrinth.cs
190 lines (150 loc) · 5.35 KB
/
Labyrinth.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Labyrinth
{
class Labyrinth
{
public const int LABYRINTH_SIZE = 7;
private readonly int LabyrintStartRow = LABYRINTH_SIZE / 2;
private readonly int LabyrinthStartCol = LABYRINTH_SIZE / 2;
private Cell[,] labyrinth;
public Cell currentCell;
public Labyrinth(Random rand)
{
GenerateLabyrinth(rand);
currentCell = labyrinth[LabyrintStartRow, LabyrintStartRow];
}
public Cell GetCell(int row, int col)
{
return labyrinth[row, col];
}
public bool TryMove(Cell cell, Direction direction)
{
int newRow;
int newCol;
FindNewCellCoordinates(cell.Row, cell.Col, direction,
out newRow, out newCol);
if (newRow < 0 || newCol < 0 ||
newRow >= labyrinth.GetLength(0) || newCol >= labyrinth.GetLength(1))
{
return false;
}
if (!labyrinth[newRow, newCol].IsEmpty())
{
return false;
}
this.labyrinth[newRow, newCol].ValueChar = '*';
this.labyrinth[cell.Row, cell.Col].ValueChar = '-';
this.currentCell = labyrinth[newRow, newCol];
return true;
}
private void FindNewCellCoordinates(int row, int col, Direction direction,
out int newRow, out int newCol)
{
newRow = row;
newCol = col;
if (direction == Direction.Up)
{
newRow = newRow - 1;
}
else if (direction == Direction.Down)
{
newRow = newRow + 1;
}
else if (direction == Direction.Left)
{
newCol = newCol - 1;
}
else if (direction == Direction.Right)
{
newCol = newCol + 1;
}
}
private void premestvane(Cell cell, Direction direction,
Queue<Cell> cellsOrder, HashSet<Cell> visitedCells)
{
int newRow;
int newCol;
FindNewCellCoordinates(cell.Row, cell.Col, direction,
out newRow, out newCol);
if (newRow < 0 || newCol < 0 ||
newRow >= labyrinth.GetLength(0) || newCol >= labyrinth.GetLength(1))
{
return;
}
if (visitedCells.Contains(labyrinth[newRow,newCol]))
{
return;
}
if (labyrinth[newRow, newCol].IsEmpty())
{
cellsOrder.Enqueue(labyrinth[newRow, newCol]);
}
}
private bool ExitFound(Cell cell)
{
bool exitFound = false;
if (cell.Row == LABYRINTH_SIZE - 1 ||
cell.Col == LABYRINTH_SIZE - 1 ||
cell.Row == 0 ||
cell.Col == 0)
{
exitFound = true;
}
return exitFound;
}
private bool ExitPathExists()
{
Queue<Cell> cellsOrder = new Queue<Cell>();
Cell startCell = labyrinth[LabyrintStartRow, LabyrinthStartCol];
cellsOrder.Enqueue(startCell);
HashSet<Cell> visitedCells = new HashSet<Cell>();
bool pathExists = false;
while (cellsOrder.Count > 0)
{
Cell currentCell = cellsOrder.Dequeue();
visitedCells.Add(currentCell);
if (ExitFound(currentCell))
{
pathExists = true;
break;
}
premestvane(currentCell, Direction.Down, cellsOrder,visitedCells);
premestvane(currentCell, Direction.Up, cellsOrder, visitedCells);
premestvane(currentCell, Direction.Left, cellsOrder, visitedCells);
premestvane(currentCell, Direction.Right, cellsOrder, visitedCells);
}
return pathExists;
}
private void GenerateLabyrinth(Random rand)
{
this.labyrinth = new Cell[LABYRINTH_SIZE, LABYRINTH_SIZE];
for (int row = 0; row < LABYRINTH_SIZE; row++)
{
for (int col = 0; col < LABYRINTH_SIZE; col++)
{
int cellRandomValue = rand.Next(0, 2);
char charValue;
if (cellRandomValue == 0)
{
charValue = Cell.CELL_EMPTY_VALUE;
}
else
{
charValue = Cell.CELL_WALL_VALUE;
}
this.labyrinth[row,col] = new Cell(row, col, charValue);
}
}
this.labyrinth[LabyrintStartRow, LabyrinthStartCol ].ValueChar = '*';
bool exitPathExists = ExitPathExists();
if (!exitPathExists)
{
GenerateLabyrinth(rand);
}
}
}
public enum Direction {Up, Down, Left, Right};
}