<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-11-01T02:52:18+00:00</updated><id>/feed.xml</id><title type="html">Jackson Bierfeldt Developer Blog</title><subtitle>Developer blog for Jackson Bierfeldt. Posts about current projects, ideas, and unanswered questions.</subtitle><entry><title type="html">A Thank You Note to My Past Self</title><link href="/2018/07/05/a-thank-you-note-to-myself-for-writing-readable-code.html" rel="alternate" type="text/html" title="A Thank You Note to My Past Self" /><published>2018-07-05T14:16:00+00:00</published><updated>2018-07-05T14:16:00+00:00</updated><id>/2018/07/05/a-thank-you-note-to-myself-for-writing-readable-code</id><content type="html" xml:base="/2018/07/05/a-thank-you-note-to-myself-for-writing-readable-code.html"><![CDATA[<p>If you’ve been programming or writing software for more than 45 minutes, you’ve definitely encountered this <a href="https://www.itworld.com/article/2898840/developers-say-the-best-code-is-readable-code.html">adjuration</a>: <em>Writing good code means writing readable code.</em></p>

<p>Following this maxim is sometimes difficult for beginners, especially when you are working on a project whose code you never expect to be displayed on someone else’s monitor. It’s easier to encourage yourself to keep up good coding hygiene when you are collaborating with a team or know that someone else is going to pick the project up after you, but maintaining a clean, readable codebase for personal projects is a little more difficult.</p>

<p>But today I ran into a reminder of how important this practice can be, even if it’s sometimes tempting to speed past carefully naming every function and to simply call them all <code class="language-plaintext highlighter-rouge">dinguscallback2</code> or <code class="language-plaintext highlighter-rouge">getfunc3</code>.</p>

<p>When I checked my email this morning, I found an email from a total stranger asking for help implementing a <a href="https://github.com/jbierfeldt/bf-quiz">little buzzfeed-style quiz generator</a> that I had written four years ago and hadn’t given a single thought to since. The main text of the email asked:</p>

<p><em>The userAnswers array captures the full collection of user clicks, including them changing their mind. Thus even if there’s only five questions in the quiz, userAnswers might have seven elements.</em></p>

<p><em>Is there an easy way to generate a vector of their final answers?</em></p>

<p>I read this an panicked a little. On the one hand, I was excited that someone had not only found one of my repos, but had decided to implement it in one of their own projects, and I naturally wanted to help them. On the other, I hadn’t looked or thought about this repo in over four years and honestly had no idea what anything in their email was referring to or how to even begin going about helping them solve their issue.</p>

<p>I nervously opened the codebase, fully expecting to waste at least an hour wading through some spaghetti code and trying to remember what exactly this program did and how exactly it did it. What I found though was a nice surprise. I found the code to be somewhat neatly organized in a version of the <a href="https://yuiblog.com/blog/2007/06/12/module-pattern/">module pattern</a> that I had apparently just learned at the time I wrote it, complete with instructive comments and usefully named functions and variables (<code class="language-plaintext highlighter-rouge">writeQuiz</code>, <code class="language-plaintext highlighter-rouge">calcResult</code>, and <code class="language-plaintext highlighter-rouge">checkInputs</code> contain much more information than <code class="language-plaintext highlighter-rouge">myfunc</code>).</p>

<p>Granted, there were a few things that made me cringe and that I would do totally differently if I were to completely rewrite the software today (such as the way I  manually manipulated class names in the DOM), but for the most part, I found myself able to quickly understand exactly what every part of the codebase was, what it did, and where the person who emailed me’s problem lay. I caught the bug right away, fixed it, and uploaded the updated version of the code to Github, all within five minutes or so.</p>

<p>Honestly, if I had opened this code and found it illegible and confusing, I probably wouldn’t have given it the time required to fix it and would have ended up just deleting the repo and apologizing to the person who emailed me.</p>

<p>This is a thank you note to my past self. Thank you for taking the time to write clear, readable code, even if a lot of it isn’t as efficient or succinct as it could be. Someone else was able to read it well enough to implement it in one of their own projects, and I was able to read it well enough to quickly spot and fix a bug.</p>

<p>Even if you don’t think your code is destined to appear before someone else’s eyes, remember, it could appear before your own in the future, maybe years in the future, and that is more than enough time to let all the details of the project that you’ve stored in your own mental RAM to fall away, causing you to re-enter almost as clueless as someone who has never seen the code before. Do yourself and your futureselves a favor and remember to always write clean, logical, and readable code; it’ll make you a <a href="https://medium.com/@CodementorIO/good-developers-vs-bad-developers-fe9d2d6b582b">better developer</a> anyway!</p>]]></content><author><name></name></author><summary type="html"><![CDATA[If you’ve been programming or writing software for more than 45 minutes, you’ve definitely encountered this adjuration: Writing good code means writing readable code.]]></summary></entry><entry><title type="html">Comparing Two Versions of “Gott und die drei Geburtsstände - Klerus, Adel und Bauern”</title><link href="/2018/06/09/comparinggottunddiedrei.html" rel="alternate" type="text/html" title="Comparing Two Versions of “Gott und die drei Geburtsstände - Klerus, Adel und Bauern”" /><published>2018-06-09T16:42:55+00:00</published><updated>2018-06-09T16:42:55+00:00</updated><id>/2018/06/09/comparinggottunddiedrei</id><content type="html" xml:base="/2018/06/09/comparinggottunddiedrei.html"><![CDATA[<p>A few days ago, I decided on a whim to visit the Austrian <a href="https://www.volkskundemuseum.at/jart/prj3/volkskundemuseum/main.jart?content-id=1377520150937&amp;rel=de">Volkskunde Museum</a> in Vienna. (The English translation of their website calls the museum: “The Austrian Museum of Folk Life and Folk Art”). The museum hosts a magnificent collection of furniture, tools, paintings, maps, models, sculptures and every other conceivable form of art, all created by Austrian peasants, farmers, and craftspeople, mostly from the 17th, 18th, and 19th centuries. The collection is designed to give a glimpse into the lives (both material and psychological) of the pre-industrial societies that created these works of art and design. Go check it out if you can.</p>

<p>One particular work caught my eye, a painting from an unknown artist created around 1800 (according to the museum placard) somewhere in Tyrol, a region in the west of Austria. Since I couldn’t find any placards or threatening iconography expressly forbidding me from taking a picture, I did:</p>

<p><img src="/assets/comp-gott-die-drei/images/museum.jpg" alt="Museum" /></p>

<div style="display: flex; flex-wrap: wrap; border-top: 2px dotted lightgrey; border-bottom: 2px dotted lightgrey; padding-top: 10px">
  <div style="width: 50%">
    <code language="">
      Klerus:<br />
      Ich mit meiner Lehr',<br />
      Viel Leut zu Gott bekehr'.<br /><br />

      Adel:<br />
      Ich mit meiner Macht,<br />
      Viel Land und Leut an mich gebracht.<br /><br />

      Bauer:<br />
      Ha Ha!<br />
      wan Gott und ich nichts thät',<br />
      Ihr beide nichts zu essen hätt'.
    </code>
  </div>
  <div style="width: 50%">
  <code language="">
    Clergy:<br />
    I, with my religious theory,<br />
    Have brought to God the world’s weary.<br /><br />

    Nobility:<br />
    I have earned, with might and power,<br />
    The farmer's field and verdant bower.<br /><br />

    Peasantry:<br />
    Ha Ha!<br />
    If God and I no fields did sow,<br />
    No food that's 'pon your table'd grow.<br /><br />
  </code>
  </div>
  <div style="width: 100%; padding: 10px; font-style: italic">
  Special thanks to <a href="https://concerningwords.wordpress.com/">Zeke Honegger</a> for working on this translation with me. He's writing a blog post about the translation process which I'll include here after it's published.
  </div>
</div>

<p>The painting depicts four figures. At the top, nestled in the clouds: God, recognizable from the white beard, scepter and globe, not to mention his angelically billowing robe-fabric and heavenly background lighting. From left to right: a member of the clergy, recognizable from the cross-adorned scepter and other ornate vestments; a member of the nobility, carrying a sword, wearing a cape, and adorned with a crown; and finally, a peasant (although a seemingly well-to-do one), wearing a brown overcoat and holding what appears to me to be a <a href="http://www.estonica.org/en/19th_century_farm_tools/?max">18–19th century version of a  scythe</a>.</p>

<p>The painting turns out to be a <a href="https://en.wikipedia.org/wiki/Estates_of_the_realm">genre painting</a> depicting God and the “three estates of the realm,” the three broad social groupings into which one was born under central European feudalism. This social determinism is even more poignant in the German word for the concept: “Geburtsstände” (lit. “birth-classes”).</p>

<p>While doing some cursory research online, I came across <a href="http://www.auktion-innsbruck.at/product/2149/23">another version</a> of the same painting, remarkably similar, but from almost a century later, 1880, and attributed to an artist in Innsbruck named Balthasar Waltl. This version of the painting has a title listed along with it: “Gott und die drei Geburtsstände - Klerus, Adel und Bauern” (“God and the three estates — Clergy, Nobility, and Peasantry”).</p>

<p><img src="/assets/comp-gott-die-drei/images/auction.jpg" alt="Auction" /></p>

<p>All the essential symbolic features of the image are the same, just painted in a slightly different style, with the exception of the addition of a book in the hand of the clergyman. Aside from this, the most notable differences I can discern are in some of the colors in the clothing of the clergyman and the peasant, which I would love to be able to explain, but unfortunately can’t. If someone reading this knows anything about why these colors might have changed from version to version, please drop me a line.</p>

<p>Here are both images side by side, for comparison:</p>

<p><img src="/assets/comp-gott-die-drei/images/combined.jpg" alt="Combined" /></p>

<div style="display: flex; flex-wrap: wrap; border-top: 2px dotted lightgrey; border-bottom: 2px dotted lightgrey; padding-top: 10px; padding-bottom: 10px;">
  <div style="width: 50%">
    <code language="">
      Klerus:<br />
      Ich mit meiner Lehr',<br />
      Viel Leut zu Gott bekehr'.<br /><br />

      Adel:<br />
      Ich mit meiner Macht,<br />
      Viel Land und Leut an mich gebracht.<br /><br />

      Bauer:<br />
      Ha Ha!<br />
      wan Gott und ich nichts thät',<br />
      Ihr beide nichts zu essen hätt'.
    </code>
  </div>
  <div style="width: 50%">
  <code language="">
    Klerus:<br />
    Ich mit meiner Lehr',<br />
    Viel Leut' zu Gott bekehr<br /><br />

    Adel:<br />
    Ich mit meiner Macht,<br />
    Viel Land u. Leut' an mich gebracht.<br /><br />

    Bauer:<br />
    Ha! Ha!<br />
    Wann Gott und ich nichts tät',<br />
    Ihr Beide nichts zu essen hätt.
  </code>
  </div>
</div>

<p>Now, to the text. While the content and meaning of the text remains the same in each version of the painting, the newer one (on the right above) features some spelling and punctuation changes, presumably incorporated by the artist to reflect changes to the writing conventions which took place during the 19th century. By comparing the texts from these two versions of the painting, one from the beginning of the 19th century, and one from the end, we can track some of these changes to writing convention, at least as they happened in mountainous, western Austria.</p>

<p>The most noticeable changes are those which concern the spelling: specifically, I’m talking about the dropping of the ‘h’ (<a href="https://de.wikipedia.org/wiki/Dehnungs-h">dehnungs-h</a>) after ‘t’, seen here in <code class="language-plaintext highlighter-rouge">tät'</code> instead of <code class="language-plaintext highlighter-rouge">thät'</code> and the change from <code class="language-plaintext highlighter-rouge">wan</code> to <code class="language-plaintext highlighter-rouge">wann</code> (but notably not yet <code class="language-plaintext highlighter-rouge">wenn</code>, a distinction which <a href="https://www.duden.de/rechtschreibung/wenn">emerged in the 19th century</a> but apparently hadn’t been adopted everywhere.)</p>

<p>The later author also made a series of changes to the punctuation and capitalization which are notable.</p>

<p>While <code class="language-plaintext highlighter-rouge">Leut</code> (shortened form of <code class="language-plaintext highlighter-rouge">Leute</code> (‘people’)) appears unapologetically without an apostrophe in the earlier version of the painting (both in the text of the clergyman and of the noble), the later version of the painting acknowledges this <a href="https://kuscholarworks.ku.edu/bitstream/handle/1808/22855/malc_1980_257-265_Keel.pdf?sequence=1">common shortening</a> by including an apostrophe. Because this <a href="https://en.wikipedia.org/wiki/Apocope">schwa-final apocope</a> was (and is) so common and wide-spread in the Bavarian Upper High German dialect spoken in Austria, it’s possible that the earlier author either wasn’t aware that <code class="language-plaintext highlighter-rouge">Leut</code> was a shortening of <code class="language-plaintext highlighter-rouge">Leute</code> because of its presumable high frequency of use, or just didn’t think it important to notate because of its line-central position (i.e. because it doesn’t matter for the rhyme-scheme, in contrast to <code class="language-plaintext highlighter-rouge">Lehr'</code> and <code class="language-plaintext highlighter-rouge">bekehr'</code>, both of which are notated as shortenings).</p>

<p>Interestingly, the later version of the painting neglects to note the word <code class="language-plaintext highlighter-rouge">bekehr</code> as a shortening of <code class="language-plaintext highlighter-rouge">bekehrt</code> at the end of the clergyman’s text, although this is noted in the earlier painting. Because, however, the sentence-final punctuation is also inexplicably omitted from this version of the painting, I have to conclude that the artists just ran out of space and omitted both markings for this reason. There’s no reason (as far as I can tell) to suspect that in the 80 or so years between the creation of these two paintings a change in convention occurred which would account for this lack of notation.</p>

<p>Something which did occur in the century between the creation of these two paintings, however, was that the double capitalization of <code class="language-plaintext highlighter-rouge">Ihr Beiden</code> (‘you both’) enjoyed a brief increase in popularity, as seen in the following <a href="https://books.google.com/ngrams/graph?content=Ihr+beide%2CIhr+Beide&amp;year_start=1800&amp;year_end=1900&amp;corpus=20&amp;smoothing=3&amp;share=&amp;direct_url=t1%3B%2CIhr%20beide%3B%2Cc0%3B.t1%3B%2CIhr%20Beide%3B%2Cc0">Google Ngram</a> data:</p>

<p><img src="/assets/comp-gott-die-drei/images/ngram.png" alt="ngram" /></p>

<p>Although the lowercase form <code class="language-plaintext highlighter-rouge">Ihr beiden</code> had overtaken the double capitalization form which appears in the later image by the date of its creation (1880), it’s not implausible to assume that the author of this version of the painting had learned this double capitalization during their primary education while younger and continued to use it despite its fallen popularity. (The question of whether to capitalize this word still plagues some German speakers today, as evidenced by many questions on forums such as Yahoo! Answers and <a href="https://www.gutefrage.net/frage/hallo-ihr-beiden-oder-hallo-ihr-beiden--gross-oder-klein-">Gutefrage.net</a>).</p>

<p>As for the double use of exclamation points at the beginning of the peasant’s text in the later version of the image, I have no idea. Maybe it was a stylistic choice akin to the changed colors on the figures’ clothing.</p>

<p>There is certainly much more to be explored in these two versions of this painting and in their accompanying texts. If anybody notices something I didn’t mention, has a better explanation for some of the changes, or knows anything more specific about the author or date of the earlier painting, I would love to hear from you! You can reach me at jbierfeldt[at]gmail.com. Thanks again to Zeke for helping out with the English translation. You can check out more of his work and poetry on his <a href="https://concerningwords.wordpress.com/">blog</a>.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[A few days ago, I decided on a whim to visit the Austrian Volkskunde Museum in Vienna. (The English translation of their website calls the museum: “The Austrian Museum of Folk Life and Folk Art”). The museum hosts a magnificent collection of furniture, tools, paintings, maps, models, sculptures and every other conceivable form of art, all created by Austrian peasants, farmers, and craftspeople, mostly from the 17th, 18th, and 19th centuries. The collection is designed to give a glimpse into the lives (both material and psychological) of the pre-industrial societies that created these works of art and design. Go check it out if you can.]]></summary></entry><entry><title type="html">Why and how I Used Dijkstra’s Algorithm for Score Calculation in a Tile-based Javascript Game</title><link href="/2018/05/01/implementing-dijkstra.html" rel="alternate" type="text/html" title="Why and how I Used Dijkstra’s Algorithm for Score Calculation in a Tile-based Javascript Game" /><published>2018-05-01T14:54:00+00:00</published><updated>2018-05-01T14:54:00+00:00</updated><id>/2018/05/01/implementing-dijkstra</id><content type="html" xml:base="/2018/05/01/implementing-dijkstra.html"><![CDATA[<p>Recently, I’ve been building a multiplayer tile-laying game inspired by games like <a href="https://en.wikipedia.org/wiki/Carcassonne_board_game">Carcassonne</a> and <a href="https://en.wikipedia.org/wiki/Catan">Settlers of Catan</a>. I’ll soon have some other blog posts about the game and some of the technologies and design patterns I’m using to build it, but today I’m going to walk through and illustrate how and why I used <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra’s Algorithm</a> to implement the scoring component of the game. Since there are already <a href="https://stackoverflow.com/questions/2856670/why-does-dijkstras-algorithm-work">so</a> <a href="https://www.youtube.com/watch?v=NHZr6P1csiY">many</a> <a href="https://www.manning.com/books/grokking-algorithms">good</a> <a href="https://mitpress.mit.edu/books/introduction-algorithms">resources</a> explaining what Dijkstra’s algorithm is and how it works, I’m writing this for somebody who already understands the basic concepts involved, and who would like to see a real-life implementation of it in a tile-based Javascript game.</p>

<h2 id="the-game--rules-and-objectives">The Game — Rules and Objectives</h2>

<p>To begin, I’m going to explain the game, so as to motivate why I even needed a pathfinding algorithm in the first place. If all you want to see is my implementation, feel free to skip to the last section.</p>

<h3 id="turns-and-tile-placement">Turns and Tile Placement</h3>

<p>Like Carcassonne, the game begins with a single tile on the board. When it is each player’s turn, they draw a random tile and must place it somewhere on the board. In order to place the tile, all of its edges must match with those surrounding it. Streets have to match up to streets, and grass has to match up with grass. Tiles can be rotated but not swapped out. So what a player will do on their turn is largely dependent on what tile they draw.</p>

<p><img src="/assets/dijkstra/images/placement_both.png" alt="Placement Example" /></p>

<p><img src="/assets/dijkstra/images/tiletypes2.png" alt="Tile Types" /></p>

<h4 id="types-of-tiles">Types of Tiles</h4>

<p>Tiles can either be simple road tiles which are used to connect other tiles, or they can be <em>special tiles</em> which include one of three game pieces on them: a mine, a factory, or a house. The basic logic of the game is as follows: <strong>Mines provide factories with materials, factories turn these materials into goods, which they then ship to houses for consumption.</strong></p>

<p>Players <em>own</em> the tiles that they place, including any special tiles. This is indicated (at least right now in this mock-up stage) by little badges with the player’s color on the bottom left of the placed tile. (See next image for example.) As tiles are laid and the board expands, paths of connected roads are formed and eventually completed. Only after a path is completed is it scored, and players receive points based on the value of the tiles that they own. We’ll talk about the tile valuation process a little later—that’s where the pathfinding algorithm comes in.</p>

<p><img src="/assets/dijkstra/images/completed_path.png" alt="Board Example" /></p>

<h4 id="scoring-the-game">Scoring the Game</h4>

<p>A path is scored whenever it gets <em>completed</em>, meaning that there is no possibility for other tiles to be added to it: it becomes a closed system. Upon completion, this closed system is analyzed to determine the value of each of the tiles within it according to the following rules:</p>

<ul>
  <li>Each tile on a completed path is automatically worth <code class="language-plaintext highlighter-rouge">1</code> point.</li>
  <li>Each mine <em>which is connected to a factory</em> is worth a number of points equal to the length of the shortest possible path between it and the factories that it supplies. If it supplies multiple factories, the distances of these paths are summed. A mine which is not connected to at least one factory is worth the default <code class="language-plaintext highlighter-rouge">1</code> point.</li>
  <li>Each factory <em>which is supplied by a mine <strong>and</strong> which is connected to a house</em> is worth a number of points equal to the length of the shortest possible path between it and the houses that it supplies <strong>multiplied by</strong><code class="language-plaintext highlighter-rouge">2</code>. If it supplies multiple houses, the distances of these paths are summed. If a factory is neither supplied by a mine nor connected to at least one house, it is only worth the default <code class="language-plaintext highlighter-rouge">1</code> point.</li>
</ul>

<p>Basically: normal tiles are worth <code class="language-plaintext highlighter-rouge">1</code> point, mines are worth however long the paths connecting them to factories are, and factories are worth however long the paths connecting them to houses are, times 2, becuase they have to be loaded by mines first, which is harder to do.</p>

<p>Since the majority of the scoring is ultimately based on the length of the paths between the special tiles, it is important that these paths be calculated thoroughly. A player should not receive extra points for their path just because a random-walk algorithm was used and took the long route to get there. This, along with the complexity of some of the rules of calculating the paths that we will see in the next section, underscores my decision to use a shortest path finding algorithm in implementing the scoring logic of the game.</p>

<p>But why Dijkstra and not <a href="https://en.wikipedia.org/wiki/A*_search_algorithm">A*</a> or <a href="https://en.wikipedia.org/wiki/Shortest_path_problem">other</a> <a href="https://stackoverflow.com/questions/1846836/the-best-shortest-path-algorithm">popular</a> <a href="https://gamedev.stackexchange.com/questions/1/what-path-finding-algorithms-are-there">shortest</a> <a href="https://www.geeksforgeeks.org/johnsons-algorithm/">distance</a> <a href="https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm">algorithms</a>? I’ll come back to this decision after filling out some details of what exactly I wanted the algorithm to do in my game. The best way to do that is by walking through a concrete example of how exactly we want a scoring event to play out.</p>

<h3 id="example">Example</h3>

<p>For this example, I am going to take the completed path that I already showed you above and walk through exactly what I want to happen when the values of the tiles are calculated as the game goes through the three rules I outlined above. As we step through the rules, I’ll show illustrations and keep a tally. (For convenience, I’m not going to list the non-special tiles, because their values won’t change after the 1st rule is applied, but keep in mind that they are there.)</p>

<h4 id="step-1">Step 1</h4>

<p>Step 1 is easy. Loop through all of the tiles on the completed path and assign a value of 1. This rewards players who used their tiles to help build and complete the path, even if they aren’t reaping benefits of industry.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// special tiles from top left</span>
<span class="c1">// to bottom left of the image</span>
<span class="nx">values</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">mine1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by red</span>
  <span class="nx">house1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by yellow</span>
  <span class="nx">factory1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by blue</span>
  <span class="nx">factory2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// owned by blue</span>
  <span class="nx">mine2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// owned by yellow</span>
  <span class="nx">mine3</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1">// owned by yellow</span>
  <span class="c1">// all other tiles = 1</span>
<span class="p">}</span>
</code></pre></div></div>

<h4 id="step-2">Step 2</h4>

<p>Now we come to the pathfinding. We need to find the optimal valid paths from each mine to each factory that it can possibly supply. What makes a path valid? Well, we want it to extend from one special tile (e.g. a mine) to its goal special tile (e.g. a factory) <em>without going through any other special tiles</em>. In other words, we only want special tiles to be able to serve as endpoints. This makes placement more strategic. The following image shows what we would like our pathfinding algorithm to do. The orange lines represent the paths being drawn from the mines to the factories.</p>

<p><img src="/assets/dijkstra/images/mines2.png" alt="Score Example" /></p>

<p>What’s happening, in prose: Yellow owns two mines (<code class="language-plaintext highlighter-rouge">mine2</code> and <code class="language-plaintext highlighter-rouge">mine3</code>), and each mine has a free path to each of the two factories owned by blue (<code class="language-plaintext highlighter-rouge">factory1</code> and <code class="language-plaintext highlighter-rouge">factory2</code>). The mine on the right (<code class="language-plaintext highlighter-rouge">mine3</code>) has two valid paths, one of length <code class="language-plaintext highlighter-rouge">3</code> and one of length <code class="language-plaintext highlighter-rouge">6</code>. Keeping in mind that this mine has an initial value of <code class="language-plaintext highlighter-rouge">1</code> from the first scoring rule, this means that the new tile value for <code class="language-plaintext highlighter-rouge">mine3</code> is <code class="language-plaintext highlighter-rouge">1 + 3 + 6 = 10</code>. A pretty nice bonus! Similarly, Yellow’s mine of the left also has two valid paths to factories, one of length <code class="language-plaintext highlighter-rouge">3</code> and one of length <code class="language-plaintext highlighter-rouge">4</code>, bringing this mine tile’s total to <code class="language-plaintext highlighter-rouge">1 + 3 + 4 = 8</code>.</p>

<p>So while it seems that Yellow is reaping a lot of points this turn, we notice that Red also owns a mine in the upper left of the path (<code class="language-plaintext highlighter-rouge">mine1</code>). Unfortunately for Red, however, Yellow’s house (<code class="language-plaintext highlighter-rouge">house1</code>) is blocking it from connecting to a factory, causing Red’s mine to only be worth the default amount of <code class="language-plaintext highlighter-rouge">1</code> point. In order to add an element of strategic placement and defensive blocking, we want special tiles to only be able to serve as end points and for it to be invalid for paths to pass through them. So this path doesn’t get completed. Sorry Red…</p>

<p>So after the calculations from step 2, the tile values look like this:</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// after step 2: mine-&gt;factory</span>
<span class="nx">values</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">mine1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by red</span>
  <span class="nx">house1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by yellow</span>
  <span class="nx">factory1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by blue</span>
  <span class="nx">factory2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// owned by blue</span>
  <span class="nx">mine2</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="p">,</span> <span class="c1">// total 8; owned by yellow</span>
  <span class="nx">mine3</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">6</span> <span class="c1">// total 10; owned by yellow</span>
  <span class="c1">// all other tiles = 1</span>
<span class="p">}</span>
</code></pre></div></div>

<h4 id="step-3">Step 3</h4>

<p>After all of the paths from the mines are calculated and scored, the game then moves on to the factories. Remember, only factories which have been supplied by mines are even eligible for shipping their goods to houses. In our example, however, both factories have been supplied, so both will attempt to draw paths to houses on path. Lucky for Blue, they own both of the factories, and with the 2x multiplier, they should expect a big payout. Let’s see what happens:</p>

<p><img src="/assets/dijkstra/images/factories1.png" alt="Factories Score" /></p>

<p>The upper factory (<code class="language-plaintext highlighter-rouge">factory1</code>) has a nice straight little jaunt to <code class="language-plaintext highlighter-rouge">house1</code> with a path length of <code class="language-plaintext highlighter-rouge">3</code>, making the value of <code class="language-plaintext highlighter-rouge">factory1</code> <code class="language-plaintext highlighter-rouge">1 + (3*2) = 7</code>. Unfortunately, Blue didn’t choose the best layout, because <code class="language-plaintext highlighter-rouge">factory1</code> is blocking the only possible path from <code class="language-plaintext highlighter-rouge">factory2</code> to <code class="language-plaintext highlighter-rouge">house1</code>, and thus misses quite a few points.</p>

<p>So after Step 3:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// after step 3: factory-&gt;house</span>
<span class="nx">values</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">mine1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by red</span>
  <span class="nx">house1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">//owned by yellow</span>
  <span class="nx">factory1</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="mi">2</span><span class="p">),</span> <span class="c1">// total 7; owned by blue</span>
  <span class="nx">factory2</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// owned by blue</span>
  <span class="nx">mine2</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="p">,</span> <span class="c1">// total 8; owned by yellow</span>
  <span class="nx">mine3</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">6</span> <span class="c1">// total 10; owned by yellow</span>
  <span class="c1">// all other tiles = 1</span>
