forked from grapeJUICE1/python-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand-cricket.py
201 lines (157 loc) · 4.37 KB
/
hand-cricket.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
import random
import copy
import sys
chosen = ''
session = 0
user = 0
dev = 0
coin = ['head' , 'tail']
choices = ['bat' , 'bowl' , 'bat']
print('Welcome to HANDCRICKET....\nThr program replicates the classic handcricket game....\nRules:\nFirst a toss is held .. if u win u are given the chance to bowl or bat..\nIf ur score is greater than that of computer..u lose\n')
def get_int(prompt):
while True:
try:
value = int(input(prompt))
break
except ValueError:
print("\nSorry, I didn't understand that.\n")
continue
return value
while True:
val = get_int('How many wickets u want in the game')
if val <= 0:
print('Enter either 1 or 0')
else:
break
wicket = copy.deepcopy(val)
while True:
ov = get_int('How many overs U want in the game')
if ov <= 0:
print('Over must be greater than 0')
else:
break
overs = copy.deepcopy(ov)
i = 0
j = 0
picked = get_int('Would You Pick Head or Tails \n [0] for heads , [1] for tails')
def toss():
global chosen
global compchoice
result = coin[random.randint(0 , 1)]
print(result)
if coin.index(result) == picked:
print('\nYou Won The Toss')
chosen = choices[get_int('Would U Like To Bat Or Bowl , [0] for bat , [1] for bowl\n')]
print(f'\nYou chose to {chosen}\n')
else:
print('\nYou Lost The Toss...Computer Won')
chosen = choices[random.randint(0 , 1)]
print(f'\nComputer chose to { choices[choices.index(chosen)+1]}')
print(f'You Will {chosen}\n')
print()
def checkSession():
global session
global user
global dev
if session == 1:
if chosen == 'bat':
if user > dev:
print(f'overs remaining {overs}\n')
print(f'You won ... Your end score was {user} ... Congrats..\nThanks for playing\n')
sys.exit()
else:
if dev > user:
print(f'overs remaining {overs}\n')
print(f'You Lost ... Computer Won....Your end score was {user}\n')
sys.exit()
if session == 2:
if user > dev:
print(f'overs remaining {overs}\n')
print(f'You won ... Your end score was {user} ... Congrats..\nThanks for playing\n')
sys.exit()
elif user < dev:
print(f'overs remaining {overs}\n')
print(f'You Lost ... Computer Won....Your end score was {user}\n')
sys.exit()
def handcricket():
global i
global wicket
global user
global dev
global session
global chosen
global overs
global j
global val
global ov
j+=1
if overs < ov or j!=1:
i+=1
checkSession()
if overs == 0 :
i = -1
session += 1
if session == 2:
checkSession()
overs = copy.deepcopy(ov)
wicket = copy.deepcopy(val)
if chosen == 'bat':
chosen = 'bowl'
print('\n\nEnd of Innings\n\nEnd of Batting...Get ready to bowl\n\n')
print(f'Your Score is {user}\nComputers score is {dev}\n')
elif chosen == 'bowl':
chosen = 'bat'
print('\n\nEnd of innings\n\nEnd of Bowling...Get ready to bat\n\n')
print(f'Your score is {user}\nComputers score is {dev}\n')
handcricket()
if i // 6 == 1:
overs -= 1
i = -1
print(f'End of one over , remaining {overs}\n')
handcricket()
if wicket == 0:
session += 1
print(f'All wickets are gone\n{overs} overs were remaining\n')
if session == 2:
checkSession()
overs = copy.deepcopy(ov)
i=-1
wicket = copy.deepcopy(val)
if chosen == 'bat' :
print('\n\nEnd of innings\n\nEnd of Batting...Get ready to bowl\n\n')
print(f'Your Score is {user}\nComputers score is {dev}\n')
chosen = 'bowl'
handcricket()
elif chosen == 'bowl':
print('\n\nEnd of innings\n\nEnd of Bowling...Get ready to bat\n\n')
print(f'Your score is {user}\nComputers score is {dev}\n')
chosen = 'bat'
handcricket()
player = get_int('Chose a number from 1 to 6\n')
if player > 6 or player < 1:
print('number must be greater than one and smaller than 6 \n')
i -= 1
handcricket()
print(f'You Chose {player}\n')
comp = random.randint(1 , 6)
print(f'Computer Chose {comp}\n')
if player == comp:
wicket -= 1
print('wicket\n')
if chosen == 'bat':
print(f'You lost a wicket , remaining wickets = {wicket} \n Ur Score is {user}\n')
handcricket()
else:
print(f'Computer Lost a wicket , remaining {wicket}\n')
handcricket()
else:
if chosen == 'bat':
user += player
print(f'Players score is {user}\n')
handcricket()
else:
dev += comp
print(f'Computers score is {dev}\n')
handcricket()
toss()
handcricket()