ServerX commited on
Commit
80f8179
·
verified ·
1 Parent(s): 08fd6a5

Create ok.py

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