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