Simple Pig Game

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Simple Pig Game

Postby XndrK » Mon Sep 02, 2013 6:36 pm

I made a Python version of the dice game Pig. Took me about 20 minutes, and it's for two players only. Still fun though.

Enjoy.

Code: Select all
"""Simple Pig game"""

from random import randint

def roll_again():
    yn = raw_input("Do you want to roll? y/n ")
    while yn.lower() != "y" and yn.lower() != "n":
        yn = raw_input("Please just use y or n, not the full words. y/n")
    else:
        if yn.lower() == "y":
            return True
        elif yn.lower() == "n":
            return False
        else:
            raise IOError("Misunderstood statement")
   



player = [["Player One", 0], ["Player Two", 0]]

while player[0][1] < 100 and player[1][1] < 100:
    for play in player:
        turnScore = 0
        die = randint(1, 6)
        print play[0] + ":"
        rollAgain = roll_again()
        while die != 1 and rollAgain:
            print die
            turnScore += die
            die = randint(1, 6)
            print play[0]
            rollAgain = roll_again()
        else:
            if die == 1 and rollAgain:
                print "You rolled a one."
                print "Your Score: " + str(play[1])
            else:
                play[1] += turnScore
                print "Your Score: " + str(play[1])


P.S. Is there a way I can upload a file to the forum?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Simple Pig Game

Postby metulburr » Mon Sep 02, 2013 6:54 pm

click upload attachment -> browse -> add the file

you can also invest in learning git, which rewards you 10 times over
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Simple Pig Game

Postby XndrK » Mon Sep 02, 2013 7:46 pm

<facepalm>

Here's the updated source code:

Code: Select all
"""Simple Pig game with rules"""

from random import randint

def roll_again():
    yn = raw_input("Do you want to roll? y/n ")
    while yn.lower() != "y" and yn.lower() != "n":
        yn = raw_input("Please just use y or n, not the full words. y/n")
    else:
        if yn.lower() == "y":
            return True
        elif yn.lower() == "n":
            return False
        else:
            raise IOError("Misunderstood statement")
   
def know_the_rules():
    yn = raw_input("Do you know the rules? y/n ")
    while yn.lower() != "y" and yn.lower() != "n":
        yn = raw_input("Please just use y or n, not the full words. y/n")
    else:
        if yn.lower() == "y":
            return True
        elif yn.lower() == "n":
            return False
        else:
            raise IOError("Misunderstood statement")


player = [["Player One", 0], ["Player Two", 0]]

#explanation message
def explain():
    print "Pig is a dice game played with one die. At the beginning of a player's"
    print "turn, the player (let's call her Alice) rolls her die. If the roll is 1,"
    print "then her turn is over. If it's anything else, she adds it to her score"
    print "and has the option to roll again. If she decides not to, she adds her"
    print "score for that turn to her total score. However, if she rolls a 1,"
    print "then nothing is added to her score and her turn is over."
    print ""
    print "Have fun!"
    print ""

yin = know_the_rules()
if yin:
    pass
else:
    explain()

#gameplay
while player[0][1] < 100 and player[1][1] < 100:
    for play in player:
        turnScore = 0
        die = randint(1, 6)
        print play[0] + ":"
        rollAgain = roll_again()
        while die != 1 and rollAgain:
            print die
            turnScore += die
            die = randint(1, 6)
            print play[0]
            rollAgain = roll_again()
        else:
            if die == 1 and rollAgain:
                print "You rolled a one."
                print "Your Score: " + str(play[1])
            else:
                play[1] += turnScore
                print "Your Score: " + str(play[1])
else:
    #Figures out who won
    if player[0][1] > player[1][1]:
        print "Player One wins!"
    elif player[1][1] > player[0][1]:
        print "Player Two wins!"
    else:
        print "It's a tie. Wow."
spam = raw_input("Press enter to close.")


And the actual game:
Pig.zip
Simple Pig dice game
(962 Bytes) Downloaded 94 times


Have fun!
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests