1. Philip Xu
  2. 2048-ml

Commits

Philip Xu  committed 8f49e7b

Improved on logic

  • Participants
  • Parent commits 6ad58a6
  • Branches default

Comments (0)

Files changed (3)

File src/board.ml

View file
  • Ignore whitespace
 let move_down = move_and_score Grid.move_down
 
 let spawn (grid, count, score) =
-  let empty_cells = Grid.empty_cells grid in
-  let num_of_empty_cells = List.length empty_cells in
-  if num_of_empty_cells <> 0 then
-    let n = Random.int num_of_empty_cells in
-    let x, y = List.nth empty_cells n in
-    let num = if (Random.int 10 = 0) then 4 else 2 in
-    (Grid.set x y num grid, count, score)
-  else
-    (grid, count, score)
+  match Grid.empty_cells grid with
+  | Some cells ->
+      let num_of_cells = List.length cells in
+      let n = Random.int num_of_cells in
+      let x, y = List.nth cells n in
+      let num = if (Random.int 10 = 0) then 4 else 2 in
+      (Grid.set x y num grid, count, score)
+  | None -> (grid, count, score)

File src/grid.ml

View file
  • Ignore whitespace
 
 let filter_some =
   let rec loop acc = function
-    | [] -> acc
     | None :: tl -> loop acc tl
     | Some e :: tl -> loop (e :: acc) tl
+    | [] ->
+        match acc with
+        | [] -> None
+        | _ -> Some acc
   in loop []
 
 let empty_cells grid =

File src/grid.mli

View file
  • Ignore whitespace
 val has_empty_cell : t -> bool
 (** [has_empty_cell e] returns true if grid [g] has any empty cell. *)
 
-val empty_cells : t -> (int * int) list
-(** [empty_cells g] returns a list of indices of all empty cells on [g]. *)
+val empty_cells : t -> (int * int) list option
+(** [empty_cells g] returns a list of indices of empty cells on [g] if any. *)
 
 val move_left : t -> t * elt list list
 (** [move_left g] returns tuple (grid, remved) as [g] being moved left. *)