-
Notifications
You must be signed in to change notification settings - Fork 4
/
robot_bidding.py
367 lines (309 loc) · 12.7 KB
/
robot_bidding.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 25 12:51:07 2017
Automated practice bidding.
Notes:
- Further define settings manipulation.
"""
__author__ = "Andrew I McClement"
from enum import Enum, auto
import itertools
from random import choice
from practice_bidding.xml_parsing.xml_parser import Bid
from practice_bidding.bridge_parser import parse_with_quit, ParseResults
from practice_bidding.redeal.redeal import Deal
class BiddingProgram:
""" The player is assumed to always sit South. """
class Players(Enum):
"""
Bridge players at the table, named after the cardinal directions.
"""
North = auto()
East = auto()
South = auto()
West = auto()
class Vulnerability(Enum):
""" Possible vulnerabilities. """
None_ = auto()
Favourable = auto()
All = auto()
Unfavourable = auto()
class ProgramMode(Enum):
""" Possible modes for the program to run in."""
# The user makes bids for South.
Default = auto()
# The program makes all bids.
Automatic = auto()
# Integers correspond to board number modulo 4 for which that player
# is dealer.
_dealer_map = {1: Players.North, 2: Players.East, 3: Players.South,
0: Players.West}
_pass = Bid("P", "Pass", [])
def __init__(self):
# Board number set to 0 as self.generate_new_deal increments board
# number by 1.
self._board_state = {"board_number": 0,
"deal_generator": Deal.prepare({}),
"deal": None,
"bidding_sequence": [],
"opening_bids": {}}
self.generate_new_deal()
self._settings = {"mode": self.ProgramMode.Default,
"display_meaning_of_bids": False,
"display_meaning_of_possible_bids": False}
@property
def _root(self):
return self._board_state["opening_bids"]
@property
def deal(self):
""" The current deal. """
return self._board_state["deal"]
def generate_new_deal(self):
"""Get a new deal."""
self._board_state["deal"] = self._board_state["deal_generator"]()
self._board_state["bidding_sequence"] = []
self._board_state["board_number"] += 1
@property
def bidding_sequence(self):
"""The bidding sequence so far."""
return self._board_state["bidding_sequence"]
@property
def board_number(self):
"""The current board number."""
return self._board_state["board_number"]
@property
def vulnerability(self):
"""Current vulnerability."""
board_number = self.board_number % 16
if board_number in {1, 8, 11, 14}:
return self.Vulnerability.None_
elif board_number in {2, 5, 12, 15}:
return self.Vulnerability.Unfavourable
elif board_number in {3, 6, 9, 0}:
return self.Vulnerability.Favourable
elif board_number in {4, 7, 10, 13}:
return self.Vulnerability.All
@property
def _dealer(self):
return self._dealer_map[(self.board_number) % 4]
# shift = index in bidding sequence. By default returns the player
# who is next to bid.
# board number is reduced by 1 since it starts at
def _bidder(self, shift=None):
if shift is None:
shift = len(self.bidding_sequence)
return self._dealer_map[(self.board_number + shift) % 4]
def parse(self, input_, exclude_settings=False):
""" Parse user input. """
result = parse_with_quit(input_)
if result == ParseResults.Settings and not exclude_settings:
self.edit_settings()
return result
def get_validated_input(self,
message,
valid_formats,
help_message=None,
exclude_settings=False,
tries=0) -> (str, ParseResults):
""" Get user input satisfying the given formats. """
input_ = None
result = None
assert ParseResults.Help not in valid_formats
# Use tries=0 as default, so must start counting from 1.
for i in itertools.count(1):
input_ = input(message)
result = self.parse(input_, exclude_settings)
if result == ParseResults.Help:
print(help_message)
elif result in valid_formats:
break
elif i == tries:
# No sensible value to return here, so must raise an error.
raise ValueError(input_)
return input_, result
@property
def _mode(self):
return self._settings["mode"]
def get_hand(self, seat=Players.South):
""" Returns the hand of the player in the given seat. """
if seat == self.Players.North:
return self.deal.north
elif seat == self.Players.East:
return self.deal.east
elif seat == self.Players.South:
return self.deal.south
elif seat == self.Players.West:
return self.deal.west
elif isinstance(seat, self.Players): # pragma: no cover
raise ValueError(seat)
else: # pragma: no cover
raise TypeError(seat, self.Players)
@classmethod
def is_passed_out(cls, bidding_sequence):
""" Checks if a bidding sequence is a passout. """
if len(bidding_sequence) < 4:
return False
last_bids = bidding_sequence[-3:]
for bid in last_bids:
if bid != cls._pass:
return False
return True
def bid(self):
""" Get the next bid, whether from the user or the program. """
# Should have defined bids before trying to bid.
assert self._root
if self.is_passed_out(self.bidding_sequence):
# No further bids can be made.
return
# Get the next bid made.
current_bidder = self._bidder()
if current_bidder in {self.Players.East, self.Players.West}:
next_bid = self._pass
elif current_bidder == self.Players.South and \
self._mode == self.ProgramMode.Default:
next_bid = self._user_bid()
else:
# Program must make a bid.
next_bid = self._program_bid(self.get_hand(current_bidder))
if ((next_bid != self._pass)
and self._settings["display_meaning_of_bids"]):
print(f"{next_bid.value}: {next_bid.description}")
self.bidding_sequence.append(next_bid)
def _program_bid(self, current_hand):
potential_bids = None
if len(self.bidding_sequence) >= 2:
current_bid = self.bidding_sequence[-2]
if current_bid != self._pass:
# Partner made a non-trivial bid.
potential_bids = [bid for bid in current_bid.children.values()
if bid.accept(current_hand)]
if potential_bids is None:
potential_bids = [bid for bid in self._root.values()
if bid.accept(current_hand)]
try:
bid = choice(potential_bids)
except IndexError:
# No acceptable bidding options.
bid = self._pass
return bid
def set_opening_bids(self, opening_bids):
""" Set the opening bids. """
self._board_state["opening_bids"] = opening_bids
def _user_bid(self):
# By default this is an opening bid.
potential_bids = self._root
if len(self.bidding_sequence) >= 2:
current_bid = self.bidding_sequence[-2]
if current_bid != self._pass:
# Our partner made a non-trivial bid.
potential_bids = current_bid.children
bid = None
while bid is None:
if self._mode == self.ProgramMode.Automatic:
# The user has changed the mode of the program.
current_hand = self.get_hand(self.Players.South)
bid = self._program_bid(current_hand)
break
print(potential_bids.keys())
selected, _ = self.get_validated_input(
"Your bid: ",
{ParseResults.BridgeBid},
help_message=("Enter a bid from one of the potential bids "
"listed. You must use a single character to "
"define the suit."))
if selected.upper() in {self._pass.value, "PASS"}:
bid = self._pass
break
selected = selected.lower()
try:
bid = potential_bids[selected]
except KeyError:
print("That was not an expected response.")
return bid
def edit_settings(self):
"""
Edit the settings of the program.
These will then be written to an xml file.
"""
print(self._settings)
settings_keys = list(self._settings.keys())
for i, key in enumerate(settings_keys):
print(f"{i}: {key}")
result = None
while result != ParseResults.Back:
message = ("Enter a key to edit the value for that key, or "
"'back' to exit the settings editor.\n"
"'Exit' will end the program.\n")
input_, result = self.get_validated_input(
message, {ParseResults.Back, ParseResults.Integer},
exclude_settings=True)
if result == ParseResults.Back:
return
try:
key = settings_keys[int(input_)]
except KeyError:
print("This is not a valid key.")
continue
if key == "mode":
input_ = input(f"Do you wish to change the mode of the program"
f" from {self._mode}? (y/n)")
result = self.parse(input_, True)
if result == ParseResults.Yes:
if self._mode == self.ProgramMode.Automatic:
self._settings["mode"] = self.ProgramMode.Default
elif self._mode == self.ProgramMode.Default:
self._settings["mode"] = self.ProgramMode.Automatic
else:
input_, result = self.get_validated_input(
f"Do you wish to change {key} from {self._settings[key]}?"
" (y/n)",
{ParseResults.Yes, ParseResults.No, ParseResults.Back})
if result == ParseResults.Yes:
self._settings[key] = not self._settings[key]
def get_contract(self, bidding_sequence=None):
""" Get the contract (including declarer) from a bidding sequence
This assumes the bidding sequence is for the current board.
"""
if bidding_sequence is None:
bidding_sequence = self.bidding_sequence
assert self.is_passed_out(bidding_sequence)
# Must have 3 passes.
last_bid = bidding_sequence[-4]
if last_bid == self._pass:
return "P"
index = len(bidding_sequence) - 4
suit = last_bid.suit
suit_bid_by_partner = False
# Work out who bid the suit first.
for i in range(index - 2, -1, -4):
partner_bid = bidding_sequence[i]
if partner_bid == self._pass:
break
elif partner_bid.suit == suit:
suit_bid_by_partner = True
break
bidder = self._bidder(index - 2 if suit_bid_by_partner else index)
contract = last_bid.value.upper()
player_map = {self.Players.North: "N", self.Players.East: "E",
self.Players.South: "S", self.Players.West: "W"}
contract += player_map[bidder]
return contract
def get_double_dummy_result(self, contract):
""" Get the number of tricks and corresponding score. """
try:
assert len(contract) == 3
assert int(contract[0]) in range(1, 8)
assert contract[1] in {"C", "D", "H", "S", "N"}
assert contract[2] in {"N", "E", "S", "W"}
except AssertionError:
raise ValueError(f"{contract} not a valid contract.")
if contract[2] in {"N", "S"}:
vulnerability = self.vulnerability in {
self.Vulnerability.All,
self.Vulnerability.Unfavourable}
else:
vulnerability = self.vulnerability in {
self.Vulnerability.All,
self.Vulnerability.Favourable}
return (self.deal.dd_tricks(contract),
self.deal.dd_score(contract, vulnerability))