[Haskell-cafe] Functional board games

Ryan Ingram ryani.spam at gmail.com
Mon Apr 21 12:08:17 EDT 2008


Bertrand Felgenhauer[1] wrote a peg solitaire game[2] using Prompt[3]
to interact with the user.

Here's the core game loop:

-- move a peg into a certain direction
data Move = Move Pos Dir

-- solitaire interface
data Request a where
    RMove :: Board -> [Move] -> Request Move -- select a move from given list
    RDone :: Board -> Int    -> Request ()   -- game over

-- implement the game logic
game :: Prompt Request ()
game = game' start

-- find possible moves, end game if there are none, otherwise ...
game' :: Board -> Prompt Request ()
game' b = do
    let options = filter (validMove b) allMoves
    if null options then prompt $ RDone b (length (pegs b))
                    else game'' b options

-- ... prompt for a move and execute it
game'' b options = do
    Move (x, y) d <- prompt $ RMove b options
    let (dx, dy) = delta d
    game' (Board (board b // [((x, y), Empty),
                              ((x + dx, y + dy), Empty),
                              ((x + 2*dx, y + 2*dy), Peg)]))

  -- ryan

[1] http://www.haskell.org/pipermail/haskell-cafe/2008-January/038301.html
[2] http://int-e.home.tlink.de/haskell/solitaire.tar.gz
[3] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadPrompt-1.0.0.1


On Mon, Apr 21, 2008 at 8:22 AM, Dougal Stanton
<dougal at dougalstanton.net> wrote:
> I'm having a go at making a functional board game (the back-end logic
>  for one, at least) and as with all good projects it raises lots of
>  questions. But I'll keep it to one this time.
>
>  Does anyone know of functional-style implementations of
>  chess/draughts/go/anything else that might give me ideas? I am writing
>  a game of Thud (yes, from the Terry Pratchett book...) but I don't
>  hold much hope of their being a functional-style Thud game already in
>  existence!
>
>  Cheers,
>
>  D.
>
>  --
>  Dougal Stanton
>  dougal at dougalstanton.net // http://www.dougalstanton.net
>  _______________________________________________
>  Haskell-Cafe mailing list
>  Haskell-Cafe at haskell.org
>  http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list