deterministic-game-engine-0.2.1: Simple deterministic game engine

Safe HaskellSafe-Inferred
LanguageHaskell2010

GameEngine

Description

Haskell library for creating simple deterministic games, such as tic-tac-toe. The engine requires a minimal set of actions related to the game, and then will run the game until a terminal state is reached.

Simple generic example below. See the specs for a more detailed example.

import GameEngine

game :: GameEngine Int Int
game = GameEngine gameActions initialState

gameActions :: GameActions Int Int
gameActions = GameActions {
   getPlayer  = -- find the next player from a game state,
   getMove    = -- find a move from the game state,
   getResult  = -- transitions from a state to another state,
   isTerminal = -- determines if the game is terminal,
   getScore   = -- get score from a terminal state
 }

initialState :: GameState Int
initialState = GameState 0

-- run the game engine until a terminal state is reached
playSimple game

Synopsis

Documentation

data GameState a Source

Constructors

GameState a 

Instances

Eq a => Eq (GameState a) 
Show a => Show (GameState a) 

data Player Source

Constructors

Player Symbol 

Instances

data Move a Source

Constructors

Move a 

Instances

Eq a => Eq (Move a) 
Show a => Show (Move a) 

data GameActions a b Source

Set of actions that defines how the game will be played

Constructors

GameActions 

Fields

getPlayer :: GameState a -> Player

Specifies which player has the move in the state

getMove :: GameState a -> Move b

Returns a legal move in the state

getResult :: GameState a -> Move b -> GameState a

The transition model, which determines the result of a move

isTerminal :: GameState a -> Bool

True if game is over, False otherwise

getScore :: GameState a -> Player -> Int

A utility function to determine the numeric value for a game that ends in a terminal state

data GameEngine a b Source

Holds information about how the game is played, and the current state of the game.

Constructors

GameEngine 

Fields

actions :: GameActions a b

Defines how the game will be played

state :: GameState a

The current state of the game

play :: Monad m => (GameState a -> m (GameState a)) -> GameEngine a b -> m Int Source

Run the provided game engine under a monadic context until a terminal state is reached. Note: provided function should act as an identity only, and should not modify the game state.

playSimple :: GameEngine a b -> Int Source

Run the provided game engine without a context until a terminal state is reached.

playIO :: (GameState a -> IO ()) -> GameEngine a b -> IO Int Source

Run the provided game engine within an IO context until a terminal state is reached.