Making a pig-game in Python

Making a pig-game in Python

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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.

Github repository:

https://github.com/martintokman/pig-game

?

要查看或添加评论,请登录

Martin Tokman的更多文章

  • Financial reporting on Power BI

    Financial reporting on Power BI

    Given an Excel file that contains financial data the goal is to create a report that shows a Profit & Loss statement…

    1 条评论
  • Data Analysis - Northwind

    Data Analysis - Northwind

    To meet the demand for an executive sales report, i have crafted a story that defined project goals and ensured…

  • Cleaning and manipulating data with Python

    Cleaning and manipulating data with Python

    Data Science is a multidisciplinary field that combines various techniques and processes to extract knowledge from…

  • Making a trading tools app in Python (Part 1)

    Making a trading tools app in Python (Part 1)

    Following my journey in learning Python I started to build a trading tools app. This will be my first complete software…

  • Making a tic-tac-toe game in Python

    Making a tic-tac-toe game in Python

    Making a tic-tac-toe in Python doesn’t require much knowledge about the language itself, the true challenge consists of…

  • Estructuras y acción del precio

    Estructuras y acción del precio

    En el mundo del trading existen muchas técnicas de análisis y ejecución de estrategias. Una de ellas es la llamada…

    1 条评论
  • Resumen semanal de mercados

    Resumen semanal de mercados

    BOLSAS: Las bolsas americanas cierran la semana con leves recortes. El S&P 500 absorbe toda la liquidez del rango…

  • Resumen semanal de mercados

    Resumen semanal de mercados

    BOLSAS: Por el lado de América las bolsas tuvieron una semana positiva con cierres casi neutrales para el S&P500 y el…

  • Resumen semanal de mercados y situación macroeconómica – Estados Unidos y Zona Euro.

    Resumen semanal de mercados y situación macroeconómica – Estados Unidos y Zona Euro.

    Estados Unidos: Los permisos de construcción vuelven a mostrar deterioro bajando aún más de lo que estaba previsto. Si…

  • La importancia de los Stops en las inversiones

    La importancia de los Stops en las inversiones

    El trading de activos financieros requiere de una serie de habilidades básicas para realizarse de manera efectiva en el…

社区洞察

其他会员也浏览了