<span class="p">}</span>
</code></pre></div></div>

<p>If it seems that the restriction on movement through other special tiles is frustrating, one only needs to look at one of the strategic possibilities depicted in the next image to understand the positive complexity they add to the game. Players must think ahead about how they want to plan their routes, all while competing with the plans of other players and against the luck of the tile draw.</p>

<p>A clever layout to maximize points. Because the <em>shortest</em> path from the factory to the house passes through the mine, making it invalid, the shortest <em>valid</em> path is longer, leading to more points (17 for the factory!):</p>

<p><img src="/assets/dijkstra/images/routearound.png" alt="Route Around" /></p>

<p>So now that I’ve shown how the game’s scoring mechanic relies on a pathfinding algorithm, I’ll explain my choice to use Dijkstra’s Algorithm over the several other popular shortest path finding algorithms available.</p>

<h3 id="why-dijkstra-and-not-a">Why Dijkstra and not A*?</h3>

<p><strong>Quick Note on Terminology</strong>: In the literature about pathfinding, the concepts of  <strong>graphs</strong>, <strong>nodes</strong>, and <strong>edges</strong> are used. A graph is an abstract data structure used to analyze the connections between objects. A graph consists of nodes which are connected to each other by edges. In the example of our game, the closed system of a completed path is a graph, each tile within it is a node, and the connections between the tiles are edges.</p>

