Having implemented Dijkstra’s algorithm, the next bit of fun was to use it to find paths through the maze. Although I had looked at his code in the book, I decided to have a go at writing my own method from scratch, rather than converting his code into C#. This is a better way to understand the algorithms, and saved me having to figure out some of the less obvious language Ruby features. The end result was actually very similar to his code, although I made use of Linq (yet again) to avoid a loop, making the code a little clearer… I called my method PathFrom, as you specify where you want to go from, and it gives you a path from there to the root of the Distances object. He called his path_to, presumably because it gives you a path to the specified cell from the root. I guess…
The main part of Dijkstra’s algorithm is implemented in the Distances class. For the most part, my code didn’t look that much different from his. The main change was that I was able to use Linq to remove an if when traversing the frontier… I broke lines 6-8 into three to make it easier to read in this blog, but in the code, it’s just one line. Linq enabled me to avoid having to check if the next cell had already been linked. Adding the numbers to the text output was very simple, as I already added the facility to display cell contents (see part 2 of this series). The challenge was putting these numbers on the UI. This turned out to be much easier than I expected, and even worked first time, which surprised me! I added a method to the window’s code behind, passing in the distances collection…
Part 4 in my ongoing series of how I implemented the code from Mazes For Programmers by Jamis Buck in C#.
In this post, I explain how I implemented the first two maze-generation algorithms in C#, and show how parts of the code were improved by using Linq.
Part 3 in my ongoing series of how I implemented the code from Mazes For Programmers by Jamis Buck in C#.
The author rendered the mazes in graphical form using a Ruby library that allows you to create PNG images. This requires you to run the code, then open the saved image, then remember to close it before running the code again, otherwise you get a “File access denied” error, all of which seemed like hard work. As I work with WPF on a day-to-day basis, I decided to use that to render my mazes, as it was quicker and easier.
This post shows how I did it.
Part 2 in my ongoing series of how I implemented the code from Mazes For Programmers by Jamis Buck in C#.
In this post, I show a couple of the places where my basic code deviated from the author’s.
Part 1 in my ongoing series of how I implemented the code from Mazes For Programmers by Jamis Buck in C#.
In this post, I waffle on about the book, why I decided to implement the code in C# rather than Ruby, and generally spend a fair bit of time not saying much. Pretty regular blog post for me!