Skip to content

Commit f7abdd0

Browse files
authored
Create dice_roll_sim.py
1 parent 342805f commit f7abdd0

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import random
2+
3+
4+
Points = []
5+
minPlayer = 2
6+
players = 0
7+
maxscore = 100
8+
DiceNum = 2
9+
gameRound = 0
10+
11+
def setPlayers():
12+
while True:
13+
players = input("How many players are playing?\n")
14+
if players.isdigit():
15+
players = int(players)
16+
if minPlayer <= players:
17+
for i in range(players):
18+
Points.append(0)
19+
return players
20+
21+
def diceroll(player, DiceNum):
22+
throw = 0
23+
print("\n\tPlayer {0}s turn:".format(player + 1),end = "")
24+
for i in range(DiceNum):
25+
print("\n\tHit Space Bar and Enter to throw die !!",end = " ")
26+
sp = input()
27+
if sp == " ":
28+
die = random.randint(1, 6)
29+
print("\t \tPlayer {0} has thrown die {1} which landed on {2}".format(player + 1, i + 1, die))
30+
throw += die
31+
else:
32+
print("your turn skipped!!")
33+
Points[player] += throw
34+
print("\n \tPlayer {0}s score for this round is : {1}".format(player + 1 , throw))
35+
print("\tPlayer {0}s total score is now: {1}".format(player + 1, Points[player]))
36+
return throw
37+
38+
def checkWin(maxscore):
39+
for player in range(players):
40+
if (Points[player] >= maxscore):
41+
print("\nPlayer {0} wins!! Congratulations!!".format(player + 1))
42+
return True
43+
44+
return False
45+
46+
47+
if __name__ == "__main__":
48+
players = setPlayers()
49+
while True:
50+
gameRound += 1
51+
print("\nRound: {0}".format(gameRound))
52+
for i in range(players):
53+
diceroll(i, DiceNum)
54+
print("\nScores after round {0} ".format(gameRound))
55+
for i in range(players):
56+
print("Player {0} --> {1}".format(i+1,Points[i]))
57+
if (checkWin(maxscore)):
58+
break

0 commit comments

Comments
 (0)