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)

package require Tcl 8.5
fconfigure stdout -buffering none
 
# Set up the grid and fill it with some mines
proc makeGrid {n m} {
    global grid mine
    unset -nocomplain grid mine
    set grid(size) [list $n $m]
    set grid(shown) 0
    set grid(marked) 0
    for {set j 1} {$j <= $m} {incr j} {
	for {set i 1} {$i <= $n} {incr i} {
	    set grid($i,$j) .
	}
    }
    set squares [expr {$m * $n}]
    set mine(count) [expr {int((rand()*0.4+0.2) * $squares)}]
    for {set count 0} {$count < $mine(count)} {incr count} {
	while 1 {
	    set i [expr {1+int(rand()*$n)}]
	    set j [expr {1+int(rand()*$m)}]
	    if {![info exist mine($i,$j)]} {
		set mine($i,$j) x
		break
	    }
	}
    }
    return $mine(count)
}
 
# Print out the grid
proc displayGrid {} {
    global grid
    lassign $grid(size) n m
    for {set j 1} {$j <= $m} {incr j} {
	set row "\t"
	for {set i 1} {$i <= $n} {incr i} {
	    append row $grid($i,$j)
	}
	puts $row
    }
}
 
# Toggle the possible-mine flag on a cell
proc markCell {x y} {
    global grid
    if {![info exist grid($x,$y)]} return
    if {$grid($x,$y) eq "."} {
	set grid($x,$y) "?"
	incr grid(marked)
    } elseif {$grid($x,$y) eq "?"} {
	set grid($x,$y) "."
	incr grid(marked) -1
    }
}
 
# Helper procedure that iterates over the 9 squares centered on a location
proc foreachAround {x y xv yv script} {
    global grid
    upvar 1 $xv i $yv j
    foreach i [list [expr {$x-1}] $x [expr {$x+1}]] {
	foreach j [list [expr {$y-1}] $y [expr {$y+1}]] {
	    if {[info exist grid($i,$j)]} {uplevel 1 $script}
	}
    }
}
 
# Reveal a cell; returns if it was a mine
proc clearCell {x y} {
    global grid mine
    if {![info exist grid($x,$y)] || $grid($x,$y) ne "."} {
	return 0; # Do nothing...
    }
    if {[info exist mine($x,$y)]} {
	set grid($x,$y) "!"
	revealGrid
	return 1; # Lose...
    }
    set surround 0
    foreachAround $x $y i j {incr surround [info exist mine($i,$j)]}
    incr grid(shown)
    if {$surround == 0} {
	set grid($x,$y) " "
	foreachAround $x $y i j {clearCell $i $j}
    } else {
	set grid($x,$y) $surround
    }
    return 0
}
 
# Check the winning condition
proc won? {} {
    global grid mine
    lassign $grid(size) n m
    expr {$grid(shown) + $mine(count) == $n * $m}
}
 
# Update the grid to show mine locations (marked or otherwise)
proc revealGrid {} {
    global grid mine
    lassign $grid(size) n m
    for {set j 1} {$j <= $m} {incr j} {
	for {set i 1} {$i <= $n} {incr i} {
	    if {![info exist mine($i,$j)]} continue
	    if {$grid($i,$j) eq "."} {
		set grid($i,$j) "x"
	    } elseif {$grid($i,$j) eq "?"} {
		set grid($i,$j) "X"
	    }
	}
    }
}
 
# The main game loop
proc play {n m} {
    set m [makeGrid $n $m]
    puts "There are $m true mines of fixed position in the grid\n"
    displayGrid
    while 1 {
	puts -nonewline "m x y/c x y/p/r: "
	if {[gets stdin line] < 0} break; # check for eof too!
	switch -nocase -regexp [string trim $line] {
	    {^m\s+\d+\s+\d+$} {
		markCell [lindex $line 1] [lindex $line 2]
	    }
	    {^c\s+\d+\s+\d+$} {
		if {[clearCell [lindex $line 1] [lindex $line 2]]} {
		    puts "KABOOM!"
		    displayGrid
		    break
		} elseif {[won?]} {
		    puts "You win!"
		    displayGrid
		    break
		}
	    }
	    {^p$} {
		displayGrid
	    }
	    {^r$} {
		puts "Resigning..."
		revealGrid
		displayGrid
		break
	    }
	}
    }
}
 
play 6 4