From 5cfdbdbcb1cf915596ab4fc3771991295f504e26 Mon Sep 17 00:00:00 2001 From: Murray Watson Date: Mon, 16 Jan 2017 18:04:33 +0000 Subject: [PATCH 1/4] Update minesweeper.py --- minesweeper.py | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/minesweeper.py b/minesweeper.py index 7adbdf1..5bf664f 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -1,58 +1,47 @@ +#!/usr/bin/env python3 from tkinter import * - -# Dummy function for feature implementation tracking -def doNothing(feature): - print("Feature not Implemented: " + feature) - +from random import randint class Field: - - board=[] - def __init__(self, base): + self.board = [] fieldFrame = Frame(base) fieldFrame.pack(side=BOTTOM) - for row in range(0,15): - self.board.append([]) - for column in range(0,20): - self.board[row].append(Box(self.board, fieldFrame, row, column)) - - + for row in range(15): + self.board.append([]) + for col in range(20): + self.board[row].append(Box(self.board, fieldFrame, row, col)) class Box(Field): - - opened = False - containsMine = True - flagged = False - def __init__(self, field, frame, row, column): + self.opened = False + self.containsMine = True if randint(1, 10) % 10 == 0 else False + self.flagged = False + self.gameField = field self.button = Button(frame, width=2, height=1) self.row = row self.column = column self.button.grid(row=row, column=column) - self.button.bind("", self.openBox()) + self.button.bind("", self.open_box) -## Running function on startup - def openBox(self): + def open_box(self, x): self.opened = True - if(self.containsMine==True): - print("Game Over") + if self.containsMine: print("Game Over") +def do_nothing(feature): + print("Feature not Implemented yet: {}".format(feature)) -# Create base window base = Tk() menu = Menu(base) base.config(menu=menu) gameMenu = Menu(menu) menu.add_cascade(label="Game", menu=gameMenu) -gameMenu.add_command(label="Options", command=lambda doNothing=doNothing: doNothing("Game Settings")) +gameMenu.add_command(label="Options", command=lambda doNothing=do_nothing: do_nothing("Game Settings")) gameMenu.add_separator() gameMenu.add_command(label="Exit", command=base.quit) - gameField = Field(base) - -base.mainloop() \ No newline at end of file +base.mainloop() From 60233adc6d1532469a63a95ab83288a8abe7b06e Mon Sep 17 00:00:00 2001 From: Murray Watson Date: Mon, 16 Jan 2017 20:06:36 +0000 Subject: [PATCH 2/4] Update minesweeper.py --- minesweeper.py | 133 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 115 insertions(+), 18 deletions(-) diff --git a/minesweeper.py b/minesweeper.py index 5bf664f..1bd6bfb 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -1,47 +1,144 @@ -#!/usr/bin/env python3 from tkinter import * -from random import randint +import random +# Dummy function for feature implementation tracking +def doNothing(feature): + print("Feature not Implemented: " + feature) + class Field: + + board=[] + height = 10 + width = 10 + noOfMines = 50 + def __init__(self, base): - self.board = [] fieldFrame = Frame(base) fieldFrame.pack(side=BOTTOM) - - for row in range(15): + for row in range(0,self.height): self.board.append([]) - for col in range(20): - self.board[row].append(Box(self.board, fieldFrame, row, col)) + for column in range(0,self.width): + self.board[row].append(Box(self.board, fieldFrame, row, column)) + + self.plantMines() + self.showMines() + + def plantMines(self): + while(self.noOfMines>0): + for row in range(0, self.height): + for column in range(0, self.width): + if (random.randint(0, 100) < 2): + if(self.board[row][column].containsMine==False): + self.board[row][column].containsMine = True + self.board[row][column].opened = True + self.noOfMines = self.noOfMines - 1 + + def showMines(self): + for row in range(0, self.height): + for column in range(0, self.width): + if(self.board[row][column].containsMine==True): + self.board[row][column].button.config(bg="blue") + class Box(Field): - def __init__(self, field, frame, row, column): - self.opened = False - self.containsMine = True if randint(1, 10) % 10 == 0 else False - self.flagged = False + opened = False + containsMine = False + flagged = False + nearbyMines = 0 + + def __init__(self, field, frame, row, column): self.gameField = field self.button = Button(frame, width=2, height=1) + self.button.config(bg="white smoke") + self.button.config(relief=GROOVE) self.row = row self.column = column self.button.grid(row=row, column=column) - self.button.bind("", self.open_box) + self.button.bind("", self.openBox) + #self.button.bind("", self.helper) + self.button.bind("", self.flagBox) + ## TODO: Calculate nearby mines somehow + + def number_mines(self): + total = [] + + #top + if self.row > 0: + if self.row < Field.height - 1: + for i in [0, 1, -1]: + try: + total.append(Field.board[self.row - 1][self.column + i].containsMine) + except IndexError: + pass + + #bottom + if self.row < Field.height - 1: + for i in [0, 1, -1]: + try: + total.append(Field.board[self.row + 1][self.column + i].containsMine) + except IndexError: + pass + + #left + if self.column > 0: total.append(Field.board[self.row][self.column - 1].containsMine) - def open_box(self, x): - self.opened = True - if self.containsMine: print("Game Over") + #right + if self.column < Field.width - 1: total.append(Field.board[self.row][self.column + 1].containsMine) -def do_nothing(feature): - print("Feature not Implemented yet: {}".format(feature)) + total = sum(total) + print("Total Number of Mines: {}".format(total)) + return total + def openBox(self, event): + x = self.number_mines() + print(self.row, self.column) + if(self.flagged==True or self.opened==True): + return 0 + else: + self.opened = True + if(self.containsMine==True): + print("Game Over") + elif(not x): + ## open box and scan for nearby boxes to open + print("No nearby mines") + return + else: + x = self.number_mines() + self.button.config(relief=SUNKEN) + self.button.config(text=x) + self.button.config(bg="snow") + self.button.config(state=DISABLED) + print(self.nearbyMines) + return + + def flagBox(self, event): + if(self.opened==True): + return 0 + else: + if(self.flagged==False): + self.flagged = True + self.button.config(bg = "red") + self.button.config(state=DISABLED) + return + elif(self.flagged==True): + self.flagged = False + self.button.config(bg="white smoke") + self.button.config(state=NORMAL) + + +# Create base window base = Tk() menu = Menu(base) base.config(menu=menu) gameMenu = Menu(menu) menu.add_cascade(label="Game", menu=gameMenu) -gameMenu.add_command(label="Options", command=lambda doNothing=do_nothing: do_nothing("Game Settings")) +gameMenu.add_command(label="Options", command=lambda doNothing=doNothing: doNothing("Game Settings")) gameMenu.add_separator() gameMenu.add_command(label="Exit", command=base.quit) + gameField = Field(base) +gameField.showMines() base.mainloop() From 7b3c73571d2cf45a35d7eafd22afe839af4fa4fb Mon Sep 17 00:00:00 2001 From: Murray Watson Date: Mon, 16 Jan 2017 20:11:39 +0000 Subject: [PATCH 3/4] Update minesweeper.py --- minesweeper.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/minesweeper.py b/minesweeper.py index 1bd6bfb..6f1e1a6 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -8,9 +8,9 @@ def doNothing(feature): class Field: board=[] - height = 10 - width = 10 - noOfMines = 50 + height = 5 + width = 5 + noOfMines = 20 def __init__(self, base): fieldFrame = Frame(base) @@ -64,21 +64,18 @@ def number_mines(self): total = [] #top - if self.row > 0: - if self.row < Field.height - 1: - for i in [0, 1, -1]: - try: - total.append(Field.board[self.row - 1][self.column + i].containsMine) - except IndexError: - pass + for i in [0, 1, -1]: + try: + total.append(Field.board[self.row - 1][self.column + i].containsMine) + except IndexError: + pass #bottom - if self.row < Field.height - 1: - for i in [0, 1, -1]: - try: - total.append(Field.board[self.row + 1][self.column + i].containsMine) - except IndexError: - pass + for i in [0, 1, -1]: + try: + total.append(Field.board[self.row + 1][self.column + i].containsMine) + except IndexError: + pass #left if self.column > 0: total.append(Field.board[self.row][self.column - 1].containsMine) @@ -109,7 +106,6 @@ def openBox(self, event): self.button.config(text=x) self.button.config(bg="snow") self.button.config(state=DISABLED) - print(self.nearbyMines) return def flagBox(self, event): From 2f83ef2e35737a104d14de9f9de45f0b4ad64328 Mon Sep 17 00:00:00 2001 From: Murray Watson Date: Mon, 16 Jan 2017 20:53:04 +0000 Subject: [PATCH 4/4] Update minesweeper.py --- minesweeper.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/minesweeper.py b/minesweeper.py index 6f1e1a6..6d3eaac 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -8,9 +8,9 @@ def doNothing(feature): class Field: board=[] - height = 5 - width = 5 - noOfMines = 20 + height = 10 + width = 10 + noOfMines = 15 def __init__(self, base): fieldFrame = Frame(base) @@ -64,18 +64,16 @@ def number_mines(self): total = [] #top - for i in [0, 1, -1]: - try: - total.append(Field.board[self.row - 1][self.column + i].containsMine) - except IndexError: - pass + if self.row != 0: + if self.column != Field.width - 1: total.append(Field.board[self.row - 1][self.column + 1].containsMine) + total.append(Field.board[self.row - 1][self.column].containsMine) + if self.column != 0: total.append(Field.board[self.row - 1][self.column - 1].containsMine) #bottom - for i in [0, 1, -1]: - try: - total.append(Field.board[self.row + 1][self.column + i].containsMine) - except IndexError: - pass + if self.row != Field.height - 1: + total.append(Field.board[self.row + 1][self.column].containsMine) + if self.column != Field.width - 1: total.append(Field.board[self.row + 1][self.column + 1].containsMine) + if self.column != 0: total.append(Field.board[self.row + 1][self.column - 1].containsMine) #left if self.column > 0: total.append(Field.board[self.row][self.column - 1].containsMine) @@ -83,6 +81,7 @@ def number_mines(self): #right if self.column < Field.width - 1: total.append(Field.board[self.row][self.column + 1].containsMine) + print(total) total = sum(total) print("Total Number of Mines: {}".format(total)) return total @@ -94,11 +93,12 @@ def openBox(self, event): return 0 else: self.opened = True - if(self.containsMine==True): + if(Field.board[self.row][self.column].containsMine==True): print("Game Over") elif(not x): ## open box and scan for nearby boxes to open print("No nearby mines") + self.button.config(bg="green") return else: x = self.number_mines()