How to resolve the algorithm Minesweeper game step by step in the Python programming language
How to resolve the algorithm Minesweeper game step by step in the Python programming language
Table of Contents
Problem Statement
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 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)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Minesweeper game step by step in the Python programming language
This is a program that simulates the game of Minesweeper, a game where the player has to find all the mines hidden in a grid.
The program first sets up the game by creating a grid of size gridsize
and randomly placing minecount
mines in the grid. The program keeps track of the position of the mines in a set called mines
.
The program then creates a display grid called showgrid
that initially shows all the cells in the grid as obscured, which is represented by a single dot '.'.
The player can then interact with the game by entering commands at the prompt. The commands are:
m x y
: Toggle where you think a mine is at position x, y.c x y
: Clear the grid starting at position x, y (and print the result).p
: Print the grid so far.r
: Resign.
If the player enters the m
command, the program toggles the cell at position (x, y) as a mine. If the player enters the c
command, the program clears the grid starting at position (x, y). If the player enters the p
command, the program prints the grid so far. If the player enters the r
command, the program prints the grid with an 'N' for unfound true mines, a 'Y' for found true mines and a '?' for where you marked clear space as a mine and exits the game.
The program checks if the player has won or lost the game after each command. The player wins if they have correctly identified all mines. The player loses if they try to clear space that starts on a mine.
Here is an example of a game of Minesweeper:
There are 16 true mines of fixed position in the grid
.....
.....
.....
.....
.....
m 1 1
.....
.....
.....
.....
.....
c 1 1
1.2..
114..
..2..
1..1.
11..1
c 3 3
1.2N.
114..
..22N
1..1N
11..1
r
Resigning!
Y....
Y12YN
.Y22N
Y..1N
Y1.Y1
You got 11 and missed 5 of the 16 mines
In this example, the player first marked the cell at (1, 1) as a mine. Then, the player cleared the cell at (1, 1). The program revealed the number of mines adjacent to each cell, and the player was able to deduce the location of the other mines. The player won the game by correctly identifying all 16 mines.
Source code in the python programming language
'''
Minesweeper game.
There is an n by m grid that has a random number of between 20% to 60%
of randomly hidden 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 neighbourhood, or as a single space ' ' if the
free point is not adjacent to any true mines.
* Of course you loose if you try to clear space that starts on a mine.
* You win when you have correctly identified all mines.
When prompted you may:
Toggle where you think a mine is at position x, y:
m <x> <y>
Clear the grid starting at position x, y (and print the result):
c <x> <y>
Print the grid so far:
p
Resign
r
Resigning will first show the grid with an 'N' for unfound true mines, a
'Y' for found true mines and a '?' for where you marked clear space as a
mine
'''
gridsize = (6, 4)
minerange = (0.2, 0.6)
try:
raw_input
except:
raw_input = input
import random
from itertools import product
from pprint import pprint as pp
def gridandmines(gridsize=gridsize, minerange=minerange):
xgrid, ygrid = gridsize
minmines, maxmines = minerange
minecount = xgrid * ygrid
minecount = random.randint(int(minecount*minmines), int(minecount*maxmines))
grid = set(product(range(xgrid), range(ygrid)))
mines = set(random.sample(grid, minecount))
show = {xy:'.' for xy in grid}
return grid, mines, show
def printgrid(show, gridsize=gridsize):
xgrid, ygrid = gridsize
grid = '\n'.join(''.join(show[(x,y)] for x in range(xgrid))
for y in range(ygrid))
print( grid )
def resign(showgrid, mines, markedmines):
for m in mines:
showgrid[m] = 'Y' if m in markedmines else 'N'
def clear(x,y, showgrid, grid, mines, markedmines):
if showgrid[(x, y)] == '.':
xychar = str(sum(1
for xx in (x-1, x, x+1)
for yy in (y-1, y, y+1)
if (xx, yy) in mines ))
if xychar == '0': xychar = '.'
showgrid[(x,y)] = xychar
for xx in (x-1, x, x+1):
for yy in (y-1, y, y+1):
xxyy = (xx, yy)
if ( xxyy != (x, y)
and xxyy in grid
and xxyy not in mines | markedmines ):
clear(xx, yy, showgrid, grid, mines, markedmines)
if __name__ == '__main__':
grid, mines, showgrid = gridandmines()
markedmines = set([])
print( __doc__ )
print( '\nThere are %i true mines of fixed position in the grid\n' % len(mines) )
printgrid(showgrid)
while markedmines != mines:
inp = raw_input('m x y/c x y/p/r: ').strip().split()
if inp:
if inp[0] == 'm':
x, y = [int(i)-1 for i in inp[1:3]]
if (x,y) in markedmines:
markedmines.remove((x,y))
showgrid[(x,y)] = '.'
else:
markedmines.add((x,y))
showgrid[(x,y)] = '?'
elif inp[0] == 'p':
printgrid(showgrid)
elif inp[0] == 'c':
x, y = [int(i)-1 for i in inp[1:3]]
if (x,y) in mines | markedmines:
print( '\nKLABOOM!! You hit a mine.\n' )
resign(showgrid, mines, markedmines)
printgrid(showgrid)
break
clear(x,y, showgrid, grid, mines, markedmines)
printgrid(showgrid)
elif inp[0] == 'r':
print( '\nResigning!\n' )
resign(showgrid, mines, markedmines)
printgrid(showgrid)
break
print( '\nYou got %i and missed %i of the %i mines'
% (len(mines.intersection(markedmines)),
len(markedmines.difference(mines)),
len(mines)) )
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Delphi programming language
You may also check:How to resolve the algorithm Numeric error propagation step by step in the Factor programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Go programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Frink programming language
You may also check:How to resolve the algorithm XML/DOM serialization step by step in the Caché ObjectScript programming language