'Python Minesweeper Game Dictionary
I am making a copy of Minesweeper for a project in my class. My professor recommended that I randomly layout 10 bombs on the game's 9x9 grid. I was able to create the dictionary but am struggling with how to place the bombs. I have the squares labeled as a1, a2, a3..., b1, b2, b3..., etc. This is the code, which I have nested in the main
function that creates the dictionary, and prints it out after the graphics window to make sure it is updating properly.
all_locations = ['a1','a2','a3','a4','a5','a6','a7','a8','a9',
'b1','b2','b3','b4','b5','b6','b7','b8','b9',
'c1','c2','c3','c4','c5','c6','c7','c8','c9',
'd1','d2','d3','d4','d5','d6','d7','d8','d9',
'e1','e2','e3','e4','e5','e6','e7','e8','e9',
'f1','f2','f3','f4','f5','f6','f7','f8','f9',
'g1','g2','g3','g4','g5','g6','g7','g8','g9',
'h1','h2','h3','h4','h5','h6','h7','h8','h9',
'i1','i2','i3','i4','i5','i6','i7','i8','i9']
i = 0
gridDictionary = {}
for x in '123456789':
for y in '123456789':
gridDictionary[x+y] = [0, 0, all_locations[i]]
i = i + 1
print(gridDictionary)
print(gridDictionary)
I am attaching all of the code below, however, it is not completed and is currently just in fragments.
# Import Python libraries
import random, graphics
from graphics import *
def drawGrid():
# Each square is 60px x 60px, there is 5px of padding along sides and top,
# and 30px at the bottom to make room for info messages / user interaction.
# Creates window named "Minesweeper"
win = graphics.GraphWin("Minesweeper", 545, 570)
win.setBackground(color_rgb(250, 249, 246))
# Outline of 9x9 grid
gridOutline = graphics.Rectangle(graphics.Point(5, 5), graphics.Point(545, 545))
gridOutline.setFill(color_rgb(211, 211, 211))
# Coordinates for squares: A1 - A9
a1 = graphics.Rectangle(graphics.Point(5, 5), graphics.Point(65, 65))
a2 = graphics.Rectangle(graphics.Point(65, 5), graphics.Point(125, 65))
a3 = graphics.Rectangle(graphics.Point(125, 5), graphics.Point(185, 65))
a4 = graphics.Rectangle(graphics.Point(185, 5), graphics.Point(245, 65))
a5 = graphics.Rectangle(graphics.Point(245, 5), graphics.Point(305, 65))
a6 = graphics.Rectangle(graphics.Point(305, 5), graphics.Point(365, 65))
a7 = graphics.Rectangle(graphics.Point(365, 5), graphics.Point(425, 65))
a8 = graphics.Rectangle(graphics.Point(425, 5), graphics.Point(485, 65))
a9 = graphics.Rectangle(graphics.Point(485, 5), graphics.Point(545, 65))
# Coordinates for squares: B1 - B9
b1 = graphics.Rectangle(graphics.Point(5, 65), graphics.Point(65, 125))
b2 = graphics.Rectangle(graphics.Point(65, 65), graphics.Point(125, 125))
b3 = graphics.Rectangle(graphics.Point(125, 65), graphics.Point(185, 125))
b4 = graphics.Rectangle(graphics.Point(185, 65), graphics.Point(245, 125))
b5 = graphics.Rectangle(graphics.Point(245, 65), graphics.Point(305, 125))
b6 = graphics.Rectangle(graphics.Point(305, 65), graphics.Point(365, 125))
b7 = graphics.Rectangle(graphics.Point(365, 65), graphics.Point(425, 125))
b8 = graphics.Rectangle(graphics.Point(425, 65), graphics.Point(485, 125))
b9 = graphics.Rectangle(graphics.Point(485, 65), graphics.Point(545, 125))
# Coordinates for squares: C1 - C9
c1 = graphics.Rectangle(graphics.Point(5, 125), graphics.Point(65, 185))
c2 = graphics.Rectangle(graphics.Point(65, 125), graphics.Point(125, 185))
c3 = graphics.Rectangle(graphics.Point(125, 125), graphics.Point(185, 185))
c4 = graphics.Rectangle(graphics.Point(185, 125), graphics.Point(245, 185))
c5 = graphics.Rectangle(graphics.Point(245, 125), graphics.Point(305, 185))
c6 = graphics.Rectangle(graphics.Point(305, 125), graphics.Point(365, 185))
c7 = graphics.Rectangle(graphics.Point(365, 125), graphics.Point(425, 185))
c8 = graphics.Rectangle(graphics.Point(425, 125), graphics.Point(485, 185))
c9 = graphics.Rectangle(graphics.Point(485, 125), graphics.Point(545, 185))
# Coordinates for squares: D1 - D9
d1 = graphics.Rectangle(graphics.Point(5, 185), graphics.Point(65, 245))
d2 = graphics.Rectangle(graphics.Point(65, 185), graphics.Point(125, 245))
d3 = graphics.Rectangle(graphics.Point(125, 185), graphics.Point(185, 245))
d4 = graphics.Rectangle(graphics.Point(185, 185), graphics.Point(245, 245))
d5 = graphics.Rectangle(graphics.Point(245, 185), graphics.Point(305, 245))
d6 = graphics.Rectangle(graphics.Point(305, 185), graphics.Point(365, 245))
d7 = graphics.Rectangle(graphics.Point(365, 185), graphics.Point(425, 245))
d8 = graphics.Rectangle(graphics.Point(425, 185), graphics.Point(485, 245))
d9 = graphics.Rectangle(graphics.Point(485, 185), graphics.Point(545, 245))
# Coordinates for squares: E1 - E9
e1 = graphics.Rectangle(graphics.Point(5, 245), graphics.Point(65, 305))
e2 = graphics.Rectangle(graphics.Point(65, 245), graphics.Point(125, 305))
e3 = graphics.Rectangle(graphics.Point(125, 245), graphics.Point(185, 305))
e4 = graphics.Rectangle(graphics.Point(185, 245), graphics.Point(245, 305))
e5 = graphics.Rectangle(graphics.Point(245, 245), graphics.Point(305, 305))
e6 = graphics.Rectangle(graphics.Point(305, 245), graphics.Point(365, 305))
e7 = graphics.Rectangle(graphics.Point(365, 245), graphics.Point(425, 305))
e8 = graphics.Rectangle(graphics.Point(425, 245), graphics.Point(485, 305))
e9 = graphics.Rectangle(graphics.Point(485, 245), graphics.Point(545, 305))
# Coordinates for squares: F1 - F9
f1 = graphics.Rectangle(graphics.Point(5, 305), graphics.Point(65, 365))
f2 = graphics.Rectangle(graphics.Point(65, 305), graphics.Point(125, 365))
f3 = graphics.Rectangle(graphics.Point(125, 305), graphics.Point(185, 365))
f4 = graphics.Rectangle(graphics.Point(185, 305), graphics.Point(245, 365))
f5 = graphics.Rectangle(graphics.Point(245, 305), graphics.Point(305, 365))
f6 = graphics.Rectangle(graphics.Point(305, 305), graphics.Point(365, 365))
f7 = graphics.Rectangle(graphics.Point(365, 305), graphics.Point(425, 365))
f8 = graphics.Rectangle(graphics.Point(425, 305), graphics.Point(485, 365))
f9 = graphics.Rectangle(graphics.Point(485, 305), graphics.Point(545, 365))
# Coordinates for squares: G1 - G9
g1 = graphics.Rectangle(graphics.Point(5, 365), graphics.Point(65, 425))
g2 = graphics.Rectangle(graphics.Point(65, 365), graphics.Point(125, 425))
g3 = graphics.Rectangle(graphics.Point(125, 365), graphics.Point(185, 425))
g4 = graphics.Rectangle(graphics.Point(185, 365), graphics.Point(245, 425))
g5 = graphics.Rectangle(graphics.Point(245, 365), graphics.Point(305, 425))
g6 = graphics.Rectangle(graphics.Point(305, 365), graphics.Point(365, 425))
g7 = graphics.Rectangle(graphics.Point(365, 365), graphics.Point(425, 425))
g8 = graphics.Rectangle(graphics.Point(425, 365), graphics.Point(485, 425))
g9 = graphics.Rectangle(graphics.Point(485, 365), graphics.Point(545, 425))
# Coordinates for squares: H1 - H9
h1 = graphics.Rectangle(graphics.Point(5, 425), graphics.Point(65, 485))
h2 = graphics.Rectangle(graphics.Point(65, 425), graphics.Point(125, 485))
h3 = graphics.Rectangle(graphics.Point(125, 425), graphics.Point(185, 485))
h4 = graphics.Rectangle(graphics.Point(185, 425), graphics.Point(245, 485))
h5 = graphics.Rectangle(graphics.Point(245, 425), graphics.Point(305, 485))
h6 = graphics.Rectangle(graphics.Point(305, 425), graphics.Point(365, 485))
h7 = graphics.Rectangle(graphics.Point(365, 425), graphics.Point(425, 485))
h8 = graphics.Rectangle(graphics.Point(425, 425), graphics.Point(485, 485))
h9 = graphics.Rectangle(graphics.Point(485, 425), graphics.Point(545, 485))
# Coordinates for squares: I1 - I9
i1 = graphics.Rectangle(graphics.Point(5, 485), graphics.Point(65, 545))
i2 = graphics.Rectangle(graphics.Point(65, 485), graphics.Point(125, 545))
i3 = graphics.Rectangle(graphics.Point(125, 485), graphics.Point(185, 545))
i4 = graphics.Rectangle(graphics.Point(185, 485), graphics.Point(245, 545))
i5 = graphics.Rectangle(graphics.Point(245, 485), graphics.Point(305, 545))
i6 = graphics.Rectangle(graphics.Point(305, 485), graphics.Point(365, 545))
i7 = graphics.Rectangle(graphics.Point(365, 485), graphics.Point(425, 545))
i8 = graphics.Rectangle(graphics.Point(425, 485), graphics.Point(485, 545))
i9 = graphics.Rectangle(graphics.Point(485, 485), graphics.Point(545, 545))
welcomeText = Text(Point(272.5, 560), "Welcome to Minesweeper, would you like to read the instructions? (Y/N)")
welcomeText.draw(win)
gridOutline.draw(win)
# Prints squares A1 - A9
a1.draw(win)
a2.draw(win)
a3.draw(win)
a4.draw(win)
a5.draw(win)
a6.draw(win)
a7.draw(win)
a8.draw(win)
a9.draw(win)
# Prints squares B1 - B9
b1.draw(win)
b2.draw(win)
b3.draw(win)
b4.draw(win)
b5.draw(win)
b6.draw(win)
b7.draw(win)
b8.draw(win)
b9.draw(win)
# Prints squares C1 - C9
c1.draw(win)
c2.draw(win)
c3.draw(win)
c4.draw(win)
c5.draw(win)
c6.draw(win)
c7.draw(win)
c8.draw(win)
c9.draw(win)
# Prints squares D1 - D9
d1.draw(win)
d2.draw(win)
d3.draw(win)
d4.draw(win)
d5.draw(win)
d6.draw(win)
d7.draw(win)
d8.draw(win)
d9.draw(win)
# Prints squares E1 - E9
e1.draw(win)
e2.draw(win)
e3.draw(win)
e4.draw(win)
e5.draw(win)
e6.draw(win)
e7.draw(win)
e8.draw(win)
e9.draw(win)
# Prints squares F1 - F9
f1.draw(win)
f2.draw(win)
f3.draw(win)
f4.draw(win)
f5.draw(win)
f6.draw(win)
f7.draw(win)
f8.draw(win)
f9.draw(win)
# Prints squares G1 - G9
g1.draw(win)
g2.draw(win)
g3.draw(win)
g4.draw(win)
g5.draw(win)
g6.draw(win)
g7.draw(win)
g8.draw(win)
g9.draw(win)
# Prints squares H1 - H9
h1.draw(win)
h2.draw(win)
h3.draw(win)
h4.draw(win)
h5.draw(win)
h6.draw(win)
h7.draw(win)
h8.draw(win)
h9.draw(win)
# Prints squares I1 - I9
i1.draw(win)
i2.draw(win)
i3.draw(win)
i4.draw(win)
i5.draw(win)
i6.draw(win)
i7.draw(win)
i8.draw(win)
i9.draw(win)
win.getMouse() # Pause to view result, click on window to close it
win.close() # Close window when done
def instructions():
print("welcome to Minesweeper!")
a = input("Do you wish to read the instructions? (Y/N): ")
if a == "N" or a == "NO": #If user inputs "N" or "NO", skips instructions and starts game
print("Let the game begin!")
elif a == "Y" or a == "YES": #Else-if user inputs "Y" or "YES", prints instructions, then starts game
print('''In Minesweeper you are presented with a grid of squares, some containing mines, some not. If you click on a
mine, you lose. When you click on a square that is not a mine, the square will declare the number of mines surrounding
it. Once all of the mineless squares are turned over, you win!''')
print("Let the game begin!")
else:
print("Please pick between (Y/N)") #Exception if user does not input correctly
main() #Restarts the program
def numbersofneighbors(gridDictionary):
for l,k in gridDictionary.items():
r,c=int(l[0]),int(l[1])
neighbortiles=[(r-1,c),(r-1,c-1),(r,c-1),(r+1,c-1),(r+1,c),(r+1,c+1),(r,c+1),(r-1,c+1)]
for n in neighbortiles:
if 1 <= n[0] <= 9 and 1 <= n[1] <= 9 and k[0]!=0:
key=str(n[0])+str(n[1])
gridDictionary[key][1]+=1
return gridDictionary
def main():
drawGrid()
#Assigns (x1, y1) and (x2, y2) values for A1 - A9
a1 = [(5, 5), (65, 65)]
a2 = [(65, 5), (125, 65)]
a3 = [(125, 5), (185, 65)]
a4 = [(185, 5), (245, 65)]
a5 = [(245, 5), (305, 65)]
a6 = [(305, 5), (365, 65)]
a7 = [(365, 5), (425, 65)]
a8 = [(425, 5), (485, 65)]
a9 = [(485, 5), (545, 65)]
#Assigns (x1, y1) and (x2, y2) values for B1 - B9
b1 = [(5, 65), (65, 125)]
b2 = [(65, 65), (125, 125)]
b3 = [(125, 65), (185, 125)]
b4 = [(185, 65), (245, 125)]
b5 = [(245, 65), (305, 125)]
b6 = [(305, 65), (365, 125)]
b7 = [(365, 65), (425, 125)]
b8 = [(425, 65), (485, 125)]
b9 = [(485, 65), (545, 125)]
#Assigns (x1, y1) and (x2, y2) values for C1 - C9
c1 = [(5, 125), (65, 185)]
c2 = [(65, 125), (125, 185)]
c3 = [(125, 125), (185, 185)]
c4 = [(185, 125), (245, 185)]
c5 = [(245, 125), (305, 185)]
c6 = [(305, 125), (365, 185)]
c7 = [(365, 125), (425, 185)]
c8 = [(425, 125), (485, 185)]
c9 = [(485, 125), (545, 185)]
#Assigns (x1, y1) and (x2, y2) values for D1 - D9
d1 = [(5, 185), (65, 245)]
d2 = [(65, 185), (125, 245)]
d3 = [(125, 185), (185, 245)]
d4 = [(185, 185), (245, 245)]
d5 = [(245, 185), (305, 245)]
d6 = [(305, 185), (365, 245)]
d7 = [(365, 185), (425, 245)]
d8 = [(425, 185), (485, 245)]
d9 = [(485, 185), (545, 245)]
#Assigns (x1, y1) and (x2, y2) values for E1 - E9
e1 = [(5, 245), (65, 305)]
e2 = [(65, 245), (125, 305)]
e3 = [(125, 245), (185, 305)]
e4 = [(185, 245), (245, 305)]
e5 = [(245, 245), (305, 305)]
e6 = [(305, 245), (365, 305)]
e7 = [(365, 245), (425, 305)]
e8 = [(425, 245), (485, 305)]
e9 = [(485, 245), (545, 305)]
#Assigns (x1, y1) and (x2, y2) values for F1 - F9
f1 = [(5, 305), (65, 365)]
f2 = [(65, 305), (125, 365)]
f3 = [(125, 305), (185, 365)]
f4 = [(185, 305), (245, 365)]
f5 = [(245, 305), (305, 365)]
f6 = [(305, 305), (365, 365)]
f7 = [(365, 305), (425, 365)]
f8 = [(425, 305), (485, 365)]
f9 = [(485, 305), (545, 365)]
#Assigns (x1, y1) and (x2, y2) values for G1 - G9
g1 = [(5, 365), (65, 425)]
g2 = [(65, 365), (125, 425)]
g3 = [(125, 365), (185, 425)]
g4 = [(185, 365), (245, 425)]
g5 = [(245, 365), (305, 425)]
g6 = [(305, 365), (365, 425)]
g7 = [(365, 365), (425, 425)]
g8 = [(425, 365), (485, 425)]
g9 = [(485, 365), (545, 425)]
#Assigns (x1, y1) and (x2, y2) values for H1 - H9
h1 = [(5, 425), (65, 485)]
h2 = [(65, 425), (125, 485)]
h3 = [(125, 425), (185, 485)]
h4 = [(185, 425), (245, 485)]
h5 = [(245, 425), (305, 485)]
h6 = [(305, 425), (365, 485)]
h7 = [(365, 425), (425, 485)]
h8 = [(425, 425), (485, 485)]
h9 = [(485, 425), (545, 485)]
#Assigns (x1, y1) and (x2, y2) values for I1 - I9
i1 = [(5, 485), (65, 545)]
i2 = [(65, 485), (125, 545)]
i3 = [(125, 485), (185, 545)]
i4 = [(185, 485), (245, 545)]
i5 = [(245, 485), (305, 545)]
i6 = [(305, 485), (365, 545)]
i7 = [(365, 485), (425, 545)]
i8 = [(425, 485), (485, 545)]
i9 = [(485, 485), (545, 545)]
all_locations = ['a1','a2','a3','a4','a5','a6','a7','a8','a9',
'b1','b2','b3','b4','b5','b6','b7','b8','b9',
'c1','c2','c3','c4','c5','c6','c7','c8','c9',
'd1','d2','d3','d4','d5','d6','d7','d8','d9',
'e1','e2','e3','e4','e5','e6','e7','e8','e9',
'f1','f2','f3','f4','f5','f6','f7','f8','f9',
'g1','g2','g3','g4','g5','g6','g7','g8','g9',
'h1','h2','h3','h4','h5','h6','h7','h8','h9',
'i1','i2','i3','i4','i5','i6','i7','i8','i9']
i = 0
gridDictionary = {}
for x in '123456789':
for y in '123456789':
gridDictionary[x+y] = [0, 0, all_locations[i]]
i = i + 1
print(gridDictionary)
main() #Runs the program
I am currently lost as to how I should continue to develop my code, and any insight would be greatly appreciated.
Solution 1:[1]
Consider this approach for building a minesweeper board (or grid):
import random
import columnar
class Square:
squares = []
def __init__( self, x,y ):
self.x = x
self.y = y
self.bomb = False
self.neighbor = 0
Square.squares.append( self )
def __repr__(self):
if self.bomb:
return " X "
if self.neighbor > 0:
return f" {self.neighbor} "
return " "
#random.seed(10) # include this line to trouble shoot
rows = 9
cols = 9
bombs = 10
grid = [ [ Square(c,r) for c in range(cols)] for r in range(rows)]
random.shuffle( Square.squares )
for sq in Square.squares[:bombs]:
sq.bomb = True
for y in range(-1,2):
for x in range(-1,2):
if y + sq.y >= 0 and y + sq.y < rows and x + sq.x >= 0 and x + sq.x < cols:
grid[ sq.y + y ][ sq.x + x ].neighbor += 1
# Here's a visualization - you can remove columnar calls (and table) if you don't want to install columnar package
table = columnar.columnar(grid)
print(table)
print(grid)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mark |