On this page
Programming Video Poker Code
Introduction
Here is my video on programming video poker.
Below is the code developed in the video.
#include <iostream>
#include <time.h>
struct card
{
int r;
int s;
};
void setup(void);
void deal(void);
void draw(int c1, int c2, int c3, int c4, int c5);
int score(card deal[]);
int score_array[2598960];
card deck[52];
int draw_combinations_array[] = { 1533939, 178365, 178365, 16215, 178365, 16215, 16215, 1081, 178365, 16215, 16215, 1081, 16215, 1081, 1081, 47, 178365, 16215, 16215, 1081, 16215, 1081, 1081, 47, 16215, 1081, 1081, 47, 1081, 47, 47, 1 };
int weighting_array[] = { 5, 43, 43, 473, 43, 473, 473, 7095, 43, 473, 473, 7095, 473, 7095, 7095, 163185, 43, 473, 473, 7095, 473, 7095, 7095, 163185, 473, 7095, 7095, 163185, 7095, 163185, 163185, 7669695 };
__int64 tot_combinations[52];
const char* hand_name_array[] = {
"Loss","Jacks or better","Two pair","Three of a kind","Straight","Flush","Full house","Four of a kind","Straight flush","Royal flush","Total" };
int win_array[] = { 0,1,2,3,4,6,9,25,50,800 };
int main()
{
time_t begtime, endtime;
begtime = time(NULL);
setup();
deal();
endtime = time(NULL);
printf("Total seconds=\t%i\n", (int)endtime - (int)begtime);
}
void setup(void)
{
int i, c1, c2, c3, c4, c5, count, score_deal_array[52], sc;
card deal[5];
for (i = 0; i <= 51; i++)
{
deck[i].r = (int)(i / 4);
deck[i].s = i % 4;
score_deal_array[i] = 0;
tot_combinations[i] = 0;
}
count = 0;
for (c1 = 0; c1 <= 47;c1++)
{
deal[0] = deck[c1];
for (c2 = c1 + 1; c2 <= 48; c2++)
{
deal[1] = deck[c2];
for (c3 = c2 + 1; c3 <= 49; c3++)
{
deal[2] = deck[c3];
for (c4 = c3 + 1; c4 <= 50; c4++)
{
deal[3] = deck[c4];
for (c5 = c4 + 1; c5 <= 51; c5++)
{
deal[4] = deck[c5];
sc = score(deal);
score_array[count] = sc;
score_deal_array[sc]++;
count++;
}
}
}
}
}
std::cerr << "count =\t" << count << "\n";
for (i = 0; i <= 9; i++)
std::cerr << hand_name_array[i] << "\t" << score_deal_array[i] << "\n";
}
void deal(void)
{
int i, c1, c2, c3, c4, c5;
int count = 0;
for (c1 = 0; c1 <= 47;c1++)
{
for (c2 = c1 + 1; c2 <= 48; c2++)
{
for (c3 = c2 + 1; c3 <= 49; c3++)
{
for (c4 = c3 + 1; c4 <= 50; c4++)
{
for (c5 = c4 + 1; c5 <= 51; c5++)
{
draw(c1,c2,c3,c4,c5);
count++;
if ((count % 1000)==0)
std::cerr << count << "\n";
}
}
}
}
}
for (i=9; i>=0; i--)
printf("%s\t%i\t%I64i\n", hand_name_array[i], win_array[i], tot_combinations[i]);
}
void draw(int c1, int c2, int c3, int c4, int c5)
{
int i,j, d1, d2, d3, d4, d5, index, sc, draw_score_array[32][10];
int count = 0;
for (i = 0; i <= 31; i++)
{
for (j = 0; j <= 9; j++)
draw_score_array[i][j] = 0;
}
for (d1 = 0; d1 <= 47; d1++)
{
for (d2 = d1 + 1; d2 <= 48; d2++)
{
for (d3 = d2 + 1; d3 <= 49; d3++)
{
for (d4 = d3 + 1; d4 <= 50; d4++)
{
for (d5 = d4 + 1; d5 <= 51; d5++)
{
sc = score_array[count];
index = 0;
if ((d1 == c1) || (d2 == c1) || (d3 == c1) || (d4 == c1) || (d5 == c1))
index += 16;
if ((d1 == c2) || (d2 == c2) || (d3 == c2) || (d4 == c2) || (d5 == c2))
index += 8;
if ((d1 == c3) || (d2 == c3) || (d3 == c3) || (d4 == c3) || (d5 == c3))
index += 4;
if ((d1 == c4) || (d2 == c4) || (d3 == c4) || (d4 == c4) || (d5 == c4))
index += 2;
if ((d1 == c5) || (d2 == c5) || (d3 == c5) || (d4 == c5) || (d5 == c5))
index += 1;
draw_score_array[index][sc]++;
count++;
}
}
}
}
}
double ev;
double max_ev = 0;
int best_play = 0;
for (i = 0; i <= 31; i++)
{
ev = 0.0;
for (j = 0; j <= 9; j++)
ev += draw_score_array[i][j] * win_array[j];
ev /= draw_combinations_array[i];
if (ev > max_ev)
{
max_ev = ev;
best_play = i;
}
}
for (j = 0; j <= 9; j++)
{
tot_combinations[j] += draw_score_array[best_play][j]* weighting_array[best_play];
}
}
int score(card deal[])
{
int straight, flush;
if ((deal[0].s == deal[1].s) && (deal[0].s == deal[2].s) && (deal[0].s == deal[3].s) && (deal[0].s == deal[4].s))
flush = 1;
else
flush = 0;
if ((deal[0].r != deal[1].r) && (deal[1].r != deal[2].r) && (deal[2].r != deal[3].r) && (deal[3].r != deal[4].r))
{
if ((deal[0].r + 4) == deal[4].r)
straight = 1;
else if ((deal[4].r == 12) && (deal[3].r == 3)) // wheel
straight = 1;
else
straight = 0;
}
else
straight = 0;
if ((straight == 1) && (flush == 1))
{
if (deal[0].r == 8)
return 9;
else
return 8;
}
else if (flush == 1)
return 5;
else if (straight == 1)
return 4;
else if ((deal[0].r == deal[3].r) || (deal[1].r == deal[4].r))
return 7;
else if ((deal[0].r == deal[2].r) && (deal[3].r == deal[4].r))
return 6;
else if ((deal[0].r == deal[1].r) && (deal[2].r == deal[4].r))
return 6;
else if ((deal[0].r == deal[2].r) || (deal[1].r == deal[3].r) || (deal[2].r == deal[4].r))
return 3;
else if ((deal[0].r == deal[1].r) && (deal[2].r == deal[3].r))
return 2;
else if ((deal[0].r == deal[1].r) && (deal[3].r == deal[4].r))
return 2;
else if ((deal[1].r == deal[2].r) && (deal[3].r == deal[4].r))
return 2;
else if ((deal[0].r == deal[1].r) && (deal[0].r >= 9))
return 1;
else if ((deal[1].r == deal[2].r) && (deal[1].r >= 9))
return 1;
else if ((deal[2].r == deal[3].r) && (deal[2].r >= 9))
return 1;
else if ((deal[3].r == deal[4].r) && (deal[3].r >= 9))
return 1;
else
return 0;
}