File size: 5,397 Bytes
80f8179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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()