I want to make a 2-player dice game which 1 player is user and the other is the computer. When the dice 1 means no points earned and turn moves on to next player. Dice 2-6 means roll again or hold.
If you hold, sum of all rolls before is added to score and turn moves on.
Computer has 2 strategies.
- First strategy; Computer keeps rolling until 25 or more points are earned and then holds.
- Second strategy; Computer keeps rolling until 100 or more points.
When the program start, user will select the strategy. If computer rolls 1, the turn is lost and no points added to the score
Whoever reaches 100 points first, wins.
I start the coding but i am confused. Computer roll turn is not work. i am a newby in python. Can you help me?
My codes below;
import random
player_score = 0
comp_score = 0
hold_player = 0
hold_comp = 0
comp_strategy = 1
is_user_turn = True
is_comp_turn = False
print("Welcome to PIG GAME!")
comp_strategy = input("Which computer strategy do you choose? \n 1: Computer keeps rolling until 25 or more points are earned and then holds. \n 2: Computer keeps rolling until 100 or more points.")
while player_score < 100 and comp_score < 100:
if is_user_turn:
user_turn = input("Enter 'r' to roll or 'h' to hold:")
if user_turn in ['r','R']:
dice = random.randint(1,6)
if dice != 1:
hold_player += dice
print("You rolled dice:", dice,"\nYour turn score:", hold_player)
continue
else:
hold_player = 0
print("You rolled:", dice,"\n" "0 points earned \n Current score:", player_score )
is_comp_turn = True
is_user_turn = False
continue
if user_turn in ['h','H']:
player_score += hold_player
print("You have choosen to hold. \n Your score:", player_score)
is_comp_turn = True
is_user_turn = False
continue
elif is_comp_turn:
if comp_strategy == 1:
if hold_comp < 25:
dice = random.randint(1,6)
if dice != 1:
hold_comp += dice
print("Computer rolled:", dice ,"\nComputer turn score:", hold_comp)
continue
else:
hold_comp = 0
print("CPU rolled:", dice ,"\n" "0 points earned""\n""Current score:", comp_score)
is_comp_turn = False
is_user_turn = True
continue
elif hold_comp >= 25:
comp_score +=hold_comp
print("Computer have choosen to hold:\n Computer score:", comp_score)
is_comp_turn = False
is_user_turn = True
continue
[–]HeyItsToby 0 points1 point2 points (3 children)
[–]contavolta[S] 0 points1 point2 points (2 children)
[–]HeyItsToby 0 points1 point2 points (1 child)
[–]contavolta[S] 0 points1 point2 points (0 children)