<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[A Jigsaw Puzzle]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>This is a fun little program that cuts a picture into pieces, splatters them around the screen, then lets the user put them back together by pushing them around with the mouse. Here are the Plain English type definitions:</p>
<p>A puzzle is a thing with a box, a picture and some pieces.<br />
A piece is a thing with a box and a picture.</p>
<p>And here is the main routine:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Create a puzzle from &#8220;c:\g4g articles\jigsaw\maga.jpg&#8221;.</span><br />
<span style="color:#00ccff;"> Display the puzzle.</span><br />
<span style="color:#00ccff;"> Show the arrow cursor.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Deque an event.</span><br />
<span style="color:#00ccff;"> If the event is nil, break.</span><br />
<span style="color:#00ccff;"> If the event&#8217;s kind is &#8220;key down&#8221;, break.</span><br />
<span style="color:#00ccff;"> If the event&#8217;s kind is not &#8220;left click&#8221;, repeat.</span><br />
<span style="color:#00ccff;"> Track the mouse on the puzzle.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Destroy the puzzle.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p>These are the Plain English routines that create a puzzle:</p>
<p><span style="color:#00ccff;">To create a puzzle from a path:</span><br />
<span style="color:#00ccff;"> Allocate memory for the puzzle.</span><br />
<span style="color:#00ccff;"> Make the puzzle&#8217;s box 8 inches by 8 inches.</span><br />
<span style="color:#00ccff;"> Center the puzzle&#8217;s box in the screen&#8217;s box.</span><br />
<span style="color:#00ccff;"> Fetch the puzzle&#8217;s picture from the path.</span><br />
<span style="color:#00ccff;"> Resize the puzzle&#8217;s picture to 8 inches by 8 inches.</span><br />
<span style="color:#00ccff;"> Center the puzzle&#8217;s picture in the puzzle&#8217;s box.</span><br />
<span style="color:#00ccff;"> Cut the puzzle into pieces.</span><br />
<span style="color:#00ccff;"> Splatter the puzzle&#8217;s pieces.</span></p>
<p><span style="color:#00ccff;">To cut a puzzle into pieces:</span><br />
<span style="color:#00ccff;"> Draw the puzzle&#8217;s picture.</span><br />
<span style="color:#00ccff;"> Put the puzzle&#8217;s left-top into a spot.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Allocate memory for a piece. Append the piece to the puzzle&#8217;s pieces.</span><br />
<span style="color:#00ccff;"> Put the spot and the spot plus 2 inches into the piece&#8217;s box.</span><br />
<span style="color:#00ccff;"> Extract the piece&#8217;s picture using the piece&#8217;s box.</span><br />
<span style="color:#00ccff;"> Add 2 inches to the spot&#8217;s left.</span><br />
<span style="color:#00ccff;"> If the spot&#8217;s left is less than the puzzle&#8217;s right, repeat.</span><br />
<span style="color:#00ccff;"> Put the puzzle&#8217;s left into the spot&#8217;s left.</span><br />
<span style="color:#00ccff;"> Add 2 inches to the spot&#8217;s top.</span><br />
<span style="color:#00ccff;"> If the spot&#8217;s top is the puzzle&#8217;s bottom, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p><span style="color:#00ccff;">To splatter some pieces:</span><br />
<span style="color:#00ccff;"> Make a box 1 inch smaller than the screen&#8217;s box.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get a piece from the pieces.</span><br />
<span style="color:#00ccff;"> If the piece is nil, break.</span><br />
<span style="color:#00ccff;"> Pick a spot anywhere in the box.<br />
Round the spot to the nearest multiple of 1/4 inch.</span><br />
<span style="color:#00ccff;"> Center the piece&#8217;s box on the spot.</span><br />
<span style="color:#00ccff;"> Center the piece&#8217;s picture on the spot.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>This is how we draw a puzzle’s pieces on the screen:</p>
<p><span style="color:#00ccff;">To display a puzzle:</span><br />
<span style="color:#00ccff;"> Clear the screen without refreshing.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get a piece from the puzzle&#8217;s pieces.</span><br />
<span style="color:#00ccff;"> If the piece is nil, break.</span><br />
<span style="color:#00ccff;"> Draw the piece&#8217;s picture.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Refresh the screen.</span></p>
<p>And this is typical of how the splattered pieces of a puzzle look:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-333035" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/jigsaw-2-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>These are the routines we use to track the user’s mouse movements:</p>
<p><span style="color:#00ccff;">To track the mouse on a puzzle:</span><br />
<span style="color:#00ccff;"> Find a piece of the puzzle given the mouse&#8217;s spot.</span><br />
<span style="color:#00ccff;"> If the piece is nil, exit.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> If the mouse&#8217;s left button is up, exit.</span><br />
<span style="color:#00ccff;"> Put the mouse&#8217;s spot into a spot.</span><br />
<span style="color:#00ccff;"> Round the spot to the nearest multiple of 1/4 inch.</span><br />
<span style="color:#00ccff;"> Center the piece&#8217;s box on the spot.</span><br />
<span style="color:#00ccff;"> Center the piece&#8217;s picture on the spot.</span><br />
<span style="color:#00ccff;"> Display the puzzle.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p><span style="color:#00ccff;">To find a piece of a puzzle given a spot:</span><br />
<span style="color:#00ccff;"> Get the piece from the puzzle&#8217;s pieces (backwards).</span><br />
<span style="color:#00ccff;"> If the piece is nil, exit.</span><br />
<span style="color:#00ccff;"> If the spot is not in the piece&#8217;s box, repeat.</span></p>
<p>Note that we “snap” the pieces to a 1/4-inch grid so they’ll fit together easily. Note also that we search for pieces in the opposite order they are drawn (ie, backwards) so that the front-most piece gets selected first.</p>
<p>This is what the pieces of our sample puzzle look like once they’ve been properly arranged:</p>
<p><img loading="lazy" class="alignnone size-large wp-image-333042" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/jigsaw-3-1024x819.jpg" alt="" width="665" height="532" /></p>
<p>It’s a painting I did a while back. I called it “Make America GREEN Again.”</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i0.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/jigsaw-2-1024x819.jpg?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[413]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>