Skip to content

Commit

Permalink
Create 11.1.3 Adding a Computer Opponent
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeHS-Solutions authored Jan 28, 2024
1 parent fb228d7 commit 225cf07
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Start with your code from the previous problem
import random

player_bank = 0
computer_bank = 0
x = 0

def player_rolling():
player_rolling = 0

while (player_bank < 100):
print("Your Rolling Score: " + str(player_rolling))
decision = input("Would you like to roll or bank?: ")

if(decision == "roll"):
die_number = random.randint(1,6)
print("\n")
print("You rolled a " + str(die_number) + "")
if (die_number > 1):
player_rolling += die_number
continue
else:
player_rolling = 0
print("You rolled a 1! You got zero for this round!\n")
return player_rolling
if (decision == "bank"):
return player_rolling
else:
return player_rolling

def computer_rolling():
computer_rolling = 0

while (computer_bank < 100):
if(computer_rolling <= 15):
die_number = random.randint(1,6)
print("\n")
if (die_number > 1):
computer_rolling += die_number
print("The computer rolled a " + str(die_number))
print("The Computer's Rolling Score: " + str(computer_rolling))
if(computer_rolling <= 15):
print("The computer chooses to roll.")
continue
else:
computer_rolling = 0
print("The computer rolled a 1. End of turn. \n")
return computer_rolling
else:
print("The computer decided to bank their points.\n")
print("End of turn")
return computer_rolling
else:
return computer_rolling

while (player_bank < 100 and computer_bank < 100):
x += 1
print("Round: " + str(x))
print("Your Bank Score: " + str(player_bank))
print("Computer's Bank Score: " + str(computer_bank))
player_bank += player_rolling()
computer_bank += computer_rolling()
else:
if(player_bank > 100 and player_bank > computer_bank):
print("Congratulations! The computer was defeated on " + str(x) + " rounds!")
elif(computer_bank > 100 and computer_bank > player_bank):
print("Good try! The computer was victorious on " + str(x) + " rounds!")
elif(player_bank > 100 and computer_bank > 100 and computer_bank == player_bank):
print("Congratulations! You both won on " + str(x) + " rounds!")

0 comments on commit 225cf07

Please sign in to comment.