## To use this code to run tournaments, do the following:## 1) Load in the student functions into R, either by pasting them into R or##    using source, for instance by doing source("studentexamples.s").## 2) Create a vector called decision1 which contains the ##    names of the students' functions. For instance, to run a tournament ##    between the 7 all in or fold functions in studentexamples.s, you can say##       decision1 = list(gravity, tommy, ursula, timemachine, vera, william, xena) ## 3) create a vector called name1 which contains the names, in quotes, ##    of the student functions. For instance,##       name1 = c("gravity","tommy","ursula","timemachine","vera","william","xena")## 4) Set the parameters in the 6 lines below number 8, or keep them as is.## 5) Enter this whole file into R, either by pasting it ##    or doing source("tournament.s").## 6) Open a graphics window by doing plot(1) and make the window large on your screen.## 7) To run one tournament, do tourn1(1).## 8) To run 100 tournaments and plot the results, do ##       z1 = many1(100)##       z2 = z1[[2]]##       barplot(z2,names.arg=name1[1:nplayers1],cex.names=.9)myfast1 = 2 ## make it 2 if you want it to go fast. 0 = slow.t1 = 0.5 ### fraction of times to show double-upst2 = 1 ### fraction of times to show eliminationschipstart1 = 1000 ## starting numnber of chips. The big blind starts at 20.winners1 = 3 ## the number of function that get points.payouts1 = c(13,8,5) ## the number of points for the winner, 2nd place, etc.## The rest of these commands, below, you probably just want to keep as is.nplayers1 = length(name1)nplayers11 = nplayers1graphiccutoff1 = .1lowercut1 = 300cardname1 = c(as.character(1:9),"T","J","Q","K","A")suitname1 = c(2,3,4,6)switch2 = function(x){    ## takes a number 1-52, and turns it into a card:     ## returns a list, where the 1st is the number (2-14), and 2nd is suit (1-4).    n = length(x)    y = list(num=x, st=rep(1,n))    for(i in c(1:n)){	a = 1	while(a>0){	    if(y$num[i]<14) a = -1 else{y$st[i] = y$st[i]+1		y$num[i] = y$num[i]-13	    }	}    }    y$num = y$num+1    y} ## end of switch2deal1 = function(numpl){    ## numpl is the number of players at the table    numcards = 2*numpl+5    crds1 = order(runif(52))[1:numcards]    crds2 = switch2(crds1)    num1 = crds2$num    suit1 = crds2$st     brdnum1 = num1[(numcards-4):numcards]    brdsuit1 = suit1[(numcards-4):numcards]    plnum1 = matrix(num1[1:(2*numpl)],ncol=2)    plsuit1 = matrix(suit1[1:(2*numpl)],ncol=2)    ## order them    for(i in c(1:numpl)){	if(plnum1[i,1]<plnum1[i,2]){	    a = plnum1[i,1]	    plnum1[i,1] = plnum1[i,2]	    plnum1[i,2] = a	    a = plsuit1[i,1]	    plsuit1[i,1] = plsuit1[i,2]	    plsuit1[i,2] = a	}    }    b9 = list(plnum1=plnum1, plsuit1=plsuit1,	brdnum1=brdnum1, brdsuit1=brdsuit1)    b9}	## end of deal1mycount1 = function(x){    ## returns sorted unique values of x and how many times each appearsb1 = sort(unique(x),decreasing=T)b2 = length(b1)b3 = rep(0,b2)for(i in 1:b2) b3[i] = sum(x == b1[i])list(v=b1, ct = b3)}straight1 = function(x){a1 = sort(unique(x))if (length(a1)<4.5) return(0)a3 = 0n = length(a1)if(a1[n] == 14) a1 = c(1,a1) ## count ace as both 1 and 14a2 = length(a1)for(j in c(5:a2)){ ## j will be the potential highest card of straight    if( sum(15^c(1:5) * a1[(j-4):j]) == sum(15^c(1:5) * ((a1[j]-4):a1[j]))) a3 = a1[j]}a3}	## end of straight1strflsh1 = function(x,y){a1 = mycount1(y)if(max(a1$ct)<4.5) return(0)a2 = c(1:length(a1$ct))[a1$ct > 4.5]a3 = a1$v[a2] ## this is the suita4 = sort(x[y == a3],decreasing=T)straight1(a4)} ## end of strflush1four1 = function(x){## 15*number of the foursome + nexta1 = mycount1(x)a2 = a1$va3 = a1$cta4 = sum(a3 > 3.5)if(a4 < 0.5) return(0)a5 = sort(a2[a3>3.5],decreasing=T)a6 = sort(c(0,x[(x != a5[1])]),decreasing=T)15*a5[1] + a6[1]}    ## end of four1full1 = function(x){a1 = mycount1(x)a2 = sort(a1$ct,decreasing=T)if(length(a2) < 1.5) return(0)if(a2[1] < 2.5) return(0)if(a2[2] < 1.5) return(0)a3 = min(c(1:length(a1$ct))[a1$ct > 2.5])a4 = a1$v[a3] ## the number of the tripa5 = a1$ct[-a3]a6 = a1$v[-a3]a7 = min(c(1:length(a5))[a5 > 1.5]) ## the number of the paira8 = a6[a7]15*a4 + a8}  ## end of full1flush1 = function(x,y){    ## Can break down if dealing with 10 cards or more, i.e. if 2 flushes are possible    ## returns 15^4 times top number + 15^3 times 2nd-highest + ...a1 = mycount1(y)if(max(a1$ct)<4.5) return(0)a2 = c(1:length(a1$ct))[a1$ct > 4.5]a3 = a1$v[a2] ## this is the suita4 = sort(x[y == a3],decreasing=T)sum(15^c(4:0) * a4[1:5])} ## end of flush1straight1 = function(x){a1 = sort(unique(x))if (length(a1)<4.5) return(0)a3 = 0n = length(a1)if(a1[n] == 14) a1 = c(1,a1) ## count ace as both 1 and 14a2 = length(a1)for(j in c(5:a2)){ ## j will be the potential highest card of straight    if( sum(15^c(1:5) * a1[(j-4):j]) == sum(15^c(1:5) * ((a1[j]-4):a1[j]))) a3 = a1[j]}a3}	## end of straight1trip1 = function(x){## 225*triple + 15*next + nexta1 = mycount1(x)a2 = a1$va3 = a1$cta4 = sum(a3 > 2.5)if(a4 < 0.5) return(0)a5 = sort(a2[a3>2.5],decreasing=T)a6 = sort(c(0,0,x[(x != a5[1])]),decreasing=T)225*a5[1] + 15*a6[1] + a6[2]}    ## end of trip1twopair1 = function(x){## 225*highpair + 15*lowpair + nexta1 = mycount1(x)a2 = a1$va3 = a1$cta4 = sum(a3>1.5)if(a4<1.5) return(0)a5 = sort(a2[a3>1.5],decreasing=T)a6 = max(c(0,a2[(a2 != a5[1]) & (a2 != a5[2])]))225*a5[1] + 15*a5[2] + a6}    ## end of twopair1	onepair1 = function(x){## 15*15*15*pair + 15*15*next + 15*next + nexta1 = unique(x)a2 = length(a1)if(a2 == length(x)) return(0)a3 = rep(0,a2)for(i in 1:a2) a3[i] = sum(x == a1[i])a4 = max(a1[a3>1.5])a5 = sort(c(x[x != a4],rep(0,3)),decreasing=T)15*15*15*a4 + 225*a5[1] + 15*a5[2] + a5[3]}nothing1 = function(x){## 15^4*highest + 15^3*next + 225*next + 15*next + nexty = c(x,rep(0,5))  ## this is in case x has length < 5a1 = sort(y,decreasing=T)15*15*15*15*a1[1] + 15*15*15*a1[2] + 225*a1[3] + 15*a1[4] + a1[5]}	## end of nothing1handeval = function(num1,suit1){    a1 = strflsh1(num1,suit1)     if(a1>0.5) return(8000000+a1)    a1 = four1(num1)    if(a1>0.5) return(7000000+a1)    a1 = full1(num1)    if(a1>0.5) return(6000000+a1)    a1 = flush1(num1,suit1)    if(a1>0.5) return(5000000+a1)    a1 = straight1(num1)    if(a1>0.5) return(4000000+a1)    a1 = trip1(num1)    if(a1>0.5) return(3000000+a1)    a1 = twopair1(num1)    if(a1>0.5) return(2000000+a1)    a1 = onepair1(num1)    if(a1>0.5) return(1000000+a1)    a1 = nothing1(num1)    return(a1)} ## end of handevalflushdraw1 = function(x){    a1 = mycount1(x)    max(a1$ct)} ## end of flushdraw1straightdraw1 = function(x){    ## returns 4 is there are 2 possibilities for a straight.     ## returns 2 for a gutshot straight draw.    ## returns 0 otherwise    ## Note: returns 26 if you already have a straight!    a1 = 0    for(i in c(2:14)){	a2 = straight1(c(i,x))	if(a2 > .5) a1 = a1 + 1    }    a1 * 2 }	## end of straightdraw1# MAIN LOOP:tourn1 = function(nplayers1=nplayers11, chipstart1 = 1000, bigstart1 = 20,     inc1 = 1.50, winners1 = 3){    ## NOTE: don't have more than 10 winners! That might mess this up.    # blinds last 10 hands, then increase by factor of inc1. (rounded)    # If # players left is 11-20, then each hand, players are split    # into 2 tables of equal size (or one off if # players is odd). Then 10 hands are played,    # no matter what.    # Small = 1/2 big (rounded to nearest integer).    # If num players > 20, then each 10 hands, players are randomly split    # into tables of size 10. Remaining players sit out 10 hands.    # a) Start loop. Initiate blinds.    # b) Assign seats. Set num of tables.    # c) For each table, play a hand. Repeat 10 times.    # d) After each hand, update nplayers1, and     # if nplayers1 <= winners1, then store winners. See if all done too.    # e) On key hands, do instant replay! (if all-in & call, and     #    total pool > 8 times big.    # f) Increase blinds and repeat b-e.        plot(c(0,nplayers1+1),c(lowercut1,chipstart1*nplayers1),pch=name1[1:nplayers1],	type="n",xlab="player number",ylab="chips",log="y")    chip1 = rep(chipstart1, nplayers1)    text(x=c(1:nplayers1),y=chip1,cex=2,labels=name1[1:nplayers1],srt=270,col=2)    chip7 = chip1    big1 = round(bigstart1)    sm1 = round(big1/2)    blinds1 = c(sm1,big1)    nleft1 = nplayers1    plleft = 1:nplayers1   ## plleft will be the indices of who's left.    places1 = rep(0,winners1)        stp3 = 0    while(stp3 < 1){	if(nleft1 > 20.5){	    ntable1 = floor(nleft1/10)	    pl1 = sample(nleft1)	    tables1 = list(tbnums = rep(10,ntable1))	    for(j in c(1:ntable1)){		tables1[[1+j]] = plleft[pl1[(1:10)+(j-1)*10]] 	    }	}	if((nleft1 < 20.5) && (nleft1 > 10.5)){	    ntable1 = 2	    pl1 = sample(nleft1)	    thalf1 = ceiling(nleft1/2)	    bhalf1 = nleft1 - thalf1	    tables1 = list(tbnums = c(thalf1, bhalf1))	    tables1[[2]] = plleft[pl1[1:thalf1]]	    tables1[[3]] = plleft[pl1[(thalf1+1):nleft1]]	}	if(nleft1 < 10.5){	    ntable1 = 1	    pl1 = sample(nleft1)	    tables1 = list(tbnums = nleft1)	    tables1[[2]] = plleft[pl1]	    ## so, with 10 players or fewer, I'm re-shuffling seats every 10 hands.	}	cat("\n Big blind is ",blinds1[2],"\n")	for(i in 1:ntable1){	    k = 0	    for(j in 1:10){		chip3 = chip1[tables1[[1+i]]]		k = k+1		if(k > tables1[[1]][i]) k = 1		cat(j)		x32 = hand1(tables1[[1]][i], tables1[[1+i]], chip3, blinds1, k, ntable1) 		chip2 = x32$chips2		chip1[tables1[[1+i]]] = chip2		chipdif8 = (abs(chip1-chip7)/pmax(chip1,chip7,rep(1,nplayers1)) > graphiccutoff1)		if(x32$draw1 > 1){		    text(x=c(1:nplayers1),y=chip1,cex=2,labels=name1[1:nplayers1],srt=270,col=2)		    chip7 = chip1		} else if(sum(chipdif8)>.5){		    text(x=c(1:nplayers1)[chipdif8],y=chip7[chipdif8],cex=2,col=0,labels=name1[chipdif8],srt=270)		    text(x=c(1:nplayers1)[chipdif8],y=chip7[chipdif8],cex=2,col=0,labels=name1[chipdif8],srt=270)		    text(x=c(1:nplayers1)[chipdif8],y=chip1[chipdif8],cex=2,col=2,labels=name1[chipdif8],srt=270)		    chip7[chipdif8] = chip1[chipdif8]		}		## Now remove eliminated players, even if they were blinds.		## This may let some people miss their big blind. Note this.		j1 = sum(chip2 < .5) ## the number eliminated.		if(j1 > .5){		    j2 = tables1[[i+1]][c(1:tables1[[1]][i])[chip2 < .5]] ## their indices		    j3 = j2[order(chip3[j2],decreasing=T)] ## ordered by how much they had before		    j4 = min(winners1,nleft1)		    nleft1 = nleft1 - j1		    if(nleft1 < winners1 - .5) places1[(nleft1+1):j4] = j3[1:(j4-nleft1)]		    cat("\n Eliminated: ",j2,".....",nleft1," players remaining.\n")		    tables1[[1]][i] = tables1[[1]][i] - j1		    tables1[[1+i]] = tables1[[1+i]][chip2>.5]		}		if(nleft1 < 1.5) break	    }	    if(nleft1 < 1.5) break	}	big1 = round(blinds1[2]*inc1)	sm1 = round(big1/2)	blinds1 = c(sm1,big1)	plleft = c(1:nplayers1)[chip1>0.5]	nleft1 = length(plleft)	if(nleft1 < 1.5){	    stp3 = 2	    places1[1] = plleft	    z2 = winners1+1	   plot(c(0,nplayers1+1),c(lowercut1,chipstart1*nplayers1),	       type="n",xlab="player number",ylab="chips",log="y")	   text(1*nplayers1/z2,nplayers1*chipstart1, "1st:",col=4,cex=2.5)	    text(2*nplayers1/z2,nplayers1*chipstart1, "2nd:",col=4,cex=2.5)	    text(3*nplayers1/z2,nplayers1*chipstart1, "3rd:",col=4,cex=2.5)	    if(z2 > 4.5) for(z1 in c(4:winners1)) text(z1*nplayers1/z2,nplayers1*chipstart1, paste(z1,"th:"),col=4,cex=2.5)	    for(z1 in c(1:winners1)) text(z1*nplayers1/(winners1+1),(nplayers1/2)*chipstart1,name1[places1[z1]],col=4,cex=2.5)	}    }    places1} ## end of tourn1        many1 = function(k){    ## runs k tournaments    d1 = matrix(0,ncol=winners1,nrow=k) ## matrix of results    d2 = rep(0,nplayers1) ## total pts for each player    for(i in 1:k){	cat("\n\n...................  TOURNAMENT NUMBER ",i,":  ................... \n\n")	d1[i,] = tourn1()	for(j in 1:winners1) d2[d1[i,j]] = d2[d1[i,j]] + payouts1[j]	print(rbind(1:nplayers1,d2))	#locator(1)    }    list(d1,d2)} ## end of many1switch2 = function(x){    ## takes a number 1-52, and turns it into a card:     ## returns a list, where the 1st is the number (2-14), and 2nd is suit (1-4).    n = length(x)    y = list(num=x, st=rep(1,n))    for(i in c(1:n)){	a = 1	while(a>0){	    if(y$num[i]<14) a = -1 else{y$st[i] = y$st[i]+1		y$num[i] = y$num[i]-13	    }	}    }    y$num = y$num+1    y} ## end of switch2deal1 = function(numpl){    ## numpl is the number of players at the tablenumcards = 2*numpl+5crds1 = order(runif(52))[1:numcards]crds2 = switch2(crds1)num1 = crds2$numsuit1 = crds2$st brdnum1 = num1[(numcards-4):numcards]brdsuit1 = suit1[(numcards-4):numcards]plnum1 = matrix(num1[1:(2*numpl)],ncol=2)plsuit1 = matrix(suit1[1:(2*numpl)],ncol=2)## order themfor(i in c(1:numpl)){if(plnum1[i,1]<plnum1[i,2]){a = plnum1[i,1]plnum1[i,1] = plnum1[i,2]plnum1[i,2] = aa = plsuit1[i,1]plsuit1[i,1] = plsuit1[i,2]plsuit1[i,2] = a}}b9 = list(plnum1=plnum1, plsuit1=plsuit1,brdnum1=brdnum1, brdsuit1=brdsuit1)b9}	## end of deal1bid1 = function(numattable1, playerseats1, chips1, blinds1, dealer1, b3, ntable1){   round1 = 1   in1 = 1*(chips1>0.5)   bet1 = rep(0,numattable1)      betlist1 = c(0)   indlist1 = c(0)   outlist1 = c(0)   ind1 = dealer1+1   if(ind1>numattable1) ind1 = 1   j = 0   prevbet = 0   currentbet = 0   better1 = 0   board1 = matrix(rep(0,10),ncol=2)   board1[1:3,1] = b3$brdnum1[1:3]   board1[1:3,2] = b3$brdsuit1[1:3]   roundbets = matrix(rep(0,(4*numattable1)),ncol=4)   pot1 = 0     stp = 0   while(stp < 1){       myout1 = 0       j = j+1       if(j==1){	   bet1[ind1] = min(chips1[ind1],blinds1[1])	   prevbet = bet1[ind1]	   better1 = ind1	   pot1 = bet1[ind1]	   roundbets[ind1,1] = bet1[ind1]	   chips1[ind1] = chips1[ind1] - bet1[ind1]	   betlist1 = c(betlist1,bet1[ind1])	   indlist1 = c(indlist1,ind1)	   outlist1 = c(outlist1,myout1)       }       if(j==2){	   bet1[ind1] = min(chips1[ind1],blinds1[2])	   currentbet = max(prevbet,blinds1[2])	   better1 = ind1	   pot1 = pot1 + bet1[ind1]	   roundbets[ind1,1] = bet1[ind1]	   chips1[ind1] = chips1[ind1] - bet1[ind1]	   betlist1 = c(betlist1,bet1[ind1])	   indlist1 = c(indlist1,ind1)	   outlist1 = c(outlist1,myout1)       }       if(j==3){better1 = ind1}  # so that big blind can raise!       if((j > 2.5) && (in1[ind1] > 0.5)){	   crds1 = matrix(c(b3$plnum1[ind1,],b3$plsuit1[ind1,]),ncol=2,byrow=F)	   bmax1 = max((bet1[-ind1] + chips1[-ind1])[in1[-ind1]>.5])	   # cat(".a. Seat ",ind1,"'s turn, bmax is ",bmax1,", lastbetter is ",better1,"...\n")	   if(bmax1 < currentbet) currentbet = bmax1	   b1 = round(decision1[[playerseats1[ind1]]](numattable1, 	            crds1, 		    board1, 		    round1, 		    currentbet - bet1[ind1], 		    chips1[ind1],		    pot1, 		    roundbets,		    blinds1[2], 		    chips1,		    ind1,		    dealer1, 		    ntable1))	   # cat("\n Seat ", ind1,": b1 was ",b1," and it was ",currentbet-bet1[ind1]," to him.")	   if(b1 > chips1[ind1]) b1 = chips1[ind1] ## if bet is more than you have, fix that.	   if(b1 > bmax1 - bet1[ind1]) b1 = bmax1 - bet1[ind1] ## can't bet more than anyone else who's in has left	    ## if bet is between 0.5 and the amount to you, make it a call.	   if((b1 > 0.5) && (b1 < currentbet - bet1[ind1])) b1 = min(chips1[ind1],currentbet - bet1[ind1])	   ## if bet is a raise of less than the big blind, make it a raise of the big blind.	   raiseamt1 = b1 - (currentbet - bet1[ind1])	   if((raiseamt1 > 0.5) && (raiseamt1 < blinds1[2])) b1 = min(blinds1[2] + currentbet - bet1[ind1],chips1[ind1])	       	   # cat("Now b1 = ",b1,". Currentbet = ",currentbet,".\n")	   if(b1 > currentbet-bet1[ind1]+.5){ ## raise	       prevbet = currentbet	       currentbet = b1+bet1[ind1]	       # cat("curbet = ",currentbet,".\n")	       better1 = ind1	       pot1 = pot1 + b1	       roundbets[ind1,round1] = roundbets[ind1,round1] + b1	       bet1[ind1] = roundbets[ind1,round1]	       in1[ind1] = 1	       chips1[ind1] = chips1[ind1] - b1	   } else if(b1 == min(chips1[ind1],currentbet-bet1[ind1])){ ## call	       pot1 = pot1 + b1	       roundbets[ind1,round1] = roundbets[ind1,round1] + b1	       bet1[ind1] = roundbets[ind1,round1]	       in1[ind1] = 1	       chips1[ind1] = chips1[ind1] - b1	   } else if((chips1[ind1]>0.5) && (b1 < min(chips1[ind1],currentbet-bet1[ind1])-.5)){ ## fold	       in1[ind1] = 0	       myout1 = 2	   }	   betlist1 = c(betlist1,bet1[ind1])	   indlist1 = c(indlist1,ind1)	   outlist1 = c(outlist1,myout1)       }       ind1 = ind1 + 1       if(ind1 > numattable1) ind1 = 1       if(better1 == ind1) stp = 2       # cat("\n\n who:",numattable1,playerseats1, chips1,".\n")       if(sum(in1) < 1.5) stp = 2     }   z3 = 0 ## now see if all the betting is over: if so, let z3 = 2.   if(sum(chips1[c(1:numattable1)[in1 > .5]] > .5) < 1.5) z3 = 2   # cat("\n Round 1 over.  z3 = ",z3,". in1 = ",in1,".\n")   list(i1=in1,p1=pot1,c1=chips1,rb=roundbets,all1=z3,bl1 = betlist1,il1 = indlist1,out1=outlist1)} ## end of bid1bid2 = function(numattable1, playerseats1, blinds1, dealer1, b3, b4, round1, ntable1){   if(b4$all1 > 1) return(b4)   in1 = b4$i1   if(sum(in1)<1.5) return(b4)   betlist1 = c(0)   indlist1 = c(0)   outlist1 = c(0)   chips1 = b4$c1   bet1 = rep(0,numattable1)      ind1 = dealer1+1   if(ind1>numattable1) ind1 = 1   currentbet = 0   better1 = dealer1+1   if(better1 > numattable1) better1 = 1   v1 = c(0,3,4,5)[round1]    board1 = matrix(rep(0,10),ncol=2)   board1[1:v1,1] = b3$brdnum1[1:v1]   board1[1:v1,2] = b3$brdsuit1[1:v1]   roundbets = b4$rb   pot1 = b4$p1     stp = 0   while(stp < 1){       out1 = 0       if(in1[ind1] > 0.5){   	   crds1 = matrix(c(b3$plnum1[ind1,],b3$plsuit1[ind1,]),ncol=2,byrow=F)	   # cat("...",playerseats1[ind1],"'s turn...")	   bmax1 = max((bet1[-ind1] + chips1[-ind1])[in1[-ind1]>.5])	   b1 = round(decision1[[playerseats1[ind1]]](numattable1, 	            crds1, 		    board1, 		    round1, 		    currentbet - bet1[ind1], 		    chips1[ind1],		    pot1, 		    roundbets,		    blinds1[2], 		    chips1,		    ind1,		    dealer1,		    ntable1))	   # cat("\n Seat ", ind1,": b1 was ",b1," and it was ",currentbet-bet1[ind1]," to him.")	   if(b1 > chips1[ind1]) b1 = chips1[ind1] ## if bet is more than you have, fix that.	   if(b1 > bmax1 - bet1[ind1]) b1 = bmax1 - bet1[ind1] ## can't bet more than anyone else who's in has left	   ## if bet is between 0.5 and the amount to you, make it a call.	   if((b1 > 0.5) && (b1 < currentbet - bet1[ind1])) b1 = min(chips1[ind1],currentbet - bet1[ind1])	   ## if bet is a raise of less than the big blind, make it a raise of the big blind.	   raiseamt1 = b1 - (currentbet - bet1[ind1])	   if((raiseamt1 > 0.5) && (raiseamt1 < blinds1[2])) b1 = min(blinds1[2] + currentbet - bet1[ind1],chips1[ind1])	   if(b1 > currentbet - bet1[ind1]+.5){ ## raise	       if(b1 > chips1[ind1]) b1 = chips1[ind1]	       currentbet = b1 + bet1[ind1]	       better1 = ind1	       pot1 = pot1 + b1	       roundbets[ind1,round1] = roundbets[ind1,round1] + b1	       bet1[ind1] = roundbets[ind1,round1]	       in1[ind1] = 1	       chips1[ind1] = chips1[ind1] - b1	   } else if(b1 == min(chips1[ind1],currentbet-bet1[ind1])){ ## call/check	       pot1 = pot1 + b1	       roundbets[ind1,round1] = roundbets[ind1,round1] + b1	       bet1[ind1] = roundbets[ind1,round1]	       in1[ind1] = 1	       chips1[ind1] = chips1[ind1] - b1	   } else if((chips1[ind1]>0.5) && (b1 < min(chips1[ind1],currentbet-bet1[ind1]))){ ## fold	       in1[ind1] = 0	       out1 = 2	   }	   betlist1 = c(betlist1,bet1[ind1])	   indlist1 = c(indlist1,ind1)	   outlist1 = c(outlist1, out1)       }       ind1 = ind1 + 1       if(ind1 > numattable1) ind1 = 1       if(better1 == ind1) stp = 2       if(sum(in1) < 1.5) stp = 2     }   z3 = 0 ## now see if all the betting is over: if so, let z3 = 2.   if(sum(chips1[c(1:numattable1)[in1 > .5]] > .5) < 1.5) z3 = 2   list(i1=in1,p1=pot1,c1=chips1,rb=roundbets,all1=z3,bl1 = betlist1,il1 = indlist1,out1=outlist1)} ## end of bid2        calcwin1 = function(numattable1,playerseats1, b3, b7){## First, for everyone who's in, evaluate their hands.    qual1 = rep(0,numattable1)    inmuch1 = rep(0,numattable1)    for(i in c(1:numattable1)){	if(b7$i1[i] > .5) {	    qual1[i] = handeval(c(b3$brdnum1[1:5],b3$plnum1[i,1:2]),		c(b3$brdsuit1[1:5],b3$plsuit1[i,1:2]))	}	inmuch1[i] = sum(b7$rb[i,])    }    z1 = order(unique(inmuch1[inmuch1 > 0.5]))    difbets = sum(unique(inmuch1[inmuch1 > 0.5])>0) ## How many unique betting amounts.    pot1 = b7$p1    chips1 = b7$c1    prevamount1 = 0        for(j in c(1:difbets)){	amount1 = unique(inmuch1[inmuch1 > 0.5])[z1[j]] ## Start with the least amount	n1 = sum(inmuch1 >= amount1)  ## How many bet at least that much.	pl1 = c(1:numattable1)[inmuch1 >= amount1]  ## Who bet at least that much	# winner1 = pl1[order(qual1[pl1],decreasing=T)[1]]  	winner1 = pl1[c(1:n1)[qual1[pl1]==max(qual1[pl1])]] ## Who has best hand (may be vector, if ties)	more1 = amount1 - prevamount1	for(k in winner1) chips1[k] = chips1[k] + round(n1*more1/length(winner1))	pot1 = pot1 - n1*more1	prevamount1 = amount1    }     chips1} ## end of calcwin1writebets1 = function(b9,y1,numattable1,b3,playerseats1,chips1,chips2,over1){    # cat("\n Round ",y1,"\n",b9$il1,"\n",b9$bl1,".\n")    drawnyet1 = rep(0,numattable1)    if((y1 == 1) || (over1<1)){	ilen1 = length(b9$il1)	rember1 = rep(-1,numattable1)	if(ilen1 > 1.5) for(j in c(2:ilen1)){	    i = b9$il1[j]	    if((y1 == 1) && (drawnyet1[i] < 1) && (j>3.5)){		    text(10*i,60,cardname1[b3$plnum1[i,1]],col=suitname1[b3$plsuit1[i,1]],cex=2)		    text(10*i+2,60,cardname1[b3$plnum1[i,2]],col=suitname1[b3$plsuit1[i,2]],cex=2)		    drawnyet1[i] = 2		    if(myfast1<1) locator(1)	    }	    if(rember1[i] != b9$bl1[j]){		text(10*i,50-5*y1,rember1[i],col=0)		text(10*i,50-5*y1,rember1[i],col=0)		if((y1>1) || (b9$bl1[j] > .5)) text(10*i,50-5*y1,b9$bl1[j])	    }	    rember1[i] = b9$bl1[j]	    if(b9$out1[j] > 1.5){		text(10*i,60,cardname1[b3$plnum1[i,1]],col=0,cex=2)		text(10*i+2,60,cardname1[b3$plnum1[i,2]],col=0,cex=2)		text(10*i,80,as.character(name1[playerseats1[i]]),cex=1+.1*b9$i1[i],col=0)		text(10*i,50,"BETS:",col=0)		text(10*i,50,"BETS:",col=0)		if(chips1[i] == chips2[i]) text(10*i,75,paste("(",chips1[i],")"),col=0)	    }	 	    if((y1 > 1.5) || (j > 3.5))  if(myfast1<1) locator(1)	}    }}   mygraphics1 = function(numattable1, playerseats1, chips1, blinds1, dealer1,b3,b4,b5,b6,b7,chips2,ntable1){    plot(c(0,10+10*numattable1),c(0,100),type="n",xlab="",ylab="",xaxt="n",yaxt="n")    a1 = paste("Blinds are",blinds1[1],"and",blinds1[2],".")    text(10,95,a1)    if(ntable1>1.5) a2 = paste(ntable1,"tables left.") else a2 = "1 table left."    text(10*numattable1,95,a2)    text(10*dealer1,90,"D")        for(j in c(1:numattable1)){	i = 2 + dealer1 + j	if(i > numattable1 + .5) i = i - numattable1	if(i > numattable1 + .5) i = i - numattable1	text(10*i,80,as.character(name1[playerseats1[i]]),cex=1+.1*b7$i1[i],col=1)	a1 = paste("(",chips1[i],")")	text(10*i,75,a1)	text(10*i,50,"BETS:")    }    ## pre-flop bets:    writebets1(b4,1,numattable1,b3,playerseats1,chips1,chips2,0)	## Now flop:    for(j in c(1:3)){	text(j/6*(10+10*numattable1),15,cardname1[b3$brdnum1[j]],col=suitname1[b3$brdsuit1[j]],cex=2)    }    if(myfast1<1) locator(1)    over1 = b4$all1    writebets1(b5,2,numattable1,b3,playerseats1,chips1,chips2,over1)    ## Now turn:    over1 = b5$all1    j = 4    text(j/6*(10+10*numattable1),15,cardname1[b3$brdnum1[j]],col=suitname1[b3$brdsuit1[j]],cex=2)    if(myfast1<1) locator(1)    writebets1(b6,3,numattable1,b3,playerseats1,chips1,chips2,over1)       ## Now river:    over1 = b6$all1    j = 5    text(j/6*(10+10*numattable1),15,cardname1[b3$brdnum1[j]],col=suitname1[b3$brdsuit1[j]],cex=2)    if(myfast1<1) locator(1)    writebets1(b7,4,numattable1,b3,playerseats1,chips1,chips2,over1)    for(i in c(1:numattable1)) if(chips1[i] != chips2[i]) text(10*i, 70, chips2[i])    for(i in c(1:numattable1)){	if(chips2[i]>chips1[i]){	    text(10*i,85,"$",cex=3)	    text(10*i,80,as.character(name1[playerseats1[i]]),cex=1+.1*b7$i1[i],col=5)	}	if(chips2[i] < .5){	    text(10*i,80,as.character(name1[playerseats1[i]]),cex=1+.1*b7$i1[i],col=2)	}    }    if(myfast1<1) locator(1)    plot(c(0,nplayers1+1),c(lowercut1,chipstart1*nplayers1),type="n",xlab="player number",ylab="chips",log="y")} ## end of mygraphics1hand1 = function(numattable1, playerseats1, chips1, blinds1, dealer1, ntable1){    ## numattable1 = number of players at the table    ## playerseats1 = list of indices, who's in seat 1, seat 2, etc.    ## chips1 = list of chips left, FOR PLAYERS AT THIS TABLE ONLY!    ## blinds = vector of small and then big blind    ## dealer1 = seat that the dealer is in.        ## ntable1 = how many tables remain.    chips2 = chips1 ## this will be the revised chips counts, at the end    if(numattable1 < 1.5) return(chips2)            ## want to return other stuff too, liks who's left?          ## No need... can do that within main loop. Just check for zeros.    b3 = deal1(numattable1)    b4 = bid1(numattable1,playerseats1, chips1, blinds1, dealer1, b3, ntable1)    # cat("\n...",b4$bl1,"\n....",b4$il1,"\n")    b5 = bid2(numattable1,playerseats1, blinds1, dealer1, b3,b4,2, ntable1)    b6 = bid2(numattable1,playerseats1, blinds1, dealer1, b3,b5,3, ntable1)    b7 = bid2(numattable1,playerseats1, blinds1, dealer1, b3,b6,4, ntable1)    chips2 = calcwin1(numattable1,playerseats1, b3, b7)     draw1 = 0    u21 = runif(1)    if(((max(chips2/(chips1+.01)) > 1.99) && (u21 < t1)) || ((max(chips1/(chips2+.01)) > 99) && (u21 < t2))) {	mygraphics1(numattable1,playerseats1,chips1,blinds1,dealer1,b3,b4,b5,b6,b7,chips2,ntable1) 	draw1 = 2    }    list(chips2=chips2,draw1=draw1)}  ## end of hand1