-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcricket.py
executable file
·302 lines (204 loc) · 6.84 KB
/
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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'''
Created on
Feb 19, 2022 - Sunday
Course work:
Python for Cricket Lovers
@author: Raja CSP
Source:
https://github.com/tactlabs/tact-python/tree/master/csp/cricket
https://www.espncricinfo.com/player/team/india-6
https://www.espncricinfo.com/series/icc-cricket-world-cup-2010-11-381449/india-squad-497114/series-squads
https://www.espncricinfo.com/series/icc-cricket-world-cup-2010-11-381449/australia-squad-500453/series-squads
'''
# Import necessary modules
import random
import time
# Local import
from cricket_constants import *
from cric_util import pgap, pline
# Constants
def get_random_number(min = 0, max = 6):
return random.randint(min, max)
def is_wicket():
r_number = get_random_number()
if(r_number % 7 == 0):
return True
if(r_number % 9 == 0):
return True
return False
def get_random_score():
r_score = get_random_number(0, 6)
if(r_score == 5):
return 4
return r_score
def ball_gap():
if(QUICK_TESTING == 1):
return
time.sleep(SLEEP_TIME_SECONDS)
def play_single_over(
chase_flag = False,
chasing_score = 0,
team_current_score = 0,
current_wicket_index = 0,
current_over = 1,
current_team_players = None
):
current_over_balls = BALLS_PER_OVER
total_score_c_over = 0
# current_team_players = team_a_players
# if(chase_flag):
# current_team_players = team_b_players
for _ball in range(current_over_balls):
ball_gap()
_ball += 1
# Check wicket
wicket_flag = is_wicket()
current_batsman = current_team_players[current_wicket_index]
if(wicket_flag):
c_run = 0
print(f"[{current_over}.{_ball}]: It's a wicket! {current_batsman} is out! ")
current_wicket_index += 1
else:
c_run = get_random_score()
total_score_c_over += c_run
team_current_score += c_run
print(f'[{current_over}.{_ball}]: {current_batsman} scored: {c_run}')
# Enable only for testing purpose
# print(f'[ball {_ball}]: run: {c_run}, total_score_current_over: {total_score_c_over}, team_current_score : {team_current_score} ')
if(chase_flag):
if(team_current_score > chasing_score):
print(f'Beat the score: chasing_score: {chasing_score}, team_innings_score : {team_current_score}')
return team_current_score, current_wicket_index
# if(c_run == 4):
# print(f"It's a fantastic Four!!")
# print(f'ball: {_ball}, run: {c_run}, total_score_current_over: {total_score_c_over} ')
pgap()
print(f'total_score_current_over : {total_score_c_over}')
return team_current_score, current_wicket_index
def print_score_board(
current_over,
team_innings_score,
wicket_index
):
print(f'Scoreboard: {team_innings_score}/{wicket_index} [{current_over} overs]')
def play_inninings(
chase_flag = False,
over_count = 1,
chasing_score = 0,
# team_current_score = 0
current_team_players = None,
):
team_innings_score = 0
for _c_over in range(over_count):
current_over = _c_over + 1
print(f'playing : over {current_over}')
pline()
if(_c_over == 0):
wicket_index = 0
team_innings_score, wicket_index = play_single_over(
chase_flag = chase_flag,
chasing_score = chasing_score,
team_current_score = team_innings_score,
current_wicket_index = wicket_index,
current_over = current_over,
current_team_players = current_team_players
)
pgap()
print_score_board(
current_over,
team_innings_score,
wicket_index
)
# print(f"trap1763 : team_innings_score: {team_innings_score}, chasing_score: {chasing_score}")
if(chase_flag):
if(team_innings_score > chasing_score):
# print(f'Beat the score: chasing_score: {chasing_score}, team_innings_score : {team_innings_score}')
return team_innings_score, wicket_index
pgap()
return team_innings_score, wicket_index
def play_team_a(team_a):
team_a_players = all_team_players[team_a]
# First team batting
print(f'{team_a} batting: ')
pgap()
team_a_total_score, wicket_index = play_inninings(
chase_flag = False,
over_count = TOTAL_OVERS,
chasing_score = 0,
current_team_players = team_a_players
)
pgap()
print(f'{team_a} scored: {team_a_total_score}, lost wickets : {wicket_index}')
pline(77)
return team_a_total_score
def play_team_b(
team_b,
chasing_score
):
# print(f"team_a_total_score : {team_a_total_score}")
team_b_players = all_team_players[team_b]
pgap()
print(f'{team_b} batting: ')
pgap()
team_b_total_score, wicket_index = play_inninings(
chase_flag = True,
chasing_score = chasing_score,
over_count = TOTAL_OVERS,
# team_current_score = 0,
current_team_players = team_b_players
)
pgap()
print(f'{team_b} scored: {team_b_total_score}, lost wickets : {wicket_index}')
return team_b_total_score, wicket_index
def choose_winner(
team_a,
team_b,
team_a_total_score,
team_b_total_score,
team_b_wickets
):
wicket_diff = 10 - team_b_wickets
if(team_b_total_score > team_a_total_score):
print(f'{team_b} won by {wicket_diff} wickets')
return team_b
if(team_b_total_score == team_a_total_score):
print(f"What a match! It's a draw!!")
# TODO: Create an exception for knockout games or tournaments so user will find the winner or do something better
return team_b # This is only for testing
score_diff = team_a_total_score - team_b_total_score
print(f'{team_a} won by {score_diff} runs')
return team_a
def play_game(team_a, team_b):
print(f"{team_a} vs {team_b}")
# First team batting
team_a_total_score = play_team_a(
team_a
)
# print(f'team_a_total_score: {team_a_total_score} ')
# Second team batting
team_b_total_score, team_b_wicket_index = play_team_b(
team_b,
team_a_total_score
)
# Choose winner
winning_team_name = choose_winner(
team_a,
team_b,
team_a_total_score,
team_b_total_score,
team_b_wicket_index
)
pline(90)
#
return winning_team_name
def startpy():
team_a = CURRENT_TEAMS[0]
team_b = CURRENT_TEAMS[1]
play_game(team_a, team_b)
if __name__ == '__main__':
startpy()
'''
Issues:
- Runs 5 should be removed : DONE
-
'''