<p>From the previous section, I showed that the pathfinding algorithm should be able to accomplish the following things:</p>

<ul>
  <li>Find the shortest possible paths from <strong>one</strong> starting node to <strong>all</strong> possible ending nodes. (e.g. from one mine to all factories).</li>
  <li>Ensure that special tiles only serve as endpoints on paths and cannot be traveled through.</li>
</ul>

<p>In looking through the possible shortest path finding algorithms which solve these problems, two stuck out as good candidates: the <a href="https://en.wikipedia.org/wiki/A*_search_algorithm">A* Search algorithm</a>, and <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra’s algorithm</a>.</p>

<p>A* seems to be a crowd favorite, at least for certain use-cases. If you poke around on game-development <a href="https://gamedev.stackexchange.com/questions/1/what-path-finding-algorithms-are-there">forums</a> and <a href="https://gamedevacademy.org/how-to-use-pathfinding-in-phaser/">blogs</a>, you see A* prized everywhere, due mainly to its increased performance over Dijkstra in searching for the shortest path between two given nodes. Whereas Dijkstra’s algorithm needs to analyze every node on a graph, A* uses <a href="https://en.wikipedia.org/wiki/Heuristic_(computer_science">heuristics</a> (basically, educated guesses) to find the shortest path between a beginning and an end node without needing to check every possible node on on entire graph. One Stack Exchange user writes: “When it comes to pathfinding, A* is pretty much the golden ticket that everyone uses.”</p>

