Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 937e7be

Browse files
authored
Created a Rolling Dice Game. Completes issue #78
1 parent 511bb1a commit 937e7be

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
> **Developer:** [Sahil Bairagi](http://Sahil-k1509.github.io)
2+
3+
### How to play
4+
0. Your System must have [python installed](https://www.python.org/) (python 3).
5+
1. Run the game. No need to install any other modules.
6+
7+
### About the Game
8+
- In Roll-a-dice, You play against a person.
9+
- Both of you throw a dice and the person with highest number on top wins that round and gets 1 point.
10+
- In case of draw, both gets 0 points.
11+
- Game is repeated for n number of rounds.
12+
- Person who wins maximum number of rounds win the game.
13+
14+
![game Roll a dice](gamerolladice.png)
15+
![game Roll a dice](rolladice2.png)
20.8 KB
Loading
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from random import randint
2+
from time import sleep
3+
from os import system as OSsys, name as OSname
4+
5+
6+
# Clear screen after each round
7+
def clearScreen():
8+
if OSname == 'nt': OSsys('cls')
9+
else: OSsys('clear')
10+
11+
12+
# Creating throw dice function
13+
def throw_dice(player_name):
14+
top = randint(1, 6)
15+
print(f'\n{player_name} threw the dice...\n'); sleep(0.3)
16+
sleep(randint(10, 30)*0.05)
17+
print_dice(top)
18+
return top
19+
20+
21+
# Creating function to visualize dice top
22+
def print_dice(top):
23+
sides = [
24+
'| |\n| * |\n| |',
25+
'| * |\n| |\n| * |',
26+
'|* |\n| * |\n| *|',
27+
'|* *|\n| |\n|* *|',
28+
'|* *|\n| * |\n|* *|',
29+
'|* *|\n|* *|\n|* *|'
30+
]
31+
print('+-----+'); sleep(0.3)
32+
print(sides[top-1])
33+
print('+-----+'); sleep(0.3)
34+
35+
def show_score(roundnum, p1, p2, p1_points, p2_points):
36+
print(f'\nRound {roundnum}'); sleep(0.3)
37+
print('------------------------------------------'); sleep(0.3)
38+
print(f'{p1} : {p1_points}'); sleep(0.3)
39+
print(f'{p2} : {p2_points}'); sleep(0.3)
40+
print('------------------------------------------\n'); sleep(0.3)
41+
42+
43+
def main():
44+
# Number of rounds
45+
done = False
46+
while not done:
47+
try:
48+
n = int(input("Enter the number of rounds: "))
49+
if n > 0:
50+
done = True
51+
else:
52+
print("Please Enter a positive number!"); sleep(0.3)
53+
except ValueError:
54+
print("please Enter an integer."); sleep(0.3)
55+
56+
57+
# Name of players and initialize points
58+
p1 = input('\nName of first player? ')
59+
p2 = input('\nName of second player? ')
60+
61+
p1_points = 0
62+
p2_points = 0
63+
64+
65+
66+
for i in range(n):
67+
print("Scoreboard:"); sleep(0.3)
68+
show_score(i+1, p1, p2, p1_points, p2_points); sleep(0.3)
69+
70+
r1 = throw_dice(p1); sleep(0.3)
71+
r2 = throw_dice(p2); sleep(0.3)
72+
73+
if r1 > r2:
74+
print(f'\n{p1} won this round.'); sleep(0.3)
75+
p1_points += 1
76+
elif r2 > r1:
77+
print(f'\n{p2} won this round.'); sleep(0.3)
78+
p2_points += 1
79+
else:
80+
print('\nIt\'s a draw.'); sleep(0.3)
81+
82+
q = input("\n\nPress enter to continue to next round. (Q to stop rounds)")
83+
84+
if q.upper() == 'Q': break
85+
86+
clearScreen()
87+
88+
print('\n\n')
89+
if p1_points > p2_points:
90+
print(f"{p1} won by {p1_points - p2_points} points."); sleep(0.3)
91+
elif p2_points > p1_points:
92+
print(f"{p2} won by {p2_points - p1_points} points."); sleep(0.3)
93+
else:
94+
print(f"Game ended in a draw..."); sleep(0.3)
95+
96+
print('\nFinal Score Board:')
97+
show_score(i+1, p1, p2, p1_points, p2_points)
98+
99+
with open('result_rolladice.txt', 'a') as file:
100+
cov = '\n------------------------------------\n'
101+
r = f'Number of rounds: {n}\n{p1}: {p1_points}\n{p2}: {p2_points}'
102+
res = cov + r + cov
103+
104+
file.write(res)
105+
106+
if __name__ == '__main__':
107+
main()
21.6 KB
Loading

0 commit comments

Comments
 (0)