Pig Game in Python

This project will learn about a very easy-to-build computer pig game in python.

Pig Game in Python

PROJECT PREREQUISITES:

In this python project, you need to know basic python. That includes.

WHAT WILL YOU LEARN?

  • How to invent your own computer games with python
  • End to End development of Python games.
  • Basic Input / Output implementation.

WHAT IS A PIG GAME(DICE GAME)?

Pig is a dice game that first appeared in print in John Scarne in 1945. It is believed that the pig game was pirates as a part of gambling.

HOW TO PLAY THE PIG GAME?

This game can be played using one or two dice together with two or more people.

It’s a very simple rule, the Player has to roll the dice, and if the total number of turns is ten, players have to roll the dice ten times. The score or each roll has to be total in the end, and if the player rolls a number 1, it will be on hold, and player 2 get the chance to roll dice ten times or until it rolls number 1.

At the end of the game, the player with the highest score wins the game.

Or in we can also play like the player who scores 100 points first wins the game.

So let’s get started with coding pig game python.

HOW TO BUILD A PIG GAME IN PYTHON

Follow the below steps to build a pig game in python.

Step 1: Firstly, we will import the necessary libraries and declare the required variables.

import random

User_Total = 0
Computer_Total = 0
Total_Turn = 10
Number_Of_Turns = 0
Computer_Turns = 0

Step 2: Now, we will declare two functions for randomly generated numbers from 1 to 10, one for the user and one for the computer.

def UserTurn():
    roll = random.randint(1,10)
    return roll

def ComputerTurn():
    roll = random.randint(1,10)
    return roll

Step 3: We implement a while loop that will execute ten times in this step. And inside the while loop, we will get users and computer roll scores.

while Number_Of_Turns <= Total_Turn:
    UserScore = UserTurn()
    if UserScore != 1:
        User_Total = User_Total + UserScore
        Number_Of_Turns = Number_Of_Turns + 1
    elif UserScore == 1:
        while(Computer_Turns <= Total_Turn):
            ComputerScore  = ComputerTurn()
            if ComputerScore != 1:
                Computer_Total = Computer_Total + ComputerScore
                Computer_Turns = Computer_Turns + 1
            else:
                break

Inside the while loop, we have implemented a condition to check the roll number; the first user will get the chance to roll, and it will check if the user has rolled one, then the computer will get an opportunity to roll the dice.

Step 4: This is the last step where we will check who is the winner of the game by using the if condition statement by checking who scores more than the other.

if User_Total > Computer_Total:
    print("User Has Won")
    print("The User Score has {}, and the Computer has Score {}".format(User_Total,Computer_Total))

elif Computer_Total > User_Total:
    print("Computer Has Won")
    print("The User Score has {}, and the Computer has Score {}".format(User_Total,Computer_Total))
else:
    print("Match has Draw with User Score: {}, and the Computer Score: {}".format(User_Total,Computer_Total))

Now let run the game and see the outputs of the game.

Output 1:

User Has Won

The User Score has 78, and the Computer has Score 36

Output 2:

Computer Has Won

The User Score has 60, and the Computer has Score 69

Output 3:

Match has Draw with the User Score: 74 and Computer Score: 74

So, at last of python pig game is ready, and it’s working fine; for better learning, you must start experiments with this code and improve it.

SUMMARY

We have to develop a simple game using simple user define functions and conditions. And saw how a python dice game with functions could be developed.

FAQS

How do you play the game Pig dice?

Pig dice games can be played with one or two dice and two or more people rolling dice. If roll gets one another player gets the opportunity to roll, and if other players get 1 in roll again, user one gets the opportunity to roll and the player to reach 100 points wins the game.

How do you make a dice game in Python?

Dice games in python can be made using functions and loops to achieve many rolls and turn and conditional statements to check the user’s total and computer totals.