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 test_leave_jail(self): state = GameState(1) player = state.players[0] # Test that leaving jail works no matter how many jail moves are left for num_turns in range(0, 3): # Set up player in jail state.apply(GroupOfChanges([GameStateChange.send_to_jail(player)])) # Decrement player's jail moves num_turns (0, 1, or 2) times for i in range(0, num_turns): state.apply( GroupOfChanges( [GameStateChange.decrement_jail_moves(player)])) # Test leaving jail, and ensure that player's jail moves are changed # correctly and that no other changes were made to the state. str_before = str(state) state.apply(GroupOfChanges([GameStateChange.leave_jail(player)])) str_after = str(state) expected_diff = [('Jail moves: %d' % (3 - num_turns), 'Jail moves: 0')] self.assertDiffGameStates(str_before, str_after, expected_diff, msg='Player did not leave jail properly')
def test_leave_jail(self): state = GameState(1) player = state.players[0] # Test that leaving jail works no matter how many jail moves are left for num_turns in range(0, 3): # Set up player in jail state.apply(GroupOfChanges([ GameStateChange.send_to_jail(player)])) # Decrement player's jail moves num_turns (0, 1, or 2) times for i in range(0, num_turns): state.apply(GroupOfChanges([ GameStateChange.decrement_jail_moves(player)])) # Test leaving jail, and ensure that player's jail moves are changed # correctly and that no other changes were made to the state. str_before = str(state) state.apply(GroupOfChanges([ GameStateChange.leave_jail(player)])) str_after = str(state) expected_diff = [ ('Jail moves: %d' % (3-num_turns), 'Jail moves: 0') ] self.assertDiffGameStates(str_before, str_after, expected_diff, msg='Player did not leave jail properly')
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 respond_to_state(self, player, new_state): if player.jail_moves > 0 and player.jail_free_count > 0: # Pick a Deck to return the jail free card to randomly # TODO: Need a better way of picking a Deck to return the jail free card to if (randint(0, 1) == 0): deck = new_state.decks[CHANCE_CARD] else: deck = new_state.decks[COMMUNITY_CHEST_CARD] use_card = GameStateChange.decrement_jail_card_count(player, deck) leave_jail = GameStateChange.leave_jail(player) return NotificationChanges(non_building_changes=[use_card, leave_jail]) else: return NotificationChanges()
def respond_to_state(self, player, new_state): if player.jail_moves > 0 and player.jail_free_count > 0: # Pick a Deck to return the jail free card to randomly # TODO: Need a better way of picking a Deck to return the jail free card to if (randint(0, 1) == 0): deck = new_state.decks[CHANCE_CARD] else: deck = new_state.decks[COMMUNITY_CHEST_CARD] use_card = GameStateChange.decrement_jail_card_count(player, deck) leave_jail = GameStateChange.leave_jail(player) return NotificationChanges( non_building_changes=[use_card, leave_jail]) else: return NotificationChanges()