{"id":630,"date":"2008-09-16T21:17:45","date_gmt":"2008-09-16T11:17:45","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=630"},"modified":"2008-12-26T12:52:22","modified_gmt":"2008-12-26T01:52:22","slug":"solving-the-circuit-game","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/16\/solving-the-circuit-game\/","title":{"rendered":"Solving the Circuit Game"},"content":{"rendered":"<h4>The Puzzle Solver Framework<\/h4>\n<p>Regular readers will be familiar with my approach to <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2005\/05\/21\/puzzling-over-puzzles\/\">puzzle-solving<\/a>, and will remember a number of <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2006\/02\/12\/a-kakuro-solver\/\">different<\/a> <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2005\/05\/21\/21\/\">puzzles<\/a> <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2005\/06\/12\/the-no-nos-and-yes-yeses-of-the-nonogram-solution\/\">that<\/a> <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2006\/09\/28\/solitaire-battleships\/\">I<\/a> <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/01\/12\/solving-slitherlinks-with-software\/\">automated<\/a> in the past.<\/p>\n<p>Back in January, <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/01\/19\/509\/\">I created a framework<\/a> to solve these types of puzzles. At the time, I wrote in the conclusion: <\/p>\n<blockquote><p>It still needs some work, and the proof of a framework isn\u00e2\u20ac\u2122t in the first application that uses it, but in the third, so I am not crowing about it too much yet.<\/p><\/blockquote>\n<p>Well, eight months later, I am one step closer to that, having used the framework to tackle the <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/13\/the-circuit-game-puzzle\/\">Circuit Game puzzle<\/a> &#8211; the second application to use it.<\/p>\n<h4>The Game Objects<\/h4>\n<p>The key part to using the framework, is working out how the puzzle can be decomposed into individual objects. <\/p>\n<p>Typically, these objects store a small amount of local state and references to the neighbourhood other similar objects they interact with. They also maintain a set of possible solution values. They are in charge of enforcing a simple set of constraints, whittling down the set of possible values until only one remains. Finally, they are responsible for letting their neighbours know about changes in their state, so the neighbours can correspondingly re-apply their constraints.<\/p>\n<p>Thus, the constraints are propagated across neighbourhoods, peer-to-peer, with little or no oversight from over-arching objects.<\/p>\n<h4>Game Objects in the Circuit Game<\/h4>\n<p>In this puzzle, each square (or cell) represented a game object.<\/p>\n<p>Each cell was responsible for maintaining its shape (e.g. Tee, Corner, etc.) This was implemented through a class hierarchy. <\/p>\n<p>Each cell was responsible for tracking its four neighbours.<\/p>\n<p>Each cell was also responsible for maintaining a list of compass directions and its disposition on whether or not a wire was exiting the cell in that direction. For each direction, the disposition could be that a wire <em>must<\/em>, <em>must not<\/em> or <em>might<\/em> be leaving the cell in that direction. Below, I describe how this was calculated.<\/p>\n<p>For the subset of shapes that could be rotated (e.g. Corner, Line, Bulb, and Tee cells, but not Cross or Empty cells), each shape was responsible for maintaining the list of valid orientations. <\/p>\n<p>By iterating through each of the valid orientations, and examining the resulting directions that the wires pointed, it was possible to mark the disposition of each compass direction. If all remaining orientations had a wire pointing up, then the disposition was <em>must<\/em>. If none of them did, it was <em>must not<\/em>. If some did, and some didn&#8217;t, it was <em>might<\/em>.<\/p>\n<p>Each of the rotatable shapes were also responsible for maintaining the key constraint in the puzzle &#8211; that this cell&#8217;s disposition of a wire, was compatible with its neighbour&#8217;s disposition of the same wire.<\/p>\n<p>For example, if my neighbour to the left thinks that there <em>must<\/em> be a wire pointing to its right (e.g. back to me), then I <em>must<\/em> have a wire pointing to my left. Any orientation of my shape that doesn&#8217;t correspond to that can be eliminated from consideration.<\/p>\n<p>To simplify the bounds-checking, I surrounded the entire puzzle in a border of Empty Cells. These cells never contacted their neighbours, so they didn&#8217;t peer past the limit of the grid. That isn&#8217;t the most sophisticated of solutions for dealing with boundary conditions, but it worked.<\/p>\n<h4>Power\/Cycles<\/h4>\n<p>As it stands, this only enforces the rules in Rotate<sup>2<\/sup> (See the <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/13\/the-circuit-game-puzzle\/\">original article<\/a> for the differences.)<\/p>\n<p>I expected that I would need to implement a global property to enforce that there should be no cycles. This supports the concept of power cells.<\/p>\n<p>I even had a comment in the code pointing out where such functionality would go.<\/p>\n<p>In practice, I found that it wasn&#8217;t required to solve the puzzles that were posed by the web-sites.<\/p>\n<p>In theory, there could exist a puzzle that this engine couldn&#8217;t solve, because it would see two possible ambiguous solutions (one false solution containing a cycle, and one without). In practice, I haven&#8217;t seen one. It remains an open challenge to come up with such a puzzle.<\/p>\n<p>[<strong>Stop Press<\/strong>: It is <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/17\/example-of-ambiguity-in-circuit-puzzle\/\">no longer open<\/a>]<\/p>\n<h4>Did it work?<\/h4>\n<p>I mentioned in the original article that these puzzles didn&#8217;t seem to have the complexity of Slitherlinks and the like. This was borne out in the run-time; these test puzzles I encoded were all solved by the engine in sub-second timeframes, even with basic graphical animation.<\/p>\n<p><a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/wp-content\/uploads\/2008\/09\/circuitpuzzlesolution_example.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.somethinkodd.com\/oddthinking\/wp-content\/uploads\/2008\/09\/circuitpuzzlesolution_example-300x206.jpg\" alt=\"Example of solved Circuit Puzzle\" title=\"Example of solved Circuit Puzzle\" width=\"300\" height=\"206\" class=\"size-medium wp-image-631\" srcset=\"https:\/\/www.somethinkodd.com\/oddthinking\/wp-content\/uploads\/2008\/09\/circuitpuzzlesolution_example-300x206.jpg 300w, https:\/\/www.somethinkodd.com\/oddthinking\/wp-content\/uploads\/2008\/09\/circuitpuzzlesolution_example.jpg 605w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This is an example of a solved puzzle; this puzzle is the one shown in my original <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/13\/the-circuit-game-puzzle\/\">Circuit Puzzle<\/a> article.<\/p>\n<p>These puzzles can be crossed off the list as beaten.<\/p>\n<h4>How did the framework go?<\/h4>\n<p>I initially used the same version of the framework as the <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/01\/19\/509\/\">Alphametics puzzle<\/a>. I found a couple of trivial bugs, with single character corrections, and made no changes to the interface.<\/p>\n<p>I found it took me a little while to remember the concepts; I need to add some documentation to give me clues to get my head back into the right context to use it.<\/p>\n<p>Related to that, I found that when I missed a tiny piece of necessary plumbing that my game objects were responsible for including, the whole object model just sat there. I couldn&#8217;t work out why nothing at all was happening &#8211; the framework couldn&#8217;t see that there was any work to do. I eventually added the missing line in, and the whole thing jumped to life.<\/p>\n<p>The <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/01\/04\/message-passing-in-my-puzzle-solving-architecture\/\">Command Queue<\/a> which I spent so much pondering about in the past, was completely in the background. I didn&#8217;t give it a moment&#8217;s thought; the framework just took care of it.<\/p>\n<p>So the framework was very successful, as is.<\/p>\n<p>Once I finished, however, I went back and made some improvements based on what I had learned.<\/p>\n<p>Firstly, I incorporated <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2008\/09\/12\/animal-fight-a-question-about-oo-mvc\/#comment-144962\">Aristotle&#8217;s suggestion<\/a> on View registration in the MVC pattern. That removed some of the more cumbersome code from the client. While it added extra calls to the View class, those calls were optional, and only have to be supported by game implementations that find the functionality useful. So the additional complexity was optional, and shouldn&#8217;t break the existing Alphametics code.<\/p>\n<p>That was the biggest change, and it was a fairly minor one.<\/p>\n<p>I also realised that I had copy-and-pasted a slab of code from the Alphametics solution. It wasn&#8217;t a large slab, but it was a complex one (dealing with PyGame events, and passing them on the the appropriate View object), so I was happy to factor it out, and add it into to a new subclass in the View.<\/p>\n<p>I&#8217;m aware that these changes aren&#8217;t exactly riveting reading, but the conclusion I want<br \/>\nyou to draw from that is that the framework did very well.<\/p>\n<p>For what it is worth, there was about 300 lines of Python code (including comments and blank lines) in the Circuit Puzzle Model, 200 lines in the View and about 30 in the Controller.<\/p>\n<h4>Conclusion<\/h4>\n<p>Ummm&#8230; My conclusions were&#8230; errr&#8230; that I concluded that&#8230; ahhh&#8230; in light of this experience&#8230; let&#8217;s see&#8230;<\/p>\n<p>Hooray for code re-use?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another puzzle solved with the same architecture as before, but this time, with added code re-use.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[33,34],"tags":[55,330,373,69,232],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-puzzle-solving","category-software-development","tag-puzzle","tag-puzzle-game-framework","tag-puzzle-solving","tag-software","tag-software-architecture"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":6,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/630\/revisions\/899"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}