packages feed

htzaar-0.0.0: src/Main.hs

module Main (main) where

import Data.List
import Data.Maybe
import System.Environment
import System.Random
import Text.Printf

import AI
import Board
import Play

main :: IO ()
main = do
  args <- getArgs
  case args of
    [a, b]             | elem a aiNames && elem b aiNames -> aiVsAI 0 a b
    ["-s", seed, a, b] | elem a aiNames && elem b aiNames -> aiVsAI (read seed) a b
    [a]                | elem a aiNames -> play (mkStdGen 0)           $ fromJust $ lookup a ais
    ["-s", seed, a]    | elem a aiNames -> play (mkStdGen $ read seed) $ fromJust $ lookup a ais
    _ -> help

aiVsAI :: Int -> String -> String -> IO ()
aiVsAI seed a b = go 1 ("white", fromJust $ lookup a ais) ("black", fromJust $ lookup b ais) (boardTree startingBoard) (mkStdGen seed)

go :: Int -> (String, AI) -> (String, AI) -> BoardTree -> StdGen -> IO ()
go n a@(aColor, aAI) b@(bColor, bAI) bt@(BoardTree _ _ branches) g
  | null branches = putStrLn $ bName ++ " Wins!"
  | otherwise     = case lookup t branches of
    Nothing -> putStrLn ("Invalid Turn: " ++ aName ++ ": " ++ show t)
    Just bt -> printf "%2i. %s %s : %-20s        (%i)\n" n aColor ("(" ++ name aAI ++ ")") (showTurn t) (length branches) >> go (n + 1) b a (swapBoardTree bt) g'
  where
  (t, g') = strategy aAI bt g
  aName  = aColor ++ " (" ++ name aAI ++ ")"
  bName  = bColor ++ " (" ++ name bAI ++ ")"
  

help :: IO ()
help = putStrLn $ unlines
  [ ""
  , "NAME"
  , "  htzaar - Haskell Tzaar, a two-player abstract strategy game."
  , ""
  , "SYNOPSIS"
  , "  htzaar [-s SEED] AI [AI]"
  , ""
  , "EXAMPLES"
  , "  htzaar lame"
  , "    Play a game against the lame AI."
  , ""
  , "  htzaar -s 1234 lame"
  , "    Play a game against the lame AI with an initial seed."
  , ""
  , "  htzaar -s 1234 lame lame"
  , "    Simulate a game with an initial seed between two instances of the lame AI."
  , ""
  , "UI CONTROLS"
  , "  left  mouse button : Select piece to move or location to move too.  Also starts a new game."
  , "  right mouse button : Undo last action."
  , "  space bar          : Pass on the second optional move."
  , ""
  , "AI LIBRARY"
  ] ++ unlines [ "  " ++ name a ++ " : " ++ description a | a <- ai ] ++ "\n"

aiNames = map name ai
ais = zip aiNames ai