import string

import random

def setupgrid ( gridsize , start , numberofmines ) :

grid = [ [ '0' for i in range ( gridsize ) ] for i in range ( gridsize ) ]

mines = generatemines ( grid , start , numberofmines )

getnumbers ( grid )

return ( grid , mines )

def showgrid ( grid ) :

gridsize = len ( grid )

horizontal = ' ' + 4 *gridsize* '-' + '-'

# Print top column letters

toplabel = ' '

for i in string . ascii_lowercase [ :gridsize ] :

toplabel = toplabel+i+ ' '

print '

' +toplabel+ '

' +horizontal

# Print left row numbers

for idx , i in enumerate ( grid ) :

row = '{0:2} |' . format ( idx+ 1 )

for j in i:

row = row+ ' ' +j+ ' |'

print row+ '

' +horizontal

print ''

def getrandomcell ( grid ) :

gridsize = len ( grid )

a = random . randint ( 0 , gridsize- 1 )

b = random . randint ( 0 , gridsize- 1 )

return ( a , b )

def getneighbors ( grid , rowno , colno ) :

gridsize = len ( grid )

row = grid [ rowno ]

column = grid [ rowno ] [ colno ]

neighbors = [ ]

for i in range ( - 1 , 2 ) :

for j in range ( - 1 , 2 ) :

if i == 0 and j == 0 : continue

elif - 1 < rowno+i < gridsize and - 1 < colno+j < gridsize:

neighbors. append ( ( rowno+i , colno+j ) )

return neighbors

# Generate mines

def generatemines ( grid , start , numberofmines ) :

gridsize = len ( grid )

mines = [ ]

for i in range ( numberofmines ) :

cell = getrandomcell ( grid )

while cell == ( start [ 0 ] , start [ 1 ] ) or cell in mines:

cell = getrandomcell ( grid )

else : mines. append ( cell )

for i , j in mines: grid [ i ] [ j ] = 'X'

return mines

def getnumbers ( grid ) :

gridsize = len ( grid )

for rowno , row in enumerate ( grid ) :

for colno , col in enumerate ( row ) :

if col != 'X' :

# Gets the values of the neighbors

values = [ grid [ r ] [ c ] for r , c in getneighbors ( grid , rowno , colno ) ]

# Counts how many are mines

grid [ rowno ] [ colno ] = str ( values. count ( 'X' ) )

def floodfill ( grid , currgrid , rowno , colno , checked = [ ] ) :

gridsize = len ( grid )

neighbors = getneighbors ( grid , rowno , colno )

for r , c in neighbors:

if ( r , c ) not in checked:

checked. append ( ( r , c ) )

if grid [ r ] [ c ] != 'X' and currgrid [ r ] [ c ] != 'F' :

currgrid [ r ] [ c ] = grid [ r ] [ c ]

if grid [ r ] [ c ] == '0' :

floodfill ( grid , currgrid , r , c )

def playagain ( ) :

choice = raw_input ( 'Play again? (y/n): ' )

if choice == 'y' : return True

return False

def playgame ( ) :

numberofmines = 10

gridsize = 9

currgrid = [ [ ' ' for i in range ( gridsize ) ] for i in range ( gridsize ) ]

showgrid ( currgrid )

grid = [ ]

flags = [ ]

helpmessage = "Type the column followed by the row (eg. a5).

To put or remove a flag, add 'f' to the cell (eg. a5f)

"

print helpmessage

while True :

while True :

lastcell = str ( raw_input ( 'Enter the cell: ' ) )

print ''

flag = False

try :

if lastcell [ 2 ] == 'f' : flag = True

except IndexError : pass

try :

if lastcell == 'help' :

print helpmessage

else :

lastcell = ( int ( lastcell [ 1 ] ) - 1 , string . ascii_lowercase . index ( lastcell [ 0 ] ) )

break

except ( IndexError , ValueError ) :

print "Invalid cell



" +helpmessage

if len ( grid ) == 0 :

grid , mines = setupgrid ( gridsize , lastcell , numberofmines )

rowno , colno = lastcell

if flag:

# Check if the cell is empty

if currgrid [ rowno ] [ colno ] == ' ' :

currgrid [ rowno ] [ colno ] = 'F'

flags. append ( ( rowno , colno ) )

# Remove a flag

elif currgrid [ rowno ] [ colno ] == 'F' :

currgrid [ rowno ] [ colno ] = ' '

flags. remove ( ( rowno , colno ) )

else : print 'Cannot put a flag there'

else :

# If there is a flag there, remove flag from list

if ( rowno , colno ) in flags:

flags. remove ( ( rowno , colno ) )

# Show cell

currgrid [ rowno ] [ colno ] = grid [ rowno ] [ colno ]

if grid [ rowno ] [ colno ] == 'X' :

print 'Game Over'

showgrid ( grid )

if playagain ( ) : playgame ( )

else : break

elif grid [ rowno ] [ colno ] == '0' :

floodfill ( grid , currgrid , rowno , colno )

showgrid ( currgrid )

if set ( flags ) == set ( mines ) :

print 'You Win'

if playagain ( ) : playgame ( )

else : break