Ask questions, share knowledge, and connect with a vibrant community on IDNLearn.com. Our platform provides prompt, accurate answers from experts ready to assist you with any question you may have.
Sagot :
Answer:
In Python:
import random
p1name = input("Player 1: "); p2name = input("Player 2: ")
p1score = 0; p2score = 0
playerTurn = 1
print(p1name+" starts the game")
roll = True
while(roll):
if playerTurn == 1:
p1 = random.randint(1,6)
print(p1name+"'s roll: "+str(p1))
if p1 == 1:
playerTurn = 2
roll = True
else:
p1score+=p1
another = input("Another Roll (y/n): ")
if another == "y" or another == "Y":
roll = True
else:
playerTurn = 2
roll = True
else:
p2 = random.randint(1,6)
print(p2name+"'s roll: "+str(p2))
if p2 == 1:
playerTurn = 1
roll = True
else:
p2score+=p2
another = input("Another Roll (y/n): ")
if another == "y" or another == "Y":
roll = True
else:
playerTurn = 1
roll = True
if p1score >= 50 or p2score >= 50:
roll = False
print(p1name+":\t"+str(p1score)+"\t"+p2name+":\t"+str(p2score))
Explanation:
Note that the dice rolling is simulated using random number whose interval is between 1 and 6
This imports the random module
import random
This line gets the name of both players
p1name = input("Player 1: "); p2name = input("Player 2: ")
This line initializes the scores of both players to 0
p1score = 0; p2score = 0
This sets the first play to player 1
playerTurn = 1
This prints the name of player 1 to begin the game
print(p1name+" starts the game")
This boolean variable roll is set to True.
roll = True
Until roll is updated to False, the following while loop will be repeated
while(roll):
If player turn is 1
if playerTurn == 1:
This rolls the dice
p1 = random.randint(1,6)
This prints the outcome of the roll
print(p1name+"'s roll: "+str(p1))
If the outcome is 1, the turn is passed to player 2
if p1 == 1:
playerTurn = 2
roll = True
If otherwise
else:
Player 1 score is updated
p1score+=p1
This asks if player 1 will take another turn
another = input("Another Roll (y/n): ")
If yes, the player takes a turn. The turn is passed to player 2, if otherwise
if another == "y" or another == "Y":
roll = True
else:
playerTurn = 2
roll = True
If player turn is 2
else:
This rolls the dice
p2 = random.randint(1,6)
This prints the outcome of the roll
print(p2name+"'s roll: "+str(p2))
If the outcome is 1, the turn is passed to player 1
if p2 == 1:
playerTurn = 1
roll = True
If otherwise
else:
Player 2 score is updated
p2score+=p2
This asks if player 2 will take another turn
another = input("Another Roll (y/n): ")
If yes, the player takes a turn. The turn is passed to player 1, if otherwise
if another == "y" or another == "Y":
roll = True
else:
playerTurn = 1
roll = True
If either of both players has scored 50 or above
if p1score >= 50 or p2score >= 50:
roll is updated to False i.e. the loop ends
roll = False
This prints the name and scores of each player
print(p1name+":\t"+str(p1score)+"\t"+p2name+":\t"+str(p2score))
Your presence in our community is highly appreciated. Keep sharing your insights and solutions. Together, we can build a rich and valuable knowledge resource for everyone. Thank you for visiting IDNLearn.com. For reliable answers to all your questions, please visit us again soon.