1+ import random
2+
3+ print ("🧩 Emoji Sliding Puzzle Game 🧩" )
4+ print ("Arrange the numbers in correct order!\n " )
5+
6+ numbers = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 0 ]
7+
8+ random .shuffle (numbers )
9+
10+ puzzle = [
11+ numbers [0 :3 ],
12+ numbers [3 :6 ],
13+ numbers [6 :9 ]
14+ ]
15+
16+ moves = 0
17+ def display_puzzle ():
18+ print ("\n 🎮 Current Puzzle:\n " )
19+ for row in puzzle :
20+ for item in row :
21+ if item == 0 :
22+ print ("⬜" , end = " " )
23+ else :
24+ print (f"{ item } ️⃣" , end = " " )
25+ print ()
26+
27+ def find_positions (choice ):
28+ empty_row = 0
29+ empty_col = 0
30+ number_row = - 1
31+ number_col = - 1
32+
33+ for i in range (3 ):
34+ for j in range (3 ):
35+ if puzzle [i ][j ] == 0 :
36+ empty_row = i
37+ empty_col = j
38+ if puzzle [i ][j ] == choice :
39+ number_row = i
40+ number_col = j
41+ return number_row , number_col , empty_row , empty_col
42+
43+
44+ def move_tile (choice ):
45+ global moves
46+ number_row , number_col , empty_row , empty_col = find_positions (choice )
47+
48+ if number_row == - 1 :
49+ print ("⚠️ Number not found!" )
50+ return
51+ if (
52+ abs (number_row - empty_row ) == 1
53+ and number_col == empty_col
54+ ) or (
55+ abs (number_col - empty_col ) == 1
56+ and number_row == empty_row
57+ ):
58+
59+ puzzle [empty_row ][empty_col ] = choice
60+ puzzle [number_row ][number_col ] = 0
61+ moves += 1
62+ print ("✅ Tile moved successfully!" )
63+
64+ else :
65+ print ("❌ Invalid move! Tile must be next to empty space." )
66+
67+ def check_win ():
68+ winning_puzzle = [
69+ [1 , 2 , 3 ],
70+ [4 , 5 , 6 ],
71+ [7 , 8 , 0 ]
72+ ]
73+ return puzzle == winning_puzzle
74+
75+ def init ():
76+ global moves
77+ while True :
78+ display_puzzle ()
79+ print (f"\n 🔄 Moves: { moves } " )
80+ if check_win ():
81+ print ("\n 🎉 Congratulations! You solved the puzzle!" )
82+ break
83+ try :
84+ choice = int (input ("\n 🎯 Enter number to move: " ))
85+ except ValueError :
86+ print ("❌ Please enter a valid number!" )
87+ continue
88+ move_tile (choice )
89+ print ("\n 👋 Thanks for playing Emoji Sliding Puzzle!\n " )
90+
91+ init ()
0 commit comments