<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lillie Schachter on Medium]]></title>
        <description><![CDATA[Stories by Lillie Schachter on Medium]]></description>
        <link>https://medium.com/@lillieschachter?source=rss-6d668eed3b13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0VVbrz9Is3wvLXycOtYRfg.jpeg</url>
            <title>Stories by Lillie Schachter on Medium</title>
            <link>https://medium.com/@lillieschachter?source=rss-6d668eed3b13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:27:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lillieschachter/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Diagonal Dominoes: A Refactor 8 years in the Making (Part 2)]]></title>
            <link>https://medium.com/@lillieschachter/diagonal-dominoes-a-refactor-8-years-in-the-making-part-2-3877faf5ae34?source=rss-6d668eed3b13------2</link>
            <guid isPermaLink="false">https://medium.com/p/3877faf5ae34</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[minimax-algorithm]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Lillie Schachter]]></dc:creator>
            <pubDate>Tue, 13 Feb 2024 22:07:29 GMT</pubDate>
            <atom:updated>2024-02-26T22:03:37.571Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome back to part 2 of the Diagonal Dominoes refactor! In <a href="https://medium.com/@lillieschachter/diagonal-dominoes-a-refactor-8-years-in-the-making-2d926276c537">part 1</a>, I walked through my original journey to this game and back to it 8 years later. I described the initial refactor process of cleaning up the code and remembering how the AI works. This time, let’s deep dive into exactly how that is.</p><p>Let’s start by looking at a simplified version of the game where each player has only 3 tiles (instead of 5), each of only 3 possible colors (instead of 4).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7c3BT1GBqZuNWk70Y8WtQg.png" /><figcaption>You thought I was done making you look at Zelle graphics? You think again, friend</figcaption></figure><p>Let’s assume that Player 1 starts by placing their green|red tile:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n68AozU5gAp1T9dM5MOjpg.png" /></figure><p>This can also be written out in such a way that we can keep track of all possible moves, even if we don’t play them. Let’s start that below:</p><pre>Tiles:<br><br>Player 1:<br>green|yellow, green|red, yellow|yellow<br><br>Player 2:<br>red|red, red|yellow, green|green<br><br><br>Moves:<br>1.                  green|red</pre><p>From the image above, we can see that player 2 has two options:</p><ol><li>Play red|red</li><li>Play red|yellow</li></ol><p>Let’s add those two our written version of the game steps.</p><pre>Tiles:<br><br>Player 1:<br>green|yellow, green|red, yellow|yellow<br><br>Player 2:<br>red|red, red|yellow, green|green<br><br><br>1.                  green|red<br>                         /    \<br>2.                    red|red  red|yellow<br>                      -------</pre><p>If player 2 plays red|red, the game is over, as player 1 has no other red tiles to place. If they play red|yellow, player 1 has two follow up options (yellow|green and yellow|yellow), and so on and so on until all possible moves are accounted for. We can see every possible move displayed below:</p><pre>Tiles:<br><br>Player 1:<br>green|yellow, green|red, yellow|yellow<br><br>Player 2:<br>red|red, red|yellow, green|green<br><br><br>1.                  green|red<br>                         /    \<br>2.                    red|red  red|yellow<br>                      -------     /     \<br>3.                          yellow|green  yellow|yellow<br>                                  /       -------------<br>4.                            green|green<br>                              -----------</pre><p>You’ll notice that these options branch out into something like a tree, with the root at the top and the options growing out from there. Which works in our favor, because there happens to be a data structure for that!</p><p>It’s called</p><p>✨wait for it ✨</p><p>….</p><p>a ✨ tree ✨.</p><p>Love it when a plan comes together.</p><p>If you don’t know what a tree is in computer science or how it works, <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">Wikipedia</a> has a pretty solid explanation—</p><figure><img alt="Screenshot of wikipedia’s tree article https://en.wikipedia.org/wiki/Tree_(data_structure)" src="https://cdn-images-1.medium.com/max/1024/1*i6idlWUPwHmX5FiGRI3f1g.png" /><figcaption>The wiki gods have spoken</figcaption></figure><p>But I will note — if this is your first introduction to trees, the rest of this article will probably (definitely) not go into enough detail for you to learn how they work.</p><p>Let’s take a look at the actual code that gets us this tree!</p><p>First thing’s first: trees are built out of ‘nodes’ of data. We know that each node of our tree will represent each possible move, and what additional moves are possible from there. So each node will need to track</p><ul><li>What tile-colors we’re working with</li><li>What tiles are possible for the other player to play in response</li><li>A third thing that will come into play later when we’re deciding which move to play, called ‘payoff’.</li></ul><figure><img alt="Screenshot of the code to create a node of the game-move tree" src="https://cdn-images-1.medium.com/max/1024/1*rrOa1DY8gN81toVmdVbW4A.png" /><figcaption>Please note, the naming and comment snark is from 21 year old Lillie. Forgive her.</figcaption></figure><p>So how are we gonna use this node-code? Using the first tile that the user places on the board, create a ‘root’ node and loop through all of the other player’s available tiles to find ones with the required color that they might place. Use each of <em>those</em> tiles to create child nodes of the root. Repeat this process with all nodes (keeping track of which tiles have already been used in which paths to avoid infinite loops) until you have built out a tree of every possible move the game can contain.</p><p>Notice I mentioned infinite loops? They shouldn’t actually be a problem, as long as our code tracks tiles appropriately — each path from the root to a leaf node represents one play-through from the start to the end of the game. This means that the code must complete one entire path before going back and finding another, so as not to lose track of which tiles have already been used in that path. This implies the use of a ‘depth first search (DFS)’ (i.e. finish one whole path before moving on to the next). One of the many concepts I had ‘technically’ learned in my AI class the year prior, but that hadn’t quite crossed the line into understanding from the chaos that was learning <a href="https://racket-lang.org/">Dr. Racket</a> for that class.</p><p>Now that we know the steps to take, and even that the algorithm we’ll use/build is a DFS, turning it into pseudocode we get:</p><pre># Using the first tile that the user places on the board, create a &#39;root&#39; node<br><br># Given an existing node A (starting with that root node),<br># For each tile in the opposite player&#39;s tile-collection:<br>#    Check <br>#    If the tile has the color you need<br>#    And If it&#39;s not already in use in this path of options<br>#        Mark the tile as &#39;in use&#39; so it can&#39;t be used again in future moves<br>#        Create a new node B with this tile<br>#        Add node B as a &#39;child&#39; of the existing node A<br>#        Call this chunk of code on this new node B (recursion!)<br>#        <br>#        Mark the tile as &#39;not in use&#39; now that we&#39;re done with it</pre><p>And if we look at the actual resulting code, that’s exactly what we do:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Qwm-Lnbv6F7V_oee" /></figure><p>This is it, this is the entire code to build the tree. (If you want to take a look at it in context, you can find the code <a href="https://github.com/lschachter/diagonalDominoes/blob/master/GameTree.py">here</a>.) If we didn’t care about how likely each move is to win the game, and instead just wanted to know every possible move, we wouldn’t need anymore code. However, the point of creating this tree is to have the code decide which move to make based on how likely that move is to win the game. And you might have noticed there’s one more line of code here than in our pseudocode, setting the ‘payoff’ of each node.</p><p>So yes, upon reaching a leaf node, the code must then set the ‘payoff’ of each node/move in that path of choices. These are the gains you are ‘maximizing’ the ‘minimum’ of. If the computer wins the round, set the payoff to 100 (this is a bit of a simplification– as written in the comments I actually</p><pre># Multiply 100 by (max depth - node depth) to prioritize faster wins</pre><p>). Whereas if the user wins, set the payoff to 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1sT4SbY-Hp2iifsL" /></figure><p>Because you’re doing a depth-first build of the tree, the leaf node of a path will always have its payoff set before any of its ancestors, which means you can use the leaf node payoffs to set their parent nodes’ payoffs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kKd9hd0AmcybZ3KQ" /></figure><p>That’s ‘all’ it takes to build out every possible move and score them according to how likely each move is to lead to a computer win. During actual game play, it then becomes a matter of tracking where you are in the tree in order to pick the child node (the tile to play) with the highest payoff. But let me assure you, that’s not all the code I wrote trying to get to that solution the first time around.</p><p>8 years ago, I couldn’t wrap my head around the idea that all nodes in a path would exist before the payoffs were being set. So I would make the whole tree, and then go back and set the payoffs afterward. I also could not for the <em>life</em> of me figure out how to look at all the possible moves at a given depth of the tree. That one requires a ‘level-order’ or ‘breadth-first’ traversal, another concept that went over my youthful head. Y’all when I tell you it’s five lines of code. It’s FIVE LINES OF CODE 😭.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zduLOU2RENW5EAykWkVk7A.png" /><figcaption>FIVE.</figcaption></figure><p>But in order to achieve that functionality, I made a whole ass extra dictionary that tracked the nodes by depth and kept track of which depth the game was currently at. The inefficiency. The over-complication.</p><p>Or. The flexibility to find a solution that got the code working without it being perfect. Thank you time for providing a kinder lens.</p><p>After all that, it was finally time to rewrite the whole thing with React and TypeScript (which remember, I did the thing and you can play it <a href="https://lillie.dev/diagonal-dominoes/">here</a>!). But that’s another post for another time. This part of the process, the initial refactor, taught me so much more than I was expecting. About refactoring in Python, certainly, but also about extending myself grace, both when looking at the past and right now. I’ve always been a big fan of revisiting things over and over again. I even wrote my college application essay about rereading A Book Series Who Must Not Be Named at different ages and getting new lessons reflected back at me. In hindsight, I’m not surprised that looking back at my own code creation had much the same effect.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3877faf5ae34" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Diagonal Dominoes: A Refactor 8 years in the Making]]></title>
            <link>https://medium.com/@lillieschachter/diagonal-dominoes-a-refactor-8-years-in-the-making-2d926276c537?source=rss-6d668eed3b13------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d926276c537</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[minimax-algorithm]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Lillie Schachter]]></dc:creator>
            <pubDate>Mon, 25 Sep 2023 23:40:45 GMT</pubDate>
            <atom:updated>2023-09-26T22:36:22.222Z</atom:updated>
            <content:encoded><![CDATA[<h3>Diagonal Dominoes: A Refactor 8 years in the Making (Part 1)</h3><blockquote>“This game is based on a mini-game in the PS2 video game of Avatar: The Last Airbender, because <em>that’s</em> the kind of nerd I am.”</blockquote><p>That’s how I introduced <a href="https://lillie.dev/diagonal-dominoes/"><strong>Diagonal Dominoes</strong></a> to my Algorithmic Game Theory class in college. For our midterm project, we were each tasked with coding up one of the algorithms we’d learned for a real applied use. I chose the ‘<a href="https://en.wikipedia.org/wiki/Minimax">minimax algorithm</a>’, which lends itself quite nicely to finding optimal solutions to simple games like Tic Tac Toe, or, naturally, the ‘Four Nations’ mini-game in the 2006 <a href="https://www.metacritic.com/game/avatar-the-last-airbender/">ATLA video game</a>. You know, a classic.</p><figure><img alt="The front of the PlayStation 2 video game Avatar: The Last Airbender" src="https://cdn-images-1.medium.com/max/1024/1*9DkvQvRCFnuUoms9Mmfcyw.png" /><figcaption>‘Classique’</figcaption></figure><p>I spent weeks crafting the board, the tile animations, the game logic, and of course, the AI. And it worked! Despite my best efforts to confuse and derail myself (which were significant), there finally came a moment when I played my game and for all intents and purposes, it was smarter than me. Genuinely, I felt like a god. It was the crowning achievement of my academic career, the complete synthesis of years of feeling like I barely understood what was happening and couldn’t possibly deserve to be there (thanks, imposter syndrome 🫠). I built an AI, and it worked!</p><p>Before anyone asks, this was <em>not </em>an AI capable of teaching itself things or taking over the world. In this game, there’s a small enough set of possible moves that the computer can</p><ol><li>Determine every possible move</li><li>Score each possible move based on how likely it is to lead to a win</li><li>Select its best option at each turn</li></ol><p>without too much processing power.</p><p>Anyways. For years after this presentation, I’d daydream about returning to that code and making it playable on the internet. This would be quite a bit of work for two reasons. First, in my partial understanding of certain concepts, I had woefully overcomplicated the code. Second, because I wrote it, as I had been taught, using a Python library called<a href="https://mcsp.wartburg.edu/zelle/python/graphics.py"> Zelle’s Graphics</a>. Installation instructions had come with our ‘Intro to Computer Science’ textbooks, and it really is intended to remain an introductory tool (for which purpose it <em>does</em> do its job!). But it was all I knew, and I did what I could. Unfortunately, that meant that to make it playable on the internet, huge swaths of the code would have had to be rewritten even if the functionality itself had been impeccable, which it was not.</p><p>As excited as I was at the thought of this project, I was horrified at the idea of opening up that code. Having gained a much deeper understanding of several concepts along my software engineering career (yay growth), I imagined the code of my ‘youth’ would be awful to the point of cringe. Plus who has time for that type of project anyway? So it sat untouched in my GitHub repositories for 8 years.</p><figure><img alt="A GitHub commit from 2023 followed by one from 2015" src="https://cdn-images-1.medium.com/max/1024/1*CnENoHtD-_zyYzyGgpiT4Q.png" /><figcaption>It had been a minute</figcaption></figure><p>Finally this Spring, I quit a job without having another lined up. I had planned and saved, and decided to take the Summer to recharge and focus on what I wanted to do. And don’t you worry, I had plenty of non-code fun! I traveled, I learned guitar, I started doing archery, I had a blast. And also, I decided that it was time to open up that repo.</p><p>I wasn’t wrong, there was a ton of unnecessary, overcomplicated code. But it didn’t make me cringe– the issues I saw made perfect sense for where my understanding had been at the time. And along with all those issues was strong separation of concerns across classes, functioning logic, and a sick little AI that could still beat my ass at this game. I was so proud of 21-year old me! She <em>made </em>that shit! How friggin’ cool. It was actually kind of healing<em> </em>to look back at this code that I had somehow become so ashamed of, and be reminded of the accomplishment. And then came the fun part.</p><p>I had decided that I was going to port the code into <a href="https://react.dev/">React</a> and <a href="https://www.typescriptlang.org/">TypeScript</a>. I wanted to strengthen my understanding of tools I hadn’t been using at work, and those two felt like a great place to start. But before I could rewrite everything in JavaScript, I needed to fix the existing Python code. I did <em>not </em>want to bring inefficiency or confusion into the new version of the game. Which brought me to the biggest question: how the f did this thing work again?</p><p>I started with the lowest hanging fruit to get myself back into it. I DRYed out the code by consolidating logic into smaller functions and using more inheritance in my objects. I removed unnecessary parameters by simplifying the flow of data. I used tools I’d learned in my professional career to speed the clean up process, deleting unused methods found with <a href="https://code.visualstudio.com/">VSCode’s</a> search functionality, running the <a href="https://black.readthedocs.io/en/stable/">Black</a> code formatter on my files to get that clean uniform Python code. I also (data-) typed all my parameters, which made it much easier to track where and how I was sending data through my codebase, and to see how to streamline it.</p><p>It was super satisfying. Every commit that erased more code than it added scratched that clean-code refactor itch, and it almost felt like I was teaching my 21-year old self all the things we’d learned in the now 9 years since we built the game (I turned 30 while I was working on the project, so I <em>might</em> have been feeling nostalgic). It was really gratifying to have such a clear depiction of how much I had grown as a software engineer, and to apply that growth directly to things I’d struggled with before. Which brings me to those pesky concepts that had gone so far over my head the first time around.</p><p>At this point, I should probably take a minute to actually explain how Diagonal Dominoes works. It’s telling how far into walking through the refactor process you can get without actually describing what you’re refactoring– much of the work above can be applied to any project you’re taking a second (or third, or eightieth) look at. But how does this game work, and what does the AI even do?</p><p>My <em>original </em>Diagonal Dominoes, the Python version, is a two-player game where Player-2 is the computer. Each player gets a set of 5 tiles (or dominoes), each of which has two colors– red, green, yellow, or blue. (For any fans of Avatar: The Last Airbender, here you might start seeing how this game relates.) The tiles are placed one at a time, starting from the bottom-left corner of the board. At each turn, the current player must place a tile with a color that matches the rightmost color currently on the board, diagonally to the right above it. It’s not very straightforward to write about, but the mechanic is fairly simple to play (which you can do<a href="https://lillie.dev/diagonal-dominoes/"> here</a>, I did the thing!).</p><figure><img alt="A human playing a tile and the computer responding with its own move" src="https://cdn-images-1.medium.com/max/1003/1*HNYv_612tx464yUUqlxE2w.gif" /><figcaption>Behold, the glorious hideousness of Zelle Graphics</figcaption></figure><p>The goal of the game is to place your tiles such that your opponent has no more valid moves to make. If you both place all of your tiles, you tie. Otherwise, the last player able to place a tile wins.</p><p>The <em>minimax </em>algorithm is used to determine which move the computer should make. I wanted the AI to always make the best possible move to try and win the game. This meant not only that if there was an immediate winning move, the computer would pick it, but that if there were only moves that risked losing, the computer would pick the one with the most opportunity for human error, thereby <em>maximizing </em>the <em>minimum </em>possible<em> </em>gain.</p><p>How do? Next time, I’ll walk through the entire algorithm implementation itself. I’ll compare the Python and JavaScript implementations and talk about the mindset shift required to keep track of the game state. I’ll also dig into my initial confusions implementing the algorithm 8 years ago, and what concepts/methods I’ve learned in the interim that helped me succeed in cleaning it up this time around. Until then, stay sane, stay safe, stay healthy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d926276c537" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>