Created
June 8, 2022 21:24
-
-
Save samwho/1f37cb502936a516c03df4fc84772bea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import enum | |
| from random import randrange | |
| from typing import Optional | |
| class QuestionGenerator: | |
| def __init__(self, category: str) -> None: | |
| self.category = category | |
| self.index = 0 | |
| def question(self) -> str: | |
| question = f"{self.category} Question {self.index}" | |
| self.index += 1 | |
| return question | |
| class Category(enum.Enum): | |
| POP = QuestionGenerator("Pop") | |
| SCIENCE = QuestionGenerator("Science") | |
| ROCK = QuestionGenerator("Rock") | |
| SPORTS = QuestionGenerator("Sports") | |
| def __str__(self) -> str: | |
| return self.value.category | |
| class Place: | |
| category: Category | |
| def __init__(self, category: Category) -> None: | |
| self.category = category | |
| def question(self) -> str: | |
| return self.category.value.question() | |
| PLACES = [ | |
| Place(Category.POP), | |
| Place(Category.SCIENCE), | |
| Place(Category.SPORTS), | |
| Place(Category.ROCK), | |
| Place(Category.POP), | |
| Place(Category.SCIENCE), | |
| Place(Category.SPORTS), | |
| Place(Category.ROCK), | |
| Place(Category.POP), | |
| Place(Category.SCIENCE), | |
| Place(Category.SPORTS), | |
| Place(Category.SPORTS), | |
| ] | |
| class Player: | |
| name: str | |
| purse: int | |
| in_penalty_box: bool | |
| def __init__(self, name: str) -> None: | |
| self.name = name | |
| self._place = 0 | |
| self.purse = 0 | |
| self.in_penalty_box = False | |
| @property | |
| def place(self) -> Place: | |
| return PLACES[self._place] | |
| def move(self, spaces: int): | |
| self._place = (self._place + spaces) % len(PLACES) | |
| def __str__(self) -> str: | |
| return self.name | |
| class Game: | |
| player: Player | |
| def __init__(self): | |
| self.players = [] | |
| self.player = None | |
| self.player_index = 0 | |
| def add_player(self, name: str): | |
| player = Player(name) | |
| self.players.append(player) | |
| print(f"{player} was added") | |
| print(f"They are player number {len(self.players)}") | |
| def set_current_player(self): | |
| self.player = self.players[self.player_index] | |
| self.player_index = (self.player_index + 1) % len(self.players) | |
| def question_was_correctly_answered(self) -> bool: | |
| """ | |
| Randomly simulates players getting a question correct or not. | |
| """ | |
| if randrange(9) != 7: | |
| print("Answer was correct!!!!") | |
| return True | |
| else: | |
| print("Question was incorrectly answered") | |
| return False | |
| def do_turn(self) -> Optional[Player]: | |
| assert ( | |
| len(self.players) > 1 | |
| ), "you must have at least two players to start the game" | |
| self.set_current_player() | |
| roll = randrange(5) + 1 | |
| print(f"{self.player} is the current player") | |
| print(f"They have rolled a {roll}") | |
| is_roll_odd = roll % 2 == 1 | |
| if self.player.in_penalty_box: | |
| if is_roll_odd: | |
| print(f"{self.player} is getting out of the penalty box") | |
| else: | |
| print(f"{self.player} is not getting out of the penalty box") | |
| return None | |
| self.player.move(roll) | |
| print(f"{self.player}'s new location is {self.player._place}") | |
| print(f"The category is {self.player.place.category}") | |
| print(self.player.place.question()) | |
| if not self.question_was_correctly_answered(): | |
| print(f"{self.player} was sent to the penalty box") | |
| self.player.in_penalty_box = True | |
| return None | |
| self.player.purse += 1 | |
| print(f"{self.player} now has {self.player.purse} Gold Coins.") | |
| self.player.in_penalty_box = False | |
| if self.player.purse == 6: | |
| return self.player | |
| return None | |
| if __name__ == "__main__": | |
| game = Game() | |
| game.add_player("Chet") | |
| game.add_player("Pat") | |
| game.add_player("Sue") | |
| while True: | |
| if winner := game.do_turn(): | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment