def _auction(num_houses, players, lambda_bid, lambda_extract_bid): players_in_auction = dict(zip(players, [True] * len(players))) properties_to_build_houses_on = self._properties_to_build_houses_on() all_changes = [] for i in range(num_houses): highest_bid = 0 highest_changes = None while True: highest_bid_for_round = highest_bid highest_changes_for_round = dict() for player in players: if not players_in_auction[player]: continue goc = lambda_bid(player, highest_bid, properties_to_build_houses_on[player], self._state) bid = 0 for change in goc: if len(change.change_in_houses) > 0: bid = lambda_extract_bid(change, state) if bid < highest_bid: players_in_auction[player] = False elif bid > highest_bid_for_round: highest_bid_for_round = bid highest_changes_for_round = {player: goc} elif bid == highest_bid_for_round: highest_changes_for_round[player] = goc if len(highest_changes_for_round) > 1: fastest_time = 10000 fastest_player = None fastest_goc = None for player, goc in highest_changes_for_round.iteritems(): # TODO: Don't use datetime, better timing solution (timeit doesn't work bc lambda params are out of scope) times = [] for i in range(3): start = datetime.datetime.now() lambda_bid(player, highest_changes_for_round, properties_to_build_houses_on[player], self._state) end = datetime.datetime.now() times.append((end - start).total_seconds()) avg_time = sum(times)/len(times) if avg_time < fastest_time: fastest_time = avg_time fastest_player = player fastest_goc = goc highest_changes_for_round = {fastest_player: fastest_goc} if highest_bid_for_round > highest_bid: highest_bid = highest_bid_for_round highest_changes = highest_changes_for_round[highest_changes_for_round.keys()[0]] if len(highest_changes_for_round) == 0 or players_in_auction.values().count(True) <= 1: break all_changes.append(highest_changes) return GroupOfChanges.combine(all_changes)
def run(self): num_players = len(self._state.players) idx = random.randint(0,num_players-1) while not self._completed(): # cash = [player.cash for player in self._state.players] # print cash player = self._state.players[idx] idx = (idx + 1) % len(self._state.players) roll = Roll() print '%s rolled a %d%s' % (player.name, roll.value, ' (doubles)' if roll.is_doubles else '') if player.jail_moves > 0 and roll.is_doubles: self._state.apply(GroupOfChanges(changes=[GameStateChange.leave_jail(player)])) elif player.jail_moves >= 2: self._state.apply(GroupOfChanges(changes=[GameStateChange.decrement_jail_moves(player)])) self._wait() continue elif player.jail_moves == 1: # TODO: Allow player to choose to use a "Get out of jail free" card pay_changes = player.pay(self._state.bank, 50, self._state) leave_changes = GroupOfChanges(changes=[GameStateChange.leave_jail(player)]) self._state.apply(GroupOfChanges.combine([pay_changes, leave_changes])) self._take_turn(player, roll.value) num_rolls = 0 max_rolls = 2 while roll.is_doubles: roll = Roll() print '%s rolled a %d%s' % (player.name, roll.value, ' (doubles)' if roll.is_doubles else '') num_rolls += 1 if num_rolls > max_rolls: self._state.apply(GroupOfChanges(changes=[GameStateChange.send_to_jail(player)])) break self._take_turn(player, roll.value)
def _pay_each_player_50(player, state): changes_paying_players = [] for other_player in state.players: if other_player != player: changes_paying_players.append( player.pay(other_player, 50, state)) all_payments = GroupOfChanges.combine(changes_paying_players) return all_payments
def _collect_50_from_every_player(player, state): changes_from_other_players = [] for other_player in state.players: if other_player != player: changes_from_other_players.append( other_player.pay(player, 50, state)) all_payments = GroupOfChanges.combine(changes_from_other_players) return all_payments
def landed(self, player, roll, state): deck = state.decks[self._card_type] draw_card = GameStateChange.draw_card(deck, player) card_lmbda = draw_card.card_drawn[deck] result_of_card = None if card_lmbda == LMBDA_GET_OUT_OF_JAIL_FREE: result_of_card = GroupOfChanges() else: result_of_card = card_lmbda(player, state) return GroupOfChanges.combine( [GroupOfChanges([draw_card]), result_of_card])
def run(self): num_players = len(self._state.players) idx = random.randint(0, num_players - 1) while not self._completed(): # cash = [player.cash for player in self._state.players] # print cash player = self._state.players[idx] idx = (idx + 1) % len(self._state.players) roll = Roll() print '%s rolled a %d%s' % (player.name, roll.value, ' (doubles)' if roll.is_doubles else '') if player.jail_moves > 0 and roll.is_doubles: self._state.apply( GroupOfChanges( changes=[GameStateChange.leave_jail(player)])) elif player.jail_moves >= 2: self._state.apply( GroupOfChanges( changes=[GameStateChange.decrement_jail_moves(player) ])) self._wait() continue elif player.jail_moves == 1: # TODO: Allow player to choose to use a "Get out of jail free" card pay_changes = player.pay(self._state.bank, 50, self._state) leave_changes = GroupOfChanges( changes=[GameStateChange.leave_jail(player)]) self._state.apply( GroupOfChanges.combine([pay_changes, leave_changes])) self._take_turn(player, roll.value) num_rolls = 0 max_rolls = 2 while roll.is_doubles: roll = Roll() print '%s rolled a %d%s' % (player.name, roll.value, ' (doubles)' if roll.is_doubles else '') num_rolls += 1 if num_rolls > max_rolls: self._state.apply( GroupOfChanges( changes=[GameStateChange.send_to_jail(player)])) break self._take_turn(player, roll.value)