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)

N = 6 : M = 5 : H = 25 : P = 0.2
 
fastgraphics
graphsize N*H,(M+1)*H
font "Arial",H/2+1,75
dim f(N,M) # 1 open, 2 mine, 4 expected mine
dim s(N,M) # count of mines in a neighborhood
 
trian1 = {1,1,H-1,1,H-1,H-1} : trian2 = {1,1,1,H-1,H-1,H-1}
mine = {2,2, H/2,H/2-2, H-2,2, H/2+2,H/2, H-2,H-2, H/2,H/2+2, 2,H-2, H/2-2,H/2}
flag = {H/2-1,3, H/2+1,3, H-4,H/5, H/2+1,H*2/5, H/2+1,H*0.9-2, H*0.8,H-2, H*0.2,H-2, H/2-1,H*0.9-2}
 
mines = int(N*M*P) : k = mines : act = 0
while k>0 
	i = int(rand*N) : j = int(rand*M)
	if not f[i,j] then 
		f[i,j] = 2 : k = k - 1		# set mine
		s[i,j] = s[i,j] + 1 : gosub adj	# count it
 
	end if
end while
togo =  M*N-mines : over = 0 : act = 1
 
gosub redraw
while not over
	clickclear
	while not clickb
		pause 0.01
	end while
	i = int(clickx/H) : j = int(clicky/H)
	if i<N and j<M then
		if clickb=1 then
			if not (f[i,j]&4) then ai = i : aj = j : gosub opencell
			if not s[i,j] then gosub adj
		else 
			if not (f[i,j]&1) then
				if f[i,j]&4 then mines = mines+1
				if not (f[i,j]&4) then mines = mines-1
				f[i,j] = (f[i,j]&~4)|(~f[i,j]&4)
			end if
		end if
		if not (togo or mines) then over = 1
		gosub redraw
	end if
end while
imgsave "Minesweeper_game_BASIC-256.png", "PNG"
end
 
redraw:
	for i = 0 to N-1
		for j = 0 to M-1
			if over=-1 and f[i,j]&2 then f[i,j] = f[i,j]|1
			gosub drawcell
		next j
	next i
	# Counter
	color (32,32,32) : rect 0,M*H,N*H,H
	color white : x = 5 : y = M*H+H*0.05
	if not over then text x,y,"Mines: " + mines
	if over=1 then text x,y,"You won!"
	if over=-1 then text x,y,"You lost"
	refresh
	return
 
drawcell:
	color darkgrey
	rect i*H,j*H,H,H
	if f[i,j]&1=0 then	# closed
		color black : stamp i*H,j*H,trian1
		color white : stamp i*H,j*H,trian2
		color grey : rect i*H+2,j*H+2,H-4,H-4
		if f[i,j]&4 then color blue : stamp i*H,j*H,flag
	else
		color 192,192,192 : rect i*H+1,j*H+1,H-2,H-2
		# Draw 
		if f[i,j]&2 then	# mine
			if not (f[i,j]&4) then color red
			if f[i,j]&4 then color darkgreen
			circle i*H+H/2,j*H+H/2,H/5 : stamp i*H,j*H,mine
		else
			if s[i,j] then color (32,32,32) : text i*H+H/3,j*H+1,s[i,j]
		end if
	end if
	return
 
adj:
	aj = j-1
	if j and i then ai = i-1 : gosub adjact
	if j then ai = i : gosub adjact
	if j and i<N-1 then ai = i+1 : gosub adjact
	aj = j
	if i then ai = i-1 : gosub adjact
	if i<N-1 then ai = i+1 : gosub adjact
	aj = j+1
	if j<M-1 and i then ai = i-1 : gosub adjact
	if j<M-1 then ai = i : gosub adjact
	if j<M-1 and i<N-1 then ai = i+1 : gosub adjact
	return
 
adjact:
	if not act then s[ai,aj] = s[ai,aj]+1 : return
	if act then gosub opencell : return
 
opencell:
	if not (f[ai,aj]&1) then 
		f[ai,aj] = f[ai,aj]|1
		togo = togo-1
	end if
	if f[ai,aj]&2 then over = -1
	return