<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tapdancing Goats &#187; Games</title>
	<atom:link href="http://www.tapdancinggoats.com/category/games/feed" rel="self" type="application/rss+xml" />
	<link>http://www.tapdancinggoats.com</link>
	<description>A parfait of physics, Linux, LaTeX, and coding tips large and small.</description>
	<lastBuildDate>Wed, 25 Jan 2012 03:14:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Puzzle 1</title>
		<link>http://www.tapdancinggoats.com/puzzle-1.htm</link>
		<comments>http://www.tapdancinggoats.com/puzzle-1.htm#comments</comments>
		<pubDate>Sat, 22 Oct 2011 08:13:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.tapdancinggoats.com/?p=740</guid>
		<description><![CDATA[Rules and hints: The solution is one word. Post a comment with your answer. I will moderate comments so incorrect solutions will not appear. Feel free to put a link to your profile or contact information in the comment with your solution, so people that are impressed with your puzzle skillz can find you. Don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Rules and hints:</p>

<ul>
<li>The solution is one word.</li>
<li>Post a comment with your answer.  I will moderate comments so incorrect solutions will not appear.</li>
<li>Feel free to put a link to your profile or contact information in the comment with your solution, so people that are impressed with your puzzle skillz can find you.</li>
<li>Don&#8217;t submit more than one solution per day or you will be disqualified.</li>
<li>If I get more than one solution within a few days, I will post them all.</li>
<li>After a solution is posted, comments will be closed.</li>
</ul>

<p><a href="http://www.tapdancinggoats.com/wp-content/uploads/2011/10/puzzle1.png"><img src="http://www.tapdancinggoats.com/wp-content/uploads/2011/10/puzzle1.png" alt="" title="puzzle1" width="320" height="512" class="aligncenter size-full wp-image-741" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tapdancinggoats.com/puzzle-1.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fox &amp; Geese in Haskell: Part 2</title>
		<link>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-2.htm</link>
		<comments>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-2.htm#comments</comments>
		<pubDate>Tue, 28 Jun 2011 03:53:45 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[fox and geese]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[monads]]></category>

		<guid isPermaLink="false">http://www.tapdancinggoats.com/?p=684</guid>
		<description><![CDATA[Last time we looked at how the board is represented in the little Fox &#38; Geese game I wrote in Haskell. This time, I&#8217;ll cover the machinery that makes the game go: moving pieces, jumping, and validating moves. The code for this part is available in my GitHub repo under tag v0.1.2. To simplify the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tapdancinggoats.com/fox-geese-in-haskell-part-1.htm">Last time</a> we looked at how the board is represented in the
little <a href="http://en.wikipedia.org/wiki/Fox_and_geese">Fox &amp; Geese</a> game I wrote in <a href="http://www.haskell.org/">Haskell</a>.  This
time, I&#8217;ll cover the machinery that makes the game go: moving pieces,
jumping, and validating moves.  The code for this part is available in
<a href="https://github.com/shadwstalkr/fox_and_geese">my GitHub repo</a> under tag <em>v0.1.2</em>.</p>

<p><span id="more-684"></span></p>

<p>To simplify the game mechanic functions, we&#8217;ll carry the game state
around in a <code>State</code> monad called <code>GameStateM</code>.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="kw1">type</span> GameStateM a = State GameState a<br />
&nbsp;</div>

<p>This is created by the input functions that we&#8217;ll see later.</p>

<p>The first thing we want is a high-level function that can be used to
move a piece.  This is easy to describe in plain English: if the move
from space A to space B is valid, move the piece on the board and
remove any piece that was jumped, then it&#8217;s the next player&#8217;s turn.
It is almost as easy to write in Haskell:</p>

<div class="dean_ch" style="white-space: wrap;"><br />
movePiece :: Position -&gt; Position -&gt; GameStateM <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
movePiece from to = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>valid, jumped<span class="br0">&#41;</span> &lt;- isMoveValid from to<br />
&nbsp; when valid $ <span class="kw1">do</span><br />
&nbsp; &nbsp; oldBoard &lt;- gets board<br />
&nbsp; &nbsp; <span class="kw1">let</span> newBoard = jumpPiece jumped . move $ oldBoard<br />
&nbsp; &nbsp; modify $ \game -&gt; game <span class="br0">&#123;</span>board = newBoard<span class="br0">&#125;</span><br />
&nbsp; &nbsp; switchPlayer<br />
&nbsp; selectPosition Nothing<br />
&nbsp;</div>

<p>We&#8217;ll look at <code>isMoveValid</code> soon.  When the move is valid, this gets the board from the current state, makes a new board with the pieces moved, then puts the new board back in the state.  The function <code>gets board</code> maps the projection <code>board</code> into the state,
so it is equivalent to <code>get &gt;&gt;= return . board</code>.  We put the new board
back into the state with <code>modify</code>, which takes a function that
modifies the current state.  So <code>modify f</code> is equivalent to
<code>get &gt;&gt;= put . f</code>.  We&#8217;ll look at <code>switchPlayer</code> and <code>selectPosition</code>
in the part about input.</p>

<p>Now, let&#8217;s define <code>move</code> and <code>jumpPiece</code>.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
move board&#8217; = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maybe"><span class="kw3">maybe</span></a> board&#8217; <span class="br0">&#40;</span>boardAfterMove board&#8217;<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>getPiece&#8217; from board&#8217;<span class="br0">&#41;</span><br />
<br />
boardAfterMove board&#8217; piece = Map.insert to piece <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . Map.delete from $ board&#8217;<br />
&nbsp;</div>

<p>In <code>move</code> we first try to get the moving piece from the board.  If it
existed, we return the board with the piece moved.  To do that, we
delete the piece from the old position and insert it at the new one.</p>

<p>Next, we take a leap.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="co1">&#8211; jumpPiece :: Maybe Position -&gt; Board -&gt; Board</span><br />
jumpPiece Nothing = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span class="kw3">id</span></a><br />
jumpPiece <span class="br0">&#40;</span>Just pos<span class="br0">&#41;</span> = Map.delete pos<br />
&nbsp;</div>

<p>Notice that these are partial functions.  If the position that was
jumped is <code>Nothing</code>, i.e. no jump, then don&#8217;t modify the board.
Otherwise, delete the piece that was jumped.</p>

<p>Now the hard part: checking if the move is valid.  We have a number of
things to check here, so let&#8217;s break it down.  A move is valid if:</p>

<ul>
<li>The destination is on the board</li>
<li>The destination space is empty</li>
<li>If the piece that is moving is a goose, then it must move forward</li>
<li>The spaces have to be adjacent or a jump</li>
<li>A jump can only occur if the destination is two spaces away from the
start along a line, and the moving piece is a fox, and the space
jumped had a goose, and the first two rules still apply</li>
</ul>

<p>Whew, that&#8217;s a lot to check.  Let&#8217;s start.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
isMoveValid :: Position -&gt; Position -&gt; GameStateM <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span class="kw4">Bool</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span class="kw4">Maybe</span></a> Position<span class="br0">&#41;</span><br />
isMoveValid from@<span class="br0">&#40;</span>fx, fy<span class="br0">&#41;</span> to@<span class="br0">&#40;</span>tx, ty<span class="br0">&#41;</span> = <span class="kw1">do</span><br />
&nbsp; Just movingPiece &lt;- gets $ getPiece from<br />
&nbsp; destinationIsEmpty &lt;- <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span class="kw3">not</span></a> . Map.member to<span class="br0">&#41;</span> &lt;$&gt; gets board<br />
&nbsp; isJump &lt;- isGoose . jumpedPos $ movingPiece<br />
&nbsp; <br />
&nbsp; valid &lt;- <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:and"><span class="kw3">and</span></a> <span class="br0">&#91;</span>onBoard,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;movingPiece == Fox || movingForward,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;destinationIsEmpty,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isAdjacent || isJump<span class="br0">&#93;</span><br />
<br />
&nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> <span class="br0">&#40;</span>valid, jumpedPos movingPiece<span class="br0">&#41;</span><br />
&nbsp;</div>

<p>We&#8217;ll need to know what piece is moving in a few places, so get it
here.  It also helps to get what we need from the state here, so we
can define helper functions which aren&#8217;t in the state monad.  Next, we
check if the destination is empty with <code>Map.member</code>.  Remember, we
don&#8217;t store empty spaces in the map.  We use a couple of helper
functions to check if the move is a jump, then we combine all of our
rules in a call to <code>and</code>.  Finally, the result of the function is if
the move is valid and which space was jumped, if any.</p>

<p>Let&#8217;s dive into these helper functions.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
dx = tx &#8211; fx<br />
dy = ty &#8211; fy<br />
onBoard = to `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span class="kw3">elem</span></a>` validPositions<br />
isAdjacent = to `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span class="kw3">elem</span></a>` adjacentPositions from<br />
movingForward = dy &gt;= <span class="nu0">0</span><br />
<br />
<span class="co1">&#8211; isGoose :: Maybe Position -&gt; GameStateM Bool</span><br />
isGoose Nothing = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> False<br />
isGoose <span class="br0">&#40;</span>Just pos<span class="br0">&#41;</span> = <span class="br0">&#40;</span>Just Goose ==<span class="br0">&#41;</span> &lt;$&gt; <span class="br0">&#40;</span>gets $ getPiece pos<span class="br0">&#41;</span><br />
&nbsp;</div>

<p>These are all very simple definitions.  We need <code>dx</code> and <code>dy</code> in a lot
of places, so they&#8217;re defined here.  <code>onBoard</code> and <code>isAdjacent</code> use
two definitions from <a href="http://www.tapdancinggoats.com/fox-geese-in-haskell-part-1.htm">part 1</a>.  The function <code>isGoose</code> checks
if a jumped space has a goose.  How do we calculate the jumped space?
We know that it has to be either two positions up, down, or sideways,
and if <code>(x + y)</code> is even then we can go diagonally also.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="co1">&#8211; jumpedPos :: Side -&gt; Maybe Position</span><br />
jumpedPos Fox<br />
&nbsp; &nbsp; <span class="co1">&#8211; Straight up or down</span><br />
&nbsp; &nbsp; | dx == <span class="nu0">0</span> &amp;&amp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span class="kw3">abs</span></a> dy == <span class="nu0">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; = Just <span class="br0">&#40;</span>fx, fy + <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span class="kw3">signum</span></a> dy<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="co1">&#8211; Sideways</span><br />
&nbsp; &nbsp; | dy == <span class="nu0">0</span> &amp;&amp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span class="kw3">abs</span></a> dx == <span class="nu0">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; = Just <span class="br0">&#40;</span>fx + <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span class="kw3">signum</span></a> dx, fy<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="co1">&#8211; Diagonally</span><br />
&nbsp; &nbsp; | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:even"><span class="kw3">even</span></a> <span class="br0">&#40;</span>fx + fy<span class="br0">&#41;</span> &amp;&amp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span class="kw3">abs</span></a> dx == <span class="nu0">2</span> &amp;&amp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span class="kw3">abs</span></a> dy == <span class="nu0">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; = Just <span class="br0">&#40;</span>fx + <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span class="kw3">signum</span></a> dx, fy + <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span class="kw3">signum</span></a> dy<span class="br0">&#41;</span><br />
jumpedPos _ = Nothing<br />
&nbsp;</div>

<p>If the jumping piece isn&#8217;t a fox, or the move isn&#8217;t a jump, return
<code>Nothing</code>.</p>

<p>This is the bare minimum of mechanics for a working fox and geese
game.  It&#8217;s actually not complete, because we don&#8217;t allow multiple
jumps, and we don&#8217;t force a player to take a jump if he can.  I will
add those later and write a new post.</p>

<p>Next time, we&#8217;ll see how to make our game interact with the world,
with input and drawing.  The source code is
<a href="https://github.com/shadwstalkr/fox_and_geese">available on GitHub</a>, so please fork it and make it better.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-2.htm/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fox &amp; Geese in Haskell: Part 1</title>
		<link>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-1.htm</link>
		<comments>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-1.htm#comments</comments>
		<pubDate>Fri, 24 Jun 2011 05:11:32 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[fox and geese]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://www.tapdancinggoats.com/?p=661</guid>
		<description><![CDATA[My next project for learning Haskell is Fox &#38; Geese. This game has simple rules and mechanics, and it doesn&#8217;t need fancy graphics. It is pretty easy to implement a simple version quickly, so I can focus more on Haskell than details of the game. I&#8217;ll go over this in a series of posts. In [...]]]></description>
			<content:encoded><![CDATA[<p>My next project for learning <a href="http://www.haskell.org/">Haskell</a> is <a href="http://en.wikipedia.org/wiki/Fox_and_geese">Fox &amp; Geese</a>.  This game has simple rules and mechanics, and it doesn&#8217;t need fancy graphics.  It is pretty easy to implement a simple version quickly, so I can focus more on Haskell than details of the game.  I&#8217;ll go over this in a series of posts.  In part 1 I&#8217;ll talk about how I represent the game board in Haskell, and in later parts I&#8217;ll discuss input handling, drawing, and game mechanics.</p>

<p><span id="more-661"></span>
But first, an overview of fox &amp; geese.  It&#8217;s similar to checkers, except that it is asymmetrical.  The rules are pretty simple:</p>

<ul>
<li>The pieces move along the lines on the board and sit on the intersections, which I&#8217;ll call &#8220;spaces&#8221; even though they should be called &#8220;points.&#8221;</li>
<li>There are only one or two foxes and fourteen to 24 geese (the variations balance the game differently)</li>
<li>The foxes can capture the geese by jumping like checkers, but the geese can&#8217;t capture the foxes</li>
<li>The geese can&#8217;t move backwards, but the foxes can move any direction</li>
<li>The geese win by occupying the nine spaces at the far side of the board or blocking the foxes so they can&#8217;t move</li>
<li>The foxes win by capturing enough geese that they can&#8217;t win.</li>
</ul>

<p>Like most old games, there are a number of variations.  Later we&#8217;ll add options that let the user customize the game to the variation he likes, but for now we&#8217;ll stick to two foxes and 24 geese.</p>

<p>And now, the program!  It will help to get the source and follow along.  The <a href="https://github.com/shadwstalkr/fox_and_geese">code is available on GitHub</a>; this part will refer to tag <em>v0.1.1</em>.  The source code is <a href="http://www.haskell.org/cabal/">cabalized</a>, so after you check it out you should be able to do <code>cabal configure</code> and <code>cabal build</code> to build it.</p>

<h3>The Board</h3>

<p>I decided to represent the board as a map from a position to a piece.  Empty spaces on the board aren&#8217;t in the map.  So, we need a type to represent our pieces in the map, and a  type for the spaces.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="kw1">import</span> <span class="kw1">qualified</span> Data.Map <span class="kw1">as</span> Map<br />
<br />
<span class="co1">&#8211; A piece in the game</span><br />
<span class="kw1">data</span> Side = Fox | Goose<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">deriving</span> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span class="kw4">Eq</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span class="kw4">Show</span></a><span class="br0">&#41;</span><br />
<br />
<span class="co1">&#8211; A space on the board, counting from the lower left.</span><br />
<span class="kw1">type</span> Position = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#41;</span><br />
<br />
<span class="kw1">type</span> Board = Map.Map Position Side<br />
&nbsp;</div>

<p>I used a qualified import for <code>Data.Map</code> because it has names that conflict with <code>Prelude</code>.</p>

<p>It&#8217;s useful to know which spaces in our 7&#215;7 grid actually exist on the board, so let&#8217;s make a list of valid positions.  First I generate all the positions in a 7&#215;7 grid, then I filter out the invalid ones.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
validPositions = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span class="kw3">not</span></a> . invalid<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> | x &lt;- <span class="br0">&#91;</span><span class="nu0">0</span>..<span class="nu0">6</span><span class="br0">&#93;</span>, y &lt;- <span class="br0">&#91;</span><span class="nu0">0</span>..<span class="nu0">6</span><span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">where</span> invalid <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:or"><span class="kw3">or</span></a> <span class="br0">&#91;</span>x &lt; <span class="nu0">2</span> &amp;&amp; y &lt; <span class="nu0">2</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x &gt; <span class="nu0">4</span> &amp;&amp; y &lt; <span class="nu0">2</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x &lt; <span class="nu0">2</span> &amp;&amp; y &gt; <span class="nu0">4</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x &gt; <span class="nu0">4</span> &amp;&amp; y &gt; <span class="nu0">4</span><span class="br0">&#93;</span><br />
&nbsp;</div>

<p>It&#8217;s also helpful to know which positions can be reached from any other position.  I call this <code>adjacentPositions</code>, and it&#8217;s more complicated than it seems because of the odd board shape, and because diagonal movements are only allowed from certain spaces.  First I generate all the possible movements<sup id="fnref:jumps"><a href="#fn:jumps" rel="footnote">1</a></sup> with <code>[(x, y) | x &lt;- [-1..1], y &lt;- [-1..1]]</code>, then filter out the movements that aren&#8217;t valid.  The result is <code>offsets</code> in the next code snippet.  Then I use those offsets to generate the possible adjacent positions and filter out those that are off the board.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
adjacentPositions :: Position -&gt; <span class="br0">&#91;</span>Position<span class="br0">&#93;</span><br />
adjacentPositions <span class="br0">&#40;</span>px, py<span class="br0">&#41;</span> =<br />
&nbsp; &nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> onBoard . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> addOffset $ offsets<br />
&nbsp; &nbsp; <span class="kw1">where</span> onBoard = <span class="br0">&#40;</span>`<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span class="kw3">elem</span></a>` validPositions<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsets = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> adjacent <span class="br0">&#91;</span><span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> | x &lt;- <span class="br0">&#91;</span><span class="nu0">-1</span>..<span class="nu0">1</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y &lt;- <span class="br0">&#91;</span><span class="nu0">-1</span>..<span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjacent delta@<span class="br0">&#40;</span>dx, dy<span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span class="kw3">not</span></a> <span class="br0">&#40;</span>delta == <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span> ||<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:odd"><span class="kw3">odd</span></a> <span class="br0">&#40;</span>px + py<span class="br0">&#41;</span> &amp;&amp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dx /= <span class="nu0">0</span> &amp;&amp; dy /= <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addOffset <span class="br0">&#40;</span>dx, dy<span class="br0">&#41;</span> = <span class="br0">&#40;</span>px + dx, py + dy<span class="br0">&#41;</span><br />
&nbsp;</div>

<p>The Haskell books I&#8217;ve been reading call this using lists as non-deterministic values.  The idea is that instead of finding one possible move, testing it, finding the next move, etc., we just put all the possible moves in a list and operate on them as one value.  It&#8217;s something you can do in imperative languages, but functional programming, and especially lazy evaluation, makes it a lot easier to work this way.</p>

<p>Another thing we need to do with our board is find out who is where.  If we try to see which piece is at a space on the board, we might get a piece or we might not.  This looks like a job for <strong>Maybe</strong>!  The map function <code>lookup</code> already gives us a <code>Maybe</code> value, so we&#8217;re good.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
getPiece :: Position -&gt; GameState -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span class="kw4">Maybe</span></a> Side<br />
getPiece pos = Map.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:lookup"><span class="kw3">lookup</span></a> pos . board<br />
&nbsp;</div>

<p>Don&#8217;t worry about <code>GameState</code> right now.  It wraps together a bunch of stuff that we need for interacting with the game.  All that&#8217;s important right now is the board, stored conveniently in record notation with the name <code>board</code>.  So <code>getPiece</code> first gets the board from the <code>GameState</code>, then it tries to lookup the <code>Position</code> in the map.  Maybe it finds a piece, maybe it finds bupkis.</p>

<p>We now have a working representation of a board for a game of fox and geese.  We also know which positions on the board are valid, what movements we can make, and we can see what is on the board.  In the next post, we&#8217;ll build the game mechanics that move pieces around the board.</p>

<p>As always, feel free to fork <a href="https://github.com/shadwstalkr/fox_and_geese">the project</a> and fix all my glaring mistakes.  Just be a dear and let me know about it in the comments.</p>

<p>What are some other ways that you might represent the board in Haskell?  Leave a comment and let us know!</p>

<p>Next, learn how to move the pieces in <a href="http://www.tapdancinggoats.com/fox-geese-in-haskell-part-2.htm">part 2</a>.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:jumps">
<p>You may notice that &#8220;all the possible movements&#8221; only includes movements to the next space.  What about jumps?  Patience, we&#8217;ll handle those later in <a href="http://www.tapdancinggoats.com/fox-geese-in-haskell-part-2.htm">game mechanics</a>.&#160;<a href="#fnref:jumps" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.tapdancinggoats.com/fox-geese-in-haskell-part-1.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spin: The Game</title>
		<link>http://www.tapdancinggoats.com/spin-the-game.htm</link>
		<comments>http://www.tapdancinggoats.com/spin-the-game.htm#comments</comments>
		<pubDate>Fri, 11 Mar 2011 07:12:23 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[physics simulation]]></category>

		<guid isPermaLink="false">http://www.tapdancinggoats.com/?p=617</guid>
		<description><![CDATA[Spin is a new Flash game I&#8217;ve been noodling on for a little while. It&#8217;s a Puzzle Bobble type game, except that the balls can freely spin like a pinwheel. The rotation is physically accurate, so as balls fall off of the board it changes speed and direction. When a ball hits the board the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tapdancinggoats.com/spin"><img src="http://www.tapdancinggoats.com/wp-content/uploads/2011/03/spin-pic_sm.png" alt="" title="Spin: The Game" width="300" height="269" class="alignright size-full wp-image-619" /></a>
<a href="http://www.tapdancinggoats.com/spin">Spin</a> is a new Flash game I&#8217;ve been noodling on for a little while.  It&#8217;s a <a href="http://en.wikipedia.org/wiki/Puzzle_Bobble">Puzzle Bobble</a> type game, except that the balls can freely spin like a pinwheel.  The rotation is physically accurate, so as balls fall off of the board it changes speed and direction.  When a ball hits the board the ball&#8217;s momentum is transferred to it, so if the board is turning slowly you can speed it up or make it turn the other way by firing the balls at it.</p>

<p>The feature list was miles long, but I&#8217;m getting too busy to work on it.  Look forward to a second version when I can get back to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tapdancinggoats.com/spin-the-game.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silly Jessica Simpson Game, Pizza Hut Codes</title>
		<link>http://www.tapdancinggoats.com/silly-jessica-simpson-game.htm</link>
		<comments>http://www.tapdancinggoats.com/silly-jessica-simpson-game.htm#comments</comments>
		<pubDate>Sun, 04 Feb 2007 19:46:57 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Pizza Hut]]></category>

		<guid isPermaLink="false">http://tapdancinggoats.com/?p=11</guid>
		<description><![CDATA[I don&#8217;t get it, but my wife loves the celebrity gossip, so when all this stuff about Jessica Simpson started coming out I whipped up this little game to make her laugh. It&#8217;s an homage to an old Mac game I haven&#8217;t seen in years called &#8220;Slick Willie.&#8221; I&#8217;m also keeping track of Pizza Hut&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t get it, but my wife loves the celebrity gossip, so when all this stuff about Jessica Simpson started coming out I whipped up this little game to make her laugh.  It&#8217;s an homage to an old Mac game I haven&#8217;t seen in years called &#8220;Slick Willie.&#8221;  I&#8217;m also keeping track of <a href="http://www.pizzahut.com/hunt/" title="Cheesy Hunt">Pizza Hut&#8217;s sweepstakes codes</a> here.  Have fun.</p>

<p>
Click on &#8220;more&#8221; to play the game.
</p>

<p><span id="more-11"></span>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="550" height="400" id="munching" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="http://www.tapdancinggoats.com/flash/munchies/munching.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="http://www.tapdancinggoats.com/flash/munchies/munching.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="munching" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></p>

<p>
  <h3>This week&#8217;s Pizza Hut code</h3>
  Send the word &#8220;WIN&#8221; to <tt>843488</tt> with your cell phone (the kids call it &#8220;texting&#8221;) to enter to win a new car, an entertainment center, free pizza, and more prizes. <a href="http://www.pizzahut.com/hunt/" title="Cheesy Hunt">Learn more.</a>
</p>

<p>
  Here&#8217;s what they&#8217;re giving away:
  <ul>
    <li>$30k for a car</li>
    <li>5 entertainment centers worth $4k each</li>

    <li>20 $400 Pizza Hut gift cards</li>
    <li>500 $5 music download gift cards</li>
  </ul>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tapdancinggoats.com/silly-jessica-simpson-game.htm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