<p>A* is basically an extended version of Dijkstra’s algorithm that uses a system of educated guesses to get to an end node while analyzing as few nodes on the graph as possible, and thus taking less time. This is useful for situations, like pathfinding in a RTS, where one has a clear start node and end node. In our example, however, where one mine might connect to several different factories, we would need to run the A* algorithm several times from the same starting node, which could cause to the algorithm to retrace its steps several times—an inefficiency we would like to avoid if possible.</p>

<p>In debating between using A* and Dijkstra, I opted for Dijkstra because I knew that I would need to reach multiple end nodes from one starting node, and that if I were using A*, I would need to run the algorithm multiple times in order to accomplish this, potentially calculating parts of the same route over and over again. Because Dijkstra’s algorithm—at least, in my implementation of it—can calculate the shortest distance to every node in a graph without needing to have a target or goal node up-front, it seems to make the most sense for my game, wherein one starting node can (and likely will) have multiple semi-overlapping paths to multiple end nodes.</p>

<h3 id="the-code--implementation-in-javascript">The Code — Implementation in Javascript</h3>

<p>Ok, now that we hopefully understand how the game works, what goals we want the algorithm to accomplish, and why I choose Dijkstra’s algorithm to accomplish these goals, we can start to look at the code. Everything is written in Javascript (ECMAScript 2016). (Note: Throughout the post, I’ve hidden a lot of the project-specific implementation details or simplified things in cases where it seemed superfluous or confusing. If you want to see the full code all in one place, you can find it <a href="/assets/dijkstra/files/pathfinding.js">here</a>.)</p>

