Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import annotations
- from collections import Counter
- from typing import Tuple
- _rolls = []
- for i in range(1, 4):
- for j in range(1, 4):
- for k in range(1, 4):
- _rolls.append(i + j + k)
- POTENTIAL_ROLLS = Counter(_rolls)
- @dataclass
- class Universe:
- positions: Tuple[int, int]
- scores: Tuple[int, int]
- count: int
- @classmethod
- def create(cls, start_position_1: int, start_position_2: int) -> Universe:
- return Universe((start_position_1 - 1, start_position_2 - 1), (0, 0), 1)
- def __hash__(self):
- return hash(self.__repr__())
- @property
- def key(self):
- return f"Universe[{self.positions}, {self.scores}]"
- @property
- def is_finised(self):
- return self.scores[0] >= 21 or self.scores[1] >= 21
- @property
- def winner(self):
- if not self.is_finised:
- raise Exception("Game is not finished")
- return 1 if self.scores[0] >= 21 else 2
- def add(self, other: Universe):
- if not self.key == other.key:
- raise Exception("These are not the same Universes")
- self.count += other.count
- def roll(self, player: int):
- new_universes = {}
- for roll, count in POTENTIAL_ROLLS.items():
- if player == 1:
- new_position = (self.positions[0] + roll) % 10
- new_score = self.scores[0] + (new_position + 1)
- universe = Universe(
- (new_position, self.positions[1]),
- (new_score, self.scores[1]),
- self.count * count
- )
- elif player == 2:
- new_position = (self.positions[1] + roll) % 10
- new_score = self.scores[1] + (new_position + 1)
- universe = Universe(
- (self.positions[0], new_position),
- (self.scores[0], new_score),
- self.count * count
- )
- else:
- raise Exception("Bruh! This is a 2 player game yo!")
- if universe.key not in new_universes:
- new_universes[universe.key] = universe
- else:
- new_universes[universe.key].add(universe)
- return list(new_universes.values())
- current_universes = [Universe.create(10, 1)]
- finished_games = {}
- player_turn = 1
- while current_universes:
- new_universes = {}
- for current in current_universes:
- for new in current.roll(player_turn):
- if new.is_finised:
- if new.key not in finished_games:
- finished_games[new.key] = new
- else:
- finished_games[new.key].add(new)
- else:
- if new.key not in new_universes:
- new_universes[new.key] = new
- else:
- new_universes[new.key].add(new)
- current_universes = list(new_universes.values())
- player_turn = 2 if player_turn == 1 else 1
- wins = {1: 0, 2: 0}
- for universe in finished_games.values():
- wins[universe.winner] += universe.count
- wins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement