AqsaAbbasi26 commited on
Commit
0e12bc4
ยท
verified ยท
1 Parent(s): 71828e5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -0
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # Game state
5
+ class GameState:
6
+ def __init__(self):
7
+ self.target_number = random.randint(1, 100)
8
+ self.attempts = 0
9
+ self.game_won = False
10
+ self.high_score = float('inf')
11
+ self.games_played = 0
12
+
13
+ def reset_game(self):
14
+ self.target_number = random.randint(1, 100)
15
+ self.attempts = 0
16
+ self.game_won = False
17
+
18
+ # Initialize game
19
+ game = GameState()
20
+
21
+ def make_guess(guess, current_attempts, current_score, games_played):
22
+ """Process the user's guess and return game state"""
23
+ if game.game_won:
24
+ return (
25
+ "๐ŸŽ‰ You already won! Click 'New Game' to play again.",
26
+ current_attempts,
27
+ current_score,
28
+ games_played,
29
+ gr.update(interactive=False)
30
+ )
31
+
32
+ try:
33
+ guess = int(guess)
34
+ if guess < 1 or guess > 100:
35
+ return (
36
+ "โŒ Please enter a number between 1 and 100!",
37
+ current_attempts,
38
+ current_score,
39
+ games_played,
40
+ gr.update(interactive=True)
41
+ )
42
+ except (ValueError, TypeError):
43
+ return (
44
+ "โŒ Please enter a valid number!",
45
+ current_attempts,
46
+ current_score,
47
+ games_played,
48
+ gr.update(interactive=True)
49
+ )
50
+
51
+ game.attempts += 1
52
+ new_attempts = game.attempts
53
+
54
+ if guess == game.target_number:
55
+ game.game_won = True
56
+ game.games_played += 1
57
+
58
+ # Update high score
59
+ if game.attempts < game.high_score:
60
+ game.high_score = game.attempts
61
+ result_msg = f"๐ŸŽ‰ CONGRATULATIONS! You guessed {game.target_number} in {game.attempts} attempts!\n๐Ÿ† NEW HIGH SCORE!"
62
+ else:
63
+ result_msg = f"๐ŸŽ‰ CONGRATULATIONS! You guessed {game.target_number} in {game.attempts} attempts!"
64
+
65
+ # Add performance feedback
66
+ if game.attempts <= 7:
67
+ result_msg += "\n๐Ÿง  Excellent! You're a guessing master!"
68
+ elif game.attempts <= 10:
69
+ result_msg += "\n๐Ÿ‘ Great job! Nice guessing skills!"
70
+ elif game.attempts <= 15:
71
+ result_msg += "\n๐Ÿ˜Š Good work! You got there in the end!"
72
+ else:
73
+ result_msg += "\n๐Ÿ’ช You made it! Practice makes perfect!"
74
+
75
+ return (
76
+ result_msg,
77
+ new_attempts,
78
+ game.high_score if game.high_score != float('inf') else 0,
79
+ game.games_played,
80
+ gr.update(interactive=False)
81
+ )
82
+
83
+ elif guess < game.target_number:
84
+ result_msg = f"๐Ÿ“ˆ Too low! Try a higher number.\n๐ŸŽฏ Attempts: {game.attempts}"
85
+ else:
86
+ result_msg = f"๐Ÿ“‰ Too high! Try a lower number.\n๐ŸŽฏ Attempts: {game.attempts}"
87
+
88
+ return (
89
+ result_msg,
90
+ new_attempts,
91
+ game.high_score if game.high_score != float('inf') else 0,
92
+ game.games_played,
93
+ gr.update(interactive=True)
94
+ )
95
+
96
+ def new_game():
97
+ """Start a new game"""
98
+ game.reset_game()
99
+ return (
100
+ "๐ŸŽฎ New game started! I'm thinking of a number between 1 and 100...",
101
+ 0,
102
+ game.high_score if game.high_score != float('inf') else 0,
103
+ game.games_played,
104
+ gr.update(interactive=True, value="")
105
+ )
106
+
107
+ def get_hint():
108
+ """Provide a helpful hint"""
109
+ if game.game_won:
110
+ return "๐ŸŽ‰ You already won! Start a new game to play again."
111
+
112
+ if game.attempts == 0:
113
+ return "๐Ÿ’ก Hint: Try starting with 50 - it's right in the middle!"
114
+
115
+ # Give range hints based on previous guesses
116
+ if game.attempts >= 5:
117
+ lower_bound = max(1, game.target_number - 15)
118
+ upper_bound = min(100, game.target_number + 15)
119
+ return f"๐Ÿ’ก Hint: The number is between {lower_bound} and {upper_bound}"
120
+ else:
121
+ return "๐Ÿ’ก Hint: Think about whether your last guess was close or far away!"
122
+
123
+ # Create the Gradio interface
124
+ with gr.Blocks(
125
+ theme=gr.themes.Soft(),
126
+ title="๐ŸŽฏ Number Guessing Game",
127
+ css="""
128
+ .game-header {
129
+ text-align: center;
130
+ color: #ff6b6b;
131
+ font-size: 2.5em;
132
+ margin-bottom: 20px;
133
+ }
134
+ .stats-container {
135
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
136
+ padding: 20px;
137
+ border-radius: 15px;
138
+ margin: 15px 0;
139
+ }
140
+ .result-display {
141
+ font-size: 1.2em;
142
+ padding: 15px;
143
+ border-radius: 10px;
144
+ text-align: center;
145
+ }
146
+ """
147
+ ) as demo:
148
+
149
+ gr.Markdown("# ๐ŸŽฏ Number Guessing Game")
150
+ gr.Markdown("### I'm thinking of a number between 1 and 100. Can you guess it?")
151
+
152
+ with gr.Row():
153
+ with gr.Column(scale=2):
154
+ # Main game area
155
+ guess_input = gr.Number(
156
+ label="Enter your guess (1-100)",
157
+ value=50,
158
+ minimum=1,
159
+ maximum=100,
160
+ step=1
161
+ )
162
+
163
+ with gr.Row():
164
+ guess_btn = gr.Button("๐ŸŽฏ Make Guess", variant="primary", size="lg")
165
+ hint_btn = gr.Button("๐Ÿ’ก Get Hint", variant="secondary")
166
+ new_game_btn = gr.Button("๐ŸŽฎ New Game", variant="stop")
167
+
168
+ result_display = gr.Textbox(
169
+ label="Game Status",
170
+ value="๐ŸŽฎ Welcome! Enter your first guess to start playing!",
171
+ interactive=False,
172
+ lines=4
173
+ )
174
+
175
+ with gr.Column(scale=1):
176
+ # Stats panel
177
+ gr.Markdown("### ๐Ÿ“Š Game Statistics")
178
+ attempts_display = gr.Number(
179
+ label="๐ŸŽฏ Current Attempts",
180
+ value=0,
181
+ interactive=False
182
+ )
183
+ high_score_display = gr.Number(
184
+ label="๐Ÿ† Best Score",
185
+ value=0,
186
+ interactive=False
187
+ )
188
+ games_played_display = gr.Number(
189
+ label="๐ŸŽฎ Games Played",
190
+ value=0,
191
+ interactive=False
192
+ )
193
+
194
+ # Game instructions
195
+ with gr.Accordion("๐Ÿ“‹ How to Play", open=False):
196
+ gr.Markdown("""
197
+ 1. **Objective**: Guess the secret number between 1 and 100
198
+ 2. **Feedback**: I'll tell you if your guess is too high or too low
199
+ 3. **Goal**: Try to guess the number in as few attempts as possible
200
+ 4. **Hints**: Use the hint button if you're stuck (available after 5 attempts)
201
+ 5. **High Score**: Beat your best score and challenge yourself!
202
+
203
+ **Tips**:
204
+ - Start with 50 (middle of the range)
205
+ - Use binary search strategy for optimal results
206
+ - Pay attention to the feedback to narrow down the range
207
+ """)
208
+
209
+ # Event handlers
210
+ guess_btn.click(
211
+ fn=make_guess,
212
+ inputs=[guess_input, attempts_display, high_score_display, games_played_display],
213
+ outputs=[result_display, attempts_display, high_score_display, games_played_display, guess_input]
214
+ )
215
+
216
+ new_game_btn.click(
217
+ fn=new_game,
218
+ outputs=[result_display, attempts_display, high_score_display, games_played_display, guess_input]
219
+ )
220
+
221
+ hint_btn.click(
222
+ fn=get_hint,
223
+ outputs=[result_display]
224
+ )
225
+
226
+ # Allow Enter key to submit guess
227
+ guess_input.submit(
228
+ fn=make_guess,
229
+ inputs=[guess_input, attempts_display, high_score_display, games_played_display],
230
+ outputs=[result_display, attempts_display, high_score_display, games_played_display, guess_input]
231
+ )
232
+
233
+ # Launch the app
234
+ if __name__ == "__main__":
235
+ demo.launch(
236
+ server_name="0.0.0.0",
237
+ server_port=7860,
238
+ share=True
239
+ )