-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticTacToe.elm
350 lines (258 loc) · 7.83 KB
/
ticTacToe.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
module TicTacToe exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import Set exposing (Set, insert, member)
import Array exposing (Array)
main : Program Never Model Msg
main =
Html.program
{ init = ( initialModel, Cmd.none )
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
------------ Model ------------
type alias Model =
{ currentPlayer : Player
, state : GameState
, crosses : Coordinates
, circles : Coordinates
, usedCells : Coordinates
}
type alias Coordinates =
Set Coordinate
type alias Coordinate =
( Int, Int )
type GameState
= Ongoing
| Draw
| Winner Player
type Player
= Cross
| Circle
initialModel : Model
initialModel =
{ currentPlayer = Cross
, state = Ongoing
, crosses = Set.empty
, circles = Set.empty
, usedCells = Set.empty
}
------------ View ------------
view : Model -> Html Msg
view model =
case model.state of
Ongoing ->
printGameView model
Winner player ->
printEndScreen winTitle (winMessage player)
Draw ->
printEndScreen drawTitle drawMessage
printEndScreen : String -> String -> Html Msg
printEndScreen title message =
div
[]
[ h1 [] [ text title ]
, div []
[ div [ class "gameArea" ]
[ h3 [] [ text message ]
, button [ onClick Restart ] [ text "Restart?" ]
]
]
]
winTitle : String
winTitle =
"There can only be one"
drawTitle : String
drawTitle =
"There can only be none"
winMessage : Player -> String
winMessage winner =
"Holy crap" ++ stringifyPlayer winner ++ ", you won this incredible game of wits. Congratulations, you are a superstar!"
drawMessage : String
drawMessage =
"Well... that was boring. You guys are equally dull..."
printGameView : Model -> Html Msg
printGameView model =
div []
[ title
, div []
[ div [ class "gameArea" ]
[ h3 [] [ currentPlayerText model.currentPlayer ]
, table []
[ tr []
[ printTableCell ( 0, 0 ) model
, printTableCell ( 1, 0 ) model
, printTableCell ( 2, 0 ) model
]
, tr []
[ printTableCell ( 0, 1 ) model
, printTableCell ( 1, 1 ) model
, printTableCell ( 2, 1 ) model
]
, tr []
[ printTableCell ( 0, 2 ) model
, printTableCell ( 1, 2 ) model
, printTableCell ( 2, 2 ) model
]
]
]
]
]
title : Html Msg
title =
h1 [] [ text "There can only be one" ]
currentPlayerText : Player -> Html Msg
currentPlayerText currentPlayer =
currentPlayer |> stringifyPlayer |> (++) "Current Player: " |> text
printTableCell : Coordinate -> Model -> Html Msg
printTableCell coord model =
td
[ onClick (CellClicked coord) ]
[ div
[ markCell coord model.crosses Cross
, markCell coord model.circles Circle
]
[]
]
markCell : Coordinate -> Coordinates -> Player -> Html.Attribute a
markCell coord usedCells player =
if Set.member coord usedCells then
stringifyPlayer player |> class
else
class "none"
stringifyPlayer : Player -> String
stringifyPlayer player =
case player of
Cross ->
"cross"
Circle ->
"circle"
------------ Update ------------
type Msg
= CellClicked Coordinate
| Restart
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Restart ->
( initialModel, Cmd.none )
CellClicked coordinate ->
( takeTurn coordinate model |> Debug.log "Hallo", Cmd.none )
takeTurn : Coordinate -> Model -> Model
takeTurn coord model =
if Set.member coord model.usedCells then
model
else
case model.currentPlayer of
Cross ->
updateCrosses coord model
Circle ->
updateCircles coord model
updateCrosses : Coordinate -> Model -> Model
updateCrosses coord model =
{ currentPlayer = nextPlayer model.currentPlayer
, state = updateGameState model coord
, crosses = Set.insert coord model.crosses
, circles = model.circles
, usedCells = Set.insert coord model.usedCells
}
updateCircles : Coordinate -> Model -> Model
updateCircles coord model =
{ currentPlayer = nextPlayer model.currentPlayer
, state = updateGameState model coord
, crosses = model.crosses
, circles = Set.insert coord model.circles
, usedCells = Set.insert coord model.usedCells
}
nextPlayer : Player -> Player
nextPlayer currentPlayer =
case currentPlayer of
Cross ->
Circle
Circle ->
Cross
updateGameState : Model -> Coordinate -> GameState
updateGameState model coord =
case (didSomeoneWin model coord) of
Just player ->
Winner player
Nothing ->
if (Set.size model.usedCells == 8) then
Draw
else
Ongoing
didSomeoneWin : Model -> Coordinate -> Maybe Player
didSomeoneWin model newMove =
let
cellsToCheck =
case model.currentPlayer of
Cross ->
Set.insert newMove model.crosses
Circle ->
Set.insert newMove model.circles
in
checkCells cellsToCheck model.currentPlayer
checkCells : Coordinates -> Player -> Maybe Player
checkCells coords currentPlayer =
let
coordsAsList =
coords |> Set.toList |> Array.fromList
xCoords =
Array.map (\( x, y ) -> x) coordsAsList
yCoords =
Array.map (\( x, y ) -> y) coordsAsList
in
checkDiagonal coords currentPlayer
|> checkDefaultWin xCoords currentPlayer
|> checkDefaultWin yCoords currentPlayer
checkDiagonal : Coordinates -> Player -> Maybe Player
checkDiagonal coords currentPlayer =
if (slash coords || backslash coords) then
Just currentPlayer
else
Nothing
slash : Coordinates -> Bool
slash coords =
member ( 0, 0 ) coords
&& member ( 1, 1 ) coords
&& member ( 2, 2 ) coords
backslash : Coordinates -> Bool
backslash coords =
member ( 0, 2 ) coords
&& member ( 1, 1 ) coords
&& member ( 2, 0 ) coords
{-
This is complicated...
This function takes all x values or all y values and counts how often
a row or column is used.
For example, if we check the xCoords, we take all x-es and count the
occurence of zeros, ones and twos. If either of those appears three
times, we have a full column.
-}
checkDefaultWin : Array Int -> Player -> Maybe Player -> Maybe Player
checkDefaultWin coords currentPlayer potentialWinner =
let
initialBuckets =
Array.fromList [ 0, 0, 0 ]
cumulatedCoordinates =
Array.foldl incrementAtPosition initialBuckets coords
threeInARow =
cumulatedCoordinates |> Array.filter (\e -> e == 3)
in
case potentialWinner of
Just a ->
potentialWinner
Nothing ->
if Array.length threeInARow > 0 then
Just currentPlayer
else
Nothing
incrementAtPosition : Int -> Array Int -> Array Int
incrementAtPosition pos array =
let
new =
Array.get pos array |> Maybe.withDefault 0 |> (+) 1
in
Array.set pos new array