Poker game simulator (texas holdem)

17 views (last 30 days)
philip curran
philip curran on 16 May 2022
Commented: philip curran on 10 Jul 2022
i am trying to simulate a poker game to become more familiar with matlab as i am fairly new. So far i have written code to generate the deck, and calculate the best hand a player can make out of the 2 hole cards and the 5 community cards. I now want to simulate the actual game:
  • players post big blind/small blind/ante
  • round of betting preflop
  • round of betting on the flop, turn, and river
  • players reveal thier cards and the best hand wins (i can use my hand rankings to do this)
i am unsure as to the methodology i could use to begin this, how do i tell matlab that a player can bet, check, call, or fold when the action is on the player? Any help greatly appreciated.

Answers (1)

Amish  Saxena
Amish Saxena on 6 Jul 2022
This question seems like a duplicate to the question present in here.
  2 Comments
philip curran
philip curran on 10 Jul 2022
this question focusses more on how the deck is organised within MATLAB, i was more curious about how betting/folding/checking would work within MATLAB. (given that there are very high number of possibilities of how the action can go in a single hand of poker)
philip curran
philip curran on 10 Jul 2022
heres what i've gotten up to so far:
i made use of a function from the MATLAB file exchange in order to rank the hands: Poker hand ranker - File Exchange - MATLAB Central (mathworks.com)
BB = 1;
SB = 0.5;
if round == 1 % preflop
if position == 1 % SB post blind
p1_chips = p1_chips - SB;
p2_chips = p2_chips - BB;
pot = [SB + BB];
elseif position == 2 % BB post blind
p1_chips = p1_chips - BB;
p2_chips = p2_chips - SB;
pot = [SB + BB];
end
holecard1 = deck(1); % deal hole cards
holecard2 = deck(2);
hand1 = [holecard1,holecard2];
[ranks,descriptions,usedcards,unusedcards] = rankp(hand1); % determine card ranking
prompt = "What action would you like to take "; % asks user to input action: check, bet, or fold. (i think i can someway connect this input to the action if statements)
action = input(prompt,"s")
elseif round == 2 % flop
holecard1 = deck(1);
holecard2 = deck(2);
flop1 = deck(3); % deal flop
flop2 = deck(4);
flop3 = deck(5);
hand2 = [holecard1,holecard2,flop1,flop2,flop3];
[ranks,descriptions,usedcards,unusedcards] = rankp(hand2);
action = [];
prompt = "What action would you like to take ";
action1 = input(prompt)
elseif round == 3 % turn
holecard1 = deck(1);
holecard2 = deck(2);
flop1 = deck(3);
flop2 = deck(4);
flop3 = deck(5);
turn = deck(6); % deal turn
hand3 = [holecard1,holecard2,flop1,flop2,flop3,turn];
[ranks,descriptions,usedcards,unusedcards] = rankp(hand3);
prompt = "What action would you like to take ";
action = input(prompt,"s")
elseif round == 4 % river
holecard1 = deck(1);
holecard2 = deck(2);
flop1 = deck(3);
flop2 = deck(4);
flop3 = deck(5);
turn = deck(6);
river = deck(7); % deal river
hand4 = [holecard1,holecard2,flop1,flop2,flop3,turn,river];
[ranks,descriptions,usedcards,unusedcards] = rankp(hand4);
prompt = "What action would you like to take ";
action = input(prompt,"s")
elseif round == 5
disp('showdown!!!!')
p1_hand = [deck(1),deck(2),deck(3),deck(4),deck(5),deck(6),deck(7)]
[ranks,descriptions,usedcards,unusedcards] = rankp(p1_hand); % determines hand rank of player 1
p1_rank = ranks;
p2_hand = [deck(8),deck(9),deck(3),deck(4),deck(5),deck(6),deck(7)]
[ranks,descriptions,usedcards,unusedcards] = rankp(p2_hand); % determines hand rank of player 2
p2_rank = ranks;
if p1_rank > p2_rank
disp('p1 wins')
p1_chips = p1_chips + pot;
elseif p2_rank > p1_rank
disp('p2 wins')
p2_chips = p2_chips + pot;
end
disp('move onto next hand') % not sure how to make this reset
end

Sign in to comment.

Categories

Find more on Card games in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!