def start_new_game(players: List[str], init_pot=100, small_blind_stake=5, max_rounds=100, use_local=True): new_game = Game(players=players, init_pot=init_pot, small_blind_stake=small_blind_stake) LOGGER.info('New game: %s', new_game) game_result = GameResult(None) game_state = GameState(players=players, player_pots=[init_pot for _ in range(len(players))]) LOGGER.debug('New game state: %s', game_state) # test with only max_rounds for pos in range(max_rounds): if len(game_state.remaining_players) == 1: game_result.set_winner(game_state.remaining_players[0]) break deck = list(Card) random.shuffle(deck) game_round = GameRound( pos=pos, small_blind=game_state.get_small_blind_player(pos), big_blind=game_state.get_big_blind_player(pos), players=game_state.remaining_players, player_pots=game_state.remaining_player_pots, player_hands=game_state.generate_player_hands(deck), small_blind_stake=small_blind_stake) LOGGER.debug('New game round: %s', game_round) state_index = -1 board_cards: List[Card] = [] # pre-flop state state_index = state_index + 1 next_move, pre_flop_state = game_state_move(state_index, "pre-flop", board_cards, game_round, game_state, None, use_local) if not next_move: round_complete(game_state, pre_flop_state) continue # post-flop state board_cards.extend([deck.pop() for __ in range(3)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, post_flop_state = game_state_move(state_index, "post-flop", board_cards, game_round, game_state, pre_flop_state, use_local) if not next_move: round_complete(game_state, pre_flop_state, post_flop_state) continue # turn state board_cards.extend([deck.pop() for __ in range(1)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, turn_state = game_state_move(state_index, "turn", board_cards, game_round, game_state, post_flop_state, use_local) if not next_move: round_complete(game_state, pre_flop_state, post_flop_state, turn_state) continue # river state board_cards.extend([deck.pop() for __ in range(1)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, river_state = game_state_move(state_index, "river", board_cards, game_round, game_state, turn_state, use_local) round_complete(game_state, pre_flop_state, post_flop_state, turn_state, river_state) if not game_result.winner: # determine winner in the remaining players by number of chips game_result.set_winner(game_state.get_player_with_most_chips()) LOGGER.debug('End game. Winner is %s', game_result.winner) return game_result.winner
from src.models.deck import Deck, Card from src.models.game import Round, Game # main class for temporary testing # Basic test: First player with highest card should win game = Game() round = Round(game) round.play(Card('Spades', 'Jack'), 'p1') # Winner round.play(Card('Spades', 'Jack'), 'p2') round.play(Card('Spades', '7'), 'p3') round.play(Card('Spades', 'Jack'), 'p4') winner, score = round.end() print("Test#1 :: {0} won by {1}".format(winner, score)) # Trump test: player with trump should win. Assuming trump was called in previous game game = Game() game.call_trump() game.trump = Card('Hearts', '6') round = Round(game) round.play(Card('Spades', 'Jack'), 'p1') round.play(Card('Spades', 'Jack'), 'p2') round.play(Card('Hearts', '6'), 'p3') # Winner round.play(Card('Spades', 'Jack'), 'p4') winner, score = round.end() print("Test#2 :: {0} won by {1}".format(winner, score)) # Trump test: player with trump should win. Assuming trump was called before the play game = Game() game.trump = Card('Hearts', '6') round = Round(game)