<h4 id="the-nodesonpath-object">The <code class="language-plaintext highlighter-rouge">nodesOnPath</code> Object</h4>

<p>The first thing to understand is the information the rest of the game provides to the pathfinding/scoring mechanism. Every time a player places a tile and ends their turn, the game checks to see if placed tile is on a completed path. (The specifics of this will be for another blog post.) If that path is completed, the game passes an object containing an array called <code class="language-plaintext highlighter-rouge">nodesOnPath</code> to the scoring mechanism. This array is populated with objects representing each tile on the completed path. A typical <code class="language-plaintext highlighter-rouge">nodesOnPath</code> array looks like this:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// nodesOnPath - array of tileObjects</span>
<span class="p">[</span>
  <span class="c1">// tileObject</span>
  <span class="p">{</span>
    <span class="na">id</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="c1">// unique id of tile</span>
    <span class="na">neighbors</span><span class="p">:</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="c1">// unique ids of neighboring tiles</span>
    <span class="na">type</span><span class="p">:</span> <span class="dl">"</span><span class="s2">factory</span><span class="dl">"</span><span class="p">,</span> <span class="c1">// tile type "mine", "factory", "house", or null</span>
    <span class="na">playedBy</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// unique id of player who owns tile</span>
    <span class="na">x</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="c1">// x coord</span>
    <span class="na">y</span><span class="p">:</span> <span class="mi">7</span> <span class="c1">// y coord</span>
  <span class="p">},</span>
  <span class="c1">// another tileObject</span>
  <span class="p">{</span>
    <span class="na">id</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span>
    <span class="na">neighbors</span><span class="p">:</span> <span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">],</span>
    <span class="na">type</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
    <span class="c1">// ...</span>
  <span class="p">}</span>
<span class="p">]</span>
</code></pre></div></div>

<p>This array is passed as the only argument to a function called <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code> which is responsible for taking the information contained in this array, and turning it into point amounts awarded to each player who owns any tiles on the newly completed path. This is accomplished in three steps, each corresponding to one of the steps outlined in Scoring Rules section above.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">calcCompletedPathScore</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// calculates the value of a completed path</span>
  <span class="c1">// for the various players</span>

  <span class="c1">// step 1: prepare nodesOnPath and set value = 1 for each tile</span>
  <span class="c1">// ...</span>

  <span class="c1">// steps 2 + 3: use Dijkstra to increase value of special tiles</span>
  <span class="c1">// ...</span>

  <span class="c1">// final: give player points equal to the value of each tile they own</span>
  <span class="c1">// ...</span>
<span class="p">}</span>
</code></pre></div></div>

<h4 id="scoring-step-1">Scoring: Step 1</h4>

<p>Before doing anything related to pathfinding and Dijkstra, the function does a little set-up and sets some initial values. The way that points will be awarded to each player is simple: each tile is given a <code class="language-plaintext highlighter-rouge">value</code> property which is an integer number of points that the player who owns the tile will receive at the end of the scoring calculation. In accordance with Step 1 of our scoring rules, each tile’s default <code class="language-plaintext highlighter-rouge">value</code> property is set to <code class="language-plaintext highlighter-rouge">1</code>, so that each tile on the path is automatically worth <code class="language-plaintext highlighter-rouge">1</code> point.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">calcCompletedPathScore</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// calculates the value of a completed path</span>
  <span class="c1">// for the various players</span>

  <span class="c1">// used to keep track of which special tiles,</span>
  <span class="c1">// if any, are on the path</span>
  <span class="kd">const</span> <span class="nx">specialTiles</span> <span class="o">=</span> <span class="p">{};</span>

  <span class="c1">// for each node on path...</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">nodesOnPath</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">// add initial value of tile for score</span>
    <span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">value</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

    <span class="c1">// if tile is a special tile</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">type</span><span class="p">)</span> <span class="p">{</span>

      <span class="c1">// set loaded and supplying properties</span>
      <span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">loaded</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
      <span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">supplying</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>

      <span class="c1">// populate specialTiles object</span>
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">specialTiles</span><span class="p">[</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">type</span><span class="p">])</span> <span class="p">{</span>
        <span class="nx">specialTiles</span><span class="p">[</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">type</span><span class="p">]</span> <span class="o">=</span> <span class="p">[];</span>
      <span class="p">}</span>
      <span class="nx">specialTiles</span><span class="p">[</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">type</span><span class="p">].</span><span class="nx">push</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="c1">// steps 2 + 3: use Dijkstra to increase value of special tiles</span>
  <span class="c1">// ...</span>

  <span class="c1">// final: give player points equal to the value of each tile they own</span>
  <span class="c1">// ...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>We also create a <code class="language-plaintext highlighter-rouge">specialTiles</code> object to be used for keeping track of which special tiles we need to pay attention to on this path, as well as set the <code class="language-plaintext highlighter-rouge">loaded</code> and <code class="language-plaintext highlighter-rouge">supplying</code> properties of each tile, which are used later in Step 3 of the scoring rules, when factories must be ‘loaded’ by mines before they can provide houses with goods.</p>

<p>If the completed path were to contain no special tiles, the scoring process would end here. The final step of the <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code> function loops through the tiles objects in <code class="language-plaintext highlighter-rouge">nodesOnPath</code> and adds the <code class="language-plaintext highlighter-rouge">value</code> property to the score of whichever player’s id corresponds with the <code class="language-plaintext highlighter-rouge">playedBy</code> property. Without any special tiles, this would always be <code class="language-plaintext highlighter-rouge">1</code> and each player would receive the same number of points as the number of tiles they own on the newly completed path.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">calcCompletedPathScore</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">)</span> <span class="p">{</span>

  <span class="c1">// step 1: prepare nodesOnPath and set value = 1 for each tile</span>
  <span class="c1">//...</span>

  <span class="c1">// steps 2 + 3: use Dijkstra to increase value of special tiles</span>
  <span class="c1">//...</span>

  <span class="c1">// for each node on the path that belongs to player,</span>
  <span class="c1">// give that player the value of the tile</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">nodesOnPath</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// playerObject has a method addPoints(int)</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">players</span><span class="p">[</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">playedBy</span><span class="p">].</span><span class="nx">addPoints</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">value</span><span class="p">);</span>
  <span class="p">}</span>

<span class="p">}</span>
</code></pre></div></div>

