let printMove m = print_string (Board.string_of_move m);;
      
let randomBoard () =
  let brd = ref (Board.initial())
  and nmoves = Random.int 60 in
  for i = 0 to nmoves do
    let player = Board.turn !brd in
    let moves = Board.legalMoves !brd player in
    let move = List.nth moves (Random.int (List.length moves)) in
    brd := Board.makeMove !brd player move
  done;
  !brd;;
    
let randomBoards n =
  let boards = Array.create n (Board.initial()) in
  print_string "Generating boards..."; print_newline();
  for i = 0 to n - 1 do
    boards.(i) <- randomBoard()
  done;
  print_string "Done"; print_newline();
  boards;;

let testLegalMoves ba =
  let beginTime = Unix.gettimeofday()
  and len = Array.length ba in
  Array.iter (function b -> Board.legalMoves b (Board.turn b); ()) ba;
  (float len) /. (Unix.gettimeofday() -. beginTime);;
    

let main () =
  if (Array.length Sys.argv) > 1 then
    let n = int_of_string (Sys.argv.(1)) in
    Random.init (int_of_float (Unix.time()));
    let ba = randomBoards n in
    print_float (testLegalMoves ba);
    print_newline()
  else ();;

Printexc.catch main ();;
