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)

(defclass minefield ()
  ((mines :initform (make-hash-table :test #'equal))
   (width :initarg :width)
   (height :initarg :height)
   (grid :initarg :grid)))
 
(defun make-minefield (width height num-mines)
  (let ((minefield (make-instance 'minefield
                                  :width width
                                  :height height
                                  :grid (make-array
                                          (list width height)
                                          :initial-element #\.)))
        (mine-count 0))
    (with-slots (grid mines) minefield
      (loop while (< mine-count num-mines)
            do (let ((coords (list (random width) (random height))))
                 (unless (gethash coords mines)
                   (setf (gethash coords mines) T)
                   (incf mine-count))))
      minefield)))
 
(defun print-field (minefield)
  (with-slots (width height grid) minefield
    (dotimes (y height)
      (dotimes (x width)
        (princ (aref grid x y)))
      (format t "~%"))))
 
(defun mine-list (minefield)
  (loop for key being the hash-keys of (slot-value minefield 'mines) collect key))
 
(defun count-nearby-mines (minefield coords)
  (length (remove-if-not
            (lambda (mine-coord)
              (and
                (> 2 (abs (- (car coords) (car mine-coord))))
                (> 2 (abs (- (cadr coords) (cadr mine-coord))))))
            (mine-list minefield))))
 
(defun clear (minefield coords)
  (with-slots (mines grid) minefield
    (if (gethash coords mines)
      (progn
        (format t "MINE! You lose.~%")
        (dolist (mine-coords (mine-list minefield))
          (setf (aref grid (car mine-coords) (cadr mine-coords)) #\x))
        (setf (aref grid (car coords) (cadr coords)) #\X)
        nil)
      (setf (aref grid (car coords) (cadr coords))
            (elt " 123456789"(count-nearby-mines minefield coords))))))
 
(defun mark (minefield coords)
  (with-slots (mines grid) minefield
    (setf (aref grid (car coords) (cadr coords)) #\?)))
 
(defun win-p (minefield)
  (with-slots (width height grid mines) minefield
    (let ((num-uncleared 0))
      (dotimes (y height)
        (dotimes (x width)
          (let ((square (aref grid x y)))
            (when (member square '(#\. #\?) :test #'char=)
              (incf num-uncleared)))))
      (= num-uncleared (hash-table-count mines)))))
 
(defun play-game ()
  (let ((minefield (make-minefield 6 4 5)))
    (format t "Greetings player, there are ~a mines.~%"
            (hash-table-count (slot-value minefield 'mines)))
    (loop
      (print-field minefield)
      (format t "Enter your command, examples: \"clear 0 1\" \"mark 1 2\" \"quit\".~%")
      (princ "> ")
      (let ((user-command (read-from-string (format nil "(~a)" (read-line)))))
        (format t "Your command: ~a~%" user-command)
        (case (car user-command)
          (quit (return-from play-game nil))
          (clear (unless (clear minefield (cdr user-command))
                   (print-field minefield)
                   (return-from play-game nil)))
          (mark (mark minefield (cdr user-command))))
        (when (win-p minefield)
          (format t "Congratulations, you've won!")
          (return-from play-game T))))))
 
(play-game)