Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Dora Jambor, dorajambor@gmail.com
- # January, 2016
- # Implementation of the game fifteen in Python
- #import time, pdb
- from Tkinter import *
- # ---------- parameters ----------
- # Dimensions
- min_dim = 3
- max_dim = 9
- # Board elements
- d = 0
- board = []
- counter = 0
- # Location of blank space
- blankx = 0
- blanky = 0
- # ---------- functions ----------
- def greet():
- print 'This is the Game of Fifteen!'
- def dim():
- # global d # if you use "return" you don't need access to global variable.
- # you can even use different name - for example `result`.
- result = int(raw_input('Insert the dimension of the board: '))
- while True:
- if result >=min_dim and result <= max_dim:
- break
- result = int(raw_input('Try again: '))
- return result
- # Initialize the board
- '''
- This fills up the two-dimensional board[][] list with numbers ascending
- from the inputted dimension to 1
- '''
- def setup():
- global blankx, blanky, game_running
- # set or reset all data and variables
- # without creating board and buttons
- # label is now invisible
- label.lower()
- # set blank
- blankx = d - 1
- blanky = d - 1
- # fill board with numbers - it changes buttons too
- # StringVar requires the use of get() and set()
- numbers = d * d
- for row in board:
- for string_var in row:
- numbers -= 1
- if numbers == 0:
- string_var.set(' ')
- else:
- string_var.set(str(numbers))
- # set other variables
- game_running = True
- # Play the game
- '''
- A function called by Tkinter that allows the user to interact with the game board
- and play the game by moving the tiles.
- '''
- def play(i, j):
- global blankx, blanky, game_running
- #print 'play i,j:', i, j
- #print 'blankx, blanky:', blankx, blanky
- if game_running:
- if (blankx, blanky) in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
- # StringVar requires the use of get() and set()
- #board[blanky][blankx] = board[j][i]
- #board[j][i] = ' '
- board[blanky][blankx].set( board[j][i].get() )
- board[j][i].set(' ')
- # you don't need it - StringVar do it for you
- #buttons[blanky][blankx]['text'] = buttons[j][i]['text']
- #buttons[j][i]['text'] = ' '
- blanky = j
- blankx = i
- if won():
- # lable is now visible
- label.lift()
- game_running = False
- # probably you don't need it - `label for winning` will call `setup`
- #else:
- # reset board (without creating new board) and restart game
- # setup()
- def won():
- # StringVar ('char') requires the use of get() and set()
- number = 0
- for row in board:
- for string_var in row:
- number += 1
- if number == d * d and string_var.get() == ' ':
- return True
- elif string_var.get() != str(number):
- return False
- return True
- # ---------- main ----------
- # - befor tkinter window -
- greet()
- d = dim()
- # - create window -
- root = Tk()
- root.config(bg = 'black', borderwidth=4)
- root.wm_title("Game of Fifteen")
- # - label for winning -
- label = Button(root, text="You won!", fg="pink",font=("Helvetica",15), comman=setup)
- label.config(bg='black')
- label.grid(row=0, column=0, ipadx=5, ipady=5)
- # - frame for the board game -
- frame = Frame(root)
- frame.config(bg='black')
- frame.grid(row=0, column=0) # added to grid after label will hide label
- # - creat board - only once - without numbers
- # fill board with StringVars
- for _ in range(d): # if you don't need `j` then you can use `_`
- row = []
- for _ in range(d): # if you don't need `i` then you can use `_`
- # new StringVar for every tile
- string_var = StringVar()
- # append StringVar object, not text from StringVar - get()
- row.append(string_var) # without .get()
- board.append(row)
- # - create buttons - only once - without numbers
- # if you use StringVar you don't need direct access to buttons
- #buttons = []
- for j, row in enumerate(board):
- #buttons_row = []
- for i, string_var in enumerate(row):
- # v = StringVar() # don't need it
- b = Label(frame, textvariable=string_var, bg='pink', font=("Helvetica", 30))
- # you can't assign normal text to textvariable
- # and you don't have to - put empty text into StringVar in board
- #if board[j][i]== d * d:
- # b = Label(frame, textvariable = ' ', bg='pink',font=("Helvetica", 30))
- b.grid(row=j, column=i, sticky="nsew", ipadx=8, padx=4, pady=4)
- # ipadx to make 3x3-board bigger and hide `label for winning`
- # if you want to click widget then maybe you should use Button()
- # and command= to assing function
- b.bind('<Button-1>',lambda e, i=i,j=j:play(i,j))
- #buttons_row.append(b)
- #buttons.append(buttons_row)
- # - fill board with numbers and start game -
- setup() # start over
- root.mainloop() # start engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement