-
Notifications
You must be signed in to change notification settings - Fork 1
/
T20BengaluruTeamSimulationSolution.py
158 lines (134 loc) · 6.28 KB
/
T20BengaluruTeamSimulationSolution.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
# -*- coding: utf-8 -*-
"""Untitled7.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1fLA_e36Hvz_L29KxOWEJENJ65qcpZ0QF
"""
# Using numpy library for random number generation
import numpy as np
class twentyTwenty:
def __init__(self, overs, runs):
self.overs = overs
self.runs = runs
def changeStrike(self, playing):
""" Reversing A list. """
return playing[::-1]
def randomRuns(self, probability, currentPlaying):
""" np.random.choince is a numpy fucntion which will give the output
based on probabiliy. P the probabilities associated with each entry for
a particular player"""
return np.random.choice(np.arange(8), p=probability[currentPlaying[0]])
def ballOverview(self, over, ball, player, score):
if score == 1:
print(str(over) + "." + str(ball + 1) + " " +
str(player) + " scores " + str(score) + " runs")
else:
print(str(over) + "." + str(ball + 1) + " " +
str(player) + " scores " + str(score) + " runs")
def print_scores(self, scores):
print("-----------------")
print("SCOREBOARD")
print("-----------------")
for player in scores:
# If player was not out then we need to add a "*" after the score
if scores[player]["Out"] is False:
print(player + " - " + str(scores[player]["Score"]) +
"* (" + str(scores[player]["Balls"]) + " balls)")
else:
print(player + " - " + str(scores[player]["Score"]) +
" (" + str(scores[player]["Balls"]) + " balls)")
def matchStart(self):
""" Match start function all the other function will be called from here.
"""
# Number of overs
overs = self.overs
# Number of runs
runs = self.runs
# Number of wickets gone. Used as flag to check if all players are out
# Wickets gone is 6 because we have only 4 wickets left.
# Assuming all 6 wickets gone as 0
wickets = 0
# Number of balls in an over
balls = 6
# List of players as only 4 player left.
players = ["Kirat Boli", "NS Nodhi", "R Rumrah", "Shashi Henra"]
# List of remaining players Since Kirat Boli, NS Nodhi are on field
remainingPlayers = players[2:]
# In general ScoreBoard will show the scores of the player which are
# currently on the field that's why we took only two players.
scoreBoard = {players[0]: {"Score": 0, "Balls": 0, "Out": False},
players[1]: {"Score": 0, "Balls": 0, "Out": False}}
# Given Probabilies
probability = {
players[0]: [0.05, 0.30, 0.25, 0.10, 0.15, 0.01, 0.09, 0.05],
players[1]: [0.10, 0.40, 0.20, 0.05, 0.10, 0.01, 0.04, 0.10],
players[2]: [0.20, 0.30, 0.15, 0.05, 0.05, 0.01, 0.04, 0.20],
players[3]: [0.30, 0.25, 0.05, 0.00, 0.05, 0.01, 0.04, 0.30]
}
# Currently playing players
currentPlaying = [players[wickets], players[wickets + 1]]
for over in range(overs):
print(' ')
print(str(overs - over) + " overs left. "+ str(runs) + " runs to win")
print(' ')
# We are using Nested Function because in an over there are 6 balls
# in every over.
for ball in range(balls):
# Selecting the random values with probabilities.
randomPosition = self.randomRuns(probability, currentPlaying)
# Increasing balls for the player on Scoreboard which is on strike.
scoreBoard[currentPlaying[0]]["Balls"] += 1
if randomPosition != 7:
# Calculating new runs.
runs = runs - randomPosition
# Increasing the score of the player
scoreBoard[currentPlaying[0]]["Score"] += randomPosition
# Print the score for that ball
self.ballOverview(over, ball, currentPlaying[0], randomPosition)
# Change strike is run is Odd.
if randomPosition % 2 != 0:
currentPlaying = self.changeStrike(currentPlaying)
else:
pass
# More than given runs made, Bengaluru wins
if runs <= 0:
print(" ")
print("Bengaluru won by " + str(4 - wickets) + " wickets and " +
str(((overs - 1 - over) * 6) +
(5 - ball)) + " balls remaining")
print(' ')
self.print_scores(scoreBoard)
return
else:
# If randno is 7 and the player is out
wickets += 1
# Set the player status to Out
scoreBoard[currentPlaying[0]]['Out'] = True
print(str(over) + "." + str(ball+1) +
" " + str(currentPlaying[0]) + " Out!")
# If all players are out, Bengaluru lost
if wickets == 3:
print("Bengaluru lost by " + str(runs) + " runs")
self.print_scores(scoreBoard)
return
else:
# Put the next player on strike
currentPlaying = [remainingPlayers[0], currentPlaying[1]]
scoreBoard[remainingPlayers[0]] = {
"Score": 0, "Balls": 0, "Out": False}
# Remove onstrike player from remaining players list
remainingPlayers.remove(remainingPlayers[0])
# Over End Change Strike
currentPlaying = self.changeStrike(currentPlaying)
if runs == 0:
print("Match tied!")
# If runs scored are less than 40 and balls are over
else:
print(" ")
print("Bengaluru lost by " + str(runs) + " runs")
print(' ')
self.print_scores(scoreBoard)
return
if __name__ == "__main__":
match = twentyTwenty(4, 40)
match.matchStart()