Skip to content

Instantly share code, notes, and snippets.

@ymollard
Created March 17, 2022 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymollard/af33d2ba828f0a894397d0a7fb94c8eb to your computer and use it in GitHub Desktop.
Save ymollard/af33d2ba828f0a894397d0a7fb94c8eb to your computer and use it in GitHub Desktop.
import time
def log(*args):
if not hasattr(log, "t0"):
log.t0 = time.time()
print("t+" + str(int(time.time()-log.t0)) + "\t: ", end="")
print(*args)
class ChessMaster:
def think_and_play(self, round: "Round", opponent: "Player"):
log("Master is thinking for round {} with opponent {}...".format(round, opponent.id))
time.sleep(1)
class Round:
def __init__(self, round, master, players):
self.players = players
self.round = round
self.master = master
def play(self, opponent_id: int):
self.players[opponent_id].think_and_play(self.round)
self.master.think_and_play(self.round, self.players[opponent_id])
class Player:
def __init__(self, id: int):
self.id = id
def think_and_play(self, round: "Round"):
log("Player {} is thinking for round {}...".format(self.id, round))
time.sleep(5)
class Simulator:
def __init__(self):
self.num_players = 3
self.num_rounds = 2
self.master = ChessMaster()
self.players = [Player(i) for i in range(self.num_players)]
self.rounds = [Round(i, self.master, self.players) for i in range(self.num_rounds)]
def simulate(self):
log("Simulating all moves...")
for round_id, round in enumerate(self.rounds):
for player_id, player in enumerate(self.players):
round.play(player_id)
log("Simulation is over")
if __name__ == "__main__":
Simulator().simulate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment