The program should randomly choose which player goes first. It should keep a total score for each player, and alternate turns between the computer players until one player ends its turn with a score of 100 or higher.
Here is how I tried to solve the problem:
import random
print("Well, hello there")
def roll_the_dice():
dice_value = random.randint(1,6)
return dice_value
def roll_AI(player):
point = 0
checker = 1
while (checker==1):
dice_value = roll_the_dice()
print("- rolled a :" , dice_value)
if dice_value==1:
print("Pigged out, mate!")
point=0
checker=0
else:
point += dice_value
print("Your total be: ",point)
print("Turns over, mate!")
return point
playerOne = 0 #Score of P1
playerTwo = 0 #Score of P2
while (playerOne<100 and playerTwo<100):
whos_turn = random.randint(1,2)
if (whos_turn==1):
print("Initial point of both of these bots be 0.")
dice_value = roll_AI(1)
playerOne+=dice_value
print("Now, player one score: ",playerOne)
else:
dice_value = roll_AI(2)
playerTwo += dice_value
print("Now, player two score: ",playerTwo)
print("GAMES OVER!")
print("Player One:",playerOne)
print("Player Two:",playerTwo)
if playerOne>playerTwo:
print("Player one wins!")
elif playerTwo>playerOne:
print("Player two wins!")
else:
print("ITS A DRAW!.")
Here is a sample output of what the program should run:
Player One’s score: 0
Player Two’s score: 0
It’s Player One’s turn
- rolled a 1
Pigged out!
Total turn score = 0
Player One’s score: 0
Player Two’s score: 0
It’s Player Two’s turn
- rolled a 6
- rolled a 2
...
Player One’s score: 85
Player Two’s score: 88
It’s Player One’s turn
- rolled a 2
- rolled a 6
- rolled a 2
- rolled a 4
- rolled a 3
Total turn score = 17
Final score: 102 vs 88
Player One wins!
My program is running an infinite while loop. Can someone guide me as to where I am going wrong?
Tried sticking to your format best I could, used a while True
loop in combination with a for
loop, if either player hit 100 on their turn the for
loop breaks to stop the next player from getting a turn, followed by the while
loop breaking
import random
players = ['player_one', 'vash']
scores = {'player_one': 0, 'vash': 0}
print('Well, hello there')
random.shuffle(players)
while True:
for i in players:
turn_score = 0
while turn_score <= 20:
roll = random.randint(1,6)
if roll == 1:
turn_score = 0
scores[i] += 0
print('\n{} rolled a {}. Pigged out!'.format(i, roll))
break
else:
turn_score += roll
print('\n{} rolled a {}.'.format(i, roll))
scores[i] += turn_score
print('Turn score: {}'.format(turn_score))
print('{} score: {} {} score: {}'.format(players[0], scores[players[0]], players[1], scores[players[1]]))
if scores[i] > 100:
break
if scores[i] > 100:
break
winner = [i for i in scores if scores[i] == max(scores.values())]
print('{} is the winner!'.format(*winner))
Well, hello there vash rolled a 2. vash rolled a 1. Pigged out! Turn score: 0 vash score: 0 player_one score: 0 player_one rolled a 5. player_one rolled a 4. player_one rolled a 5. player_one rolled a 6. player_one rolled a 6. Turn score: 26 vash score: 0 player_one score: 26 .... vash score: 89 player_one score: 94 vash rolled a 3. vash rolled a 4. vash rolled a 4. vash rolled a 4. vash rolled a 3. vash rolled a 6. Turn score: 24 vash score: 113 player_one score: 94 vash is the winner!