There is an n by m grid that has a random number (between 10% to 20% of the total number of tiles, though older implementations may use 20%..60% instead) of randomly placed mines that need to be found.

Positions in the grid are modified by entering their coordinates where the first coordinate is horizontal in the grid and the second vertical. The top left of the grid is position 1,1; the bottom right is at n,m.

 The total number of mines to be found is shown at the beginning of the game.
 Each mine occupies a single grid point, and its position is initially unknown to the player
 The grid is shown as a rectangle of characters between moves.
 You are initially shown all grids as obscured, by a single dot '.'
 You may mark what you think is the position of a mine which will show as a '?'
 You can mark what you think is free space by entering its coordinates.
 If the point is free space then it is cleared, as are any adjacent points that are also free space- this is repeated recursively for subsequent adjacent free points unless that point is marked as a mine or is a mine.
 Points marked as a mine show as a '?'.
 Other free points show as an integer count of the number of adjacent true mines in its immediate neighborhood, or as a single space ' ' if the free point is not adjacent to any true mines.
 Of course you lose if you try to clear space that has a hidden mine.
 You win when you have correctly identified all mines.
The Task is to create a program that allows you to play minesweeper on a 6 by 4 grid, and that assumes all user input is formatted correctly and so checking inputs for correct form may be omitted.
You may also omit all GUI parts of the task and work using text input and output.

Note: Changes may be made to the method of clearing mines to more closely follow a particular implementation of the game so long as such differences and the implementation that they more accurately follow are described.


C.F: wp:Minesweeper (computer game)

exception Lost
exception Won
 
let put_mines g m n mines_number =
  let rec aux i =
    if i < mines_number then
    begin
      let x = Random.int n
      and y = Random.int m in
      if g.(y).(x)
      then aux i
      else begin
        g.(y).(x) <- true;
        aux (succ i)
      end
    end
  in
  aux 0
 
let print_abscissas n =
  print_string "\n      "; for x = 1 to n do print_int (x mod 10) done;
  print_string "\n      "; for x = 1 to n do print_char '|' done;
  print_newline()
 
let print_display d n =
  print_abscissas n;
  Array.iteri (fun y line ->
    Printf.printf " %2d - " (y+1);  (* print ordinates *)
    Array.iter print_char line;
    print_newline()
  ) d;
  print_newline()
 
let reveal d g n =
  print_abscissas n;
  Array.iteri (fun y line ->
    Printf.printf " %2d - " (y+1);  (* print ordinates *)
    Array.iteri (fun x c ->
      print_char (
        match c, g.(y).(x) with
        | '0'..'9', _ -> c
        | '.', true -> 'x'
        | '?', true -> 'X'
        | '?', false -> 'N'
        | '.', false -> '.'
        | _ -> c)
    ) line;
    print_newline()
  ) d;
  print_newline()
 
let toggle_mark d x y =
  match d.(y).(x) with
  | '.' -> d.(y).(x) <- '?'
  | '?' -> d.(y).(x) <- '.'
  | _ -> ()
 
let rec feedback g d x y =
  if d.(y).(x) = '.' then
  begin
    let n = ref 0 in  (* the number of mines around *)
    for i = (pred y) to (succ y) do
      for j = (pred x) to (succ x) do
        try if g.(i).(j) then incr n
        with _ -> ()
      done;
    done;
    match !n with
    | 0 ->
        (* recursive feedback when no mines are around *)
        d.(y).(x) <- ' ';
        for j = (pred y) to (succ y) do
          for i = (pred x) to (succ x) do
            try feedback g d i j
            with _ -> ()
          done
        done
    | _ ->
        d.(y).(x) <- (string_of_int !n).[0]
  end
 
let clear_cell g d x y =
  if g.(y).(x)
  then (d.(y).(x) <- '!'; raise Lost)
  else feedback g d x y
 
let rec user_input g d =
  try
    let s = read_line() in
    match Scanf.sscanf s "%c %d %d" (fun c x y -> c,x,y) with
    | 'm', x, y -> toggle_mark d (x-1) (y-1)
    | 'c', x, y -> clear_cell g d (x-1) (y-1)
    | _ -> raise Exit
  with Exit | Scanf.Scan_failure _
  | Invalid_argument "index out of bounds" ->
      print_string "# wrong input, try again\n> ";
      user_input g d
 
let check_won g d =
  let won = ref true in
  Array.iteri (fun y line ->
    Array.iteri (fun x c ->
      match c, g.(y).(x) with
      | '.', _ -> won := false
      | '?', false -> won := false
      | _ -> ()
    ) line
  ) d;
  if !won then raise Won
 
let minesweeper n m percent =
  let round x = int_of_float (floor (x +. 0.5)) in
  let mines_number = round ((float (n * m)) *. percent) in
  (* the ground containing the mines *)
  let g = Array.make_matrix m n false in
  put_mines g m n mines_number;
  Printf.printf "# You have to find %d mines\n" mines_number;
  (* what's displayed to the user *)
  let d = Array.make_matrix m n '.' in
  try
    while true do
      print_display d n;
      print_string "> ";
      user_input g d;
      check_won g d;
    done
  with
  | Lost ->
      print_endline "# You lost!";
      reveal d g n
  | Won ->
      print_endline "# You won!";
      reveal d g n
 
let () = 
  Random.self_init();
  let ios, fos = int_of_string, float_of_string in
  let n, m, percent =
    try ios Sys.argv.(1), ios Sys.argv.(2), fos Sys.argv.(3)
    with _ ->
      try ios Sys.argv.(1), ios Sys.argv.(2), 0.2
      with _ -> (6, 4, 0.2)
  in
  minesweeper n m percent;
;;