@@ -15,14 +15,17 @@ def __init__(self, frame_size):
15
15
self .pieces = self ._generate_piece ()
16
16
17
17
self ._setup ()
18
+ self .randomize_puzzle ()
18
19
19
20
def _generate_cell (self ):
20
21
cells = []
21
22
c_id = 0
22
23
for col in range (self .grid_size ):
24
+ new_row = []
23
25
for row in range (self .grid_size ):
24
- cells .append (Cell (row , col , self .cell_size , c_id ))
26
+ new_row .append (Cell (row , col , self .cell_size , c_id ))
25
27
c_id += 1
28
+ cells .append (new_row )
26
29
return cells
27
30
28
31
def _generate_piece (self ):
@@ -35,40 +38,58 @@ def _generate_piece(self):
35
38
return puzzle_pieces
36
39
37
40
def _setup (self ):
38
- for cell in self .grid :
39
- piece_choice = random .choice (self .pieces )
40
- cell .occupying_piece = piece_choice
41
- self .pieces .remove (piece_choice )
41
+ for row in self .grid :
42
+ for cell in row :
43
+ tile_piece = self .pieces [- 1 ]
44
+ cell .occupying_piece = tile_piece
45
+ self .pieces .remove (tile_piece )
42
46
43
- def _get_cell_from_id (self , given_id ):
44
- for cell in self .grid :
45
- if cell .c_id == given_id :
46
- return cell
47
+ def randomize_puzzle (self ):
48
+ moves = [(0 , 1 ),(0 , - 1 ),(1 , 0 ),(- 1 , 0 )]
49
+ for i in range (30 ):
50
+ shuffle_move = random .choice (moves )
51
+ for row in self .grid :
52
+ for cell in row :
53
+ tile_x = self .grid .index (row ) + shuffle_move [0 ]
54
+ tile_y = row .index (cell ) + shuffle_move [1 ]
55
+ if tile_x >= 0 and tile_x <= 2 and tile_y >= 0 and tile_y <= 2 :
56
+ new_cell = self .grid [tile_x ][tile_y ]
57
+ if new_cell .occupying_piece .img == None :
58
+ c = (cell , new_cell )
59
+ try :
60
+ c [0 ].occupying_piece , c [1 ].occupying_piece = c [1 ].occupying_piece , c [0 ].occupying_piece
61
+ except :
62
+ return False
63
+ else :
64
+ continue
47
65
48
66
def _is_move_valid (self , click ):
49
67
moves = {
50
- 79 : 1 ,
51
- 80 : - 1 ,
52
- 81 : 3 ,
53
- 82 : - 3
68
+ 79 : ( 0 , 1 ) ,
69
+ 80 : ( 0 , - 1 ) ,
70
+ 81 : ( 1 , 0 ) ,
71
+ 82 : ( - 1 , 0 )
54
72
}
55
- for cell in self .grid :
56
- move_id = cell .c_id + moves [click .scancode ]
57
- if move_id >= 0 and move_id <= 8 :
58
- new_cell = self ._get_cell_from_id (move_id )
59
- if new_cell .occupying_piece .img == None :
60
- return (cell , new_cell )
61
- else :
62
- continue
73
+ for row in self .grid :
74
+ for cell in row :
75
+ move = moves [click .scancode ]
76
+ tile_x = self .grid .index (row ) + move [0 ]
77
+ tile_y = row .index (cell ) + move [1 ]
78
+ if tile_x >= 0 and tile_x <= 2 and tile_y >= 0 and tile_y <= 2 :
79
+ new_cell = self .grid [tile_x ][tile_y ]
80
+ if new_cell .occupying_piece .img == None :
81
+ return (cell , new_cell )
82
+ else :
83
+ continue
63
84
64
85
def handle_click (self , click ):
65
86
c = self ._is_move_valid (click )
66
87
try :
67
- # print(c[0].c_id, c[1].c_id)
68
88
c [0 ].occupying_piece , c [1 ].occupying_piece = c [1 ].occupying_piece , c [0 ].occupying_piece
69
89
except :
70
90
return False
71
91
72
92
def draw (self , display ):
73
- for cell in self .grid :
74
- cell .draw (display )
93
+ for row in self .grid :
94
+ for cell in row :
95
+ cell .draw (display )
0 commit comments