so
writing this one with C ( as ANSI as possible )
need advice/tips/criticism
hopefully all the above
don't hold back, I'll read em all
the idea is that one day there will be ui/gui
user will choose:
table size
what hole cards he has
and
possibly
flop
turn
river
the software will then make about 10'000 rounds
and then give a percentage of wins
also,
pay attention to the random number generator
time-function will give a new srand-seed only once per second
not sure if this is random enough
the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct kortti /* a card */
{
int maa; /* suit 0-3 */
int arvo; /* value 0-12 */
} KORTTI;
typedef struct kasi /* a holdem hand(2 cards) .. not yet in use */
{
KORTTI a;
KORTTI b;
} KASI;
typedef struct ll /* linklist for the deck of cards */
{
KORTTI item;
struct ll *seuraava;
} LL;
LL * luo_ll(void); /* create linklist eg the deck */
KORTTI * arvokortti(LL **linklist); /* pick a random card from from created by luo_ll()*/
int tuhoakortti(KORTTI *kortan,LL **linklist); /* destroys a card from the deck */
void virhe(int status); /* for serious failures and program shutdown */
void tulosta_kortti(int mode,int maa,int arvo); /* given mode(0 or 1), suit and value, will print the card in an understandable way */
float calc_odds(KORTTI *kortit,int k); /*args: player1's cards,[flop,turn,river] */
int main(void)
{
int k = 3;
KORTTI *kortit = malloc(k*sizeof(KORTTI));
(*kortit).maa=0;
(*kortit).arvo=0;
(*(kortit+1)).maa=1;
(*(kortit+1)).arvo=0;
(*(kortit+2)).maa = 2;
(*(kortit+2)).arvo = 0;
float res = calc_odds(kortit,k);
printf("float res = %f\n",res);
return EXIT_SUCCESS;
}
float calc_odds(KORTTI *kortit,int k)
{
srand(time(NULL));
LL *ptr = 0;
ptr = luo_ll();
if(ptr == 0)
virhe(2);
int i,t;
for(i=0;i<k;++i)
{
t = tuhoakortti((kortit+i),&ptr);
printf("destroia tulos: %d\n",t);
}
/* yllä olevan tuhoamisen voisi yhdistää korttien siirtoon pakasta */
/* luotavaan tietorakenteeseen eli kortit siis "siirretään" */
/* luo tietorakenne pelaajien käsille ja flop/turn/river */
/* jaa kortit muille pelaajilla ja flop/turn/river */
ptr = 0;
return 3.14;
}
int tuhoakortti(KORTTI *kortan,LL **linklist)
{
int i;
LL *edellinen = *linklist;
*linklist = (**linklist).seuraava;
for(i=0;;++i)
{
if( (**linklist).item.maa == (*kortan).maa && (**linklist).item.arvo == (*kortan).arvo )
break;
else
{
edellinen = *linklist;
*linklist = (**linklist).seuraava;
}
}
(*edellinen).seuraava = (**linklist).seuraava;
free(*linklist);
return EXIT_SUCCESS;
}
KORTTI * arvokortti(LL **linklist)
{
int rint = ( rand() % 1000 ) + 100;
int i;
LL *edellinen;
KORTTI *newcard = malloc(sizeof(KORTTI));
if(newcard==NULL)
virhe(59);
for(i=0;i<rint;++i)
{
edellinen = *linklist;
*linklist = (**linklist).seuraava;
}
(*newcard).maa = (**linklist).item.maa;
(*newcard).arvo = (**linklist).item.arvo;
(*edellinen).seuraava = (**linklist).seuraava;
free(*linklist);
return newcard;
}
LL * luo_ll(void)
{
LL *eka = malloc(sizeof(LL));
if(eka == NULL)
virhe(59);
LL *current = NULL;
current = eka;
(*eka).item.maa = 0;
(*eka).item.arvo = 0;
int i;
int maa=0;
int arvo=1;
for(i=0;i<51;++i)
{
LL *new = malloc(sizeof(LL));
if(new==NULL)
virhe(59);
(*new).item.maa = maa;
(*new).item.arvo = arvo;
current->seuraava = new;
current = new;
if(arvo<12)
++arvo;
else
{
arvo=0;
maa+=1;
}
}
(*current).seuraava = eka;
return eka;
}
void virhe(int status)
{
if(status==59)
printf("malloc problem\n");
if(status==2)
printf("linklist creation failure\n");
exit(1);
}
void tulosta_kortti(int mode,int maa,int arvo) /* mode 0 = short, 1=long */
{
if(arvo>0&&arvo<10)
{
printf("%2d",arvo+1);
}
switch(arvo)
{
case 0:
printf(" A");
break;
case 10:
printf(" J");
break;
case 11:
printf(" Q");
break;
case 12:
printf(" K");
break;
}
switch(maa)
{
case 0:
if(mode==0)
printf("H");
else
printf(" of Hearts");
break;
case 1:
if(mode==0)
printf("S");
else
printf(" of Spades");
break;
case 2:
if(mode==0)
printf("D");
else
printf(" of Diamonds");
break;
case 3:
if(mode==0)
printf("C");
else
printf(" of Clubs");
break;
}
}