diff --git a/.idea/UNI_Python.iml b/.idea/UNI_Python.iml
index 71bf193..d599f79 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 c20271c..59b5b2e 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 0e0aa4a..4c16fdf 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -143,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 9c4348c..65aea60 100644
--- a/ha_05/loosen_janniclas_1540907_06.py
+++ b/ha_05/loosen_janniclas_1540907_06.py
@@ -1,4 +1,7 @@
+import json
+import os
import random
+from os.path import exists
class CeaserChiffre:
@@ -8,12 +11,12 @@ class CeaserChiffre:
if chiffre is not None:
self.chiffre = chiffre
else:
- self._create_chiffre()
+ self.renew_chiffre()
def __str__(self):
return str(self.chiffre)
- def _create_chiffre(self):
+ def renew_chiffre(self):
codes = {}
chars = list(range(0, 255))
for char in range(0, 255):
@@ -39,42 +42,192 @@ class CeaserChiffre:
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}
+class Rational:
+ numerator = 0
+ divisor = 1
def __str__(self):
- return "bot: " + str(self.scores["bot"]) + " - player: " + str(self.scores["player"])
+ return "%d/%d" % (self.numerator, self.divisor)
+
+ def __init__(self, a=0, b=1):
+ if isinstance(a, Rational):
+ self.numerator = a.numerator
+ self.divisor = a.divisor
+ self.shorten()
+ elif isinstance(a, int) and isinstance(b, int):
+ if b == 0:
+ print("Divisor cannot be zero!")
+ exit(1)
+ if b < 0:
+ b = -b
+ a = -a
+ self.numerator = a
+ self.divisor = b
+ self.shorten()
+ else:
+ print("Numerator and divisor must be integer.")
+ exit(1)
+
+ def gcd(self, a, b):
+ while b != 0:
+ (a, b) = (b, a % b)
+ return a
+
+ def lcm(self, a, b):
+ return abs(a * b) // self.gcd(a, b)
+
+ def lcd(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ return self.lcm(self.divisor, other.divisor)
+
+ def shorten(self):
+ d = self.gcd(self.numerator, self.divisor)
+ self.numerator = self.numerator // d
+ self.divisor = self.divisor // d
+
+ # addition
+ def __add__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+
+ lcm = self.lcm(self.divisor, other.divisor)
+
+ fac1 = lcm // self.divisor
+ fac2 = lcm // other.divisor
+
+ new_divisor = lcm
+ new_nominator = self.numerator * fac1 + other.numerator * fac2
+ return Rational(new_nominator, new_divisor)
+
+ def __radd__(self, other):
+ return self.__add__(other)
+
+ # subtraction
+ def __sub__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ divisor_lcm = self.lcm(self.divisor, other.divisor)
+ new_nominator = (self.numerator * divisor_lcm / self.divisor) - (other.numerator * divisor_lcm / other.divisor)
+ return Rational(int(new_nominator), int(divisor_lcm))
+
+ def __rsub__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ divisor_lcm = self.lcm(self.divisor, other.divisor)
+ new_nominator = (other.numerator * divisor_lcm / other.divisor) - (self.numerator * divisor_lcm / self.divisor)
+ return Rational(int(new_nominator), int(divisor_lcm))
+
+ # multiplication
+ def __mul__(self, other):
+ if isinstance(other, Rational):
+ return Rational(self.numerator * other.numerator, self.divisor * other.divisor)
+ else:
+ return Rational(self.numerator * other, self.divisor)
+
+ def __rmul__(self, other):
+ return self.__mul__(other)
+
+ # division
+ def __truediv__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ new_numerator = self.numerator * other.divisor
+ new_divisor = self.divisor * other.numerator
+ return Rational(new_numerator, new_divisor)
+
+ def __rtruediv__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ new_numerator = self.divisor * other.numerator
+ new_divisor = self.numerator * other.divisor
+ return Rational(new_numerator, new_divisor)
+
+ def __lt__(self, other):
+ divisor_lcm = self.lcd(other)
+ return (self.numerator * divisor_lcm / self.divisor) < (other.numerator * divisor_lcm / other.divisor)
+
+ def __gt__(self, other):
+ divisor_lcm = self.lcd(other)
+ return (self.numerator * divisor_lcm / self.divisor) > (other.numerator * divisor_lcm / other.divisor)
+
+ def __le__(self, other):
+ divisor_lcm = self.lcd(other)
+ return (self.numerator * divisor_lcm / self.divisor) <= (other.numerator * divisor_lcm / other.divisor)
+
+ def __ge__(self, other):
+ divisor_lcm = self.lcd(other)
+ return (self.numerator * divisor_lcm / self.divisor) >= (other.numerator * divisor_lcm / other.divisor)
+
+ def __eq__(self, other):
+ if not isinstance(other, Rational):
+ other = Rational(other)
+ # all constructed Rationals are already shortened
+ return self.divisor == other.divisor and self.numerator == other.numerator
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+
+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.choices = {key: 0 for key in self.rules}
+ self.player.choices = {key: 0 for key in self.rules}
+ if os.path.exists('storage.json'):
+ os.remove('storage.json')
- 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"])
+ 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"]:
- print("The player wins.")
- else:
+ 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):
+ 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.choices[players_choice] += 1
+ self.player.choices[players_choice] += 1
result = self.rules[players_choice][bots_choice]
if result > 0:
self.scores["player"] += 1
@@ -82,11 +235,17 @@ class Game:
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()) + "): ")
- if players_choice not in self.rules:
- raise ValueError("Invalid choice. Please choose from: " + ', '.join(self.rules.keys()))
- return players_choice.lower()
+ 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 = ""
@@ -105,19 +264,76 @@ class Game:
return bots_choice
def _easy(self):
- bots_choice = random.choice(list(self.choices.keys()))
+ bots_choice = random.choice(list(self.rules.keys()))
return bots_choice
def _hard(self):
- total = sum(self.choices.values())
+ total = sum(self.player.choices.values())
if total == 0:
return self._easy()
- probabilities = {choice: self.choices[choice] / total for choice in self.choices}
+ 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"]
+
+
+def heron(a, x, run):
+ a = Rational(a)
+ x = Rational(x)
+
+ for i in range(run):
+ x = x - (x * x - a) / (2 * x)
+ return x
+
+
+# start = Rational(3, 2)
+# sol = heron(2, start, 10000)
+# print(sol)
text = '''
Liebe/r Empfänger,
@@ -150,4 +366,4 @@ rock_paper_scissor_lizard_spock = {
}
# game = Game(rock_paper_scissor_lizard_spock, "hard")
-# game.match(5)
+# game.match(10)