## what can they use?## 1) number of players at table## 2) their cards## 3) board## 4) betting round## 5) current bet (amount to them)## 6) chips they have left at the moment## 7) pot## 8) history of total betting for each player in each round## 9) big blind## 10) tablechips## 11) which seat they're in## 12) who's the dealer## 13) how many tables are left in the tournament## 1) numattable1.   integer.##    number of players at the table#### 2) cards1.    2x2 matrix.## cards1[1,1] = the number of card 1 (between 2 and 14). cards[1,2] = suit of card 1 (a number between 1 and 4).## cards1[2,1] = the number (2-14) of card 2. cards[2,2] = suit of card 2 (1-4).  Ace = 14.##		## 3) board1.    5x2 matrix.  ## board1[1,1] = number of first card (2-14).## board1[1,2] = suit (1-4) of first card. ## Both are zero if the card hasn't been seen yet. ## For instance, if bettinground1 < 3, then board[4,1] = board1[4,2] = 0.#### 4) round1.  integer.## 1 = preflop, 2 = after flop, 3 = after turn, 4 = after river.#### 5) currentbet.    integer.##   how much MORE it is to you to stay in, right now.##		## 6) mychips1.     integer.## how many chips you have left at the moment.##	## 7) pot1.    integer.## how much is in the pot at the moment.##		## 8) roundbets.   numattable1 x 4 matrix.## roundbets[i,j] = total amount the player in seat i put in, in betting round j.#### 9) blinds1.    integer.## big blind amount. (regardless of whether the player in the big blind can afford it).##		## 10) chips1.  vector of length numattable1.## chips1[i] = how many chips the player in seat i has left.##		## 11) ind1.    integer.## Which seat you're in.  (So, mychips1 = chips1[ind1]).##		## 12) dealer1.  integer.## Which seat the dealer is in. ## (If only 2 players are left, then I use the convention that the "dealer" is the big blind.)#### 13) tablesleft.  integer.## How many tables are left in the tournament (including yours). yosef = function(numattable1, crds1, board1,  round1, currentbet,  mychips1, pot1,     roundbets, blinds1, chips1, ind1, dealer1, tablesleft){    ## if pair of at least 9, then all in      ## if nobody's bet (or raised the blinds) yet, then bet (or raise the blinds) the minimum amount possible.    ## note: if you're big blind, or if you've already called, then    ## those cases have to be handled separately, since for instance if     ## someone's raised you exactly one more big blind, then you should fold, but it will look like     ## nobody's raised yet since it'll again be one big blind to you.    a1 = 0    bigb = dealer1 + 2    if(bigb > numattable1) bigb = bigb - numattable1    if(round1 == 1){	if((roundbets[ind1,1] < blinds1 - .5) && (currentbet < blinds1+.5)){	    a1 = min(2*blinds1, mychips1)	} else if ((ind1 == bigb) && (currentbet < blinds1 - .5)) a1 = min(blinds1, mychips1)    }    if((round1 > 1.5) && (currentbet < .5)) a1 = min(blinds1, mychips1)    if((crds1[1,1] == crds1[2,1]) && (crds1[2,1] > 8.5)) a1 = mychips1    a1} ## end of yosefzelda = function(numattable1, crds1, board1,  round1, currentbet,  mychips1, pot1,     roundbets, blinds1, chips1, ind1, dealer1, tablesleft){    a1 = 0 ## how much I'm gonna end up betting. Note that the default is zero.    a2 = min(mychips1, currentbet) ## how much it costs to call        if(round1 == 1){ ## pre-flop:	## AK: Make a big raise if nobody has yet. Otherwise call.	## AQ: call a small raise, or make one if nobody has yet.	## AJ, AT, KQ, KJ, QJ: call a tiny raise.	## A9, KT, K9, QT, JT, T9: call a tiny raise if in late position (within 2 of the dealer).	## Suited A2-AJ: call a small raise.	## 22-99: call a small raise.	## TT-KK: make a huge raise. If someone's raised huge already, then go all in.	## AA: make a small raise. If there's been a raise already, then double how much it is to you.		a3 = 2*blinds1+1 ## how much a tiny raise would be	a4 = 4*blinds1+1 ## how much a small raise would be	a5 = max(8*blinds1,mychips1/4)+1 ## how much a big raise would be	a6 = max(12*blinds1,mychips1/2)+1 ## how much a huge raise would be	a7 = dealer1 - ind1	if(a7 < -.5) a7 = a7 + numattable1 ## your position: a7 = how many hands til you're dealer		if((crds1[1,1] == 14) && (crds1[2,1] == 13)){	    a1 = max(a2,a5)	}	if((crds1[1,1] == 14) && (crds1[2,1] == 12)){	    if(a2 < a4){		a1 = a4	    } else if(a2 > a5){		a1 = 0	    } else a1 = a2	}	if(((crds1[1,1] == 14) && ((crds1[2,1] < 11.5) && (crds1[2,1] > 9.5))) || 	    ((crds1[1,1] == 13) && (crds1[2,1] > 10.5)) ||	    ((crds1[1,1] == 12) && (crds1[2,1] == 11))){	    if(a2 < a3) a1 = a2	}	if(((crds1[1,1] == 14) && (crds1[2,1] == 9)) || 	    ((crds1[1,1] == 13) && ((crds1[2,1] == 10) || (crds1[2,1] == 9))) ||	    ((crds1[1,1] == 12) && (crds1[2,1] == 10)) ||	    ((crds1[1,1] == 11) && (crds1[2,1] == 10)) ||	    ((crds1[1,1] == 10) && (crds1[2,2] == 9))){	    if((a2 < a3) && (a7<2.5)) a1 = a2	}	if((crds1[1,2] == crds1[2,2]) && (crds1[1,1] == 14) && (crds1[2,1] < 11.5)){	    if(a2<a4) a1 = a2	    ## Note: this trumps the previous section, since it comes later in the code.	}	if((crds1[1,1] == crds1[2,1])){ ## pairs:	    if(crds1[1,1] < 9.5){		if(a2 < a4) a1 = a2	    } else if(crds1[1,1] < 13.5){		if(a2<a5) a1 = a5 else a1 = mychips1	    } else {		if(a2 < blinds1 + .5) a1 = a4 else a1 = min(2*a2,mychips1)	    }	}    }    if(round1 == 2){ ## post-flop: 	## If there's a pair on the board and you don't have a set, then check/call up to small bet.	## Same thing if there's 3-of-a-kind on the board and you don't have a full house or more.	## If you have top pair or an overpair or two pairs or a set, make a big bet (call any bigger bet). 	## Otherwise, if nobody's made even a small bet yet, then with prob. 20% make a big bluff bet.	## If you're the last to decide and nobody's bet yet, then increase this prob. to 50%.	## If you have an inside straight draw or flush draw then make a small bet (call any bigger bet).	## If you have a straight or better, then just call.	## Otherwise fold.		a5 = min(sum(roundbets[,1]),mychips1) ## how much a big bet would be (prev round's pot size)	a6 = min(.5*sum(roundbets[,1]),mychips1) ## how much a small bet would be		x = handeval(c(crds1[1:2,1], board1[1:3,1]), c(crds1[1:2,2], board1[1:3,2])) ## what you have	x1 = handeval(c(board1[1:3,1]),c(board1[1:3,2])) ## what's on the board	y = straightdraw1(c(crds1[1:2,1], board1[1:3,1]))	z = flushdraw1(c(crds1[1:2,2], board1[1:3,2]))	topcard1 = max(board1[1:3,1])	a7 = runif(1) ## random number uniformly distributed between 0 and 1	a8 = (1:numattable1)[roundbets[,1] == roundbets[ind1,1]] ## others who can still bet with you	## The next 5 lines may seem weird, but the purpose is explained in the next comment:	a9 = a8 - dealer1	for(i in 1:length(a9)) if(a9[i]<.5) a9[i] = a9[i] + numattable1	a10 = ind1 - dealer1	if(a10 < .5) a10 = a10 + numattable1	a11 = 2*(a10 == max(a9))   ## So a11 = 2 if you're last to decide; otherwise a11 = 0.		if((x1 > 1000000) && (x < 3000000)){	    if(a2 < a6) a1 = a2	} else if((x1 > 3000000) && (x < 6000000)){	    if(a2 < a6) a1 = a2	} else if(x > 1000000 + 15^3*topcard1){	    a1 = max(a5,a2)	} else if((a2 < a6) && ((a7 < .20) || ((a7 < .50) && (a11>1)))){	    a1 = a6	}	if((y == 4) || (z == 4)) a1 = max(a6, a2)	if(x > 4000000) a1 = a2    }    if(round1 == 3){ ## after turn: 	## If there's a pair on the board and you don't have a set, then check/call up to small bet.	## Same thing if there's 3-of-a-kind on the board and you don't have a full house or more.	## Otherwise, if you have top pair or better, go all in.	## If you had top pair or overpair but now don't, then check/call a medium bet but fold to more.	## If you have an inside straight draw or flush draw then check/call a medium bet as well.	## Otherwise check/fold.	a6 = min(1/3*sum(roundbets[,1:2]),mychips1) ## small bet (1/3 of prev round's pot size)	a5 = min(.75*sum(roundbets[,1:2]),mychips1) ## medium bet (3/4 of prev round's pot size)	x = handeval(c(crds1[1:2,1], board1[1:4,1]), c(crds1[1:2,2], board1[1:4,2])) ## what you have	x1 = handeval(c(board1[1:4,1]),c(board1[1:4,2])) ## what's on the board	y = straightdraw1(c(crds1[1:2,1], board1[1:4,1]))	z = flushdraw1(c(crds1[1:2,2], board1[1:4,2]))	topcard1 = max(board1[1:4,1])	oldtopcard1 = max(board1[1:3,1])	if((x1 > 1000000) && (x < 3000000)){	    if(a2 < a6) a1 = a2	} else if((x1 > 3000000) && (x < 6000000)){	    if(a2 < a6) a1 = a2	} else if(x > 1000000 + 15^3*topcard1){	    a1 = mychips1	} else if(x > 1000000 + 15^3*oldtopcard1){	    if(a2 < a5) a1 = a2	} else if((y == 4) || (z == 4)){	    if(a2 < a5) a1 = a2	}    }    if(round1 == 4){ ## after river: 	## If there's a pair on the board and you don't have a set, then check/call up to small bet.	## Same thing if there's 3-of-a-kind on the board and you don't have a full house or more.	## Otherwise, if you have two pairs or better, go all in.	## If you have one pair, then check/call a small bet.	## With nothing, go all-in with probability 10%; otherwise check/fold.	a6 = .45+runif(1)/10  ## random number between .45 and .55	a5 = min(a6*sum(roundbets[,1:3]),mychips1) ## small bet: around 1/2 of pot size; VARIES RANDOMLY	x = handeval(c(crds1[1:2,1], board1[1:5,1]), c(crds1[1:2,2], board1[1:5,2]))	x1 = handeval(c(board1[1:5,1]),c(board1[1:5,2])) ## what's on the board	if((x1 > 1000000) && (x < 3000000)){	    if(a2 < a5) a1 = a2	} else if((x1 > 3000000) && (x < 6000000)){	    if(a2 < a5) a1 = a2	} else if(x > 2000000){	    a1 = mychips1	} else if(x > 1000000){	    if(a2 < a5) a1 = a2	} else if(runif(1)<.10){	    a1 = mychips1	}    }    round(a1)} ## end of zelda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tester1 = function(x,k){    t1 = Sys.time()    for(i in 1:k){	## First the easy stuff: numattable1, round1, ind1, dealer1, chips1, mychips1, blinds1, tablesleft	numattable1 = sample(9,1)+1  ## random integer between 2 and 10 (inclusive).	round1 = sample(4,1)  ## random integer between 1 and 4 (inclusive).	ind1 = sample(numattable1,1) ## random integer between 1 and numattable1.	dealer1 = sample(numattable1,1) ## random integer between 1 and numattable1.	chips1 = sample(20000,numattable1, rep=T) ## a vector of random numbers between 1 and 20000.	chips1 = chips1 * (runif(numattable1)<.8) ## make these zero (which means all in) 20% of the time.	chips1[ind1] = sample(20000,1) ## but not you -- you have chips left.	mychips1 = chips1[ind1]	blinds1 = sample(4001,1) + 19 ## random integer between 20 and 4020.	tablesleft = sample(3,1) ## random integer between 1 and 3		## Now the harder ones: currentbet, roundbets, pot1, crds1, and board1	currentbet = sample(5000,1) ## random integer between 1 and 5000.	x1 = runif(1) ## random number between 0 and 1	if(x1 < .40) currentbet = 0 ## 40% of the time I'll make currentbet 0, since that is common	if(x1 > .80) currentbet = blinds1 ## 20% of the time I'll make currentbet equal to the big blind.		roundbets = matrix(rep(0,(4*numattable1)),ncol=4)	for(j2 in 1:numattable1){	    roundbets[j2,1:round1] = sample(5,round1,rep=T)*blinds1	    if(runif(1) < .5) roundbets[j2,1] = 0	}	## make sure small blind and big blind paid their blinds	smb1 = dealer1 + 1	if(smb1 > numattable1) smb1 = smb1 - numattable1	bigb = dealer1+2	if(bigb > numattable1) bigb = bigb - numattable1	if(roundbets[smb1,1] < .5) roundbets[smb1,1] = round(blinds1/2)	if(roundbets[bigb,1] < .5) roundbets[bigb,1] = blinds1	currentbet = max(roundbets[,round1]) - roundbets[ind1,round1]	## Be careful: if you're not the big blind and if the big blind doesn't have enough chips, then it still may be blinds1 to you:	if(round1 == 1){	    bmax1 = max((roundbets[-ind1,1] + chips1[-ind1]))	    if(bmax1 < currentbet) currentbet = bmax1	}	## Note: this was sloppy, and things could happen here that couldn't really happen. 	## For instance, an opponent could have bet zero (i.e. folded) in round 1 but bet big in round 2.	## So, if you DON'T have a problem here, then you're sure to be good to go.	## But if you DO have a problem here sometimes, that could possibly be because of these artificial scenarios, and	## maybe you're actually good to go.	## Still, this might be useful in detecting syntax errors and other obvious problems.	pot1 = sum(roundbets[,1:round1])	b3 = deal1(numattable1)	crds1 = matrix(c(b3$plnum1[ind1,],b3$plsuit1[ind1,]),ncol=2,byrow=F)	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]	x2 = x(numattable1, crds1, board1,  round1, currentbet,  mychips1, pot1, 	    roundbets, blinds1, chips1, ind1, dealer1, tablesleft)	# cat("\n On trial ",i,", current bet to you is ",currentbet,", and you have ",mychips1," chips, and you bet ",x2,".")    }    t2 = Sys.time()    t3 = as.numeric(t2-t1)/k    print(t1)    print(t2)    cat("\n It took an average of ",t3," seconds to run the function.\n")}## source(hands101.s)## tester1(xena,1000)## tester1(ursula,1000)