-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.elm
139 lines (109 loc) · 3.92 KB
/
Game.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module Game exposing (..)
import Random
import Dice exposing (..)
import Scoresheet exposing (..)
import Html exposing (Html, button, div, text, table, tr, td, th)
import Html.Events exposing (onClick)
type Roll
= FirstRoll
| SecondRoll
| LastRoll
type GameState
= GameState Scoresheet Dice Roll Random.Seed
type Action
= UpdateScorecard (Dice -> Scoresheet -> Scoresheet)
| NewGame
| Roll
| UpdateDice Dice
initialState seed =
let
( initialDice, seedNext ) =
rollAll (Random.initialSeed seed)
in
GameState newScoresheet initialDice FirstRoll seedNext
newGameButton : Html.Html Action
newGameButton =
button [ onClick NewGame ] [ text "New Game" ]
rollDiceButton (GameState _ _ roll _) =
button [ onClick Roll ] [ text (rollButtonText roll) ]
rollButtonText roll =
case roll of
FirstRoll ->
"Roll Again"
SecondRoll ->
"Last Roll"
LastRoll ->
"No Rolls Left"
update : Action -> GameState -> GameState
update action (GameState scoresheet dice roll seed) =
let
newstate =
case action of
Roll ->
case roll of
FirstRoll ->
let
( dice2, seed2 ) =
rollUnheld dice seed
in
GameState scoresheet dice2 SecondRoll seed2
SecondRoll ->
let
( dice2, seed2 ) =
rollUnheld dice seed
in
GameState scoresheet dice2 LastRoll seed2
LastRoll ->
GameState scoresheet dice LastRoll seed
UpdateScorecard setter ->
let
( dice2, seed2 ) =
rollAll seed
in
GameState (setter dice scoresheet) dice2 FirstRoll seed2
NewGame ->
let
( dice2, seed2 ) =
rollAll seed
in
GameState newScoresheet dice2 FirstRoll seed2
UpdateDice newDice ->
GameState scoresheet newDice roll seed
in
newstate
scoresheet (GameState sb dice roll seed) =
let
textCol title =
tr [] [ th [] [ text title ] ]
boxColumn title s =
let
score box =
case box of
Played s ->
text <| toString s
Available setter ->
button [ onClick (UpdateScorecard setter) ] [ text " - " ]
in
tr [] [ td [] [ text title ], td [] [ score s ] ]
in
table []
[ textCol "Upper Section"
, boxColumn "Aces" sb.aces
, boxColumn "Twos" sb.twos
, boxColumn "Threes" sb.threes
, boxColumn "Four" sb.fours
, boxColumn "Fives" sb.fives
, boxColumn "Sixes" sb.sixes
, boxColumn "Bonus" (Played (upperBonus sb))
, textCol "Lower Section"
, boxColumn "3 of a kind" sb.threeOfAKind
, boxColumn "4 of a kind" sb.fourOfAKind
, boxColumn "Full House" sb.fullHouse
, boxColumn "Small Straight" sb.smallStraight
, boxColumn "Large Straight" sb.largeStraight
, boxColumn "Yahtzee" sb.yahtzee
, boxColumn "Chance" sb.chance
, boxColumn "Total" (Played (totalScore sb))
]
view ((GameState sheet dice roll seed) as gamestate) =
div [] [ Dice.view UpdateDice dice, (scoresheet gamestate), rollDiceButton gamestate, newGameButton ]