Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,137 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
3 |
|
4 |
+
class HangmanGame:
|
5 |
+
def __init__(self):
|
6 |
+
self.reset_game()
|
7 |
+
|
8 |
+
# Optional: Add a small dictionary of words if you want predefined options
|
9 |
+
self.word_list = [
|
10 |
+
"PYTHON", "PROGRAMMAZIONE", "COMPUTER", "INTELLIGENZA",
|
11 |
+
"SVILUPPATORE", "TECNOLOGIA", "ALGORITMO"
|
12 |
+
]
|
13 |
+
|
14 |
+
def reset_game(self):
|
15 |
+
self.word_to_guess = ""
|
16 |
+
self.guessed_letters = []
|
17 |
+
self.attempts_left = 6
|
18 |
+
self.display_word = []
|
19 |
+
self.game_over = False
|
20 |
+
self.win = False
|
21 |
+
|
22 |
+
def start_game(self, word):
|
23 |
+
# Reset the game first
|
24 |
+
self.reset_game()
|
25 |
+
|
26 |
+
# Convert word to uppercase
|
27 |
+
word = word.upper()
|
28 |
+
|
29 |
+
# Validate input
|
30 |
+
if not word or not word.isalpha():
|
31 |
+
return "Inserisci una parola valida!", "", 6
|
32 |
+
|
33 |
+
self.word_to_guess = word
|
34 |
+
self.display_word = ['_'] * len(word)
|
35 |
+
|
36 |
+
return "Gioco iniziato! Indovina la parola.", ' '.join(self.display_word), 6
|
37 |
+
|
38 |
+
def make_guess(self, guess):
|
39 |
+
# If game is already over, prevent further guessing
|
40 |
+
if self.game_over:
|
41 |
+
return "Il gioco è terminato. Riavvia per giocare di nuovo.", ' '.join(self.display_word), self.attempts_left
|
42 |
+
|
43 |
+
# Validate guess
|
44 |
+
guess = guess.upper()
|
45 |
+
if not guess or not guess.isalpha():
|
46 |
+
return "Inserisci una lettera o parola valida.", ' '.join(self.display_word), self.attempts_left
|
47 |
+
|
48 |
+
# Full word guess
|
49 |
+
if len(guess) > 1:
|
50 |
+
if guess == self.word_to_guess:
|
51 |
+
self.display_word = list(self.word_to_guess)
|
52 |
+
self.game_over = True
|
53 |
+
self.win = True
|
54 |
+
return "Complimenti! Hai indovinato la parola!", ' '.join(self.display_word), self.attempts_left
|
55 |
+
else:
|
56 |
+
# Penalty for incorrect full word guess
|
57 |
+
self.attempts_left -= 2
|
58 |
+
if self.attempts_left <= 0:
|
59 |
+
self.game_over = True
|
60 |
+
return f"Mi dispiace, hai esaurito i tentativi. La parola era: {self.word_to_guess}", ' '.join(self.display_word), 0
|
61 |
+
return "Parola errata!", ' '.join(self.display_word), self.attempts_left
|
62 |
+
|
63 |
+
# Single letter guess
|
64 |
+
if guess in self.guessed_letters:
|
65 |
+
return "Hai già provato questa lettera!", ' '.join(self.display_word), self.attempts_left
|
66 |
+
|
67 |
+
self.guessed_letters.append(guess)
|
68 |
+
|
69 |
+
# Check if letter is in the word
|
70 |
+
if guess in self.word_to_guess:
|
71 |
+
# Update display word with correctly guessed letters
|
72 |
+
for i in range(len(self.word_to_guess)):
|
73 |
+
if self.word_to_guess[i] == guess:
|
74 |
+
self.display_word[i] = guess
|
75 |
+
|
76 |
+
# Check if word is complete
|
77 |
+
if '_' not in self.display_word:
|
78 |
+
self.game_over = True
|
79 |
+
self.win = True
|
80 |
+
return "Complimenti! Hai indovinato la parola!", ' '.join(self.display_word), self.attempts_left
|
81 |
+
|
82 |
+
return "Lettera corretta!", ' '.join(self.display_word), self.attempts_left
|
83 |
+
else:
|
84 |
+
# Incorrect guess
|
85 |
+
self.attempts_left -= 1
|
86 |
+
|
87 |
+
if self.attempts_left <= 0:
|
88 |
+
self.game_over = True
|
89 |
+
return f"Mi dispiace, hai esaurito i tentativi. La parola era: {self.word_to_guess}", ' '.join(self.display_word), 0
|
90 |
+
|
91 |
+
return "Lettera errata!", ' '.join(self.display_word), self.attempts_left
|
92 |
|
93 |
+
# Create game instance
|
94 |
+
game = HangmanGame()
|
95 |
|
96 |
+
def start_interface(word):
|
97 |
+
message, display, attempts = game.start_game(word)
|
98 |
+
return message, display, attempts
|
99 |
+
|
100 |
+
def guess_interface(guess):
|
101 |
+
message, display, attempts = game.make_guess(guess)
|
102 |
+
return message, display, attempts
|
103 |
+
|
104 |
+
# Create Gradio interface
|
105 |
+
with gr.Blocks() as demo:
|
106 |
+
gr.Markdown("# 🎲 Gioco dell'Impiccato")
|
107 |
+
|
108 |
+
# Status displays
|
109 |
+
status_output = gr.Textbox(label="Stato del Gioco", interactive=False)
|
110 |
+
word_display = gr.Textbox(label="Parola", interactive=False)
|
111 |
+
attempts_display = gr.Number(label="Tentativi Rimasti", interactive=False)
|
112 |
+
|
113 |
+
# Word input section
|
114 |
+
with gr.Row():
|
115 |
+
word_input = gr.Textbox(label="Inserisci una parola da indovinare")
|
116 |
+
start_btn = gr.Button("Inizia Gioco")
|
117 |
+
|
118 |
+
# Guess input section
|
119 |
+
with gr.Row():
|
120 |
+
guess_input = gr.Textbox(label="Inserisci una lettera o la parola completa")
|
121 |
+
guess_btn = gr.Button("Indovina")
|
122 |
+
|
123 |
+
# Button actions
|
124 |
+
start_btn.click(
|
125 |
+
start_interface,
|
126 |
+
inputs=word_input,
|
127 |
+
outputs=[status_output, word_display, attempts_display]
|
128 |
+
)
|
129 |
+
|
130 |
+
guess_btn.click(
|
131 |
+
guess_interface,
|
132 |
+
inputs=guess_input,
|
133 |
+
outputs=[status_output, word_display, attempts_display]
|
134 |
+
)
|
135 |
+
|
136 |
+
if __name__ == "__main__":
|
137 |
+
demo.launch()
|