import json import os import random from matplotlib import pyplot as plt class CeaserChiffre: chiffre = {} def __init__(self, chiffre=None): if chiffre is not None: self.chiffre = chiffre else: self.renew_chiffre() def __str__(self): return str(self.chiffre) def renew_chiffre(self): codes = {} chars = list(range(0, 255)) for char in range(0, 255): rand = random.choice(chars) chars.remove(rand) codes[chr(char)] = chr(rand) self.chiffre = codes def encode(self, text): return self._work(text, self.chiffre) def decode(self, text): chiffre = {value: key for key, value in self.chiffre.items()} return self._work(text, chiffre) def _work(self, text, chiffre): result = [] for i in range(len(text)): if text[i] in chiffre: result.append(chiffre[text[i]]) else: result.append(text[i]) return ''.join(result) class Game: scores = {"player": 0, "bot": 0} mode = "easy" player = None stop = False def __init__(self, rules, mode="easy"): self.mode = mode.lower() self.rules = rules self.player = None self.scores = {"player": 0, "bot": 0} self.stop = False if os.path.exists("storage.json"): load = input("Do you want to load the save game? (yes or no) ") if str.lower(load) == "yes": with open('storage.json', 'r') as file: self.player = Player("player", {key: 0 for key in self.rules}) self._load_dump(json.load(file)) print("\n") return modes = ["easy", "hard", "god"] if self.mode not in modes: raise ValueError("Invalid mode. Please choose from: " + ', '.join(modes)) name = input("Enter your name: ") self.player = Player(str.lower(name), {key: 0 for key in self.rules}) print("\n") def __str__(self): return "bot: " + str(self.scores["bot"]) + " - " + self.player.name + ": " + str(self.scores["player"]) def reset(self): self.scores["player"] = 0 self.scores["bot"] = 0 self.player.choices = {key: 0 for key in self.rules} if os.path.exists('storage.json'): os.remove('storage.json') def match(self, games=10): while sum(self.scores.values()) < games and not self.stop: self._play() print("\n") if self.scores["player"] > self.scores["bot"] and not self.stop: print(self.player.name + " wins.") self.reset() elif not self.stop: print("The bot wins.") self.reset() else: print("The match is interrupted.") def _play(self): players_choice = self._get_players_choice() if players_choice == "exit": return self.exit() bots_choice = self._get_bots_choice(players_choice) self.player.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 exit(self): backup = self._create_dump() with open('storage.json', 'w') as file: json.dump(backup, file) self.stop = True def _get_players_choice(self): players_choice = input("Enter your move (" + ', '.join(self.rules.keys()) + " or exit): ").lower() if players_choice not in self.rules and players_choice != "exit": raise ValueError("Invalid choice. Please choose from: " + ', '.join(self.rules.keys()) + ", exit") return players_choice 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 bots_choice in self.rules: if self.rules[bots_choice][players_choice] == 1: return bots_choice def _easy(self): bots_choice = random.choice(list(self.rules.keys())) return bots_choice def _hard(self): total = sum(self.player.choices.values()) if total == 0: return self._easy() probabilities = {choice: self.player.choices[choice] / total for choice in self.player.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 def _create_dump(self): player = self.player.create_dump() dump = { "player": player, "mode": self.mode, "rules": self.rules, "scores": { "player": self.scores["player"], "bot": self.scores["bot"] } } return json.dumps(dump) def _load_dump(self, dump): dump = json.loads(dump) self.player.load_dump(dump["player"]) self.scores = dump["scores"] self.mode = dump["mode"] self.rules = dump["rules"] class Player: name = "player" choices = {} def __init__(self, name, choices): self.choices = choices self.name = name def __str__(self): return self.name def create_dump(self): dump = { "name": self.name, "choices": self.choices } return json.dumps(dump) def load_dump(self, dump): dump = json.loads(dump) self.name = dump["name"] self.choices = dump["choices"] text = ''' Liebe/r Empfänger, Herzliche Grüße aus dem hohen Norden! Ich hoffe, diese Nachricht erreicht dich in guter Verfassung und fügt einen Hauch von festlicher Stimmung zu deinem Tag hinzu. Ich wünsche dir Freude, Gelächter und einen festlichen Geist, der dein Herz erwärmt. Genieße die Magie der Feiertage! Herzliche Grüße, Rudolph ''' # ceaser = CeaserChiffre() # 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} } def throw_and_plot(n, c): plot(throw_cubes(n, c)) def throw_cubes(n=100, cubes=2): entries = {} for i in range(n): x = 0 for c in range(cubes): x += random.randint(1, 6) if x in entries: entries[x] += 1 else: entries[x] = 1 return entries def plot(data): x = list(data.keys()) y = list(data.values()) plt.bar(x, y) plt.show() throw_and_plot(10000, 2) 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_lizard_spock, "hard") # game.match(10)