-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
65 lines (55 loc) · 2.75 KB
/
game.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
import random
import sys
import player
import property
import tree
from node import Node
class Game:
def __init__(self, players):
self.players: list[player.Player] = players
self.current_player = players[0]
self.turn = 0
self.properties = property.properties # Add this line to define the 'board' attribute
def roll_dice(self):
die = random.randint(1, 6)
return die
def next_player(self):
index = self.players.index(self.current_player)
self.current_player = self.players[(index + 1) % len(self.players)]
def play_game(self):
root = Node(self.properties, self.players[0], self.players[1], "non-chance", None)
mono_tree = tree.MonopolyTree(root)
# The higher the level of intelligence, the more time it takes to make a decision,
# but the decisions are more rational.
intelligence_level = 8
mono_tree.generate_tree(intelligence_level)
Node.Eval(mono_tree)
current_node = root
while True:
if current_node.current_player.balance > 2000 or current_node.second_player.balance < 0:
print(f"player {current_node.current_player.ID} win !")
sys.exit(0)
elif current_node.current_player.balance < 0 or current_node.second_player.balance > 2000:
print(f"player {current_node.second_player.ID} win !")
sys.exit(0)
if len(current_node.action) == 0:
mono_tree = tree.MonopolyTree(current_node)
mono_tree.generate_tree(intelligence_level)
Node.Eval(mono_tree)
if current_node.node_type == "chance":
dice = self.roll_dice()
name = "zero" if current_node.current_player.ID == 0 else "one"
print(f"player {name} got {dice}")
current_node = current_node.action[dice - 1][1]
else:
if current_node.current_player.ID == 0:
current_node.action.sort(key=lambda tup: tup[1].zero_value, reverse=True)
print(
f"{current_node.action[0][0]} ( position: {current_node.properties[current_node.current_player.position].name} ) (cash before action:{current_node.current_player.balance})")
current_node = current_node.action[0][1]
else:
current_node.action.sort(key=lambda tup: tup[1].one_value, reverse=True)
print(
f"{current_node.action[0][0]} ( position: {current_node.properties[current_node.current_player.position].name} ) (cash before action:{current_node.current_player.balance})")
current_node = current_node.action[0][1]
print()