-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmae.py
101 lines (83 loc) · 3.2 KB
/
gmae.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
import random as rd
from typing import Tuple
def check_guess(guess: int, number: int) -> Tuple[str, str]:
"""Check guess against number and return appropriate message and color."""
if guess == number:
return "🎉 Congratulations! You got the number!", "success"
difference = abs(guess - number)
message = ""
if guess > number:
if difference <= 5:
message = "You are very close! But a little too high! Try again!"
elif difference <= 15:
message = "You are close to the answer! Just too high. Try again!"
elif difference < 25:
message = "You're pretty off! Too High! Try again!"
else:
message = "You are way off! Too high!"
else:
if difference <= 5:
message = "You are very close! But a little too low! Try again!"
elif difference <= 15:
message = "You are close to the answer! Just too low. Try again!"
elif difference < 25:
message = "You're pretty off! Too Low! Try again!"
else:
message = "You are way off! Too low!"
# Return message and color based on how close the guess is
color = "warning" if difference <= 15 else "error"
return message, color
def init_session_state():
"""Initialize session state variables if they don't exist."""
if 'number' not in st.session_state:
st.session_state.number = rd.randint(0, 100)
if 'attempts' not in st.session_state:
st.session_state.attempts = 0
if 'game_over' not in st.session_state:
st.session_state.game_over = False
def reset_game():
"""Reset the game state."""
st.session_state.number = rd.randint(0, 100)
st.session_state.attempts = 0
st.session_state.game_over = False
# Main app
st.set_page_config(page_title="Number Guessing Game", page_icon="🎲")
# Initialize session state
init_session_state()
# App title and description
st.title("🎲 Number Guessing Game")
st.markdown("""
Try to guess the number between 0 and 100!
I'll give you hints about whether your guess is too high or too low.
""")
# Game interface
col1, col2 = st.columns([3, 1])
with col1:
guess = st.number_input(
"Enter your guess:",
min_value=0,
max_value=100,
step=1,
key="guess_input"
)
with col2:
if st.button("Make Guess"):
st.session_state.attempts += 1
message, color = check_guess(guess, st.session_state.number)
if guess == st.session_state.number:
st.session_state.game_over = True
st.session_state.last_message = message
st.session_state.last_color = color
# Display message if there is one
if 'last_message' in st.session_state:
st.markdown(f"### {st.session_state.last_message}")
# Show attempts counter
st.markdown(f"**Attempts:** {st.session_state.attempts}")
# Show reset button if game is over
if st.session_state.game_over:
if st.button("Play Again"):
reset_game()
# Add some visual flair at the bottom
st.markdown("---")
st.markdown("Made with ❤️ using Streamlit")