Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,74 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Interfaccia Gradio
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
|
3 |
+
# Logica del gioco Impiccato
|
4 |
+
class HangmanGame:
|
5 |
+
def __init__(self):
|
6 |
+
self.reset_game()
|
7 |
+
|
8 |
+
def start_game(self, word):
|
9 |
+
self.word_to_guess = word.upper()
|
10 |
+
self.guessed_letters = ["_"] * len(self.word_to_guess)
|
11 |
+
self.remaining_attempts = 6
|
12 |
+
return self.display_word()
|
13 |
+
|
14 |
+
def guess(self, letter):
|
15 |
+
letter = letter.upper()
|
16 |
+
if len(letter) > 1:
|
17 |
+
if letter == self.word_to_guess:
|
18 |
+
self.guessed_letters = list(self.word_to_guess)
|
19 |
+
return "Complimenti! Hai indovinato la parola!"
|
20 |
+
else:
|
21 |
+
self.remaining_attempts -= 2
|
22 |
+
else:
|
23 |
+
found_letter = False
|
24 |
+
for i in range(len(self.word_to_guess)):
|
25 |
+
if self.word_to_guess[i] == letter:
|
26 |
+
self.guessed_letters[i] = letter
|
27 |
+
found_letter = True
|
28 |
+
if not found_letter:
|
29 |
+
self.remaining_attempts -= 1
|
30 |
+
|
31 |
+
return self.display_word()
|
32 |
+
|
33 |
+
def display_word(self):
|
34 |
+
return " ".join(self.guessed_letters), f"Tentativi rimasti: {self.remaining_attempts}"
|
35 |
+
|
36 |
+
def reset_game(self):
|
37 |
+
self.word_to_guess = ''
|
38 |
+
self.guessed_letters = []
|
39 |
+
self.remaining_attempts = 6
|
40 |
+
|
41 |
+
|
42 |
+
game = HangmanGame()
|
43 |
+
|
44 |
+
# Funzioni Gradio per interagire
|
45 |
+
def start_game(word):
|
46 |
+
return game.start_game(word)
|
47 |
+
|
48 |
+
def guess(letter):
|
49 |
+
return game.guess(letter)
|
50 |
+
|
51 |
+
def reset():
|
52 |
+
game.reset_game()
|
53 |
+
return "", "Tentativi rimasti: 6"
|
54 |
|
55 |
# Interfaccia Gradio
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("## Benvenuto al Gioco dell'Impiccato!")
|
58 |
+
|
59 |
+
word_input = gr.Textbox(label="Inserisci una parola", type="str")
|
60 |
+
start_button = gr.Button("Inizia il gioco")
|
61 |
+
|
62 |
+
word_display = gr.Textbox(label="Parola da indovinare", interactive=False)
|
63 |
+
guess_input = gr.Textbox(label="Inserisci una lettera", type="str")
|
64 |
+
guess_button = gr.Button("Indovina")
|
65 |
+
|
66 |
+
result_display = gr.Textbox(label="Risultato", interactive=False)
|
67 |
+
reset_button = gr.Button("Ricomincia")
|
68 |
+
|
69 |
+
start_button.click(start_game, inputs=word_input, outputs=[word_display, result_display])
|
70 |
+
guess_button.click(guess, inputs=guess_input, outputs=[word_display, result_display])
|
71 |
+
reset_button.click(reset, outputs=[word_display, result_display])
|
72 |
+
|
73 |
+
# Esegui l'app Gradio su Hugging Face
|
74 |
+
demo.launch()
|