55emojis = ["🍎" , "🚗" , "⚽" , "🐍" , "🎧" , "🔥" , "🌈" , "🚀" ]
66HIGH_SCORE_FILE = "highscore.txt"
77
8- while True :
9- print ("\n 🎮 Welcome to Emoji Memory Game!" )
10-
11- if not os .path .exists (HIGH_SCORE_FILE ):
12- try :
13- with open (HIGH_SCORE_FILE , "w" ) as file :
14- file .write ("0" )
15- except Exception :
16- pass
17-
18- high_score = 0
8+ # Load or initialize high score ONCE at the start of the program
9+ if not os .path .exists (HIGH_SCORE_FILE ):
1910 try :
20- with open (HIGH_SCORE_FILE , "r " ) as file :
21- high_score = int ( file .read (). strip () or "0" )
11+ with open (HIGH_SCORE_FILE , "w " ) as file :
12+ file .write ( "0" )
2213 except Exception :
23- high_score = 0
14+ pass
15+
16+ high_score = 0
17+ try :
18+ with open (HIGH_SCORE_FILE , "r" ) as file :
19+ high_score = int (file .read ().strip () or "0" )
20+ except Exception :
21+ high_score = 0
2422
23+ # Main Game Loop
24+ while True :
25+ print ("\n 🎮 Welcome to Emoji Memory Game!" )
2526 print (f"🏅 High Score: { high_score } \n " )
2627
2728 print ("🎯 Select Difficulty" )
5051 score = 0
5152 level = 1
5253
54+ # Gameplay Loop
5355 while True :
5456 sequence = random .choices (emojis , k = level + 2 )
5557
7880 print ("Correct sequence was:" , " " .join (sequence ))
7981 break
8082
83+ # Game Over Processing
8184 print ("\n 🎯 Game Over!" )
8285 print (f"🏆 Final Score: { score } " )
8386
8487 if score > high_score :
8588 print ("🔥 NEW HIGH SCORE!" )
89+ high_score = score # Update local memory variable
8690 try :
8791 with open (HIGH_SCORE_FILE , "w" ) as file :
8892 file .write (str (score ))
9195 else :
9296 print (f"🏅 High Score remains: { high_score } " )
9397
98+ # Replay Loop Fix
99+ play_again = False
94100 while True :
95101 replay = input ("\n Play again? (y/n): " ).strip ().lower ()
96- if replay in ['y' , 'yes' , 'n' , 'no' ]:
102+ if replay in ['y' , 'yes' ]:
103+ play_again = True
104+ break
105+ elif replay in ['n' , 'no' ]:
106+ play_again = False
97107 break
98108 print ("⚠️ Invalid input. Please enter 'y' or 'n'." )
99109
100- if replay in [ 'n' , 'no' ] :
110+ if not play_again :
101111 print ("👋 Thanks for playing!" )
102- break
112+ break # Safely breaks the main outer loop and exits the game
0 commit comments