Skip to content

Commit 37eb24d

Browse files
committed
minor fix
1 parent c9db9cd commit 37eb24d

12 files changed

+20
-13
lines changed

.vs/PathAlgorithms/v16/.suo

0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

BFS.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ void initialize()
6868

6969
public void prepare_shortest_Path(Tuple<int, int> current)
7070
{
71-
//MessageBox.Show(current.Item1.ToString() + " " + current.Item2.ToString());
71+
/* Recursively generates the shortest path (which was prepared while BFS) */
7272
shortestPath.Add(current);
7373

74-
if (current.Item1 == 0 && current.Item2 == 0)
74+
if (current.Item1 == startX && current.Item2 == startY)
7575
return;
7676
else prepare_shortest_Path(Path[current]); ;
7777
}
7878

7979
public List<Tuple<int, int>> get_shortest_Path()
8080
{
8181
shortestPath.Reverse();
82-
MessageBox.Show(shortestPath.Count.ToString());
82+
/* [DEBUG] MessageBox.Show(shortestPath.Count.ToString()); */
8383
return shortestPath;
8484
}
8585

@@ -88,7 +88,7 @@ public Tuple<List<Tuple<int,int>>,List<Tuple<int,int>>> get_BFS_path()
8888

8989
var queue = new Queue<Tuple<int, int, int>>(); // generic queue declaration
9090
queue.Enqueue(Tuple.Create(startX, startY, 0)); // insert the starting point
91-
91+
bool found = false;
9292
while (queue.Count != 0) // while still elements in queue
9393
{
9494
var curr = queue.Dequeue(); // get the next element and pop it out the queue
@@ -98,6 +98,7 @@ public Tuple<List<Tuple<int,int>>,List<Tuple<int,int>>> get_BFS_path()
9898
if (curr.Item1 == endX && curr.Item2 == endY) // if the target found break
9999
{
100100
MessageBox.Show(curr.Item3.ToString()+" steps");
101+
found = true;
101102
break;
102103
}
103104

@@ -123,7 +124,7 @@ public Tuple<List<Tuple<int,int>>,List<Tuple<int,int>>> get_BFS_path()
123124
}
124125
}
125126
}
126-
prepare_shortest_Path(Tuple.Create(endX, endY));
127+
if(found==true) prepare_shortest_Path(Tuple.Create(endX, endY));
127128
return Tuple.Create(toReturn,get_shortest_Path());
128129
}
129130
#endregion

Form1.Designer.cs

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Form1.cs

+10-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Form1()
1818
prepareBoard();
1919
}
2020

21-
int startX, startY, endX, endY;
21+
int startX=2, startY=2, endX=16, endY=16;
2222

2323
void prompt_user()
2424
{
@@ -64,7 +64,6 @@ public void wait(int milliseconds)
6464
{
6565
timer1.Enabled = false;
6666
timer1.Stop();
67-
//Console.WriteLine("stop wait timer");
6867
};
6968
while (timer1.Enabled)
7069
{
@@ -120,7 +119,7 @@ void ColorTheBoard(List<Tuple<int, int>> path, bool clr)
120119
board[xx, yy].Value = iterator; //If shortest path then mark current steps
121120
}
122121

123-
if (xx == 6 && yy == 8)
122+
if (xx == endX && yy == endY)
124123
{
125124
board[xx, yy].Style.BackColor = toColor;
126125
board[xx, yy].Value = "ok!";
@@ -160,17 +159,20 @@ private void RunBFS_Click(object sender, EventArgs e)
160159
xWalls = walls.Item1;
161160
yWalls = walls.Item2;
162161
// BFS(startX,startY,ebdX,endY, row,col, (position of walls ))
163-
BFS bfs = new BFS(0, 0, 6, 8, 20, 20, xWalls, yWalls); // create BFS object
162+
BFS bfs = new BFS(startX, startY, endX, endY, 19, 20, xWalls, yWalls); // create BFS object
164163
Tuple<List<Tuple<int, int>>, List<Tuple<int, int>>> paths = bfs.get_BFS_path(); // run BFS and get the paths (traversal and shortest)
165164
List<Tuple<int, int>> path = paths.Item1;
166165
List<Tuple<int, int>> shortestPath = paths.Item2;
167166
//MessageBox.Show(path.Count.ToString());
168167

169168
ColorTheBoard(path, false); // colors the board
170169

171-
MessageBox.Show("Now showing the shortest path...");
172-
173-
ColorTheBoard(shortestPath, true);
170+
if (shortestPath.Count() == 0) { MessageBox.Show("No path for the selected cells..."); }
171+
else
172+
{
173+
MessageBox.Show("Now showing the shortest path...");
174+
ColorTheBoard(shortestPath, true);
175+
}
174176

175177
}
176178

@@ -185,7 +187,7 @@ private void RunDFS_Click(object sender, EventArgs e)
185187
xWalls = walls.Item1;
186188
yWalls = walls.Item2;
187189
// (sX,Sy,eX,eY, row,col)
188-
DFS dfs = new DFS(0, 0, 6, 8, 20, 20, xWalls, yWalls); // create BFS object
190+
DFS dfs = new DFS(startX, startY, endX, endY, 19, 20, xWalls, yWalls); // create DFS object
189191

190192
List<Tuple<int, int>> path = dfs.get_DFS_path(); // run DFS get the cells traversed in BFS order
191193
//MessageBox.Show(path.Count.ToString());

bin/Debug/PathAlgorithms.exe

512 Bytes
Binary file not shown.

bin/Debug/PathAlgorithms.pdb

2 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

obj/Debug/PathAlgorithms.exe

512 Bytes
Binary file not shown.

obj/Debug/PathAlgorithms.pdb

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)