packages feed

htzaar-0.0.2: src/AI/Lame.hs

-- | An example AI player.
module AI.Lame (lame) where

import System.Random

import AI.Utils
import Board

lame :: AI
lame = AI
  { name = "lame"
  , description = "Randomly selects the next valid turn."
  , strategy = winNext lameStrategy
  }

-- | The lame strategy picks a valid turn at random.  If a two-move turn is available, it picks one.  (wow, pretty smart!)
lameStrategy :: Strategy
lameStrategy (BoardTree _ _ branches) g = (turns !! i, g')
  where
  allTurns = fst $ unzip branches
  goodTurns = [ (m1, Just m2) | (m1, Just m2) <- allTurns ]
  turns = if null goodTurns then allTurns else goodTurns
  (i, g') = randomR (0, length turns - 1) g