-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
148 lines (126 loc) · 4.73 KB
/
player.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from random import SystemRandom as Random
from turn import Turn
from game import Game
from card import Card
class Player:
def __init__(self, name, player_id, hand):
self.name = name
self.player_id = player_id
self.hand = hand
self.stack = []
self.init_game()
def __repr__(self):
return "Player " + self.name + ": " + self.hand.__repr__()
def init_game(self):
self.game = Game("weiter") # dummy player
def play(self, turn, history):
print("Player", self.name, "may put down", self.hand.get_playable_cards(turn.get_suit()))
choice = Random().choice(self.hand.get_playable_cards(turn.get_suit()))
self.hand.play(choice)
turn.add_card(choice)
def set_game(self, game, announcer):
self.hand.sort(game)
self.announcer = announcer
self.game = game
class AIPlayer(Player):
def __init__(self, name, player_id, hand):
Player.__init__(self, name, player_id, hand)
def init_game(self):
print("Estimating hand: " + str(self.hand))
solo, solo_suit = self.solo_probability()
wenz = self.wenz_probability()
sauspiel, sauspiel_suit = self.sauspiel_probability()
if solo > wenz and solo > 0.6:
self.game = Game("Solo", solo_suit)
elif wenz > 0.6:
self.game = Game("Wenz")
elif sauspiel > 0.6:
self.game = Game("Sauspiel", sauspiel_suit)
else:
self.game = Game("weiter")
def solo_probability(self):
max_probability = -1.0
max_probability_suit = None
for suit in Card.SUITS:
game = Game("Solo", suit) # get suit from context
def solo_card_estimator(card):
if game.is_trump(card):
return { # TODO: test and tweak these values
"O": 0.13,
"U": 0.12,
"A": 0.10,
"X": 0.09,
"K": 0.06,
"9": 0.05,
"8": 0.05,
"7": 0.05
}[card.number]
else:
return {
"A": 0.05,
"X": 0.03,
"K": 0.01,
"9": 0.00,
"8": 0.00,
"7": 0.00
}[card.number]
probability = self.hand.evaluate(solo_card_estimator)
print("Estimating " + str(game) + ": " + str(probability))
if probability > max_probability:
max_probability = probability
max_probability_suit = suit
return max_probability, max_probability_suit
def wenz_probability(self):
def wenz_card_estimator(card):
return {
"U": 0.18,
"A": 0.07,
"X": 0.03,
"K": 0.01,
"O": 0.00,
"9": 0.00,
"8": 0.00,
"7": 0.00
}[card.number]
probability = self.hand.evaluate(wenz_card_estimator)
print("Estimating Wenz: " + str(probability))
return probability
def sauspiel_probability(self):
max_probability = -1.0
max_probability_called_suit = None
for suit in Card.SUITS:
game = Game("Sauspiel", suit) # get suit from context
def sauspiel_card_estimator(card):
if game.is_trump(card):
return { # TODO: test and tweak these values
"O": 0.12,
"U": 0.12,
"A": 0.10,
"X": 0.10,
"K": 0.07,
"9": 0.07,
"8": 0.07,
"7": 0.07
}[card.number]
else:
return {
"A": 0.05,
"X": 0.03,
"K": 0.01,
"9": 0.00,
"8": 0.00,
"7": 0.00
}[card.number]
probability = self.hand.evaluate(sauspiel_card_estimator)
print("Estimating " + str(game) + ": " + str(probability))
if probability > max_probability:
max_probability = probability
max_probability_called_suit = suit
return max_probability, max_probability_called_suit
def start_round(self, history):
turn = Turn(self.name)
self.play(turn, history)
return turn
class HumanPlayer(Player):
def __init__(self, name, player_id, hand):
Player.__init__(self, name, player_id, hand)