Feb-13-2019, 07:59 PM
(This post was last modified: Feb-13-2019, 08:12 PM by ichabod801.)
I've been working on my python assignment and have been hitting a lot of road blocks when trying to make my game of pig. I know I need an outside loop to go back and forth for the turns of the computer and human but it hasn't been working and was hoping to get some help. I was told that I can write everything in a main function and that I only need two while loops.
these are the rules,
The first player to accumulate a score of 100 wins.
The human goes first.
After one roll (of both dice), a player has the choice to "hold" or to roll again unless he rolls doubles.
A player rolls two six sided dice. Certain conditions apply:
If both dice are ones, then you add 25 to your turn score and you must roll again.
If one dice is one, then your turn is over and your turn score is set to zero.
If both dice match ("doubles"), other than two ones, then you gain twice the sum of the dice, and you must roll again. For example, if you rolled two fours then you would gain 16 and have to roll again.
For any other dice combination, you just add the dice total to your turn score and you have the choice of rolling again.
If you complete your turn without rolling a one, that is to say that you chose not to roll again, then your turn score is added to your accumulated game score.
here is what my code looks like right now
these are the rules,
The first player to accumulate a score of 100 wins.
The human goes first.
After one roll (of both dice), a player has the choice to "hold" or to roll again unless he rolls doubles.
A player rolls two six sided dice. Certain conditions apply:
If both dice are ones, then you add 25 to your turn score and you must roll again.
If one dice is one, then your turn is over and your turn score is set to zero.
If both dice match ("doubles"), other than two ones, then you gain twice the sum of the dice, and you must roll again. For example, if you rolled two fours then you would gain 16 and have to roll again.
For any other dice combination, you just add the dice total to your turn score and you have the choice of rolling again.
If you complete your turn without rolling a one, that is to say that you chose not to roll again, then your turn score is added to your accumulated game score.
here is what my code looks like right now
import random def main(): player_score = 0 computer_score = 0 while player_score or computer_score < 100 : die1 = random.randint(1,6) die2 = random.randint(1,6) print("You rolled a {} and a {}".format(die1, die2)) total = die1 + die2 doubletotal = 2 * total yes = True no = False if die1 == die2 : if die1 != 1 : print("{} has been added to your score.".format(doubletotal)) print("You must roll again. Press enter.") input() elif die1 == 1 : print("25 has been added to your score.") print("You must roll again. Press enter.") input() if die1 == 1 or die2 == 1 : if die1 != die2 : print("Your turn is over nothing has been added to you score.") else : print("{} has been added to your score.".format(total)) again = str(input("Would you like to roll again, yes or no?")) if yes : print("Rolling...") else : print("It is now the computer's turn") if die1 == die2 : if die1 != 1 : new_score = player_score + doubletotal elif die1 == 1 : new_score = player_score + 25 else : new_score = player_score + total print("Your new score is {}".format(new_score)) player_score = player_score + new_score die1 = random.randint(1,6) die2 = random.randint(1,6) print("The computer rolled a {} and a {}".format(die1, die2)) total = die1 + die2 doubletotal = total * 2 if die1 == die2 : if die1 != 1 : final = computer_score + doubletotal print("{} has been added to the computer's score.".format(doubletotal)) print("The computer's score is now, {}".format(final)) elif die1 == 1 : final2 = computer_score + 25 print("25 has been added to the computer's score.") print("The computer's score is now, {}".format(final2)) if die1 == 1 or die2 == 1 : print("The computer's turn is over nothing has been added to the computer's score.") else : final3 = computer_score + total print("{} has been added to the computer's score.".format(total)) print("The computer's score is now, {}".format(final3)) print("The computer's score is now {}.".format(computer_score)) player_score = player_score + 1 computer_score = computer_score + 1 main()