* with this program. If not, see <https://www.gnu.org/licenses/>.
*)
-type 't interface_state = { x: int; y: int; current: 't; running: bool }
+type 't interface_state = { x: int; y: int; current: 't }
let render
(type t)
(m : (module Automata.Automaton with type t = t))
(board : t Automata.board)
(inter : t interface_state) =
+ let set_current inter = Automata.set inter.x inter.y inter.current in
+ let chpos dx dy ({ x; y; _ } as inter) = { inter with x = x + dx; y = y + dy } in
+ let chcur f ({ current; _ } as inter) = { inter with current = f current } in
let module M = (val m) in
if Graphics.key_pressed ()
then
match Graphics.read_key () with
- | ' ' ->
- Automata.set inter.x inter.y inter.current board;
- inter
- | 'a' -> { inter with current = M.prev inter.current }
- | 'e' -> { inter with current = M.next inter.current }
- | 'z' -> { inter with y = inter.y + 1 }
- | 'q' -> { inter with x = inter.x - 1 }
- | 's' -> { inter with y = inter.y - 1 }
- | 'd' -> { inter with x = inter.x + 1 }
- | 'Z' ->
- Automata.set inter.x inter.y inter.current board;
- { inter with y = inter.y + 1 }
- | 'Q' ->
- Automata.set inter.x inter.y inter.current board;
- { inter with x = inter.x - 1 }
- | 'S' ->
- Automata.set inter.x inter.y inter.current board;
- { inter with y = inter.y - 1 }
- | 'D' ->
- Automata.set inter.x inter.y inter.current board;
- { inter with x = inter.x + 1 }
- | '\t' -> { inter with running = true }
- | _ -> inter
- else inter
+ | ' ' -> (Automata.set inter.x inter.y inter.current board, inter)
+ | 'a' -> (board, chcur M.prev inter)
+ | 'e' -> (board, chcur M.next inter)
+ | 'z' -> (board, chpos 0 1 inter)
+ | 'q' -> (board, chpos (-1) 0 inter)
+ | 's' -> (board, chpos 0 (-1) inter)
+ | 'd' -> (board, chpos 1 0 inter)
+ | 'Z' -> (set_current inter board, chpos 0 1 inter)
+ | 'Q' -> (set_current inter board, chpos (-1) 0 inter)
+ | 'S' -> (set_current inter board, chpos 0 (-1) inter)
+ | 'D' -> (set_current inter board, chpos 1 0 inter)
+ | '\t' -> (Automata.update m board, inter)
+ | _ -> (board, inter)
+ else (board, inter)
let run (type t) (m : (module Automata.Automaton with type t = t)) =
let module M = (val m) in
let rec aux board inter =
render m board inter;
Graphics.synchronize ();
- let inter = inputs m board inter in
- aux (if inter.running then Automata.update m board else board) { inter with running = false }
+ let board, inter = inputs m board inter in
+ aux board inter
in
- aux (Automata.initial m) { x = 0; y = 0; current = M.default; running = false }
+ aux (Automata.initial m) { x = 0; y = 0; current = M.default }
let () =
Graphics.open_graph "";