1

I am currently doing an assignment towards making a minesweeper like game in python using classes. For the backbone of the assignment we are just meant to import the professors premade class and create our own main function to make the class operate. We are not allowed to see the code behind the class and simply have to go off what is described in each class.

the init method is described as constructing a game board that is a square with cells that are filled in by question mark images and has the parameters of (self,size). the hideMines method is hide mines on game board and return a bool indicating if that was successful and uses parameters (self,num). The last is a probe method which finds the closest cell to your mouse click and reveals its icon as well as returns a tuple showing the number of cells probed and the mines remaining and uses parameters (self,x,y).

At this point in my code, the board is created and once my prompt for hiding mines is entered, the middle cell on the board is automatically uncovered and clicking does nothing. My question is, what do I need to make the click properly uncover the icon given my little amount of info?

import turtle
import game

def main():
wn = turtle.Screen()
board = wn.numinput("Numeric Input", "Enter desired board size: ")
board = int(board)
mine = game.Game(board)
nummine = wn.numinput("Numeric Input", "Enter desired number of mines: ")
nummine = int(nummine)
mine.hideMines(nummine)
def closure():
    (find,rem) = mine.probe(board,board)

    if rem == 0:

        over = wn.textinput("Text Input", "Would you like to play again? (Y)es or (N)o")

        if over == 'Y':
            main()
        else:
            turtle.bye()
s = wn.getscreen()
s.onscreenclick(closure())

main()

ruen125
  • 35
  • 4
  • `s.onscreenclick(closure)`? Please refer to [mcve] next time you ask a question, you have provided too much unnecessary information that isn't relevant to your core problem of not being able to respond to the user clicking. – eugenhu Mar 16 '18 at 07:13
  • I'll make sure to keep that in mind for further questions! I have also tried (closure) and receive an error stating that closure takes 0 positional arguments, but 2 were given – ruen125 Mar 16 '18 at 07:17
  • `closure` gets called with the x and y coordinates of the click, you'll need to modify the function definition so it will accept these two extra arguments. `def closure(x, y):` – eugenhu Mar 16 '18 at 07:20

1 Answers1

1

I guess you may want the following code. Without game.py I cannot make sure it works. Anyway happy coding...

import turtle
import game

def main():
    def closure(x, y):
        find, rem = mine.probe(x, y)        
        if rem == 0:
            over = turtle.getscreen().textinput("Text Input", "Would you like to play again? (Y)es or (N)o")
            if over and over[0] == 'Y':
                # Some codes for resetting the board
                pass
            else:
                turtle.bye()

    wn = turtle.Screen()

    board = wn.numinput("Numeric Input", "Enter desired board size: ")
    board = int(board)
    mine = game.Game(board)

    nummine = wn.numinput("Numeric Input", "Enter desired number of mines: ")
    nummine = int(nummine)
    mine.hideMines(nummine)

    wn.onscreenclick(closure)
    wn.mainloop()

main()

[Update] To avoid global mine, closure() went inside main().

ChoF
  • 568
  • 1
  • 4
  • 9