154 lines
4.8 KiB
Python
154 lines
4.8 KiB
Python
import random
|
|
|
|
|
|
class CeaserChiffre:
|
|
chiffre = {}
|
|
|
|
def __init__(self, chiffre=None):
|
|
if chiffre is not None:
|
|
self.chiffre = chiffre
|
|
else:
|
|
self._create_chiffre()
|
|
|
|
def __str__(self):
|
|
return str(self.chiffre)
|
|
|
|
def _create_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}
|
|
choices = {}
|
|
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}
|
|
|
|
def __str__(self):
|
|
return "bot: " + str(self.scores["bot"]) + " - player: " + str(self.scores["player"])
|
|
|
|
def reset(self):
|
|
self.scores["player"] = 0
|
|
self.scores["bot"] = 0
|
|
self.choices = {key: 0 for key in self.rules}
|
|
|
|
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 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.choices.keys()))
|
|
return bots_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 = '''
|
|
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}
|
|
|
|
}
|
|
|
|
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(5)
|