Rules for a pig-game are the following.

Players will roll dice in turns trying to get the threshold needed to win the game. In each turn, the player will have the chance to roll dice an unlimited number of times until the dice is 1, in which case the player will lose the overall accumulated score, or he doesn’t want to roll again, and it passes the turn to the next player. The first player that reaches the hardcoded threshold will win the game.

The general flow will be starting by asking the number of players, after that, the app will have to manage a kind of “session” that will be controlling the results of the current player during his turn. For each dice rolled the app will have to decide whether it have to accumulate the score and ask the player to roll again, set global accumulated score to cero if the dice results in a 1 or pass the turn to the next player.

The general flowchart will be the following.

The control of the current player’s turn is done by a function called roll_dice. The function receives the overall scores of all players and the count of the current player, this count is the number of player in the list of players. The counter I will be controlling where in the list it will add the accumulated score for each round. When the current user decides to play, the function generates a random number from 1 to 6 simulating the roll dice.

If the number is equal to 1, the global score of the user is set to 99, that is the code I will use for telling the general flow that the current user lose his global score and it will set to 0 when the function ends. If the number is not 0, the function checks if the global score plus the recent diced roll sums the threshold or more, in that case I’ll set the current player’s global score to 1111, that is the code to tell the general flow that this user has win the game and the function will end and return to general flow.

If neither of the above is true, the function will accumulate the resulting number of the rolled dice and ask the player if he wants to roll again. This cycle will be repeated until the user responds no to the roll again question or the rolled dice is equal to 1.

The flowchart of roll_dice function will be the following.

When the game ends, it asks to play again. If the answer is yes, it will restart the whole game. If the answer is no, it will show a “goodbye” message and exits.

The game is done in a modularized way, I wrote one file for the general flow and another module to make the roll_dice function to make the overall code easier to read and don’t have a single file with too many lines.

I have added Docstring to the function to make it easier to understand it.