aboutsummaryrefslogtreecommitdiff
path: root/motus/js/state.ml
blob: 57a37944244e85c0e07bde982feb285cd01309b2 (plain)
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
open StdLabels
open Motus_lib

type letter_validity =
  | Wellplaced
  | Misplaced
  | Missing

type proposition = (Jstr.t * letter_validity) option list

type state =
  { analysis : Persistence.t
  ; rules : Criteria.t list
  ; length : int
  ; propositions : proposition list
  ; current_prop : proposition
  ; fields : Brr.El.t list
  }

let init () =
  { analysis = Persistence.empty_data ()
  ; rules = []
  ; length = 0
  ; propositions = []
  ; current_prop = []
  ; fields = []
  }


module App = Application.Make (struct
  type t = state
end)

(** Get the current rules to apply with from the field list *)
let get_current_rules : proposition -> Criteria.t list =
 fun prop ->
  let rules = ref [] in
  List.iteri prop ~f:(fun i prop ->
      Option.iter
        (fun (letter, validity) ->
          if Jstr.equal Jstr.empty letter
          then ()
          else
            let char = String.get (Jstr.to_string letter) 0 in

            match validity with
            | Missing ->
                rules := Criteria.add (Criteria.NotContain (char, None)) !rules
            | Misplaced ->
                rules :=
                  Criteria.add (Criteria.NotContain (char, Some i)) !rules;
                rules := Criteria.add (Criteria.Contain (char, None)) !rules
            | Wellplaced ->
                rules := Criteria.add (Criteria.Contain (char, Some i)) !rules
          )
        prop );
  List.rev !rules


(** Compare two states *)
let eq : state -> state -> bool =
 fun s1 s2 ->
  (s1.length, s1.rules, s1.current_prop, s1.propositions, s1.analysis)
  = (s2.length, s2.rules, s2.current_prop, s2.propositions, s2.analysis)