has anyone come accross any good poker games created in c which come with the code
mainly looking for texas holdem but any kind is okay im not fussy i just want to play one and see how it is coded.
Printable View
has anyone come accross any good poker games created in c which come with the code
mainly looking for texas holdem but any kind is okay im not fussy i just want to play one and see how it is coded.
What exactly are you having problems with? It shouldn't be that hard to make one. Are you trying to create the AI for one? Or do you just want one that deals out some cards to some players and they each take a turn, or what?
That looks like a fairly good start.Code:create a deck of 52 different cards
shuffle deck
for number of cards per hand
for each player
deal one card
for each round
for each player
switch round
betting round:
display amount to bet to stay in, if any
ask amount to bet
get bet
card exchanging
ask which cards to get rid of
get rid of those cards
deal that many new cards
calculate winner
give the bets to the winner
Quzah.
i know most of that but im not too sure of the ai and them giving up the bad cards having been not very specific in the earlier post.
also the other big bit would be is there any easy way to calculate the winner coz first you got to tell what cards the computer has then work out their best hand every time to see if its worth a big or small bet or just to fold
I guess a good start would be to hard code what the winning hands are. Hands that are better than others have a different value
(im sure that doesnt make sense to anybody but me)
How about something like this?
Seems to work anyway :)Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char *cardname(int num)
{
static char buf[10];
char suits[] = "HCDS";
char *cards[] = { " 2", " 3", " 4", " 5", " 6", " 7", " 8",
" 9", "10", " J", " Q", " K", " A"};
sprintf(buf, "%s%c", cards[num%13], suits[num/13]);
return buf;
}
char *cardval(int num)
{
static char *cards[] = { "2", "3", "4", "5", "6", "7", "8",
"9", "10", "J", "Q", "K", "A"};
return cards[num%13];
}
int is_x_of_a_kind(int *vals, int x, int *val, int except)
{
int i, ctr;
for(ctr = 0, *val = vals[0], i = 1;i < 5;++i)
{
if(vals[i] == except)
continue;
if(vals[i] == *val)
ctr++;
else
{
if(ctr == x-1)
return 1;
ctr = 0;
*val = vals[i];
}
}
return ctr == x-1;
}
int is_four_of_a_kind(int *vals, int *val)
{
return is_x_of_a_kind(vals, 4, val, -1);
}
int is_three_of_a_kind(int *vals, int *val)
{
return is_x_of_a_kind(vals, 3, val, -1);
}
int is_pair(int *vals, int *val)
{
return is_x_of_a_kind(vals, 2, val, -1);
}
int is_two_pair(int *vals, int *val1, int *val2)
{
if(!is_x_of_a_kind(vals, 2, val1, -1))
return 0;
if(!is_x_of_a_kind(vals, 2, val2, *val1))
return 0;
return 1;
}
int is_full_house(int *vals, int *val1, int *val2)
{
return is_three_of_a_kind(vals, val1) && is_pair(vals, val2);
}
int is_flush(int *vals, int *suits, int *val)
{
int i;
for(i = 0;i < 4;++i)
if(suits[i] != suits[i+1])
return 0;
*val = vals[4];
return 1;
}
int is_straight(int *vals, int *val)
{
int i;
for(i = 0;i < 4;++i)
if(vals[i]+1 != vals[i+1])
return 0;
*val = vals[4];
return 1;
}
int is_straight_flush(int *vals, int *suits, int *val)
{
return is_flush(vals, suits, val) && is_straight(vals, val);
}
void print_hand_rank(int *hand)
{
int i, j, temp;
int vals[5], suits[5];
int highcard1, highcard2;
for(i = 0;i < 5;++i)
{
vals[i] = hand[i]%13;
suits[i] = hand[i]/13;
}
for(i = 0;i < 5;++i)
for(j = i+1;j < 5;++j)
if(vals[i] > vals[j])
{
temp = vals[i];
vals[i] = vals[j];
vals[j] = temp;
temp = hand[i];
hand[i] = hand[j];
hand[j] = temp;
temp = suits[i];
suits[i] = suits[j];
suits[j] = temp;
}
if(is_straight_flush(vals, suits, &highcard1))
printf("Straight flush - %s high\n", cardval(highcard1));
else if(is_four_of_a_kind(vals, &highcard1))
printf("4 of a kind - %s high\n", cardval(highcard1));
else if(is_full_house(vals, &highcard1, &highcard2))
{
char buf[5];
strcpy(buf, cardval(highcard1));
printf("Full house - %s's over %s's\n", buf, cardval(highcard2));
}
else if(is_flush(vals, suits, &highcard1))
printf("Flush - %s high\n", cardval(highcard1));
else if(is_straight(vals, &highcard1))
printf("Straight - %s high\n", cardval(highcard1));
else if(is_three_of_a_kind(vals, &highcard1))
printf("3 of a kind - %s high\n", cardval(highcard1));
else if(is_two_pair(vals, &highcard1, &highcard2))
{
char buf[5];
strcpy(buf, cardval(highcard1));
printf("2 pair - %s's and %s's\n", buf, cardval(highcard2));
}
else if(is_pair(vals, &highcard1))
printf("Pair of %s's\n", cardval(highcard1));
else
printf("%s high\n", cardval(vals[4]));
}
int main(void)
{
int deck[52], hand[5];
int i, j, card, temp;
srand(time(NULL));
for(i = 0;i < 52;++i)
deck[i] = i;
for(i = 0;i < 10;++i)
for(j = 0;j < 52;++j)
{
card = rand()%52;
temp = deck[j];
deck[j] = deck[card];
deck[card] = temp;
}
for(i = 0;i < 5;++i)
hand[i] = deck[i];
for(i = 0;i < 5;++i)
printf("%s ", cardname(hand[i]));
putchar('\n');
print_hand_rank(hand);
return 0;
Code:itsme@itsme:~/C$ ./poker
5S 4D 8S AS 5H
Pair of 5's
itsme@itsme:~/C$ ./poker
6C 7S 9D JD 10H
J high
itsme@itsme:~/C$ ./poker
JH 6S 5H 6D JC
2 pair - 6's and J's
It just goes to show if you slack off long enough, someone will do everything for you.
Quzah.
Just the hand calculation part. And it's not quite good enough for comparing 2 hands absolutely, but it's improvable. No decision making or anything in this code.Quote:
Originally Posted by quzah