Advertisement
Guest User

fifteen.v3

a guest
Jan 7th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # Dora Jambor, dorajambor@gmail.com
  3. # January, 2016
  4. # Implementation of the game fifteen in Python
  5.  
  6. import time, pdb
  7. from Tkinter import *
  8.  
  9. # ---------- parameters ----------
  10. # Dimensions
  11. min_dim = 3
  12. max_dim = 9
  13.  
  14. # Location of blank space
  15. blankx = 0
  16. blanky = 0
  17.  
  18. # ---------- functions ----------
  19. def greet():
  20.     print 'This is the Game of Fifteen!'
  21.  
  22. def dim():
  23.     result = int(raw_input('Insert the dimension of the board: '))
  24.     while True:
  25.         if result >=min_dim and result <= max_dim:
  26.             break
  27.         result = int(raw_input('Try again: '))
  28.     return result    
  29.            
  30.  
  31. # Play the game
  32. '''
  33. A function called by Tkinter that allows the user to interact with the game board
  34. and play the game by moving the tiles.
  35. '''  
  36. def play(i,j):
  37.     global blankx, blanky, game_running
  38.     print 'play i,j:', i, j
  39.     print 'blankx, blanky:', blankx, blanky
  40.    
  41.     if game_running:
  42.  
  43.         if (blankx, blanky) in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
  44.             board[blanky][blankx].set(board[j][i].get())
  45.             board[j][i].set(' ')
  46.             blanky = j
  47.             blankx = i
  48.            
  49.             if won():
  50.                 # lable is now visible
  51.                 bn.lift()
  52.                 game_running = False
  53.    
  54.                                                                        
  55. def won():
  56.     number = 0
  57.  
  58.     for j, row in enumerate(board):
  59.         for i, char in enumerate(row,1):
  60.             number += 1
  61.             if number == d * d and char.get() == ' ':
  62.                 return True
  63.             elif char.get() != str(number):
  64.                 return False
  65.     return True
  66.    
  67.    
  68. # Initialize the board
  69. '''
  70. This sets up/resets all data and variables - and fills and updates board with numbers.
  71. '''
  72. def setup():
  73.     global blankx, blanky, game_running
  74.     # set blank coordinates
  75.     blankx = d - 1
  76.     blanky = d - 1
  77.    
  78.     # this is where bn is marked as not defined - "global name 'bn' is not defined"
  79.     bn.lower()
  80.    
  81.     # fill/update board with numbers
  82.     numbers = d * d
  83.    
  84.     for row in board:
  85.         for tile in row:
  86.             numbers -= 1
  87.             if numbers == 0:
  88.                 tile.set('')
  89.             else:
  90.                 tile.set(str(numbers))
  91.  
  92.     # continue game
  93.     game_running = True
  94.  
  95.  
  96.    
  97. # ---------- main ----------
  98. board = []
  99. greet()
  100. d = dim()
  101. buttons = []
  102. setup()
  103.  
  104. # create window
  105. root = Tk()
  106. root.config(bg = 'black', borderwidth=4)
  107. root.wm_title("Game of Fifteen")
  108.  
  109. # label for winning
  110. bn = Button(root, text="You won!\n <click to start>", command=setup)
  111. bn.grid(row=0, column=0, ipadx=3, ipady=10)
  112.  
  113. # frame for the board game
  114. frame = Frame(root)
  115. frame.config(bg='black')
  116. frame.grid(row=0, column=0)
  117.  
  118. # Fil board with StringVars
  119. for i in range(d):
  120.     row = []
  121.     for j in range(d):
  122.         var_text = StringVar()
  123.         row.append(var_text)
  124.     board.append(row)
  125.  
  126. setup()
  127.              
  128. for j, row in enumerate(board):
  129.     for i, string_var in enumerate(row):
  130.         b = Label(frame, textvariable=string_var, bg='pink', font=("Helvetica", 30), relief=RAISED)  
  131.         b.grid(row=j, column=i, sticky="nsew", ipadx=8, padx=4, pady=4)
  132.         b.bind('<Button-1>',lambda e, i=i,j=j:play(i,j))
  133.  
  134. game_running = True
  135. root.mainloop()    # start engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement