curses set game

root / Main.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import qualified System.Environment as Env
import qualified System.Exit as Exit
import qualified System.Console.GetOpt as GetOpt
--import qualified System.Posix.Signals as Signal
import qualified Control.Monad as Monad

import qualified Interface
import qualified Display
import Game

defaultOpts = GameOptions{
  gameInfinite = False,
  gamePlayers = []
}

options :: [GetOpt.OptDescr (GameOptions -> GameOptions)]
options = [
  GetOpt.Option "c" ["continuous"]
    (GetOpt.NoArg (\o -> o{ gameInfinite = True }))
    "make the game continuous with an infinite deck",
  GetOpt.Option "p" ["player"]
    (GetOpt.ReqArg (\n o -> o{ gamePlayers = gamePlayers o ++ [n] }) "NAME")
    "add a player with NAME (one for each)"
  ]
header = unlines [
  "Usage: set [OPTIONS]",
  "Play a game of Set.  Completely unauthorized by http://www.setgame.com."
  ]
usage = GetOpt.usageInfo header options

allPlayers = all 1 where
  all n (x:l) = x:all (succ n) l
  all n [] = map (\i -> "Player " ++ show i) [n..]

listOr [] x = x
listOr l _ = l

main = do
  args <- Env.getArgs
  let (optl, argl, errl) = GetOpt.getOpt GetOpt.Permute options args
  Monad.unless (null argl && null errl) $ do
    mapM_ putStrLn errl
    putStrLn usage
    Exit.exitFailure
  let opts = foldl (flip ($)) defaultOpts optl
  --Signal.installHandler Signal.sigINT (Signal.CatchOnce $ putStrLn "hello" >> Exit.exitFailure) Nothing
  Interface.runInterface $ do
  Display.initDisplay
  user <- Env.getEnv "USER"
  let players = allPlayers $ gamePlayers opts `listOr` [user]
  Game.start opts{ gamePlayers = players }
  Exit.exitWith Exit.ExitSuccess