Пример #1
0
def __redraw (b,g,x,y):
    """
    This function draws the board. Positions x and y are used to test
    which bomb icon has to be drawn.

    :param b: the board of buttons
    :type b: list of list of ``button``
    :param g: the minesweeper game
    :type g: game
    :param x: the x-coordinate of the cell
    :type x: int
    :param y: the y-coordinate of the cell
    :type y: int

    """
    global img
    width,height = (minesweeper.get_width(g),minesweeper.get_height(g))
    for i in range(width):
        for j in range(height):
            cell = minesweeper.get_cell(g,j,i)
            button = b[i][j]
            if minesweeper.is_revealed(cell):
                if minesweeper.is_bomb(cell):
                    new_img = img[10]
                    if x == j and y == i:
                        new_img = img[11]
                else:
                    new_img = img[minesweeper.number_of_bombs_in_neighborhood(cell)]
                button.config(relief=tk.FLAT,image=new_img, command = "")
            elif minesweeper.is_hypothetic_bomb(cell):
                button.config(image=img[12])
            else:
                button.config(image=img[9])
Пример #2
0
def display_game(game):
    """
    display the game in stdout
    :param game: game
    :type game: a minesweeper game
    :return: None
    :rType: NoneType
    :UC: none
    """
    display_line = "+---"*ms.get_width(game) 
    display_line += "+"
    print(" ", end="")
    for i in range(ms.get_width(game)-1):
        print("  ", i, end="")
    print("  ",ms.get_width(game)-1)
    for h in range(ms.get_height(game)):
        numerotation = ""
        print(" ",display_line)
        print(h ,"",end="")
        for l in range(ms.get_width(game)):
            character = " "
            cell = ms.get_cell(game, h, l)
            if ms.is_revealed(cell):
                if ms.is_bomb(cell):
                    character = "B"
                else:
                    character = ms.number_of_bombs_in_neighborhood(cell)
            elif ms.is_hypothetic_bomb(cell):
                    character = "?"
            print("| ",character, end="")
        print("|")
    print(" ",display_line)
Пример #3
0
def keyboard_input(game):
    """
    :param game: game
    :type game: a minesweeper game
    :return: the player input action
    :rtype: tuple of the action (posX, posY, action)
    :UC: none
    """
    try:
        data_in = input("Your play x,y,C (C=(R)eval,(S)et,(U)nset): ")
        ldata = data_in.split(',')
        x = int(ldata[0])
        y = int(ldata[1])
        c = ldata[2]
        assert x >= 0 and x < ms.get_height(game)
        assert y >= 0 and x < ms.get_width(game)
        c = c.upper()
        assert c == 'R' or c == 'S' or c == 'U'
        return (x, y, c)
    except AssertionError:
        print("Numbers must be in range of the game")
        keyboard_input(game)
    except IndexError:
        print ('There must be two numbers and one letter separated by a comma (,)')
        keyboard_input(game)
    except TypeError:
        print ('There must be two numbers and one letter separated by a comma (,)')
        keyboard_input(game)
    except ValueError:
        print ("x and y must be integers and c must be R or S or U")
        keyboard_input(game)
Пример #4
0
def show_grid (game):
    for x in range(m.get_width(game)):
       print()
       for y in range(m.get_height(game)):
          cell=m.get_cell(game,x,y)
          if m.is_revealed(cell) :
             if m.is_bomb(cell):
                print ("B",end='')
             else : 
                print (m.number_of_bombs_in_neighborhood(cell),end='')
          else :  
             print(" ",end='')   
    print()
Пример #5
0
def create (g):
    """
    This function creates the graphical board from a game. It also
    launches the event loop. Thus, this is the only function to run to
    have a functional graphical board.

    :param g: the minesweeper game
    :type g: game
    :return: None
    """
    global img
    # create a new Tk window
    win = tk.Tk()
    # define the window title
    win.title ('Minesweeper')
    # load images
    iconpath = os.path.join(os.path.dirname(os.path.abspath(__file__)),"icons")
    img = [
        tk.PhotoImage(file=os.path.join(iconpath,"0.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"1.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"2.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"3.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"4.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"5.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"6.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"7.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"8.gif")),
        tk.PhotoImage(file=os.path.join(iconpath,"9.gif")),  # unrevealed
        tk.PhotoImage(file=os.path.join(iconpath,"10.gif")), # bomb explosed
        tk.PhotoImage(file=os.path.join(iconpath,"11.gif")), # bomb discovered
        tk.PhotoImage(file=os.path.join(iconpath,"12.gif")), # flag
        tk.PhotoImage(file=os.path.join(iconpath,"13.gif"))  # question
    ]
    # create the graphical board made of Tk buttons
    width,height = (minesweeper.get_width(g),minesweeper.get_height(g))
    b = []
    for i in range(width):
        b.insert(i,[])
        for j in range(height):
            button = tk.Button(win,padx=0,pady=0, width=19, height=19, image=img[9])
            button.grid(column = i, row = j)
            b[i].insert(j,button)
            # bind the right-click event
            button.bind("<Button-3>",partial(__changeflag,b=b,g=g,i=j,j=i))
            # bind the left-click event
            button.config(command=partial(__changestate,b,g,j,i))

    # event loop
    win.mainloop()
Пример #6
0
def __disable_game (b,g):
    """
    This function is called once the player looses. The chosen behavior
    is to shade the board and to unbind events.

    :param b: the board of buttons
    :type b: list of list of ``button``
    :param g: the minesweeper game
    :type g: dict

    """
    width,height = (minesweeper.get_width(g),minesweeper.get_height(g))
    for i in range(width):
        for j in range(height):
            button = b[i][j]
            button.config(state=tk.DISABLED)
            button.bind("<Button-3>","")       
Пример #7
0
def __block_game (b,g):
    """
    This function is called once the player wins. The chosen behavior
    is to let the board as it and to unbind events.

    :param b: the board of buttons
    :type b: list of list of ``button``
    :param g: the minesweeper game
    :type g: game

    """
    width,height = (minesweeper.get_width(g),minesweeper.get_height(g))
    for i in range(width):
        for j in range(height):
            button = b[i][j]
            button.config(command="")
            button.bind("<Button-3>","")