<p>It’s the stuff that happens in Steps 2 and 3 of this process which is really of interest to us here in this article, however, so let’s start taking a look at the code which actually implements Dijkstra’s algorithm and assigns value to the special mine, factory, and house tiles.</p>

<h4 id="steps-2-and-3-pathfinding">Steps 2 and 3: Pathfinding</h4>

<p>(Note: My implementation of Dijkstra’s algorithm was heavily inspired by that of Stella Chung’s, which she documents in a blog post <a href="https://hackernoon.com/how-to-implement-dijkstras-algorithm-in-javascript-abdfd1702d04">here.</a>)</p>

<p>The following is the component of the <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code> function which deals with pathfinding. I left out most of the nitty-gritty details of validity checking for clarity, but the structure should be clear. You’ll notice that there are two main functions which this component relies on which we haven’t looked at yet: <code class="language-plaintext highlighter-rouge">getDijkstraTree</code> and <code class="language-plaintext highlighter-rouge">getOptimalPathFromTree</code>. We’ll look at the actual functions for each of these more in a second, but for now, just pay attention to where they are being called in the <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code> function.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">calcCompletedPathScore</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">)</span> <span class="p">{</span>

  <span class="c1">// step 1: prepare nodesOnPath and set value = 1 for each tile</span>
  <span class="kd">const</span> <span class="nx">specialTiles</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="c1">// populate specialTiles...</span>
  <span class="c1">// ...</span>

  <span class="c1">// steps 2 + 3: use Dijkstra to increase value of special tiles</span>
  <span class="kd">const</span> <span class="nx">calcSpecialTileBonus</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">specialTiles</span><span class="p">,</span> <span class="nx">startType</span><span class="p">,</span> <span class="nx">endType</span><span class="p">,</span> <span class="nx">bonusAmount</span><span class="p">,</span> <span class="nx">mustBeLoaded</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">// check if there is at least one of each startType and endType on path...</span>
    <span class="c1">// if startType must be loaded, check if it is...</span>
    <span class="c1">// for each instance of startType on path...</span>

    <span class="kd">let</span> <span class="nx">startId</span> <span class="o">=</span> <span class="c1">// id of tile to be start node</span>
    <span class="kd">let</span> <span class="nx">dijkstraTree</span> <span class="o">=</span> <span class="nx">getDijkstraTree</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">,</span> <span class="nx">startId</span><span class="p">);</span>

    <span class="c1">// for each valid endType on path...</span>

    <span class="kd">let</span> <span class="nx">finishId</span> <span class="o">=</span> <span class="c1">// id of tile to be end node</span>
    <span class="kd">let</span> <span class="nx">optimalPath</span> <span class="o">=</span> <span class="nx">getOptimalPathFromTree</span><span class="p">(</span><span class="nx">dijkstraTree</span><span class="p">,</span> <span class="nx">finishId</span><span class="p">);</span>

    <span class="c1">// check path validity...</span>

    <span class="c1">// if valid:</span>
    <span class="c1">// add path length to value of tile</span>
    <span class="nx">nodeOnPath</span><span class="p">[</span><span class="nx">startId</span><span class="p">].</span><span class="nx">value</span> <span class="o">+=</span> <span class="nx">optimalPath</span><span class="p">.</span><span class="nx">length</span> <span class="o">*</span> <span class="nx">bonusAmount</span><span class="p">;</span>

    <span class="c1">// if invalid:</span>
    <span class="c1">// no valid path — unscored</span>
    <span class="k">return</span>
  <span class="p">}</span>

  <span class="c1">// mine-&gt;factory, bonus: 1, mustBeLoaded = false</span>
  <span class="nx">calcSpecialTileBonus</span><span class="p">(</span><span class="nx">specialTiles</span><span class="p">,</span> <span class="dl">'</span><span class="s1">mine</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">factory</span><span class="dl">'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="kc">false</span><span class="p">);</span>
  <span class="c1">// factory-&gt;house, bonus: 2, mustBeLoaded = true</span>
  <span class="nx">calcSpecialTileBonus</span><span class="p">(</span><span class="nx">specialTiles</span><span class="p">,</span> <span class="dl">'</span><span class="s1">factory</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">house</span><span class="dl">'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>


  <span class="c1">// final: give player points equal to the value of each tile they own</span>
  <span class="c1">// ...</span>

<span class="p">}</span>
</code></pre></div></div>

<p>For each valid start node, we call a function <code class="language-plaintext highlighter-rouge">getDijkstraTree</code> which will return a data structure that contains the shortest possible paths from the start node to every node on the graph. You’ll notice that we only call this function <em>once</em> per start node. This function is where all the heavy lifting is done, so we want to call it as few times as possible. Then, once the <code class="language-plaintext highlighter-rouge">dijkstraTree</code> has been built, we call a function <code class="language-plaintext highlighter-rouge">getOptimalPathFromTree</code> for each valid end node. This function simply looks up the path between the start node and the given end node in the already built <code class="language-plaintext highlighter-rouge">dijkstraTree</code> object, and stores it as <code class="language-plaintext highlighter-rouge">optimalPath</code>. After checking for validity, the length of <code class="language-plaintext highlighter-rouge">optimalPath</code> is then used to set the value of the tile, which will later be used to assign points to the player.</p>

<p>We wrap all of this up into a function called <code class="language-plaintext highlighter-rouge">calcSpecialTileBonus</code> and then call it twice, once with <code class="language-plaintext highlighter-rouge">'mine'</code> and <code class="language-plaintext highlighter-rouge">'factory'</code> as the startTypes and endTypes, and once with <code class="language-plaintext highlighter-rouge">'factory'</code> and <code class="language-plaintext highlighter-rouge">'house'</code>. The second time, you’ll notice that we include in the arguments a flag that tells us to check if <code class="language-plaintext highlighter-rouge">loaded == true</code> and the modifier <code class="language-plaintext highlighter-rouge">2</code> for calculating the bonus. It seemed like a good idea to abstract this rather than write it twice because 90% of the code would be shared between the two cases.</p>

<h4 id="implementing-dijkstras-algorithm">Implementing Dijkstra’s Algorithm</h4>

<p>Let’s turn now to the actual implementation of the algorithm and look at both functions <code class="language-plaintext highlighter-rouge">getDijkstraTree</code>, <code class="language-plaintext highlighter-rouge">getOptimalPathFromTree</code>, as well as the helper functions which support them.</p>

<p>For Dijkstra’s algorithm, we want to keep track of three things:</p>
<ul>
  <li>the cost of getting to each node from the start node</li>
  <li>the chain of previous or parent nodes used in traversing the graph</li>
  <li>a list of unvisited or unprocessed nodes</li>
</ul>

<p>My implementation, following <a href="https://hackernoon.com/how-to-implement-dijkstras-algorithm-in-javascript-abdfd1702d04">Stella Chung</a>, uses a list of processed nodes instead of a list of unprocessed nodes, but the end result is the same.</p>

