Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Minesweeper
import tkinter as tk
import random
class Model(object):
"""Crates a board and adds mines to it"""
def create_grid(self):
"""Create a self.width by self.height grid of elements with value
0"""
self.grid = [[0] * self.width for i in range(self.height)]
def add_mines(self):
"""Randomly adds the amount of self.num_mines to grid"""
def get_coords():
row = random.randint(0, self.height - 1)
col = random.randint(0, self.width - 1)
return row, col
for i in range(self.num_mines):
row, col = get_coords()
while self.grid[row][col] == "b":
row, col = get_coords()
self.grid[row][col] = "b"
for i in self.grid:
print(i)
class View(tk.Frame):
"""Creates a main window and grid of button cells"""
def create_widgets(self):
"""Create cell button widgets"""
self.buttons = {}
for i in range(self.height):
for j in range(self.width):
self.buttons[str(i) + "," + str(j)] = tk.Button(
self.master, width=5, bg="grey")
self.buttons[str(i) + "," + str(j)].grid(row=i + 1,
column=j + 1)
def disp_loss(self):
"""Display the loss label when loss condition is reached"""
self.top_panel.loss_label.grid(row=0, columnspan=5)
def disp_win(self):
"""Display the win label when win condition is reached"""
self.top_panel.win_label.grid(row=0, columnspan=5)
class TopPanel(tk.Frame):
"""Create top panel which houses reset button and win/loss and
mines left labels."""
def create_widgets(self):
self.reset_button = tk.Button(self.master, width=7, text="Reset")
self.reset_button.grid(row=0, columnspan=int((self.width * 7) / 2))
# Create win and loss labels
self.loss_label = tk.Label(text="You Lose!", bg="red")
self.win_label = tk.Label(text="You Win!", bg="green")
# Create number of mines remaining label
self.mine_count = tk.StringVar()
self.mine_count.set("Mines remaining: " + str(self.num_mines))
self.mines_left = tk.Label(textvariable=self.mine_count)
self.mines_left.grid(row=0, columnspan=5)
class Controller():
"""Sets up button bindings and minsweeper game logic.
def bindings(self):
"""Set up reveal cell and flag cell key bindings"""
for i in range(self.height):
for j in range(self.width):
# Right click bind to reveal decision method
self.view.buttons[str(i) + "," + str(j)].bind(
"<Button-1>",
lambda event, index=[i, j]: self.reveal(event, index))
# Left click bind to flag method
self.view.buttons[str(i) + "," + str(j)].bind(
"<Button-3>",
lambda event, index=[i, j]: self.flag(event, index))
# Set up reset button
self.view.top_panel.reset_button.bind("<Button>", self.reset)
def increment():
try:
if self.model.grid[pos[0]][pos[1]] == "b":
return 1
except IndexError:
pass
return 0
additions = [
[i, j + 1], [i + 1, j], [i + 1, j + 1], [i, j - 1],
[i + 1, j - 1], [i - 1, j], [i - 1, j + 1], [i - 1, j - 1]
]
# Adds 1 to num_mines if cell is adjacent to a mine
for pos in additions:
if 0 <= pos[0] <= self.height - 1 and 0 <= pos[
1] <= self.width - 1:
num_mines += increment()
return num_mines
def win(self):
"""Display win"""
self.view.hide_labels("mine")
self.view.disp_win()
self.game_state = "win"
def loss(self):
"""Display loss. Reveal all cells when a mine is clicked"""
self.view.hide_labels("mine")
for i in range(self.height):
for j in range(self.width):
val = self.give_val([i, j])
self.reveal_cell(val, [i, j])
self.view.disp_loss()
def update_mines(self):
"""Update mine counter"""
mines_left = self.num_mines - len(self.cells_flagged)
if mines_left >= 0:
self.view.top_panel.mine_count.set(
"Mines remaining: " + str(mines_left))
def main():
n = input("Pick a difficulty: Easy, Medium, or Hard. ")
values = []
n = n.split(',')
for number in n:
number = int(number)
values.append(number)
print (values)
if __name__ == "__main__":
main()