Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
2
Indexable
Never
module Robots : sig
  type t
  val create : bool array array -> (int -> int * int -> int * int) -> t
  val move : t -> int * int -> unit
  val print : t -> unit
end = struct
  type t = {
    mutable world : bool array array;
    next_pos : int -> int * int -> int * int;
  }

  let create initial_positions next_pos =
    { world = initial_positions; next_pos }

  let move robots pos =
    let n = Array.length robots.world in
    let (x, y) = pos in
    let (next_x, next_y) = robots.next_pos n pos in
    if next_x >= 0 && next_x < n && next_y >= 0 && next_y < n && robots.world.(x).(y) then
      begin
        robots.world.(x).(y) <- false;
        robots.world.(next_x).(next_y) <- true
      end

  let print robots =
    let n = Array.length robots.world in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
        if robots.world.(i).(j) then
          print_string "R "
        else
          print_string "x ";
      done;
      print_newline ()
    done
end