From ea73e64fb1900db41f78fd5ca5bd3b1ed7da3845 Mon Sep 17 00:00:00 2001 From: Loosen-IT Date: Mon, 4 Dec 2023 21:29:26 +0100 Subject: [PATCH] 04/12/2023 --- .idea/UNI_Python.iml | 2 +- .idea/misc.xml | 2 +- .idea/workspace.xml | 8 ++- ha_05/loosen_janniclas_1540907_06.py | 93 ++++++++++++++++++++++------ 4 files changed, 81 insertions(+), 24 deletions(-) diff --git a/.idea/UNI_Python.iml b/.idea/UNI_Python.iml index d599f79..71bf193 100644 --- a/.idea/UNI_Python.iml +++ b/.idea/UNI_Python.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 59b5b2e..c20271c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9f32f70..0e0aa4a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,8 +5,10 @@ - + + + @@ -139,6 +143,6 @@ - + \ No newline at end of file diff --git a/ha_05/loosen_janniclas_1540907_06.py b/ha_05/loosen_janniclas_1540907_06.py index 4e8b8fd..9c4348c 100644 --- a/ha_05/loosen_janniclas_1540907_06.py +++ b/ha_05/loosen_janniclas_1540907_06.py @@ -45,25 +45,78 @@ class Game: mode = "easy" def __init__(self, rules, mode="easy"): + modes = ["easy", "hard", "god"] + self.mode = mode.lower() + if mode.lower() not in modes: + raise ValueError("Invalid mode. Please choose from: " + ', '.join(modes)) self.rules = rules self.choices = {key: 0 for key in self.rules} - self.mode = mode + + def __str__(self): + return "bot: " + str(self.scores["bot"]) + " - player: " + str(self.scores["player"]) def reset(self): self.scores["player"] = 0 - self.scores["player"] = 0 + self.scores["bot"] = 0 + self.choices = {key: 0 for key in self.rules} - def play(self, players_choice): + def match(self, games=5): + dist = 0 + while sum(self.scores.values()) < games and dist < games / 2: + self.play() + dist = abs(self.scores["player"] - self.scores["bot"]) + print("\n") + if self.scores["player"] > self.scores["bot"]: + print("The player wins.") + else: + print("The bot wins.") + + def play(self): + players_choice = self._get_players_choice() + bots_choice = self._get_bots_choice(players_choice) + self.choices[players_choice] += 1 + result = self.rules[players_choice][bots_choice] + if result > 0: + self.scores["player"] += 1 + elif result < 0: + self.scores["bot"] += 1 + print(self) + + def _get_players_choice(self): + players_choice = input("Enter your move (" + ', '.join(self.rules.keys()) + "): ") if players_choice not in self.rules: raise ValueError("Invalid choice. Please choose from: " + ', '.join(self.rules.keys())) + return players_choice.lower() + + def _get_bots_choice(self, players_choice): + bots_choice = "" + if self.mode == "easy": + bots_choice = self._easy() + elif self.mode == "hard": + bots_choice = self._hard() + elif self.mode == "god": + bots_choice = self._god(players_choice) + print("The bot chooses " + bots_choice) + return bots_choice def _god(self, players_choice): - for choice in self.rules: - + for bots_choice in self.rules: + if self.rules[bots_choice][players_choice] == 1: + return bots_choice - def _easy(self, choice): + def _easy(self): + bots_choice = random.choice(list(self.choices.keys())) + return bots_choice - def _hard(self, choice): + def _hard(self): + total = sum(self.choices.values()) + if total == 0: + return self._easy() + probabilities = {choice: self.choices[choice] / total for choice in self.choices} + likely = random.choices(list(probabilities.keys()), weights=list(probabilities.values()))[0] + for choice in self.rules[likely].keys(): + if self.rules[likely][choice] < 0: + return choice text = ''' @@ -82,19 +135,19 @@ Rudolph # print(ceaser.decode(ceaser.encode(text))) rock_paper_scissor = { - "rock": {"rock": 0, "paper": -1, "scissor": 1}, - "paper": {"rock": 1, "paper": 0, "scissor": -1}, - "scissor": {"rock": -1, "paper": 1, "scissor": 0} + "rock": {"rock": 0, "paper": -1, "scissor": 1}, + "paper": {"rock": 1, "paper": 0, "scissor": -1}, + "scissor": {"rock": -1, "paper": 1, "scissor": 0} - } +} rock_paper_scissor_lizard_spock = { - "rock": {"rock": 0, "paper": -1, "scissor": 1, "lizard": 1, "spock": -1}, - "paper": {"rock": 1, "paper": 0, "scissor": -1, "lizard": -1, "spock": 1}, - "scissor": {"rock": -1, "paper": 1, "scissor": 0, "lizard": 1, "spock": -1}, - "lizard": {"rock": -1, "paper": 1, "scissor": -1, "lizard": 0, "spock": 1}, - "spock": {"rock": 1, "paper": -1, "scissor": 1, "lizard": -1, "spock": 0} - } - -game = Game(rock_paper_scissor) -print(game.choices) + "rock": {"rock": 0, "paper": -1, "scissor": 1, "lizard": 1, "spock": -1}, + "paper": {"rock": 1, "paper": 0, "scissor": -1, "lizard": -1, "spock": 1}, + "scissor": {"rock": -1, "paper": 1, "scissor": 0, "lizard": 1, "spock": -1}, + "lizard": {"rock": -1, "paper": 1, "scissor": -1, "lizard": 0, "spock": 1}, + "spock": {"rock": 1, "paper": -1, "scissor": 1, "lizard": -1, "spock": 0} +} + +# game = Game(rock_paper_scissor_lizard_spock, "hard") +# game.match(5)