SimReA<-matrix(data = 0,ncol = 7)
SimReA<-as.data.frame(SimReA)
colnames(SimReA)<-c("SNo", "Batsman", "Bowler","Ball No.", "Probability","Outcome", "Run" )
print(SimReA)
#Inning 1
library("RecordLinkage")
WktCount = 0
OvCOunt = 0
Status = TRUE
i<-1
while((WktCount<nrow(TeamA) && OvCOunt<20) == TRUE){
#Initialising the first inning
#Updation
if(Status == FALSE)
{
break
print("Loop End!")
}else{
if(i == 1)
{
SimReA[i, "SNo"]<-i
}else{
#Updating Sno
SimReA[i, "SNo"]<-i
# The number of batsman on the field will be same as the number of wickets down
#If once a batsman gets out then never returns on the strike in a match
if(isTRUE(SimReA[i-1,"Outcome"] == "Wkt" | i == 2) == TRUE )
{
SimReA[i,"Batsman"]<-TeamA[WktCount+1, "Player Name"]
# WktCount<-WktCount+1
}else{
SimReA[i, "Batsman"]<-SimReA[i-1, "Batsman"]
}
print(SimReA)
# Using randomly choosen bowler because same bowler can pitch multiple times in a over
if (SimReA[i-1, "Ball No."] %% 6 == 0)
{
SimReA[i, "Bowler"]<-sample(x = TeamB$`Player Name`, size = 1,replace = TRUE)
}else
{
SimReA[i, "Bowler"]<-SimReA[i-1, "Bowler"]
}
print(SimReA)
SimReA$`Ball No.`[i]<-SimReA$`Ball No.`[i-1]+1
if(SimReA[i, "Ball No."] %% 6 == 0)
{
OvCOunt<-OvCOunt+1
}else{
OvCOunt<-OvCOunt
}
print(SimReA)
#Simulation
SimReA[i,"Probability"]<-runif(n = 1)
#Getting the bts class and bwl class
btsclass<-newbts$Class[which.max(levenshteinSim(str1 = SimReA[i, "Batsman"], str2 = newbts$Player))]
bwlclass<-newbwl$Class[which.max(levenshteinSim(str1 = SimReA[i, "Bowler"], str2 = newbwl$Player))]
if(SimReA[i, "Probability"]<=Outcome_0[btsclass, bwlclass])
{
SimReA[i, "Outcome"]<-0
}else if (SimReA[i, "Probability"]>Outcome_0[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_1[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-1
}else if (SimReA[i, "Probability"]>Outcome_1[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_2[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-2
}else if (SimReA[i, "Probability"]>Outcome_2[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_3[btsclass, bwlclass]){
SimRe[i, "Outcome"]<-3
}else if (SimReA[i, "Probability"]>Outcome_3[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_4[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-4
}else if (SimReA[i, "Probability"]>Outcome_4[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_6[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-6
}else if (SimReA[i, "Probability"]>Outcome_6[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_Wd[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-"Wd"
}else if(SimReA[i, "Probability"]>Outcome_Wd[btsclass,bwlclass] && SimReA[i, "Probability"]<=Outcome_Wkt[btsclass, bwlclass]){
SimReA[i, "Outcome"]<-"Wkt"
WktCount<-WktCount+1
}
print(SimReA)
#Run updation
if(SimReA[i, "Outcome"]==0)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+0
}else if (SimReA[i, "Outcome"] == 1)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+1
}else if (SimReA[i, "Outcome"] == 2)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+2
}else if (SimReA[i, "Outcome"] == 3)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+3
}else if (SimReA[i, "Outcome"] == 4)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+4
}else if (SimReA[i, "Outcome"] == 6)
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+6
}else if (SimReA[i, "Outcome"] == "Wd")
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]+1
}else if (SimReA[i, "Outcome"] == "Wkt")
{
SimReA[i, "Run"]<-SimReA[i-1, "Run"]
}
print(SimReA)
}
}
#Status updation
if((WktCount<nrow(TeamA) && OvCOunt<=20) == TRUE)
{
Status <- TRUE
}else{
Status<- FALSE
}
print(SimReA)
i<-i+1
SimReA<-rbind(SimReA, 0)
print(SimReA)
print("")
print(Status)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
}
This shows the output as:
SNo Batsman Bowler Ball No. Probability Outcome Run
1 1 0 0 0 0.0000000 0 0
2 2 Rahul Dravid Brendon McCullum 1 0.6002869 0 0
3 3 Rahul Dravid Brendon McCullum 2 0.1408549 0 0
4 4 Rahul Dravid Brendon McCullum 3 0.8087207 0 0
5 5 Rahul Dravid Brendon McCullum 4 0.9750249 0 0
6 6 Rahul Dravid Brendon McCullum 5 0.3061971 0 0
7 7 Rahul Dravid Brendon McCullum 6 0.4286289 0 0
8 8 Rahul Dravid Chris Gayle 7 0.5328367 0 0
9 9 Rahul Dravid Chris Gayle 8 0.7416640 0 0
10 10 Rahul Dravid Chris Gayle 9 0.3549797 0 0
11 11 Rahul Dravid Chris Gayle 10 0.6067692 0 0
12 12 Rahul Dravid Chris Gayle 11 0.8602620 0 0
13 13 Rahul Dravid Chris Gayle 12 0.6689219 0 0
14 14 Rahul Dravid Ricky Ponting 13 0.7810466 Wd 1
15 15 Rahul Dravid Ricky Ponting 14 0.0000000 0 0
Error: object 'SimRe' not found