<p>Following the <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Algorithm">algorithm definition</a> on the Wikipedia page, we first create our lists, set the cost of the start node to <code class="language-plaintext highlighter-rouge">0</code> and the unknown costs of the rest of the nodes on the graph to <code class="language-plaintext highlighter-rouge">Infinity</code>:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">getDijkstraTree</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">startId</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// build a tree of costs and parents from startId</span>
  <span class="c1">// to every other node on the path</span>

  <span class="c1">// create initial objects and lists</span>
  <span class="kd">const</span> <span class="nx">costs</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="kd">const</span> <span class="nx">parents</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="kd">const</span> <span class="nx">processed</span> <span class="o">=</span> <span class="p">[];</span>

  <span class="c1">// set start cost = 0</span>
  <span class="nx">costs</span><span class="p">[</span><span class="nx">startId</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

  <span class="c1">// set tentative cost for all other nodes to Infinity</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">path</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">path</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">id</span> <span class="o">!=</span> <span class="nx">startId</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">costs</span><span class="p">[</span><span class="nx">path</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="kc">Infinity</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="c1">// currentNode = start node</span>
  <span class="kd">let</span> <span class="nx">currentNode</span> <span class="o">=</span> <span class="nx">getLowestNode</span><span class="p">(</span><span class="nx">costs</span><span class="p">,</span> <span class="nx">processed</span><span class="p">);</span>

  <span class="c1">//...</span>
<span class="p">};</span>
</code></pre></div></div>

<p>The helper function <code class="language-plaintext highlighter-rouge">getLowestNode</code> on the last line above returns the lowest cost, unprocessed node in the graph. This way, the algorithm is always choosing the optimal node as the next one to process. The first time it is run, it will return the start node, of course, because it is the only non-Infinity node in the <code class="language-plaintext highlighter-rouge">costs</code> object.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">getLowestNode</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">costs</span><span class="p">,</span> <span class="nx">processed</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">costs</span><span class="p">).</span><span class="nx">reduce</span><span class="p">((</span><span class="nx">lowest</span><span class="p">,</span> <span class="nx">node</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">lowest</span> <span class="o">===</span> <span class="kc">null</span> <span class="o">||</span> <span class="nx">costs</span><span class="p">[</span><span class="nx">node</span><span class="p">]</span> <span class="o">&lt;</span> <span class="nx">costs</span><span class="p">[</span><span class="nx">lowest</span><span class="p">])</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">processed</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="nx">node</span><span class="p">))</span> <span class="p">{</span>
        <span class="nx">lowest</span> <span class="o">=</span> <span class="nx">node</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="nx">lowest</span><span class="p">;</span>
  <span class="p">},</span> <span class="kc">null</span><span class="p">);</span>
<span class="p">};</span>

</code></pre></div></div>

<p>In the rest of <code class="language-plaintext highlighter-rouge">getDijkstraTree</code>, we perform steps 3–7 of Wikipedia’s algorithm definition.</p>

<p>For each node, we get its current cost (which will be the total thus-far cost required to reach it from the start node) and the costs of its neighbors. Since our graph is just a grid of connected tiles, we use <code class="language-plaintext highlighter-rouge">1</code> for the edge cost for connections to blank tiles and <code class="language-plaintext highlighter-rouge">1001</code> for connections to special tiles. (The game has a limited number of tiles and turns and always will end before a blank path longer than 1000 tiles can be created, so this number is safe.) This ensures that the lowest cost path will always be through blank tiles, and thus implements the rule that paths are not allowed to travel through special tiles.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">getDijkstraTree</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">startId</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">//...</span>

  <span class="c1">// currentNode = start node</span>
  <span class="kd">let</span> <span class="nx">currentNode</span> <span class="o">=</span> <span class="nx">getLowestNode</span><span class="p">(</span><span class="nx">costs</span><span class="p">,</span> <span class="nx">processed</span><span class="p">);</span>

  <span class="c1">// as long as there are nodes which are not in processed...</span>
  <span class="k">while</span> <span class="p">(</span><span class="nx">currentNode</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">let</span> <span class="nx">cost</span> <span class="o">=</span> <span class="nx">costs</span><span class="p">[</span><span class="nx">currentNode</span><span class="p">];</span> <span class="c1">// cost of current node</span>
    <span class="kd">let</span> <span class="nx">children</span> <span class="o">=</span> <span class="nx">getNodeById</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">currentNode</span><span class="p">).</span><span class="nx">neighbors</span><span class="p">;</span>
    <span class="kd">let</span> <span class="nx">childrenCosts</span> <span class="o">=</span> <span class="nx">getCostsOfNeighbors</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">children</span><span class="p">);</span> <span class="c1">// 1 or 1001</span>

    <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">n</span> <span class="k">in</span> <span class="nx">childrenCosts</span><span class="p">)</span> <span class="p">{</span>
      <span class="kd">let</span> <span class="nx">newCost</span> <span class="o">=</span> <span class="nx">cost</span> <span class="o">+</span> <span class="nx">childrenCosts</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span> <span class="c1">// cost of current node + travel</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">costs</span><span class="p">[</span><span class="nx">n</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nx">newCost</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">costs</span><span class="p">[</span><span class="nx">n</span><span class="p">]</span> <span class="o">=</span> <span class="nx">newCost</span><span class="p">;</span>
        <span class="nx">parents</span><span class="p">[</span><span class="nx">n</span><span class="p">]</span> <span class="o">=</span> <span class="nx">currentNode</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>

    <span class="c1">// add currentNode to processed list after processing</span>
    <span class="nx">processed</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">currentNode</span><span class="p">);</span>

    <span class="c1">// get lowest, unprocessed node</span>
    <span class="nx">currentNode</span> <span class="o">=</span> <span class="nx">getLowestNode</span><span class="p">(</span><span class="nx">costs</span><span class="p">,</span> <span class="nx">processed</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="c1">// return path, costs, and parents for use with getOptimalPathFromTree</span>
  <span class="k">return</span> <span class="p">{</span><span class="nx">path</span><span class="p">,</span> <span class="nx">costs</span><span class="p">,</span> <span class="nx">parents</span><span class="p">};</span>
<span class="p">};</span>
</code></pre></div></div>

<p>(Note: <code class="language-plaintext highlighter-rouge">getNodeById</code> and <code class="language-plaintext highlighter-rouge">getCostsOfNeighbors</code> are just helper functions which respectively use the node’s unique id to access its <code class="language-plaintext highlighter-rouge">neighbors</code> property and assign a cost of either <code class="language-plaintext highlighter-rouge">1</code> or <code class="language-plaintext highlighter-rouge">1001</code> depending on whether the neighbor is a blank or special tile. You can see them <a href="/assets/dijkstra/files/pathfinding.js">here</a>.)</p>

<p>Once we have the costs of the current node and all of its neighbors, we tentatively add the cost of the current node to the cost of each of its neighbors, and check whether the currently stored lowest cost for each neighbor node is higher than the tentative one we just calculated. If the already stored cost <em>is</em> higher, that means we just found a less-costly way to reach this node from the start node, so we store that instead and record the current node as the ‘parent’ of the neighbor node we just updated.</p>

<p>Once all the neighbors of the current node have been checked out, we then mark the current node as processed, meaning that it will never be visited again, and get a new current node: whichever has the lowest cost from start and has yet to be processed. This loop is completed until every node on the graph has been processed. What we are left with are two objects: <code class="language-plaintext highlighter-rouge">costs</code> and <code class="language-plaintext highlighter-rouge">parents</code>, which we return and designate as <code class="language-plaintext highlighter-rouge">dijkstraTree</code> in our <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code> function.</p>

<p>Now that we have a calculated tree containing the costs to each node from the start node, we can use it to find the optimal path to each of the end nodes that we are interested in by using <code class="language-plaintext highlighter-rouge">getOptimalPathFromTree</code>. We pass it the tree that we calculated in <code class="language-plaintext highlighter-rouge">getDijkstraTree</code> as well as the unique id of the end node that we are interested in finding the optimal path to.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">getOptimalPathFromTree</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">tree</span><span class="p">,</span> <span class="nx">finishId</span><span class="p">)</span> <span class="p">{</span>

  <span class="c1">// return array of actual nodes on the optimal path</span>
  <span class="kd">let</span> <span class="nx">optimalPath</span> <span class="o">=</span> <span class="p">[</span><span class="nx">getNodeById</span><span class="p">(</span><span class="nx">tree</span><span class="p">.</span><span class="nx">path</span><span class="p">,</span> <span class="nx">finishId</span><span class="p">)];</span>
  <span class="kd">let</span> <span class="nx">parent</span> <span class="o">=</span> <span class="nx">tree</span><span class="p">.</span><span class="nx">parents</span><span class="p">[</span><span class="nx">finishId</span><span class="p">];</span>
  <span class="k">while</span> <span class="p">(</span><span class="nx">parent</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">optimalPath</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">getNodeById</span><span class="p">(</span><span class="nx">tree</span><span class="p">.</span><span class="nx">path</span><span class="p">,</span> <span class="nx">parent</span><span class="p">));</span>
    <span class="nx">parent</span> <span class="o">=</span> <span class="nx">tree</span><span class="p">.</span><span class="nx">parents</span><span class="p">[</span><span class="nx">parent</span><span class="p">];</span>
  <span class="p">}</span>
  <span class="nx">optimalPath</span><span class="p">.</span><span class="nx">reverse</span><span class="p">();</span>

  <span class="c1">// return int of the target Score</span>
  <span class="c1">// used to check validity. if &gt;2000,</span>
  <span class="c1">// then invalid</span>
  <span class="kd">const</span> <span class="nx">targetScore</span> <span class="o">=</span> <span class="nx">tree</span><span class="p">.</span><span class="nx">costs</span><span class="p">[</span><span class="nx">finishId</span><span class="p">];</span>

  <span class="k">return</span> <span class="p">{</span><span class="nx">optimalPath</span><span class="p">,</span> <span class="nx">targetScore</span><span class="p">};</span>
