-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
255 lines (234 loc) · 8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Dice game
# Callum 18/07/22 OCR GCSE Computer Science Python Project
import time
import random
# Variables
loginTries = 0
p1logged = False
p2logged = False
p1user = ''
p2user = ''
p1total = 0
p2total = 0
p1rolls = 0
p2rolls = 0
# Decide whether to log in or register
def decideLogin(p1logged, p2logged, p1user, p2user):
if p1logged == False or p2logged == False:
print("Please pick an option:")
print("1. Register")
print("2. Login")
print(" ")
decision = input("Option: ")
if decision == "1":
register()
elif decision == "2":
login(loginTries, p1logged, p2logged, p1user, p2user)
elif decision == "3":
testingLogin(p1user, p2user)
else:
pass
#Register
def register():
print("Register")
user = input("Enter a username: ")
pword = input("Enter a password: ")
file = open("users.txt", "a")
if len(pword) <= 4:
print("Password must be at least 5 characters")
register()
elif len(pword) >= 4:
file.write(user)
file.write(" ")
file.write(pword)
file.write("\n")
file.close()
decideLogin(p1logged, p2logged, p1user, p2user)
#Easier login for when testing the program
def testingLogin(p1user, p2user):
user1 = "TestUser"
pword1 = "TestPass"
user2 = "TestUser2"
pword1 = "TestPass2"
p1user = user1
p2user = user2
start(p1user, p2user)
return p1user
return p2user
#Login
def login(loginTries, p1logged, p2logged, p1user, p2user):
if loginTries == 3:
print("Too many login attempts. Please try again later")
exit()
if p1logged == False:
print("Player 1 Login")
else:
print("Player 2 Login")
user = input("Username: ")
pword = input("Password: ")
print("Loading...")
time.sleep(1.5)
for line in open("users.txt", "r").readlines():
usersfile = line.split()
if user == usersfile[0] and pword == usersfile[1]:
print("Welcome!")
if p1logged == False:
print("Player 1 Logged in")
p1logged = True
p1user = user
decideLogin(p1logged, p2logged, p1user, p2user)
return p1user
else:
print("Player 2 Logged in")
p2logged = True
p2user = user
start(p1user, p2user)
return p2user
return True
print("Your username/password is incorrect, try again")
loginTries = loginTries + 1
login(loginTries, p1logged, p2logged, p1user, p2user)
return loginTries
return False
#Player 1 roll
def p1roll(p1user, p1total, p1rolls, p2user, p2rolls, p2total):
print(p1user + "'s turn")
print("--------------------------------")
if p1rolls != 5:
rolled = input(p1user + ", Type 'roll' to roll the dice: ")
print("Amount rolled: ", p1rolls)
if rolled == "roll":
print("Rolling.")
time.sleep(0.8)
print("Rolling..")
time.sleep(0.8)
print("Rolling...")
time.sleep(0.8)
rolloutput = random.randint(1, 6)
rolloutput2 = random.randint(1, 6)
print("You rolled a ", rolloutput)
print("And a ", rolloutput2)
p1score = rolloutput + rolloutput2
if rolloutput == rolloutput2:
print("Rolled a double, rolling 1 dice again!")
print("Rolling.")
time.sleep(0.8)
print("Rolling..")
time.sleep(0.8)
print("Rolling...")
time.sleep(0.8)
rolloutput3 = random.randint(1, 6)
print("You rolled a ", rolloutput3)
p1score = p1score + rolloutput3
if p1score % 2 == 0:
#even
p1score = p1score + 10
p1total = p1total + p1score
print("You rolled an even number so + 10 to your score!")
else:
#odd number
p1score = p1score - 5
p1total = p1total + p1score
print("You rolled an odd number so - 5 to your score!")
if p1score <0:
p1score = 0
print("Your score went into minus numbers! It has been set to 0")
p1total = p1total + p1score
print(p1user + "'s Total points: ", p1score)
p1rolls = p1rolls + 1
p1score = 0
print("--------------------------------")
p2roll(p2user, p2total, p2rolls, p1user, p1rolls, p1total)
else:
"Please enter 'roll' "
p1roll(p1user, p1total, p1rolls, p2user, p2rolls)
else:
p2roll(p2user, p2total, p2rolls, p1user, p1rolls, p1total)
return p1total
return p1rolls
#Player 2 roll
def p2roll(p2user, p2total, p2rolls, p1user, p1rolls, p1total):
print(p2user + "'s turn")
print("--------------------------------")
if p2rolls != 5:
rolled = input(p2user + ", Type 'roll' to roll the dice: ")
print("Amount rolled: ", p2rolls)
if rolled == "roll":
print("Rolling.")
time.sleep(0.8)
print("Rolling..")
time.sleep(0.8)
print("Rolling...")
time.sleep(0.8)
rolloutput = random.randint(1, 6)
rolloutput2 = random.randint(1, 6)
print("You rolled a ", rolloutput)
print("And a ", rolloutput2)
p2score = rolloutput + rolloutput2
if rolloutput == rolloutput2:
print("Rolled a double, rolling 1 dice again!")
print("Rolling.")
time.sleep(0.8)
print("Rolling..")
time.sleep(0.8)
print("Rolling...")
time.sleep(0.8)
rolloutput3 = random.randint(1, 6)
print("You rolled a ", rolloutput3)
p2score = p2score + rolloutput3
if p2score % 2 == 0:
#even
p2score = p2score + 10
p2total = p2total + p2score
print("You rolled an even number so + 10 to your score!")
else:
#odd number
p2score = p2score - 5
p2total = p2total + p2score
print("You rolled an odd number so - 5 to your score!")
if p2score <0:
p1score = 0
print("Your score went into minus numbers! It has been set to 0")
p2total = p2total + p2score
print(p2user + "'s Total points: ", p2score)
p2rolls = p2rolls + 1
p2score = 0
print("--------------------------------")
p1roll(p1user, p1total, p1rolls, p2user, p2rolls, p2total)
else:
"Please enter 'roll' "
p2roll(p2user, p2total, p2rolls, p1user, p1rolls)
elif p2rolls == 5:
stats(p1user, p2user, p1total, p2total)
return p2total
return p2rolls
# Runs after both players logged in
def start(p1user, p2user):
print("Player 1: ", p1user)
print("Player 2: ", p2user)
p1roll(p1user, p1total, p1rolls, p2user, p2rolls, p2total)
#Runs after the game finishes
def stats(p1user, p2user, p1total, p2total):
winner = ''
if p1total > p2total:
winner = p1user
elif p2total > p1total:
winner = p2user
elif p1total == p2total:
winner = "It was a draw"
print("\n" * 3)
print("--------------------------------")
print(" _____ _ _ ")
print(" / ____| | | | ")
print(" | (___ | |_ __ _| |_ ___ ")
print(" \___ \| __/ _` | __/ __|")
print(" ____) | || (_| | |_\__ \ ")
print(" |_____/ \__\__,_|\__|___/")
print("--------------------------------")
print("WINNER:")
print(winner)
print("--------------------------------")
print( p1user," total: ", str(p1total))
print( p2user," total: ", str(p2total))
pass
decideLogin(p1logged, p2logged, p1user, p2user)