-
Notifications
You must be signed in to change notification settings - Fork 1
/
State.elm
86 lines (73 loc) · 2.38 KB
/
State.elm
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module State where
import GameTypes exposing (..)
import Helpers exposing (..)
import Piece
import Player
{- Is the game ongoing in the given state? -}
isOngoing : State -> Bool
isOngoing state =
case state.gameState of
Ongoing -> True
Connected opponentName -> True
_ -> False
isNotStarted : State -> Bool
isNotStarted state =
case state.gameState of
NotStarted -> True
WaitingForPlayers -> True
_ -> False
isAtMainMenu : State -> Bool
isAtMainMenu state =
state.gameState == NotStarted && state.gameType /= HumanVsHumanRemote
isSettingUpRemoteGame : State -> Bool
isSettingUpRemoteGame state =
state.gameState == NotStarted && state.gameType == HumanVsHumanRemote
{- Does neither player have any tiles left in the given state? -}
isGameOver : State -> Bool
isGameOver state =
(isOngoing state) && (Player.noTilesInHand Red state) && (Player.noTilesInHand Blue state)
{- Must the current player pass in the given state? -}
mustPass : State -> Bool
mustPass state =
(isOngoing state) && (not <| isSwitchingPlayers state) && Player.noTilesInHand state.turn state
{- Is it currently a Human's turn (as opposed to a Cpu or Remote)? -}
isPlayerTurn : State -> Bool
isPlayerTurn state =
(isOngoing state) && ((Player.getType state.turn state) == Human)
isSwitchingPlayers : State -> Bool
isSwitchingPlayers state =
case state.turn of
SwitchingTo _ -> True
_ -> False
pieceHeld : State -> Maybe Piece
pieceHeld state =
case state.heldPiece of
Just idx ->
let hand = Player.getHand state.turn state
pieceStr = hand !! idx
in
Just (Piece.fromString pieceStr)
Nothing -> Nothing
nextPlayer : State -> Player
nextPlayer state =
let next = Player.next state.turn
in
if state.gameType == HumanVsHumanLocal
then SwitchingTo next
else next
leadingPlayer : State -> Maybe Player
leadingPlayer state =
let redScore = getU "Red" state.score
blueScore = getU "Blue" state.score
in
if | redScore > blueScore -> Just Red
| blueScore > redScore -> Just Blue
| otherwise -> Nothing
endStateMsg : State -> String
endStateMsg state =
let redScore = getU "Red" state.score
blueScore = getU "Blue" state.score
in
if | redScore == blueScore -> "Tie game!"
| redScore > blueScore -> getU "Red" state.playerNames ++ " wins!"
| redScore < blueScore -> getU "Blue" state.playerNames ++ " wins!"