liveBook · Manning

Play (dice game)

As explained in Learn AI-Assisted Python Programming, Second Edition

play is a book-specific Python function that runs the complete two-player dice game without parameters or a return value.

play is the top-level Python function for Bogart, a two-player dice game. Called as play(), it initializes the pot, player scores, and randomly selected first player, then coordinates turns until someone wins. It takes no parameters and returns no value; its effects are gameplay, state updates, and printed messages. section 10.4.3

The function coordinates these game steps:

  • Display the pot, both players’ chip totals, and the current player before each turn. section 10.4.3
  • Call take_full_turn to roll dice, continue or stop the turn, and update the pot. section 10.4.3
  • Award the pot when the current player wins chips, reset the pot, and switch players unless the game has ended. section 10.4.2 section 10.4.3
  • Finish by printing the winning player after game_over detects a victory condition. section 10.4.3

Running the game

Calling play() starts a complete interactive game. The function chooses Player 1 or Player 2 at random, prints status before each turn, gathers dice rolls and decisions through helper functions, and eventually prints the winner. section 10.4.3

Listing 10.12 play function in Bogart
def play():
    '''
    Play the game until the game is over.
    
    The pot starts with 0 chips, and each player starts with 0 chips.
    
    Randomly decide whether player 1 or player 2 goes first.
    
    Before each turn, print three lines of information:
    1. The number of chips in the pot
    2. The number of chips that each player has
    3. Whether it is player 1's turn or player 2's turn
    
    Take a full turn for the current player. 
    If they won the chips, add the chips in the pot to the 
    total for that player and reset the pot to have 0 chips.
 
    Then, switch to the other player's turn.
 
    Once the game is over, print the current player 
    (that's the player who won).
    '''
    pot_chips = 0       #1
    player1 = 0         #1
    player2 = 0         #1
    current_player = random.randint(1, 2)      #2
    rolls = []
    while not game_over(player1, player2, rolls):     #3
        print('Pot chips:', pot_chips)                 #4
        print('Player 1 chips:', player1)              #4
        print('Player 2 chips:', player2)              #4
        print('Player', current_player, 'turn')        #4
        pot_chips, rolls = take_full_turn(pot_chips)     #5
        if wins_chips(rolls):                           #6
            if current_player == 1:                         #7
                player1 += pot_chips                       #8
            else:
                player2 += pot_chips                      #9
            pot_chips = 0                              #10
        current_player = switch_player(player1, player2,  #11
                                       rolls, current_player)
    print('Player', current_player, 'wins!')   #12

Use the complete play-function listing to trace initialization, the gameplay loop, chip awards, player switching, and final winner reporting. section 10.4.3

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Descends to initialization, looping, helper-function calls, state updates, and output, while delegating dice and turn logic.
Examples
Uses the Bogart dice game, the play() call, chip totals, random first-player selection, dice rolls, and winner output.
What you can do after
Build it from scratch by decomposing a game into helper functions and connecting them through a top-level control loop.
How it connects
Connects top-down design, functions, loops, random number generation, game state, Boolean termination tests, and player switching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.