diff --git a/pig.py b/pig.py index 7d7b3fe..ab74f04 100644 --- a/pig.py +++ b/pig.py @@ -12,49 +12,42 @@ turn = "player" winning_score = 50 -while not done: +def play(turn, done, player_temp_total, player_total): while turn == "player" and not done: print() print("Player:", player_total, "Computer:", comp_total) - print("It's your turn!") + print("It's ", turn, " turn!") roll = random.randint(1,6) - print("You rolled a", roll) + print(turn, " rolled a", roll) if roll == 1: - turn = "computer" + if(turn == "player"): + turn = "computer" + else: + turn = "player" player_temp_total = 0 print("PIG! Too bad! Your total is currently:", player_total) else: player_temp_total += roll print("You currently have " + str(player_temp_total) + " banked.") - choice = input("Do you wish to roll again (y/n)?: ") - if choice == 'n': - player_total += player_temp_total - player_temp_total = 0 - print("Your total socre is now:", player_total) - turn = "computer" + if(turn == "player"): + choice = input("Do you wish to roll again (y/n)?: ") + if choice == 'n': + player_total += player_temp_total + player_temp_total = 0 + print("Your total socre is now:", player_total) + turn = "computer" + else: + if player_temp_total > 6 or player_total + player_temp_total > winning_score: + print("The computer has chosen to end its turn.") + player_total += player_temp_total + player_temp_total = 0 + print("The computer's score is now:", player_total) + turn = "player" if player_total > winning_score: print("You win! " + str(player_total) + " to " + str(comp_total)) done = True - while turn == "computer" and not done: - print() - print("Player:", player_total, "Computer:", comp_total) - print("It's the computer's turn!") - roll = random.randint(1,6) - print("The computer rolled a", roll) - if roll == 1: - turn = "player" - comp_temp_total = 0 - print("PIG! Too bad! The computer's total is currently:", comp_total) - else: - comp_temp_total += roll - print("The computer has " + str(comp_temp_total) + " banked.") - if comp_temp_total > 6 or comp_total + comp_temp_total > winning_score: - print("The computer has chosen to end its turn.") - comp_total += comp_temp_total - comp_temp_total = 0 - print("The computer's score is now:", comp_total) - turn = "player" - if comp_total > winning_score: - print("The computer wins! " + str(comp_total) + " to " + str(player_total)) - done = True \ No newline at end of file +while not done: + play(turn, done, player_temp_total, player_total) + play(turn, done, comp_temp_total, comp_total) +done = True \ No newline at end of file