AptlyDigital commited on
Commit
9eb0481
ยท
verified ยท
1 Parent(s): 7104bdf

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -192
app.py DELETED
@@ -1,192 +0,0 @@
1
- import gradio as gr
2
- import random
3
- import time
4
-
5
- class NumberGuessingGame:
6
- def __init__(self):
7
- self.reset_game()
8
-
9
- def reset_game(self):
10
- self.secret_number = random.randint(1, 100)
11
- self.attempts = 0
12
- self.max_attempts = 10
13
- self.game_history = []
14
- self.game_over = False
15
- return self.get_initial_state()
16
-
17
- def get_initial_state(self):
18
- return (
19
- "๐ŸŽฏ **NUMBER GUESSING GAME**\n\n"
20
- f"I've picked a number between 1 and 100.\n"
21
- f"You have **{self.max_attempts} attempts** to guess it!\n\n"
22
- "Type your guess below and click 'Submit Guess' or press Enter.\n\n"
23
- f"๐Ÿ”ฎ **Secret number ready!** (It's {self.secret_number}, but don't peek!)\n"
24
- "---"
25
- ), "", "\n".join(self.game_history) if self.game_history else "Game started!"
26
-
27
- def make_guess(self, guess_input):
28
- if self.game_over:
29
- return self.get_initial_state()
30
-
31
- if not guess_input.strip():
32
- return self.get_current_state()
33
-
34
- try:
35
- guess = int(guess_input)
36
- except ValueError:
37
- self.game_history.append(f"โŒ '{guess_input}' is not a valid number!")
38
- return self.get_current_state()
39
-
40
- self.attempts += 1
41
-
42
- # Check the guess
43
- if guess < 1 or guess > 100:
44
- self.game_history.append(f"โš ๏ธ Guess {guess} is out of range (1-100)")
45
- elif guess == self.secret_number:
46
- self.game_history.append(f"๐ŸŽ‰ **CORRECT!** You guessed {guess} in {self.attempts} attempts!")
47
- self.game_over = True
48
- elif guess < self.secret_number:
49
- diff = self.secret_number - guess
50
- if diff <= 10:
51
- self.game_history.append(f"๐Ÿ“ˆ Guess {guess}: Close! Try a bit higher")
52
- else:
53
- self.game_history.append(f"๐Ÿ“ˆ Guess {guess}: Too low!")
54
- else:
55
- diff = guess - self.secret_number
56
- if diff <= 10:
57
- self.game_history.append(f"๐Ÿ“‰ Guess {guess}: Close! Try a bit lower")
58
- else:
59
- self.game_history.append(f"๐Ÿ“‰ Guess {guess}: Too high!")
60
-
61
- # Check if game over
62
- if self.attempts >= self.max_attempts and not self.game_over:
63
- self.game_history.append(f"๐Ÿ˜ž **GAME OVER!** The number was {self.secret_number}")
64
- self.game_over = True
65
-
66
- # Give hint after 5 attempts
67
- if self.attempts == 5 and not self.game_over:
68
- hint_range = 20
69
- lower = max(1, self.secret_number - hint_range)
70
- upper = min(100, self.secret_number + hint_range)
71
- self.game_history.append(f"๐Ÿ’ก **Hint:** The number is between {lower} and {upper}")
72
-
73
- # Last attempt warning
74
- if self.attempts == self.max_attempts - 1 and not self.game_over:
75
- self.game_history.append("๐Ÿšจ **Warning:** Last attempt coming up!")
76
-
77
- return self.get_current_state()
78
-
79
- def get_current_state(self):
80
- status = (
81
- f"๐ŸŽฏ **Game in Progress**\n"
82
- f"Attempts: {self.attempts}/{self.max_attempts}\n"
83
- f"Last secret number: ||{self.secret_number}|| (hover to reveal)\n"
84
- "---\n"
85
- )
86
-
87
- if self.game_over:
88
- status = "๐ŸŽฏ **Game Finished!** Click 'New Game' to play again!\n---\n"
89
-
90
- history_text = "\n".join(self.game_history[-10:]) # Show last 10 messages
91
-
92
- return status, "", history_text
93
-
94
- # Create game instance
95
- game = NumberGuessingGame()
96
-
97
- # Create Gradio interface
98
- with gr.Blocks(title="Number Guessing Game", theme=gr.themes.Soft()) as demo:
99
- gr.Markdown("# ๐ŸŽฏ Number Guessing Game")
100
- gr.Markdown("Guess the secret number between 1 and 100!")
101
-
102
- with gr.Row():
103
- with gr.Column(scale=2):
104
- status_display = gr.Markdown(game.get_initial_state()[0])
105
-
106
- with gr.Row():
107
- guess_input = gr.Textbox(
108
- label="Your Guess",
109
- placeholder="Enter a number 1-100...",
110
- scale=4
111
- )
112
- submit_btn = gr.Button("Submit Guess", variant="primary", scale=1)
113
-
114
- with gr.Row():
115
- new_game_btn = gr.Button("๐Ÿ”„ New Game", variant="secondary")
116
- hint_btn = gr.Button("๐Ÿ’ก Get Hint")
117
-
118
- history_display = gr.Textbox(
119
- label="Game History",
120
- value="Game started!",
121
- lines=10,
122
- interactive=False
123
- )
124
-
125
- with gr.Column(scale=1):
126
- gr.Markdown("### ๐Ÿ“Š Game Info")
127
- gr.Markdown("""
128
- **How to play:**
129
- 1. Enter a number between 1-100
130
- 2. Get feedback (too high/low)
131
- 3. Guess within 10 attempts!
132
-
133
- **Tips:**
134
- - Start with 50
135
- - Use binary search strategy
136
- - Pay attention to hints!
137
-
138
- **Difficulty:** ๐ŸŸข Beginner
139
- """)
140
-
141
- attempts_display = gr.Textbox(
142
- label="Attempts",
143
- value="0/10",
144
- interactive=False
145
- )
146
-
147
- # Define button actions
148
- def on_submit(guess):
149
- status, clear_input, history = game.make_guess(guess)
150
- return status, clear_input, history, f"{game.attempts}/{game.max_attempts}"
151
-
152
- def on_new_game():
153
- status, clear_input, history = game.reset_game()
154
- return status, clear_input, history, "0/10"
155
-
156
- def on_hint():
157
- if not game.game_over and game.attempts > 0:
158
- hint_range = 20
159
- lower = max(1, game.secret_number - hint_range)
160
- upper = min(100, game.secret_number + hint_range)
161
- game.game_history.append(f"๐Ÿ’ก **Hint:** The number is between {lower} and {upper}")
162
-
163
- return game.get_current_state()[0], "", game.get_current_state()[2], f"{game.attempts}/{game.max_attempts}"
164
-
165
- # Connect components
166
- submit_btn.click(
167
- fn=on_submit,
168
- inputs=guess_input,
169
- outputs=[status_display, guess_input, history_display, attempts_display]
170
- )
171
-
172
- guess_input.submit(
173
- fn=on_submit,
174
- inputs=guess_input,
175
- outputs=[status_display, guess_input, history_display, attempts_display]
176
- )
177
-
178
- new_game_btn.click(
179
- fn=on_new_game,
180
- inputs=[],
181
- outputs=[status_display, guess_input, history_display, attempts_display]
182
- )
183
-
184
- hint_btn.click(
185
- fn=on_hint,
186
- inputs=[],
187
- outputs=[status_display, guess_input, history_display, attempts_display]
188
- )
189
-
190
- # Launch the app
191
- if __name__ == "__main__":
192
- demo.launch()