2015-09-28 06:02:42

Hi, I have been learning Python for a while slowly making my way through Learn Python the Hard way. I stopped coding for a while, but I wanted to make sure I remembered everything I had already learned by building a game of Pig before I move on.

I seem to have the basic logic of the game down, but for some reason it won't change turn. I have gone over my logic multiple times, but it seems right. I am also getting some small bugs if you play long enough, but perhaps once I get my change turn function working correctly it will straiten itself out.
Can someone help me figure out what's wrong?

Here's my code.


import random

def roll(die, turnTotal, currentPlayer, playerList):
    """create the random counter or die and output the result to the screen, and check if the die roll is 1, if so set turn total to 0 and pass the player variable to the next player."""
    die = random.randrange(1,7)
    if die == 1:
        turnTotal = 0
        changeTurn(currentPlayer, playerList)
        print("Pig!!! \nYou rolled a 1. It is now the other players turn")       
    else:
        turnTotal[0] += die
        print("You rolled a %s" % die)
        print("your total for this turn is \n%s." % turnTotal[0])

def changeTurn(currentPlayer, playerList):
    """change turn between player 1 and player 2."""
    if currentPlayer[0] == playerList[0][0]:
        return playerList[1]
    else:
        return playerList[0]

def game():
    player1 = ["Player1", 0]
    player2 = ["Player2", 0]
    playerList = [player1, player2]
    currentPlayer = random.choice(playerList)
    turnTotal = [0]
    winner = None
    die = 0
    print("Pig\n")
    while not winner:
        if currentPlayer[1] < 100:
            print("It is %s\'s turn.\n" % currentPlayer[0])
            print("%s\'s score is %s, and %s\'s score is %s." % (playerList[0][0], playerList[0][1], playerList[1][0], playerList[1][1]))
            rollDice = input("press enter to roll, or hold/h to keep your current total: ")
            if rollDice == "":
                roll(die, turnTotal, currentPlayer, playerList)
            elif rollDice == "hold" or rollDice == "h":
                currentPlayer[1] += turnTotal[0]
                turnTotal[0] -= turnTotal[0]
                changeTurn(currentPlayer, playerList)
        else:
            winner = currentPlayer
    print("Congradulations! %s won!" % winner[0])

def GameDescription():
    """outputs a descriptionof the game Pig, provides an input box, so that when the the user presses enter the game will return to the main menu"""
    print("""Pig is a two player game where one player rolls one die as many times as he or \nshe wants. However, if the player rolls a 1 their score for that turn is 0, and it becomes the other player\'s turn. Each time you roll the die the face value of the die adds to your turn total and if you decide you have taken enough of a \nwrisk for that turn, type \"hold\" or \"h.\" This will add the turn total to your \noverall score. The objective is to be the first person to reach 100.""")
    input("Press enter to return to the main menu. ")
    main()

def main():
    """The main menu of the game. provides the user with 3 options, play, rules, or exit."""
    while True:
        print("Welcome to the dice game Pig.")
        mainMenu = input("1 to play \n2 to learn about the rules for the game \n3 to exit: ")
        if mainMenu == "1":
            game()
        elif mainMenu == "2":
            gameDescription()
        elif mainMenu == "3":
            break

main()

2015-09-30 07:51:41

I have noticed 2 things:

1. In roll(die, ...) the "die" argument isn't used because it is always rewritten by "die = random.randrange(1,7)". So you can remove this argument and the initialization in game(): "die = 0".

2. changeTurn() returns a value, but game() doesn't get the returned value, so changeTurn() doesn't have any effect.

2015-09-30 18:55:04

I got it working. I had to set
currentPlayer equal to my changeTurn function. Which makes sense to me.
I also changed a few things, most obviously I got rid of the roll function. I couldn't get the program to work the way I wanted it to with the die variable in a separate function. I am sure it is possible though.

Here is my new code.

import random

def changeTurn(currentPlayer, playerList):
    """change turn between player 1 and player 2."""
    if currentPlayer[0] == playerList[0][0]:
        return playerList[1]
    else:
        return playerList[0]

def game():
    player1 = ["Player1", 0]
    player2 = ["Player2", 0]
    playerList = [player1, player2]
    currentPlayer = random.choice(playerList)
    turnTotal = [0]
    winner = None
    print("Pig\n")
    while not winner:
        print("It is %s\'s turn.\n" % currentPlayer[0])
        print("The total roll for this turn is %s" % turnTotal[0])
        print("%s\'s score is %s, and %s\'s score is %s." % (playerList[0][0], playerList[0][1], playerList[1][0], playerList[1][1]))
        if currentPlayer[1] < 100:
            rollDice = input("press enter to roll, or hold/h to keep your current total: ")
            if rollDice == "":
                die = random.randrange(1,7)
                print("You rolled a %s" % die)
                if die == 1:
                    print("PIG!!! Your score for this turn is 0. It is now the other players turn")
                    currentPlayer = changeTurn(currentPlayer, playerList)
                    turnTotal[0] = 0
                else:
                    turnTotal[0] += die
            elif rollDice == "hold" or rollDice == "h":
                currentPlayer[1] += turnTotal[0]
                turnTotal[0] = 0
                currentPlayer = changeTurn(currentPlayer, playerList)
        else:
            winner = currentPlayer
    print("Congradulations! %s won!" % winner[0])

def gameDescription():
    """outputs a descriptionof the game Pig, provides an input box, so that when the the user presses enter the game will return to the main menu"""
    print("""Pig is a two player game where one player rolls one die as many times as he or \nshe wants. However, if the player rolls a 1 their score for that turn is 0, and it becomes the other player\'s turn. Each time you roll the die the face value of the die adds to your turn total and if you decide you have taken enough of a \nwrisk for that turn, type \"hold\" or \"h.\" This will add the turn total to your \noverall score. The objective is to be the first person to reach 100.""")
    input("Press enter to return to the main menu. ")
    main()

def main():
    """The main menu of the game. provides the user with 3 options, play, rules, or exit."""
    while True:
        print("Welcome to the dice game Pig.")
        print("Choose an option from below")
        mainMenu = input("1 to play \n2 to learn about the rules for the game \n3 to exit \n")
        if mainMenu == "1":
            game()
        elif mainMenu == "2":
            gameDescription()
        elif mainMenu == "3":
            break

main()