<?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 Eric Ford Consulting on Medium]]></title>
        <description><![CDATA[Stories by Eric Ford Consulting on Medium]]></description>
        <link>https://medium.com/@ericfordconsulting?source=rss-3f0399ccbb78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ezl76iIigfLV5ICd1IO03A.jpeg</url>
            <title>Stories by Eric Ford Consulting on Medium</title>
            <link>https://medium.com/@ericfordconsulting?source=rss-3f0399ccbb78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:31:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ericfordconsulting/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[Recreating Jamulator using the Claude AI Coding Agent]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@ericfordconsulting/recreating-jamulator-using-the-claude-ai-coding-agent-f34e7e03ace6?source=rss-3f0399ccbb78------2"><img src="https://cdn-images-1.medium.com/max/1227/1*5PjfNS6OnsCPs6m5z5cJfA.png" width="1227"></a></p><p class="medium-feed-snippet">I&#x2019;ve published a few apps in the Apple App Store, mostly to present as a portfolio to potential employers. I never put any effort into&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@ericfordconsulting/recreating-jamulator-using-the-claude-ai-coding-agent-f34e7e03ace6?source=rss-3f0399ccbb78------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@ericfordconsulting/recreating-jamulator-using-the-claude-ai-coding-agent-f34e7e03ace6?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/f34e7e03ace6</guid>
            <category><![CDATA[music]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[claude]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Sun, 19 Apr 2026 00:04:08 GMT</pubDate>
            <atom:updated>2026-04-19T00:08:25.004Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Exploring XCode 26 AI]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@ericfordconsulting/exploring-xcode-26-ai-7c0666accac7?source=rss-3f0399ccbb78------2"><img src="https://cdn-images-1.medium.com/max/894/1*W3j2guQAN0COneq2NOUaXg.png" width="894"></a></p><p class="medium-feed-snippet">I messed with vibe coding a year ago and found it to be a mixed bag. At the time I was developing a game, kind of a hybrid of chess and&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@ericfordconsulting/exploring-xcode-26-ai-7c0666accac7?source=rss-3f0399ccbb78------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@ericfordconsulting/exploring-xcode-26-ai-7c0666accac7?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c0666accac7</guid>
            <category><![CDATA[agents]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Wed, 08 Apr 2026 23:14:05 GMT</pubDate>
            <atom:updated>2026-04-08T23:19:51.448Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[SwiftUI Game Coding, Chapter 11]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-game-coding-chapter-11-f86b15d15688?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/f86b15d15688</guid>
            <category><![CDATA[canvas]]></category>
            <category><![CDATA[make-your-own-gradient]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Sun, 07 Dec 2025 22:49:19 GMT</pubDate>
            <atom:updated>2025-12-07T22:49:19.087Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating complex shapes and filling them using Canvas</h4><p>I just now ran across an interesting problem. I am drawing a candle that’s sitting in a candlestick. The cylinder that is the candle is easy. The interesting part is making a base that has a lip around it. Once that shape is drawn, it would look good filled with a gradient.</p><p>Here’s an outline of the cylinder and two ellipses that are the beginning of the base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*CQArqM7OCx4k0zbocVkNMQ.png" /></figure><p>I learned that quad curves can’t simulate half an ellipse. Then I found this method: let trimmed = lipEllipsePath.trimmedPath(from: 0.0, to: 0.5). I drew it in red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*FqV953vLx5CTgLYY_ddM5A.png" /></figure><p>I tried many many ways to create the front and back lip shapes so that I could fill them with gradients. My original plan was to draw each lip with two trimmed ellipses along with lines connecting their ends. But Canvas doesn’t transition well between these types. I saw some info online suggesting that if I calculated the endpoints using trigonometry sine and cosine functions, sin and cos, that then I could connect the lines so that the fill command would behave the way I wanted it to. But there was a problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*cQ--MSgjL7BDdIurcQUoBQ.png" /></figure><p>The trig functions that I found in multiple places online did not match up the endpoints with the trimmed ellipse. They would match at 12:00, 3:00, 6:00 and 9:00, but they would slip away everywhere else. I thought I just wasn’t coding the math right, so I reached out to my buddy Guy, a retired math teacher, and the formulae he sent me seemed to match up with my code. I suspect that this is an Apple bug, but after spending a depressing day trying to get it working I decided that I had jumped down a rat hole and that I needed to move on.</p><p>I tried a few kluges to make the gradient idea work, and then I decided to try rolling my own. I saw that you can even call stroke() with a gradient, but I don’t think the ends of a stroke that thick will look right. I decided to try a series of half ellipse strokes in gradated colors. I wrote the code in steps, starting with just the rectangles which are used to define containers for the ellipses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*c6ayd1XzTadQcMN1VYtE5Q.png" /></figure><p>Then using those to draw ellipses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*0hO54_DaTwVM1l418XW2jg.png" /></figure><p>I was planning to then trim this to make the bottom lip, then do another for the top, when I realized that I was already done! So I added the bottom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*r4911XTwoz0BYAqSKrUHiw.png" /></figure><p>Then the part that holds the candle, with a little lip that I made using the same code (which I will show you after all these pictures), and then the candlestick.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*oE3u13wrOe3sOXS0bL6p7w.png" /></figure><p>And then a little loop to put your finger through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*bk4WF2wSz9ZT_llSrvbSlA.png" /></figure><p>Now we just need some flames. Here’s the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bfaa95b899f594e97779961c1f6d3dd/href">https://medium.com/media/3bfaa95b899f594e97779961c1f6d3dd/href</a></iframe><p>Here’s the flame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*bS1CBaHQtcweUF8d30mAcA.png" /></figure><p><strong>Coming up: </strong>Chapter 12, more canvas drawing</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f86b15d15688" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI Game Coding — Chapter 10]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-game-coding-chapter-10-2b039ece2f57?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b039ece2f57</guid>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Sun, 23 Nov 2025 22:58:07 GMT</pubDate>
            <atom:updated>2025-12-03T20:23:45.840Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI Game Coding — Chapter 10</h3><h4>Back to the Drawing Board</h4><p>Yesterday I had to drive for two hours to pick up an amp I had left at a gig. I was irritated to have to stop coding in the middle of the day, but on the gorgeous drive through the NorCal wine country I had so many ideas about what to code next, and what to write about that code as well, that I remembered that stepping away from the screen (okay, the four screens) once in a while could stimulate different parts of my brain.</p><p>After coming up with something I’m calling blast fire, to animate behind the broken pieces of the bomb (and the piece it is blowing up), so it doesn’t just look like those things broke into pieces, so it looks like some gunpowder was involved, I realized that my visualization may have come from my childhood.</p><p><a href="https://ap2hyc.com/2016/10/bam-pow-cultural-relevance-batman-movie/">BAM! POW! The Cultural Relevance of Batman: The Movie</a></p><p>When I got home, I dove into coding blast fire, and immediately ran into something interesting. I wanted to make a five pointed star shape, but I wanted some randomness, for the same reasons I added randomness to the animation so far; to keep things fresh. My stepson James (a dedicated gamer among many other things), praised this.</p><p>Randomly choosing x, y coordinate pairs looked like a rabbit hole I didn’t want to go down. I wanted to randomly choose a clock position, within a certain range, then translate that to an x, y pair. Enter radians.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccb970e33e43d78a0f8d416cc64bc514/href">https://medium.com/media/ccb970e33e43d78a0f8d416cc64bc514/href</a></iframe><p>How many of us took a trigonometry class and thought, “I’m never going to use this”? Enter sin and cos (short for cosine). So radians is a clock position, and radius is the distance away from the center. Kind of like asking is it the hour hand or the minute hand. I want the points of my star to be distributed around the clock somewhat randomly, but I need them to be in separate segments. I can’t have all of them between 1 o’clock and 3 o’clock for example. I also don’t want any of them touching, so they’ll need a little padding; parts of their segment that are off limits. So I’ll take the point count (I’m starting with 5) and divide up the clock, then randomly select a clock position from each segment that isn’t too close to one edge or the other of that segment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e466ff2bb43297e8309223b24bcbc020/href">https://medium.com/media/e466ff2bb43297e8309223b24bcbc020/href</a></iframe><p>Note that the number of radians in the circumference of a full circle is 2 π, so we divide that by the number of segments, then on the next line we divide that by 4 * numberOfSegments which gives us padding that is about 1/20 of the segment size. Since I am just now learning about radians, I wasn’t sure if they would translate to both negative and positive x, y coordinate pairs, so I coded a little test so I could view the results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*0AuQJS0uElM8RKvZGCtPdg.png" /></figure><p>I ran it a few times to make sure the points would support a five pointed star, and it worked. So let’s draw that. We’ll use these points to be the points of the star, then we’ll find a point in between each pair that’s closer to the center. Then we just connect them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77a3591d286589f904a6f4d469acdf0e/href">https://medium.com/media/77a3591d286589f904a6f4d469acdf0e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*8STJpD4edFpz9uwk17zllQ.png" /><figcaption>First Star</figcaption></figure><p>Now we fill it, and add smaller, nested orange and red stars inside it, then we’ll animate the size and opacity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*a6Kve-_jNspSBiCAqcTtSA.png" /></figure><p>It’s like a cartoon from the 60s. Here’s one that happened by accident that I also really like. Maybe even more 60s-ish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*ThbYiBz7JIQIQcEKAZLPUg.png" /></figure><p>I had animated the previous star and been luke warm about the results. Tried animating size and opacity, still I would call it barely acceptable. But this one… check it out!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*zIVIpM0DOjvWWvpDD1SnOg.gif" /></figure><p>I think it looks very explosiony (hmmm… maybe not a word?). I still want to try a few tweaks like animating the size and opacity. But also, right now the bomb fragments and the part of the piece that is exploding get randomly bigger and smaller. I have a hunch that I should change the code to lean towards the big side so they don’t get lost in this new explosion(y?) background.</p><p><em>A hint on writing these articles on the web: I downloaded an app called GIFfun, and then changed my code to show one pic at a time, command-shift-3 to capture a screenshot, Preview app to crop, then drag all the pics into GIFfun and I can put that GIF on the page here so you can see an approximation of what happens in my app. Might be quicker to record the screen and post that, but I’m sticking with this for the time being.</em></p><p>It turned out that it took me a few hours to figure out how to combine the use of PhaseAnimator with my Canvas based views. Here’s some PhaseAnimator code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/269094bad90121cac13e81311211b30f/href">https://medium.com/media/269094bad90121cac13e81311211b30f/href</a></iframe><p>PhaseAnimator seems to go through all the phases, then return to the first one. At first I thought this was my bug, but I found other coders talking about it on Stack Overflow, so in the last phase I set my observed var to nil to prevent phase 1 from showing up again. This wouldn’t be too important in this scenario except I want to try reducing opacity and increasing size, so ending back at square one isn’t what I want. And since I am using that code to shut off the animation, I don’t have to turn off the trigger. All the sample code out there uses a button to turn the trigger on and off. I had difficulty turning off the trigger in code (instead of a button). Might be my bad, not understanding exactly how or where to turn off the trigger, but it could also be a concurrency issue.</p><p>Let’s try some size and opacity animation and see if we like the results. At the same time, I will bias the bomb fragments and pieces of the exploding playing piece to come more forward than backward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*IQidfhaAW3BwrEs5Q9CNrA.gif" /></figure><p>This web animation looks a lot less smooth than it does in the app.</p><p>Here’s the code for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c00bf6ed68f21457156b4593a81e3bd0/href">https://medium.com/media/c00bf6ed68f21457156b4593a81e3bd0/href</a></iframe><p><strong>Coming up:</strong> I’m changing my Chapter 11 topic. I’ll write about the exploding brick wall animation later. Right now I am fascinated by a different drawing problem: drawing my candlestick. So, back to the drawing board again…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b039ece2f57" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI — Game Coding, Chapter 9]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-game-coding-chapter-9-fe501e4df7dc?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe501e4df7dc</guid>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Wed, 19 Nov 2025 00:50:48 GMT</pubDate>
            <atom:updated>2025-11-21T16:31:10.203Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI — Game Coding, Chapter 9</h3><h4>3-D Animations</h4><p>I want to start with a bomb exploding. I’ll cut it up into pieces and then send them shooting in arcs while spinning in 3-D. There’s a very handy SwiftUI view modifier, .rotation3DEffect that will add that third dimension for us.</p><p>First, let’s draw some bomb fragments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c60e98d6eff04074254867d06206eeb7/href">https://medium.com/media/c60e98d6eff04074254867d06206eeb7/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*Npu0RaHNZn9ae2YArC7d_g.png" /></figure><p>Add the other three fragments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aa615fff89b48ea26a2f604bb082d99/href">https://medium.com/media/2aa615fff89b48ea26a2f604bb082d99/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*sgI9RM5aA-XRIYsIhvOSow.png" /></figure><p>Then we want the squat cylinder on top.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28db9f59081e99f395c46728940d7191/href">https://medium.com/media/28db9f59081e99f395c46728940d7191/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*7qxIf28kmYJHSvKUDz-87Q.png" /><figcaption>Now we have the pieces (maybe the fuse burned up?)</figcaption></figure><p>To animate this explosion we can start by making the pieces fly away from each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*NsLpA6nrvGMf4nSe0lfGdg.gif" /><figcaption>Distance only animation</figcaption></figure><p>I do this by adding offsets to the view model, then assigning them in view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/889ce04a42e0ff9b3af12562a75f716f/href">https://medium.com/media/889ce04a42e0ff9b3af12562a75f716f/href</a></iframe><p>Then we animate them by changing their .offset. We’ll create an extension of our BoardView.ViewModel and create a method that performs the animation, then resets the variables when it’s done. This is in the middle of a move, so we have some other things to handle here too. Play a sound, set the bombExplosionInfo, which is how the view gets notified to update, replacing the bomb image with the animated fragments. We also have an explosionCompletion() method passed to us where we finish up our move, then let the AI take its turn. We have it passed in so that it doesn’t execute until the animation is done.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea757323bd31d580d069ab9eefc7d196/href">https://medium.com/media/ea757323bd31d580d069ab9eefc7d196/href</a></iframe><p>Now we animate the sizes of the fragments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*E3j69SjvNrBGnWxapxtGrA.gif" /><figcaption>Change the size as well</figcaption></figure><p>And the pièce de résistance will be to rotate each fragment in 3-D.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*JQJ9I3Z-BRX6rJxyonsPTA.gif" /><figcaption>3-D Rotation</figcaption></figure><p>Of course, these fragments shouldn’t just stay where they end up, so let’s animate the opacity to zero. Here’s all of it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/590eb9e9bfeb51a5db73e8ad34080239/href">https://medium.com/media/590eb9e9bfeb51a5db73e8ad34080239/href</a></iframe><p>And I think explosions shouldn’t be the same, so let’s introduce some randomness. We’ll add that to hitABomb().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4aa4654665be2774eee6551a9bdcb67/href">https://medium.com/media/e4aa4654665be2774eee6551a9bdcb67/href</a></iframe><p>This code is too verbose. Also some of the ranges of the randomized animation variables are a bit off, causing some imbalances in the trajectories of the fragments. Here is some refined, refactored, tuned up animation code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b8d1b8e351ec09b3fce22451f081a80/href">https://medium.com/media/4b8d1b8e351ec09b3fce22451f081a80/href</a></iframe><p>And here is a tightened up view to go with it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03b9487727dff986299801cf54b2b4df/href">https://medium.com/media/03b9487727dff986299801cf54b2b4df/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*lorpXxZBv-i-wr9c6Mpdtw.gif" /></figure><p><strong>Coming up:</strong> Back to the drawing board. I think this animation could use a blast of cartoon fire. I will need that for the dynamite animation as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe501e4df7dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI Game Coding, Chapter 8]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-game-coding-chapter-8-cc3eb8507410?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc3eb8507410</guid>
            <category><![CDATA[fog-of-war]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[chess-variants]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Fri, 07 Nov 2025 19:38:37 GMT</pubDate>
            <atom:updated>2025-11-07T19:38:37.996Z</atom:updated>
            <content:encoded><![CDATA[<h4>Randomly Populate the Room-Maze, then adapt the Game AI</h4><p>Yesterday, after completing the random room/maze generator, I wrote some code to randomly populate each maze with both pieces and tools they can use. Then I had my son (okay, okay, step-son) James try it out. He wasn’t fond of the game AI, which struck him as timid.</p><p>Given that these mazes are intended to be combined with the fog-of-war, where most of the board starts out hidden and has to be uncovered, he felt that the game AI just sat in the dark, waiting for him to stick his neck out.</p><p>The game AI, and I think it’s fair to start referring to it that way because now I’m going to imbue it with some pseudo-intelligence, the game AI right now is modeled after normal chess. It doesn’t know any squares are hidden. And furthermore, it isn’t very aware of the tools and their value. It has no urge to move into enemy territory. I’m going to modify it to be playing a little more of the same game that we are.</p><p>But first, the somewhat straightforward code to populate the board.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/979e15132c736165681dca1867536790/href">https://medium.com/media/979e15132c736165681dca1867536790/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*Vd3Zzp6hoMcITQyOwFTq0w.png" /><figcaption>Populated Board</figcaption></figure><p>The details on this 12 x 12 are a little small on my iPhone, particularly when I want to see what tools an enemy piece is carrying. I’m also thinking starting with a smaller board might be a more gentle introduction. Also, I don’t like the distribution of the tools; it should be a little more even.</p><p>But before addressing any of this, I want to play around with the game AI. The first thing I want to do is apply the same visibility rules. So we’ll keep track of which squares the AI can “see”.</p><p>So right now the AI generates moves, then generates the opponent’s moves, and when it reaches the limit of search depth, it will score material and also mobility. If squares are hidden, and therefore enemy pieces are hidden, it shouldn’t be allowed to calculate the moves of those pieces. We’ll need to change the material algorithm so that it calculates the change in material rather than adding up and then comparing all the material on each team, since it won’t have access to hidden enemies.</p><p>Also, we will want to encourage the AI to reveal squares, including valuing picking up candles and lamps, both of which increase the amount of squares revealed. And later we should also devalue both of those tools as more and more of the board gets revealed.</p><p>I spent yesterday afternoon in a major refactor. Now the evaluation method will need to maintain state between moves; specifically, which squares it is allowed to see. This state is maintained for the human player as part of the BoardView.ViewModel. But as of now, the squares that are visible to the game AI are not part of a view.</p><p>The more I look at the strategy for this new game AI, the more I think about what my strategy is when playing against the game AI. Or what it would be against another person (and yes, I’ll implement that eventually).</p><p>An obvious goal is to reveal hidden squares, so I’ll assign points to that. It’s also valuable to grab tools, and in the case of both dynamite and lamps, to use them. And having a ghost, maybe extra points for moving into a wall. Capturing a piece would have full value if it’s not near hidden squares, but that will be comparatively rare. So getting full points for a capture where the capturing piece might then be attacked from hidden squares would count for more if we had a piece defending that square. In other words, a double attack on the square allows us to capture more safely. Also, if we capture a piece more valuable than our piece that does the capture, we should at least get the difference in points even if we lose our capturer.</p><p>This seems like enough to get started with. We’ll see how it feels to play against these heuristics. Here’s a start.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/685c4d57a66e67ee457a14dcc6e777e0/href">https://medium.com/media/685c4d57a66e67ee457a14dcc6e777e0/href</a></iframe><p>Here’s how shouldHide() works, pretty similar to countAdjacentHiddenSquares().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63948f848e8c15089ab98d0cd7c86554/href">https://medium.com/media/63948f848e8c15089ab98d0cd7c86554/href</a></iframe><p>Then we need to update the squaresAICanSee array after the AI makes its move.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e517ed09d7866e0288f14d27e2bd7a3c/href">https://medium.com/media/e517ed09d7866e0288f14d27e2bd7a3c/href</a></iframe><p>Then, just to see if this code was working as intended, I added some view code to display the squares that are not hidden from the AI. I’m not sure if I want to keep the view working this way. I might want to create a new tool that reveals what the AI can see, maybe just once, or maybe temporarily.</p><p>I changed the color of the gray squares, the ones that are hidden from me, the human player. The dark gray squares are hidden to both players. If the AI can see them, even though they are hidden to me, I make them light gray. Then, if a square is visible to me and also visible to the AI, I give it a thin blue rectangle. Here’s what it looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HNmg66XUSBntYDQcqB9EAA.png" /><figcaption>AI can see the lighter gray squares, and also the ones with thin blue rectangles in them</figcaption></figure><p>So far the fog of war AI only prioritizes revealing hidden squares. We need to add in some basic attack and defense, like avoiding getting captured, and getting points for capturing. To start with, we’ll just go two ply deep: make the AI moves, then check the responses. First let’s assign some semi-arbitrary values to wounds, captures, and grabbing tools.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf0ed1f9fe3ece4e63be559d9300629d/href">https://medium.com/media/bf0ed1f9fe3ece4e63be559d9300629d/href</a></iframe><p>Later we can get more specific about the value of abilities and individual tools. In order to calculate the other team’s response to the move we are evaluating, we need to clone the board so we can make our (okay, really the AI’s) move, then generate the other team’s responses based only on the squares we (in this case the AI) can see. So first, let’s generate that subset of legal moves.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f8317f30bde9b89d4353c585a3ef1a4/href">https://medium.com/media/2f8317f30bde9b89d4353c585a3ef1a4/href</a></iframe><p>A more detailed version might only generate moves that our team can see, like if the start and end and the squares in between are all visible. But we’re just approximating right now, so let’s evaluate those moves we generated and modify the score of our own (the AI’s) move based on that. That way the AI will watch out for losing its pieces.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc38ee1a7df4d3a0acce52062ba6661c/href">https://medium.com/media/cc38ee1a7df4d3a0acce52062ba6661c/href</a></iframe><p>Later we can improve the AI’s use of tools. Right now, for example, it doesn’t blow its dynamite. That’s going to get crazy! I also want to do a little animation for dynamite, and I’d also like a version where when a hidden AI piece blows the dynamite maybe there’s a smoke cloud in the hidden squares. I think that might be a good next chapter. There are all kinds of animations that would make this game more fun.</p><p><strong>Coming up:</strong> in Chapter 9 we will explore some animations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc3eb8507410" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI — Deeper Coding, Chapter 7]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-7-a78c6148f85c?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/a78c6148f85c</guid>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Mon, 03 Nov 2025 22:55:54 GMT</pubDate>
            <atom:updated>2025-11-04T17:03:34.400Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI — Game Coding, Chapter 7</h3><h4>A Semi-Random Room-Maze Maker</h4><p>My game called Prongs has quite a few variations right now. The one with all the features, working name - Kitchen Sink, has walls that create rooms. Most of the board is hidden. Just the squares my pieces are on, and those adjacent to them, are revealed. The board right now is only 12 by 12. Not really enough squares to constitute a maze. Plus, we’re making rooms. A typical maze would just have paths that are one square wide. We need room to do battle in. Still, I think maze becomes a reasonable description when we hide most of the squares. Then you are venturing into unknown territory.</p><p>I read several articles online about maze generating algorithms, but I think in this particular circumstance it will be better to roll our own. I can envision starting in one corner, say the upper left, and creating a room made up of two walls and the left and top edges of the board. Then we’ll want to poke a hole in each wall. In order for the room to be unpredictable, we can choose the height and width using randomness within certain limits.</p><p>Here’s a beginning.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3a532021f536bdc5f8e617a42fa8d48/href">https://medium.com/media/e3a532021f536bdc5f8e617a42fa8d48/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*yNyeDXOdsADZO0wYTI-m_Q.png" /><figcaption>First Whack</figcaption></figure><p>Let’s make a few rooms in a row.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c1c568b656f2b664552544253ede92e/href">https://medium.com/media/6c1c568b656f2b664552544253ede92e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*Wbo_PM1wcTDP-F-xuMCTjQ.png" /><figcaption>If the random X values had been bigger, we would have crashed</figcaption></figure><p>When we’re within reach of the right wall, we need to just make a horizontal wall; don’t need the vertical wall to go with it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fa5b50a6f5898be3100c97010b25506/href">https://medium.com/media/3fa5b50a6f5898be3100c97010b25506/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*L0-RVLWINMjJh6PpV60Jfw.png" /><figcaption>Hit the Wall</figcaption></figure><p>Soon we will want to poke holes in these walls, but there’s a scenario we should take care of first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*FBvLynlxV1jE_JtadKX0nw.png" /><figcaption>The third room is more than 1 square taller than the previous room, leaving a gap</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/367210401774cf4e440841fb103a6ec9/href">https://medium.com/media/367210401774cf4e440841fb103a6ec9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jRiYBx_cty6h0ypVnBtSig.png" /></figure><p>In order for pieces to move from room to room, we’ll need some holes in those walls.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/115c9ed3d574b4bac6747c99ebb6db2f/href">https://medium.com/media/115c9ed3d574b4bac6747c99ebb6db2f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*G9Y-9phn2i0MjE30Fb1DyA.png" /><figcaption>Make a Hole in the Wall</figcaption></figure><p>The second row will be more complicated. It would be better not to put tall rooms right beneath other tall ones, and likewise to avoid putting short rooms right below the short ones. It would also be more interesting to have the widths of the rooms continue to be a bit random. Let’s try a random X location, then set the height of the new room according to the previous Y locations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e491660681187cc3b410f9b1dda6d62/href">https://medium.com/media/4e491660681187cc3b410f9b1dda6d62/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*q_DIV-XfVlAbwVSbR6vDNA.png" /></figure><p>We keep track of the Y values for each row of rooms in the lastRows array. Then when we make a room in the next row, we first randomly choose a width, then get the maximum Y value from the row above: let max = lastRows[currentRow -1][xTracker..&lt;xTracker + xStep].max()!</p><p>Let’s finish this middle row, then make a few more.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25730b5e176ff96c1b4b860f017fd6d0/href">https://medium.com/media/25730b5e176ff96c1b4b860f017fd6d0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*mw0dtiGC10pdhzaD3VG-ow.png" /><figcaption>Some middle rows of rooms</figcaption></figure><p>And finally, we do a similar thing on the bottom that we did on the right edge.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfcefa20aa0d2dd346051b4ecedb8410/href">https://medium.com/media/bfcefa20aa0d2dd346051b4ecedb8410/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*U1yucPb2Mjj3o6X89hGaVQ.png" /><figcaption>Final Room Maze</figcaption></figure><p>Now when the pieces are set up quasi-randomly on the board, we will dodge the wall, then hide most of the board. Then we can have infinite levels, each generated randomly.</p><p><strong>Coming up:</strong> in Chapter 8 we will randomly populate this room maze with both pieces and tools, then we will modify the game AI to be a little bit more engaging.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a78c6148f85c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI — Deeper Coding, Chapter 6]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-6-9ead2bb2b6eb?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ead2bb2b6eb</guid>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Fri, 31 Oct 2025 00:37:56 GMT</pubDate>
            <atom:updated>2025-11-04T17:03:15.794Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI — Game Coding, Chapter 6</h3><h4>How alpha/beta pruning optimizes the MinMax algorithm: aka, returning to recursion, aka creeping towards an AI solution</h4><p>How do we pick a move for the computer? To start with, we need a list of legal moves in a given board position.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f2da853381cee5e42b9504a539e0067/href">https://medium.com/media/3f2da853381cee5e42b9504a539e0067/href</a></iframe><p>This is a method inside a class called Board. For this chapter, we don’t need the details on how the class Manreturns a list of the legal moves it can make.</p><p>We could pick a move at random (an option I offer in my game). These methods are inside a class called MoveTree.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ce23dfee6c70ecf05a8b419f7ba1031/href">https://medium.com/media/6ce23dfee6c70ecf05a8b419f7ba1031/href</a></iframe><p>You can see that getBestMove()has four options; the random one is the simplest, and may make better moves than kamikaze.</p><p>Kamikaze calls the recursive method miniMax(), but in this case the search depth is zero, so it doesn’t recurse. Meaning that it will make all of the computer’s* moves and then evaluate the resulting board position and return the move with the best score. It won’t check any of red’s responses, so if it captures a low value piece (like a pawn in chess) with a high value piece (like a queen in chess), it will ignore the fact that this may result in the loss of the high value piece. Kamikaze, right?</p><p><em>*the computer plays the blue team, so I’ll just call it blue from now on</em></p><p>The most primitive evaluation of a board position is to add up the assigned values of your own pieces (material), then subtract the values of the enemy pieces. I will add another layer called mobility. You simply call generateMoves()and use the move count to represent mobility. Material is a lot more important, and permanent, than mobility, so when you assign value to material, make it 10 to 20 times more important. So in chess a pawn is worth 1, but it’s value is equivalent to a mobility of at least 10, usually. Mobility is more crucial before you enter the endgame, but the endgame gets searched more thoroughly, so we don’t necessarily need to lower the value of mobility in the endgame.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c453327f95daad988b68803f1a6be32e/href">https://medium.com/media/c453327f95daad988b68803f1a6be32e/href</a></iframe><p>Now we have the basis for a recursive algorithm called miniMax(). I built miniMax()in two pieces: an outer wrapper that returns the best move, and an inner recursive core that makes moves for each team until it reaches the search depth, then returns the score based on the best enemy moves for a given move. Once the core has been called for every move, the outer wrapper returns the move with the best score.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1fcf7d46861b2f89fc8d65357e7ed5e/href">https://medium.com/media/b1fcf7d46861b2f89fc8d65357e7ed5e/href</a></iframe><p>We clone the board and make our moves on the clone. This way we can just throw away the clone when we’re done with it instead of restoring the starting position, which would require a lot of work, such as, for example, restoring captured pieces. In some situations it may be more efficient to make the move on the real board and then undo the move after you’ve evaluated that branch of the tree. I haven’t done a comparison in my game, and I am putting that off until I decide whether it’s worth optimizing the move generator. Also, in SwiftUI, there is a possibility that the view subscribes to the board, in which case using the real board instead of a clone might move the pieces around in the view while calculating the best move*.</p><p><em>*that would probably look pretty cool!</em></p><p>Notice that miniMaxCore()ends with a negate()method. This was needed because in Swift, we are using Int.maxand Int.min, which when negated with a simple minus sign can cause numeric overflow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f222310056d9df09a3f56696d1a26a19/href">https://medium.com/media/f222310056d9df09a3f56696d1a26a19/href</a></iframe><p>So miniMax()makes each of your moves, then each opponent move in response and returns a negative value for the enemy move. And it keeps recursing, red — blue — red — blue, until it either detects the end of game, or it reaches the search depth. In my game, which has yet to be optimized for speed here, a search depth of 4 seems to take forever. So 3 works, but is kind of biased. 2 is the default. It’s not that smart. I personally like that. Most chess apps have to be extremely dumbed down for me to have a ghost of a chance.</p><p>This situation may be improved on when, in the future, I step over the line into genuine AI and try using the Monte Carlo Tree Search algorithm, which will somewhat arbitrarily follow some moves all the way to the end of game, and then try to leverage the results to pick the best paths to explore. In my opinion, using some algorithmic guesswork like this, rather than an algorithm that covers every move equally to whatever depth time and memory allow, is where we cross the line from calling the game brain an engine, to calling it AI.</p><p>In the meantime, there is a way to speed up miniMax(). The miniMax() algorithm does some searching that is pointless. In chess, if I make a move that loses my queen, do we want to keep searching enemy responses to find one that captures my rook? No. That branch of the search tree should be lopped off. That’s where alpha/beta pruning comes in. When you search for examples of this algorithm it will usually be called negamax().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22109b6c2505a4c511fd6665cbd7e287/href">https://medium.com/media/22109b6c2505a4c511fd6665cbd7e287/href</a></iframe><p>The key here, which saves so much time, is if bestScore ≥ beta { break }. So we can stop searching. This is where we know we’re not going to find any moves in the rest of this layer’s move list that will make a difference as to whether we choose this branch of the tree or not. We already have our answer, so let’s save some time. Both alpha and beta start at what is essentially infinity (alpha starts at negative infinity), and get reduced, get closer to each other in value, as moves get scored. The defaults in the input parameters, alpha: Int = Int.min, and beta: Int = Int.maxcan be confusing in a recursive situation. They are only used the first time in, at the top level.</p><p>In order to illustrate the step by step mechanics of negamax(), any real world example from a game will have way too much information to sift through in the form of a move tree. I tried doing an example using tic-tac-toe. Way too complicated! To explain this in a way that doesn’t take an hour or two to communicate, here’s a pic from a page on <a href="https://www.geeksforgeeks.org/dsa/minimax-algorithm-in-game-theory-set-4-alpha-beta-pruning/">geeksforgeeks.org</a> that has a thorough explanation*</p><p><em>*they show pseudocode and explain the mechanics in blow by blow detail</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*0kTy9XS9eExw8MRu5aFhAg.jpeg" /><figcaption>This is position A, after one move was made from the engine’s legal move list</figcaption></figure><p>Blue is exploring one move, A, in its generated move list. The human (red) has two moves: B and C. If red makes move B, then the game engine (blue) has two responses: D and E. If blue moves D, red reaches the search depth limit with its next two moves that have a value of 3 and 5. Blue assumes that red will make its best move: 5. So blue tries moving E. Red has two moves. The first one is 6, blue doesn’t care about the second one. He already knows E is going to be a worse choice for him than D based on the value of 6, so blue is going to pick D which has a max of 5. Doesn’t matter if E could go up to 9, we don’t care, because 6 is already worse for us than 5.</p><p>The miniMax()algorithm would go ahead and calculate E, seeing if it goes up to 9. Alpha/beta pruning, aka negamax(), will skip it. The search results will be the same for both algorithms, but negamax() is significantly faster. In some situations this will allow a greater search depth which will give smarter results. But my game isn’t there yet.</p><p>The other branch, move C, will result in a value of 2. So blue, looking for the realistic result of move A, picks the worst case scenario when it is playing its best, which will be red making move B, where red’s best fighting gives it a score of 5. Blue goes on to score the rest of its list and picks the move where red’s score is the lowest.</p><p>This is one of the more complicated algorithms I have thus far encountered, but I am guessing that the Monte Carlo Tree Search algorithm will put this to shame, at least in the realm of complexity. If you still want more clarity in uderstanding the blow by blow mechanics here, go ahead and surf over to <a href="https://www.geeksforgeeks.org/dsa/minimax-algorithm-in-game-theory-set-4-alpha-beta-pruning/">geeksforgeeks.org</a> and read their details.</p><p><strong>Coming up:</strong> in Chapter 7 we will explore how to create a semi-random maze made out of rooms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ead2bb2b6eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI — Deeper Coding, Chapter 5]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-5-55bbb0d0ddaa?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/55bbb0d0ddaa</guid>
            <category><![CDATA[chess-variants]]></category>
            <category><![CDATA[ghosts]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ai-image-generation]]></category>
            <category><![CDATA[deeper-coding]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Thu, 30 Oct 2025 01:48:46 GMT</pubDate>
            <atom:updated>2025-11-04T17:02:56.700Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI — Game Coding, Chapter 5</h3><h4>Getting Ghosted</h4><p>If you read <a href="https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-3-9c64ca0f568e">Chapter 3</a>, in the last paragraph I came up with the idea of a ghost tool. The beginning of <a href="https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-4-d80a45a070e1">Chapter 4</a> also mentions this. It started as an excuse, and was followed by more excuses for not writing about it (and implementing it).</p><p>I had already made some pretty good ghost costume images on midjourney.com. But the image I liked was of the costume being worn. I need to take that further and see if I can get it to create a ghost costume that is waiting to be worn. Then a piece can pick it up and wear it.</p><p>My first prompt worked well enough that I’m going to try some editing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*JWXDooqw1YfokFhpFijW-w.png" /><figcaption>Prompt: cartoon outline a sheet that is a ghost costume not being worn</figcaption></figure><p>I tried the one on the left, keeping the background, and I didn’t like it. It didn’t look too good when it was shrunk down to fit in a square on the board, not to mention when a piece is carrying it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/115/1*U-slCclrvl3viA662Qz7aw.png" /><figcaption>First try, ghost on square</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/114/1*W7NdWQ5U-erZEjr0mbR4Ug.png" /><figcaption>First try, piece carrying a ghost</figcaption></figure><p>Let’s see how the second one works. This time it should be easier to erase the background.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/1*VF9zm5MoafCe_UrOQaOMrQ.png" /><figcaption>Second try</figcaption></figure><p>Better, but still doesn’t shrink well. Number three is the most cartoony, I think it might work best.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/123/1*930B0vo8nS6ZaQojTG0_rA.png" /><figcaption>Best yet</figcaption></figure><p>The edges need more cleaning up, but it’s good enough for now. I like the facial expression. I think instead of my idea that the pieces should wear the ghost costume, I’ll save that for a possible later refinement stage, and instead have them carry the ghost like any other tool, but draw the piece with .opacity(0.5), or whatever percentage looks best.</p><p>Here’s a piece carrying a ghost next to one that’s not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*5VS_9nVsXviAF7j6lfANzA.png" /><figcaption>The opacity looks ghost-like at 0.3</figcaption></figure><p>Now I go into the code that generates moves. It doesn’t allow moving into or through a wall. So instead of determining only if the target square is a wall, I’ll also need to check for the ghost tool.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6326395611e9f75e620c076dc68f300/href">https://medium.com/media/d6326395611e9f75e620c076dc68f300/href</a></iframe><p>I like the opacity of 0.3, but when the piece is inside a wall it needs to be darker. So here is a view modifier that takes that into account.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/183c39b69738466061a5593983d0b5f4/href">https://medium.com/media/183c39b69738466061a5593983d0b5f4/href</a></iframe><p>Here’s a ghost in a wall.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*Hvf7E5D4xWAnGdCCTf_iLQ.png" /><figcaption>Ghost in a wall, opacity 0.9</figcaption></figure><p>It’s still pretty faint, but that keeps it ghostly. Notice that the blue piece got scared off.</p><p>After making those changes it seemed like I should change my initial rules for the ghost tool. My first idea was that when a piece enters the ghost world by picking up a ghost tool, that they can’t capture (or wound) pieces in the normal world, and vice-versa. They could do battle with enemy ghost pieces, but they’d have to drop their ghost tool to re-enter the normal world.</p><p>But that requires a fair amount of logic. So when I got the views working without that logic, I moved my ghost piece into a wall that was next to an enemy, and he had to run away, kind of like he was scared. So I think what I will implement is that ghosts interact normally, but they have the advantage of not being attackable when they are in a wall. And I think I will allow them to move into bomb squares as well without setting off the bomb. When, starting inside a wall or a bomb, they capture another piece, I think they should lose their ghost tool, thus limiting the power of the ghost tool to something reasonable. If they only wound the piece they get to stay a ghost. And an enemy should be able to attack them on a bomb square and blow both the ghost and itself up <em>(hi Winston!)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*NBuT-5nLIqYq2-HB19D1cw.png" /><figcaption>Ghost occupying a bomb square</figcaption></figure><p>Quite a few changes were needed in view code to allow pieces to move into and through walls. Previously for example, there was no tap gesture attached to a wall square or a bomb square. It was a surprise to me that attaching the normal tap gesture worked fine; I realized that anyone without a ghost would behave normally. A normal piece couldn’t start on that square, and also couldn’t end its move on (or through) a wall.</p><p>I’ve been reading Paul Hudson’s book, Everything but the Code, to learn about monetizing apps. I think this last feature has convinced me to enter the home stretch and put this game in the app store. I’m sure it will take me a month to get all my ducks in a row. So the next chapter I write will be about the engine that lets the app pick the best move. It’s not really AI, but it’s getting close…</p><p><strong>Coming up:</strong> adding Alpha-Beta Pruning to the MinMax algorithm</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55bbb0d0ddaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI — Deeper Coding, Chapter 4]]></title>
            <link>https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-4-d80a45a070e1?source=rss-3f0399ccbb78------2</link>
            <guid isPermaLink="false">https://medium.com/p/d80a45a070e1</guid>
            <category><![CDATA[chess-variants]]></category>
            <category><![CDATA[deeper-coding]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Eric Ford Consulting]]></dc:creator>
            <pubDate>Tue, 28 Oct 2025 23:16:36 GMT</pubDate>
            <atom:updated>2025-11-04T17:02:39.481Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI — Game Coding, Chapter 4</h3><h4>Recursion</h4><p>At the end of <a href="https://medium.com/@ericfordconsulting/swiftui-deeper-coding-chapter-3-9c64ca0f568e">Chapter 3</a>, as I was trying to excuse my way out of not investing a lot of coding into fixing a small nitpick having to do with the way the line between adjoining squares that have a brick wall pattern kind of interrupts the flow of the bricks, I invented a scenario where we needed those dividing lines: a ghost tool that, when a piece is carrying it, they can walk through walls, or just occupy the wall itself.</p><p>This is a long preamble to something I am not going to be writing about in this article. I’m about 51% confident that I should go ahead and write this chapter on recursion first. Part of the reason for this is that when I started asking around for ideas about what kind of tools would allow a piece to become a ghost, but just temporarily, until they let go of the tool, my daughter Nisha gave me the idea that it should be a sheet; like a halloween costume. I will need to figure out how to draw a sheet on a canvas, and then perhaps make the ghost piece have partial opacity, and the sheet would be on top of the piece, and perhaps have less opacity (or is it more? anyway, be more see-through).</p><p>Then I’ll need to write the logic to allow the piece to walk through walls. Okay, I’m very excited, but I’m also determined to keep up my momentum in writing these articles, so, recursion it is. Just so you know, I found this recursive feature to be a very interesting challenge and lots of fun.</p><p>The need arose when I wanted to hand off tools from one piece to another piece on the same team, but the target piece had to be on an adjacent square. Not very difficult. You can hand off tools as many times as you like without using up your tune. But it’s annoying to have to pass the tool one square at a time if you had, say, a chain of adjoining pieces and wanted to pass it to a piece many links down that chain.</p><p>Here’s a pic just before we hit our problem… no, I mean, uh, recursion opportunity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*S168L84mgYpMegn-0vA4-g.png" /><figcaption>Just before we decide to handoff the sword</figcaption></figure><p>Kind of jumping into the deep end here… let me explain some of what you’re seeing. The piece you see on the white square is the piece that’s been selected. The squares that are showing red frames, the ones right next to the selected piece, are the squares the selected piece can legally move to*.</p><ul><li><em>sorry Winston **</em></li><li><em>** see Chapter 2</em></li></ul><p>The gray squares (or for you Brits, the grey squares), are hidden squares. There are plenty of enemy (blue) pieces in there, and also some bombs. When you (red) make a move, any hidden squares touching the square you landed on (and, perhaps obviously, the square you landed on) get revealed.</p><p>Two of these graphics were generated by AI (midjourney.com): the dynamite and the candle. These will be replaced with some code drawing on a canvas. I’m not averse to using AI generated graphics. In fact, the upcoming ghost tool is looking promising.</p><p>Below the game board are pics of a sword and a candle. Those are tools that the selected piece is carrying right now. The sword has a red frame around it showing that we have selected that tool. When we press the Hand Off button, we want circle outlines to appear around the friendly pieces we can hand off to* <em>(see above)</em>.</p><p>Here’s what that will look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*b7gGNKyXL4Co29L1vUq9dg.png" /><figcaption>Hand Off button has been pressed</figcaption></figure><p>The pic of the sword below the game board now has a lilac colored circle background, and the pieces that can receive the sword have lilac colored circular frames around them. Two of those pieces are adjacent to the selected piece that has the sword. That would be easy to code. The other two pieces form a chain of pieces that are touching. That’s where the recursion will come in.</p><p>But recursion wasn’t my first idea. First I thought I could move outward in rings and mark each square that was part of a chain of friendly pieces. This was the first time in a long time that I felt I needed to walk away from the screen and brainstorm with a pencil and paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VqwcwedlxOpZf9Pug_81GA.png" /><figcaption>Old fashioned designing</figcaption></figure><p>I wrote some preliminary code that wasn’t too dependent on the algorithm I would later choose.</p><p>First we need a couple of arrays to keep track of the status of our squares.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9cfeb497a2543552c93ecb5a5ab9d64/href">https://medium.com/media/a9cfeb497a2543552c93ecb5a5ab9d64/href</a></iframe><p>We’re doing this coding in BoardView.ViewModel, though currently that file is way too big and subject to (even more) refactoring. Here’s code to respond to the Hand Off button. We set an instance variable, then take action if doitis true, otherwise erase any previous results.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d48d8b6715bbc996daf4d1c4b4fbbfcf/href">https://medium.com/media/d48d8b6715bbc996daf4d1c4b4fbbfcf/href</a></iframe><p>Here is code to mark adjacent squares.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6436840a7cfe78ff0039a62bb7231db0/href">https://medium.com/media/6436840a7cfe78ff0039a62bb7231db0/href</a></iframe><p>Next we need to return an array of handoffables that connect to a given square. Handoffables will be tuples containing file and rank: (Int, Int). They are not actually defined in code; in the code they’re just tuples. Since we need to append to the array deep inside the markSquare()code, let’s combine those two methods above into a new method that returns an array. The first method gets the neighbors for a single square. At the bottom we’ll add another method that takes an array as input and concatenates the handoffables for each square in the input array and then returns that as an array. This is a setup for what we need in order to do recursion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfb766751a199ac20c8c723148ab4da6/href">https://medium.com/media/dfb766751a199ac20c8c723148ab4da6/href</a></iframe><p>Now that this is taking shape, recursion becomes the most readily apparent solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03180c280ffef761fbcc987224817f0d/href">https://medium.com/media/03180c280ffef761fbcc987224817f0d/href</a></iframe><p>moveStart is a tuple, an instance variable in the ViewModelthat gets filled when a piece is selected. It’s a little misleading that we populate handoffables with moveStartin order to prime the pump, but it’s worth it to have that be the name of what gets returned, and also checked for empty.</p><p>So in each iteration, we get all the handoffable adjacent squares, meanwhile marking them as such as we go along, until there aren’t any more.</p><p>Then we will need a view modifier to show the handoffable squares.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c49ebeefa7bfe9dc3d00c0f9a4dfd81a/href">https://medium.com/media/c49ebeefa7bfe9dc3d00c0f9a4dfd81a/href</a></iframe><p>And finally we add that modifier to the square view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/494434c2faafefb3dd8d81894806adc3/href">https://medium.com/media/494434c2faafefb3dd8d81894806adc3/href</a></iframe><p>This is surrounded by a lot of view code, but we’ll save those details for another chapter.</p><p><strong>Coming up:</strong> in Chapter 5 we will cover our much anticipated Ghost feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OQvDxhyJZ-U2KvrqX9ipjQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d80a45a070e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>