<span class="p">};</span>
</code></pre></div></div>

<p>The function starts with the end node, gets its parent node, and pushes it to an an array <code class="language-plaintext highlighter-rouge">optimalPath</code>. It then gets the parent node of this node, and so forth, until it retraces its way back to the start node, which has no parent. In doing this, it recreates the path taken from the start node to the end node. It returns this path, along with the final score of this final node, designated as <code class="language-plaintext highlighter-rouge">targetScore</code>. Because we set connections to special tiles to be <code class="language-plaintext highlighter-rouge">1001</code>, any targetScore higher than <code class="language-plaintext highlighter-rouge">2000</code> must indicate a path that passes through another special tile, and thus is invalid. We check for this in <code class="language-plaintext highlighter-rouge">calcCompletedPathScore</code>.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">calcSpecialTileBonus</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">specialTiles</span><span class="p">,</span> <span class="nx">startType</span><span class="p">,</span> <span class="nx">endType</span><span class="p">,</span> <span class="nx">bonusAmount</span><span class="p">,</span> <span class="nx">mustBeLoaded</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// ...</span>

  <span class="c1">// check if there is at least one of each startType and endType on path...</span>
  <span class="c1">// if startType must be loaded, check if it is...</span>
  <span class="c1">// for each instance of startType on path...</span>

  <span class="kd">let</span> <span class="nx">startId</span> <span class="o">=</span> <span class="p">;</span><span class="c1">// id of tile to be start node</span>
  <span class="kd">let</span> <span class="nx">dijkstraTree</span> <span class="o">=</span> <span class="nx">getDijkstraTree</span><span class="p">(</span><span class="nx">nodesOnPath</span><span class="p">,</span> <span class="nx">startId</span><span class="p">);</span>

  <span class="c1">// for each valid endType on path...</span>

  <span class="kd">let</span> <span class="nx">finishId</span> <span class="o">=</span> <span class="c1">// id of tile to be end node</span>
  <span class="kd">let</span> <span class="nx">optimalPath</span> <span class="o">=</span> <span class="nx">getOptimalPathFromTree</span><span class="p">(</span><span class="nx">dijkstraTree</span><span class="p">,</span> <span class="nx">finishId</span><span class="p">);</span>

  <span class="c1">// check path validity...</span>

  <span class="c1">// if greater than 2000, then invalid</span>
	<span class="k">if</span> <span class="p">(</span><span class="nx">optimalPath</span><span class="p">.</span><span class="nx">targetScore</span> <span class="o">&lt;</span> <span class="mi">2000</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// add path length to value of tile</span>
    <span class="nx">nodeOnPath</span><span class="p">[</span><span class="nx">startId</span><span class="p">].</span><span class="nx">value</span> <span class="o">+=</span> <span class="nx">optimalPath</span><span class="p">.</span><span class="nx">length</span> <span class="o">*</span> <span class="nx">bonusAmount</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="c1">//...</span>
<span class="p">};</span>
</code></pre></div></div>

<p>If the optimal path returned by <code class="language-plaintext highlighter-rouge">getOptimalPathFromTree</code> is valid, we then use the length of this path (along with the <code class="language-plaintext highlighter-rouge">*2</code> modifier in the case of factories and houses) to change the value of the special tiles on the completed path, and thus to allow proper score calculation based on the rules we set out to implement.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I hope that you were able to see from this example what it looks like to implement Dijkstra’s (or any other shortest path finding algorithm) in a grid- or tile-based game like the one I’ve been developing as well as some of the design decisions that went into choosing this algorithm. If I were building a more dynamic, performance intensive game like a real-time strategy or tower defense game, I would probably use A* or some version of it.</p>

<p>Although I tried to keep my mind bent towards efficiency while building this, I’m sure there are plenty of places where it could be improved. Many suggest that Dijkstra’s algorithm should be implemented with a <a href="https://en.wikipedia.org/wiki/Fibonacci_heap">Fibonacci heap</a>, but due to the relatively small sizes of the graphs that my game is capable of generating, it seemed to be far more trouble than it would be worth to implement this. But perhaps for a future blog post!</p>

<p>If there is anything that I missed or could improve, please reach out to me at <a href="mailto:jbierfeldt@gmail.com">jbierfeldt@gmail.com</a> and let me know!</p>

<h2 id="inspiration-and-resources">Inspiration and Resources:</h2>
<ul>
  <li><a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm</a></li>
  <li><a href="https://en.wikipedia.org/wiki/A-star_search_algorithm">https://en.wikipedia.org/wiki/A-star_search_algorithm</a></li>
  <li><a href="https://hackernoon.com/how-to-implement-dijkstras-algorithm-in-javascript-abdfd1702d04">https://hackernoon.com/how-to-implement-dijkstras-algorithm-in-javascript-abdfd1702d04</a></li>
  <li><a href="https://en.wikipedia.org/wiki/Maze_solving_algorithm">https://en.wikipedia.org/wiki/Maze_solving_algorithm</a></li>
  <li><a href="https://gamedev.stackexchange.com/questions/1/what-path-finding-algorithms-are-there">https://gamedev.stackexchange.com/questions/1/what-path-finding-algorithms-are-there</a></li>
  <li><a href="https://mitpress.mit.edu/books/introduction-algorithms">https://mitpress.mit.edu/books/introduction-algorithms</a></li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[Recently, I’ve been building a multiplayer tile-laying game inspired by games like Carcassonne and Settlers of Catan. I’ll soon have some other blog posts about the game and some of the technologies and design patterns I’m using to build it, but today I’m going to walk through and illustrate how and why I used Dijkstra’s Algorithm to implement the scoring component of the game. Since there are already so many good resources explaining what Dijkstra’s algorithm is and how it works, I’m writing this for somebody who already understands the basic concepts involved, and who would like to see a real-life implementation of it in a tile-based Javascript game.]]></summary></entry></feed>