|
| 1 | +""" |
| 2 | +Crea un pequeño juego que consista en adivinar palabras en un número máximo de intentos: |
| 3 | +- El juego comienza proponiendo una palabra aleatoria incompleta |
| 4 | + - Por ejemplo "m_ur_d_v", y el número de intentos que le quedan |
| 5 | +- El usuario puede introducir únicamente una letra o una palabra (de la misma longitud que |
| 6 | + la palabra a adivinar) |
| 7 | + - Si escribe una letra y acierta, se muestra esa letra en la palabra. Si falla, se resta |
| 8 | + uno al número de intentos |
| 9 | + - Si escribe una resolución y acierta, finaliza el juego, en caso contrario, se resta uno |
| 10 | + al número de intentos |
| 11 | + - Si el contador de intentos llega a 0, el jugador pierde |
| 12 | +- La palabra debe ocultar de forma aleatoria letras, y nunca puede comenzar |
| 13 | + ocultando más del 60% |
| 14 | +- Puedes utilizar las palabras que quieras y el número de intentos que consideres |
| 15 | +""" |
| 16 | + |
| 17 | +import random |
| 18 | + |
| 19 | +class Game: |
| 20 | + """ |
| 21 | + A simple word guessing game where the player has to guess |
| 22 | + a randomly chosen word by suggesting letters or the full word. |
| 23 | +
|
| 24 | + Attributes: |
| 25 | + words (list[str]): List of possible words for the game. |
| 26 | + replace_probabilities (float): Maximum percentage of letters that can be hidden. |
| 27 | + max_attempts (int): Maximum number of attempts allowed. |
| 28 | + random_word (str): The randomly chosen word for the game. |
| 29 | + current_lyrics (list[str]): The current state of the word with hidden/revealed letters. |
| 30 | + attempts_left (int): Number of attempts the player has left. |
| 31 | + """ |
| 32 | + |
| 33 | + words = [ |
| 34 | + "murcielago", "python", "programacion", "desarrollo", "reto", "palabra", "adivina", "juego", |
| 35 | + "computadora", "teclado", "raton", "pantalla", "internet", "variable", "funcion", "bucle", |
| 36 | + "condicional", "lista", "diccionario", "conjunto", "tupla" |
| 37 | + ] |
| 38 | + replace_probabilities = 0.6 |
| 39 | + |
| 40 | + def __init__(self, max_attempts: int = 6) -> None: |
| 41 | + """ |
| 42 | + Initialize the game with a random word and hidden letters. |
| 43 | +
|
| 44 | + Args: |
| 45 | + max_attempts (int): Maximum number of attempts allowed. Default is 6. |
| 46 | + """ |
| 47 | + self.max_attempts = max_attempts |
| 48 | + self.random_word = random.choice(self.words) |
| 49 | + self.current_lyrics = self._generate_incomplete_word() |
| 50 | + self.attempts_left = self.max_attempts |
| 51 | + |
| 52 | + def _generate_incomplete_word(self) -> list[str]: |
| 53 | + """ |
| 54 | + Generate the word with randomly hidden letters, ensuring no more |
| 55 | + than 60% of the word is hidden. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + list[str]: The word represented as a list with some letters replaced by '_'. |
| 59 | + """ |
| 60 | + max_hidden = int(len(self.random_word) * self.replace_probabilities) |
| 61 | + num_letters_to_hide = random.randint(1, max_hidden) |
| 62 | + hidden_positions = random.sample(range(len(self.random_word)), num_letters_to_hide) |
| 63 | + |
| 64 | + return [ |
| 65 | + "_" if i in hidden_positions else char |
| 66 | + for i, char in enumerate(self.random_word) |
| 67 | + ] |
| 68 | + |
| 69 | + def main(self) -> None: |
| 70 | + """ |
| 71 | + Run the main game loop until the player wins or runs out of attempts. |
| 72 | + """ |
| 73 | + while self.attempts_left > 0: |
| 74 | + self._display_round() |
| 75 | + user_input = input("Ingresa una letra o la palabra completa: ").lower() |
| 76 | + |
| 77 | + if len(user_input) == 1 and user_input.isalpha(): |
| 78 | + self._guess_letter(user_input) |
| 79 | + elif len(user_input) == len(self.random_word): |
| 80 | + self._guess_word(user_input) |
| 81 | + else: |
| 82 | + print("Entrada invalida. Intente de nuevo.") |
| 83 | + |
| 84 | + if "_" not in self.current_lyrics: |
| 85 | + print(f"Felicidades!! Completaste la palabra: {self.random_word}") |
| 86 | + return |
| 87 | + |
| 88 | + print(f"Te quedaste sin intentos! La palabra era: '{self.random_word}'.") |
| 89 | + |
| 90 | + def _guess_letter(self, letter: str) -> None: |
| 91 | + """ |
| 92 | + Check if the guessed letter is in the word. |
| 93 | +
|
| 94 | + Args: |
| 95 | + letter (str): The guessed letter. |
| 96 | + """ |
| 97 | + if letter in self.random_word: |
| 98 | + for i, char in enumerate(self.random_word): |
| 99 | + if char == letter: |
| 100 | + self.current_lyrics[i] = letter |
| 101 | + print(f"Correcto! La palabra ahora es: {''.join(self.current_lyrics)}") |
| 102 | + else: |
| 103 | + self.attempts_left -= 1 |
| 104 | + print(f"Respuesta incorrecta. Intentos restantes: {self.attempts_left}") |
| 105 | + |
| 106 | + def _guess_word(self, word: str) -> None: |
| 107 | + """ |
| 108 | + Check if the guessed word matches the random word. |
| 109 | +
|
| 110 | + Args: |
| 111 | + word (str): The guessed word. |
| 112 | + """ |
| 113 | + if word == self.random_word: |
| 114 | + print(f"Ganaste! La palabra es: '{self.random_word}'.") |
| 115 | + self.current_lyrics = list(self.random_word) |
| 116 | + return |
| 117 | + else: |
| 118 | + self.attempts_left -= 1 |
| 119 | + print(f"Respuesta incorrecta. Intentos restantes: {self.attempts_left}") |
| 120 | + |
| 121 | + def _display_round(self) -> None: |
| 122 | + """ |
| 123 | + Display the current round number, attempts left, and the incomplete word. |
| 124 | + """ |
| 125 | + print(f"\nRonda: {(self.max_attempts - self.attempts_left) + 1} de {self.max_attempts}") |
| 126 | + print(f"Palabra incompleta: {''.join(self.current_lyrics)}") |
| 127 | + print("-" * 50) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + game = Game() |
| 132 | + game.main() |
0 commit comments