/
manager.py
executable file
·65 lines (60 loc) · 2.36 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from market import Market
from mine import Mine
import heapq
# The Manager class represents the game manager, which is in charge of
# running the event loop of the game (playOneRound)
class Manager:
# __init___
# this constructs the Manager object
def __init__(self):
self.world = {}
self.market = Market()
self.players = []
self.miningQueue = []
# addPlayer
# @player(object): a player object. See player.py for details
# @initialState(dict): the initial state of the given player
def addPlayer(self, player, initialState: dict):
self.players.append(player)
self.world[player.name] = initialState
# playOneRound
# run one round of the game and apply the actions proposed by each player
# this is the "event loop" of the game
def playOneRound(self):
for player in self.players:
if player.free:
print("-------" + player.name + "'s turn-------")
actions = player.generateActions(self.world, self.market)
print(player.name, "proposed:")
for action in actions:
print(action.toString())
if isinstance(action, Mine):
player.free = False
heapq.heappush(self.miningQueue, [action.difficulty, action])
else:
action.execute(self.world)
print("-" * (len(player.name) + 21))
print()
self.runMiningQueue()
self.market.settle(self.world)
# runMiningQueue
# update the mining queue
def runMiningQueue(self):
for pairs in self.miningQueue:
pairs[0] = pairs[0] - 1
while self.miningQueue and self.miningQueue[0][0] == 0:
self.miningQueue[0][1].execute(self.world)
heapq.heappop(self.miningQueue)
# run
# run the game with the given round limit
# @rounds: the round limit
def run(self, rounds: int):
for i in range(0, rounds):
print("-----------Round " + str(i + 1) + "-----------")
self.playOneRound()
print("-------End of Round " + str(i + 1) + "--------")
print("World State:", self.world)
print()
print("------------Market------------")
self.market.printOrderBook()
print()