-
Notifications
You must be signed in to change notification settings - Fork 12
/
Interactions.fs
44 lines (37 loc) · 1.57 KB
/
Interactions.fs
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
namespace Canopy2048
open canopy
open runner
module Interactions =
let lostSelector = css ".game-message.game-over"
let winSelector = css ".game-message.game-won"
let gameOverSelector = css ".game-message.game-won, .game-message.game-over"
let lost () =
match someElement <| lostSelector with
| None -> false
| Some(_) -> true
let won () =
match someElement <| winSelector with
| None -> false
| Some(_) -> true
let gameEnded () =
match someElement <| gameOverSelector with
| None -> false
| Some(_) -> true
let state () =
elements ".tile"
|> List.map(fun tile ->
let classes = tile.GetAttribute("class").Split(' ')
let pointClass = classes |> Array.find(fun classs -> classs.StartsWith("tile-") && not (classs.StartsWith("tile-position-")))
let point = pointClass.Split('-').[1] |> int
let rowColumnClass = classes |> Array.find(fun classs -> classs.StartsWith("tile-position-"))
let column = rowColumnClass.Split('-').[2] |> int
let row = rowColumnClass.Split([|'-'|]).[3] |> int
{ Row = row; Col = column }, point )
// removing the duplicates due to merges:
// group cell by identical row, col
// and keep the one with largest value,
// hacky but works...
|> Seq.groupBy (fun (pos,value) -> pos.Row, pos.Col)
|> Seq.map (fun ((row,col),cells) ->
cells |> Seq.maxBy (fun (pos,value) -> value))
|> Map.ofSeq