Spaces:
Running
Running
Download app.py from ServerX/hangman-game: direct link, hf CLI and curl.
- Browser
- Download file 5.4 kB
-
https://huggingface.co/spaces/ServerX/hangman-game/resolve/main/app.py
- Command line
-
hf download hf://spaces/ServerX/hangman-game/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ServerX/hangman-game/resolve/main/app.py
5.4 kB
| import gradio as gr | |
| class HangmanGame: | |
| def __init__(self): | |
| self.reset_game() | |
| def reset_game(self): | |
| self.word_to_guess = "" | |
| self.guessed_letters = [] | |
| self.attempts_left = 6 | |
| self.display_word = [] | |
| self.game_over = False | |
| self.win = False | |
| def start_game(self, word): | |
| # Reset the game first | |
| self.reset_game() | |
| # Convert word to uppercase | |
| word = word.upper() | |
| # Validate input | |
| if not word or not word.isalpha(): | |
| return "Inserisci una parola valida!", "", 6 | |
| self.word_to_guess = word | |
| self.display_word = ['_'] * len(word) | |
| return "Gioco iniziato! Secondo giocatore: indovina la parola.", ' '.join(self.display_word), 6 | |
| def make_guess(self, guess): | |
| # If game is already over, prevent further guessing | |
| if self.game_over: | |
| return "Il gioco è terminato. Riavvia per giocare di nuovo.", ' '.join(self.display_word), self.attempts_left | |
| # Validate guess | |
| guess = guess.upper() | |
| if not guess or not guess.isalpha(): | |
| return "Inserisci una lettera o parola valida.", ' '.join(self.display_word), self.attempts_left | |
| # Full word guess | |
| if len(guess) > 1: | |
| if guess == self.word_to_guess: | |
| self.display_word = list(self.word_to_guess) | |
| self.game_over = True | |
| self.win = True | |
| return "Complimenti! Hai indovinato la parola!", ' '.join(self.display_word), self.attempts_left | |
| else: | |
| # Penalty for incorrect full word guess | |
| self.attempts_left -= 2 | |
| if self.attempts_left <= 0: | |
| self.game_over = True | |
| return f"Mi dispiace, hai esaurito i tentativi. La parola era: {self.word_to_guess}", ' '.join(self.display_word), 0 | |
| return "Parola errata!", ' '.join(self.display_word), self.attempts_left | |
| # Single letter guess | |
| if guess in self.guessed_letters: | |
| return "Hai già provato questa lettera!", ' '.join(self.display_word), self.attempts_left | |
| self.guessed_letters.append(guess) | |
| # Check if letter is in the word | |
| if guess in self.word_to_guess: | |
| # Update display word with correctly guessed letters | |
| for i in range(len(self.word_to_guess)): | |
| if self.word_to_guess[i] == guess: | |
| self.display_word[i] = guess | |
| # Check if word is complete | |
| if '_' not in self.display_word: | |
| self.game_over = True | |
| self.win = True | |
| return "Complimenti! Hai indovinato la parola!", ' '.join(self.display_word), self.attempts_left | |
| return "Lettera corretta!", ' '.join(self.display_word), self.attempts_left | |
| else: | |
| # Incorrect guess | |
| self.attempts_left -= 1 | |
| if self.attempts_left <= 0: | |
| self.game_over = True | |
| return f"Mi dispiace, hai esaurito i tentativi. La parola era: {self.word_to_guess}", ' '.join(self.display_word), 0 | |
| return "Lettera errata!", ' '.join(self.display_word), self.attempts_left | |
| # Create game instance | |
| game = HangmanGame() | |
| def start_interface(word): | |
| message, display, attempts = game.start_game(word) | |
| return message, display, attempts | |
| def guess_interface(guess): | |
| message, display, attempts = game.make_guess(guess) | |
| return message, display, attempts | |
| def generate_cast_link(): | |
| """Generate a static Hugging Face Spaces share link.""" | |
| return "https://serverx-hangman-game.hf.space/" | |
| # Create Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🎲 Gioco dell'Impiccato per Due Giocatori") | |
| gr.Markdown("## Primo giocatore: inserisci la parola da indovinare") | |
| gr.Markdown("## Secondo giocatore: cerca di indovinare la parola") | |
| # Status displays | |
| status_output = gr.Textbox(label="Stato del Gioco", interactive=False) | |
| word_display = gr.Textbox(label="Parola", interactive=False) | |
| attempts_display = gr.Number(label="Tentativi Rimasti", interactive=False) | |
| # Cast link display | |
| cast_link_output = gr.Textbox(label="Link per condividere il gioco", interactive=False) | |
| generate_link_btn = gr.Button("Genera Link di Condivisione") | |
| # Word input section | |
| with gr.Row(): | |
| word_input = gr.Textbox(label="Primo giocatore: Inserisci una parola segreta", type="password") | |
| start_btn = gr.Button("Inizia Gioco") | |
| # Guess input section | |
| with gr.Row(): | |
| guess_input = gr.Textbox(label="Secondo giocatore: Inserisci una lettera o la parola completa") | |
| guess_btn = gr.Button("Indovina") | |
| # Button actions | |
| start_btn.click( | |
| start_interface, | |
| inputs=word_input, | |
| outputs=[status_output, word_display, attempts_display] | |
| ) | |
| guess_btn.click( | |
| guess_interface, | |
| inputs=guess_input, | |
| outputs=[status_output, word_display, attempts_display] | |
| ) | |
| # Generate cast link action | |
| generate_link_btn.click( | |
| generate_cast_link, | |
| outputs=cast_link_output | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |