@@ -15,14 +15,17 @@ def __init__(self, frame_size):
1515 self .pieces = self ._generate_piece ()
1616
1717 self ._setup ()
18+ self .randomize_puzzle ()
1819
1920 def _generate_cell (self ):
2021 cells = []
2122 c_id = 0
2223 for col in range (self .grid_size ):
24+ new_row = []
2325 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 ))
2527 c_id += 1
28+ cells .append (new_row )
2629 return cells
2730
2831 def _generate_piece (self ):
@@ -35,40 +38,58 @@ def _generate_piece(self):
3538 return puzzle_pieces
3639
3740 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 )
4246
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
4765
4866 def _is_move_valid (self , click ):
4967 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 )
5472 }
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
6384
6485 def handle_click (self , click ):
6586 c = self ._is_move_valid (click )
6687 try :
67- # print(c[0].c_id, c[1].c_id)
6888 c [0 ].occupying_piece , c [1 ].occupying_piece = c [1 ].occupying_piece , c [0 ].occupying_piece
6989 except :
7090 return False
7191
7292 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