Commits

Philip Xu committed 6ad58a6

Moved input logic into module Input

Comments (0)

Files changed (5)

 
 type move = Up | Down | Left | Right
 
-type action = Move of move | Reset | Quit | Invalid
-
 type state = Playing | Win | Lose
 
 type t = state * Board.t
 type move = Up | Down | Left | Right
 (** Type of game movement. *)
 
-type action = Move of move | Reset | Quit | Invalid
-(** Type of game action. *)
-
 type state = Playing | Win | Lose
 (** Type of game state. *)
 
 
 open Game
 
+type action = Move of move | Reset | Quit | Invalid
+
 let key_to_action = function
   | 'k' -> Move Up
   | 'j' -> Move Down
       | 3 | 27 -> Quit  (* <Ctrl-C> = 3, <ESC> = 27 *)
       | _ -> Invalid
 
-let get_action () = Graphics.read_key () |> key_to_action
+let action () = Graphics.read_key () |> key_to_action
     Interface to user input.
  *)
 
-val get_action : unit -> Game.action
+type action = Move of Game.move | Reset | Quit | Invalid
+(** Type of game action. *)
+
+val action : unit -> action
 (** [get_action] waits and return the next action from user input. *)
  * License: BSD New, see LICENSE for details.
  *)
 
-open Game
-
-let get_action = Input.get_action
-
 let render game =
   Tui.render game;
   Gui.render game
 
 let init () = Game.init 4 4
 
-let main_loop game get_action render =
+let main_loop action render game =
   let rec loop game' =
     render game';
-    match get_action () with
-    | Move m -> loop (move m game')
-    | Reset -> loop (init ())
-    | Quit -> print_endline "Quit."
-    | Invalid -> loop game'
+    match action () with
+    | Input.Move m -> loop (Game.move m game')
+    | Input.Reset -> loop (init ())
+    | Input.Quit -> print_endline "Quit."
+    | Input.Invalid -> loop game'
   in loop game
 
 let main () =
   Tui.init game;
   Gui.init game;
   try
-    main_loop game get_action render;
+    main_loop Input.action render game;
   with Graphics.Graphic_failure _ -> ();
   Gui.cleanup ();
   Tui.cleanup ()