-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 11.1.3 Adding a Computer Opponent
- Loading branch information
CodeHS-Solutions
authored
Jan 28, 2024
1 parent
fb228d7
commit 225cf07
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Unit 11: The Game of Pig/11.1 The Game of Pig/11.1.3 Adding a Computer Opponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |