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])
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)
def play(game): """ require action to the player and execute it :param game: game :type game: a minesweeper game :return: None :rtype: NoneType :UC: none """ action = keyboard_input(game) x = action[0] y = action[1] a = action[2] if a == 'R': ms.reveal_all_cells_from(game,x, y) elif a == 'S': cell = ms.get_cell(game, x, y) ms.set_hypothetic(cell) elif a == 'U': cell = ms.get_cell(game, x, y) ms.unset_hypothetic(cell)
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()
def __changeflag (evt,b,g,i,j): """ This function is called on right-click on a button. :param b: the board of buttons :type b: list of list of ``button`` :param g: the minesweeper game :type g: game :param i: the x-coordinate of the cell :type i: int :param j: the y-coordinate of the cell :type j: int """ cell = minesweeper.get_cell(g,i,j) if not minesweeper.is_hypothetic_bomb(cell): minesweeper.set_hypothetic(cell) else: minesweeper.unset_hypothetic(cell) __redraw(b,g,i,j) __test_end (b,g)
import sys import minesweeper as msp if __name__ == "__main__": assert len(sys.argv)==4, "Give me more arguments, please." assert (int(sys.argv[1])>0 and int(sys.argv[2])>0 and int(sys.argv[3])>0), "All these arguments should be positive." g = msp.make_game(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[2])) while(msp.get_state(g)==msp.GameState(3)): msp.draw(g) tmp = input("Your play x,y,C (C=(R)eval,(S)et,(U)nset):") str = tmp.split(" ") print(str) if(str[2]=="R"): msp.reveal(msp.get_cell(g,int(str[0]),int(str[1]))) continue if(str[2]=="S"): msp.set_hypothetic(msp.get_cell(g,int(str[0]),int(str[1]))) if(get_state(g)==msp.GameState(1)): print("You won.") else: print("You lost.")