Buonasera, stavo provando questo programmino dove si ha un mazzo di carte, si mescolano, si da una mano di poker da 5 carte e si verifica se ci sono coppie, tris e altre cose:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
//prototipi
void shuffle(unsigned int deck[][FACES]);
void deal(unsigned int deck[][FACES], unsigned int hand[][2],
char *suit[], char *face[]);
void pair(unsigned int hand[][2], char *suit[], char *face[]);
void threeOfKind(unsigned int hand[][2], char *suit[], char *face[]);
void fourOfKind(unsigned int hand[][2], char *suit[], char *face[]);
void straightHand(unsigned int hand[][2], char *suit[], char *face[]);
void flushHand(unsigned int hand[][2], char *suit[], char *face[]);
int main()
{
char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};\\Array per i semi
char *face[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};\\ Array per i numeri
unsigned int deck[SUITS][FACES];\\Array per il mazzo
unsigned int hand[5][2];\\Array per la mano
for(size_t row = 0; row < SUITS; ++row)
{
for(size_t column = 0; column < FACES; ++column)
{
deck[row][column] = 0;
}
}
srand(time(NULL));
shuffle(deck);\\mescola le carte
deal(deck, hand, suit, face);\\distribuisci la mano
pair(hand, suit, face);\\Verifica le coppie
threeOfKind(hand, suit, face);\\Verifica tris
fourOfKind(hand, suit, face);\\Verifica 4 carte uguali
flushHand(hand, suit, face);\\Verifica colore
straightHand(hand, suit, face);\\Verifica scala
system("PAUSE");
}
\\mescola le carte
void shuffle(unsigned int deck[][FACES])
{
size_t row;
size_t column;
for(unsigned int card = 1; card < CARDS; ++card)
{
do
{
row = rand() % SUITS;
column = rand() % FACES;
} while(deck[row][column] != 0);
deck[row][column] = card;
}
}
\\distribuisci la mano
void deal(unsigned int deck[][FACES], unsigned int hand[][2],
char *suit[], char *face[])
{
unsigned int r = 0;
puts("The hand is:\n");
for(size_t card = 1; card < 6; ++ card){
for(size_t row = 0; row < SUITS; ++row){
for(size_t column = 0; column < FACES; ++column){
if(deck[row][column] == card)
{
hand[r][0] = row;
hand[r][1] = column;
printf("%5s of %-8s\n", face[column], suit[row]);
++r;
}
}
}
}
puts("\n");
}
\\Verifica le coppie
void pair(unsigned int hand[][2], char *suit[], char *face[])
{
unsigned int counter[FACES] = {0};
for(size_t r = 0; r < 5; ++r){
++counter[hand[r][1]];
}
for(size_t p = 0; p < FACES; ++p){
if(counter[p] == 2){
printf("The hand contains a pair of %ss.\n", face[p]);
}
}
}
\\Verifica il tris
void threeOfKind(unsigned int hand[][2], char *suit[], char *face[])
{
unsigned int counter[FACES] = {0};
for(size_t r = 0; r < 5; ++r){
++counter[hand[r][1]];
}
for(size_t t = 0; t < FACES; ++t){
if(counter[t] == 3){
printf("The hand contains three %ss.\n", face[t]);
}
}
}
\\Verifica 4 carte uguali
void fourOfKind(unsigned int hand[][2], char *suit[], char *face[])
{
unsigned int counter[FACES] = {0};
for(size_t r = 0; r < 5; ++r){
++counter[hand[r][1]];
}
for(size_t k = 0; k < FACES; ++k){
if(counter[k] == 4){
printf("The hand contains four %ss.\n", face[k]);
}
}
}
\\Verifica colore
void flushHand(unsigned int hand[][2], char *suit[], char *face[])
{
unsigned int counter[SUITS] = {0};
for(size_t r = 0; r < 5; ++r){
++counter[hand[r][0]];
}
for(size_t f = 0; f < FACES; ++f){
if(counter[f] == 5){
printf("The hand contains a flush %ss.\n", suit[f]);
}
}
}
\\Verifica scala
void straightHand(unsigned int hand[][2], char *suit[], char *face[])
{
unsigned int s[5];
unsigned int temp;
for(size_t r = 0; r < 5; ++r){
s[r] = hand[r][1];
}
for(size_t pass = 1; pass < 5; ++pass){
for(size_t comp = 0; comp < 4; ++comp){
if(s[comp] > s[comp+1])
{
temp = s[comp];
s[comp] = s[comp+1];
s[comp+1] = temp;
}
}
}
if(s[0] == s[1]-1 && s[1] == s[2]-1 && s[2] == s[3]-1 && s[3] == s[4]-1)
{
printf("The hand contains a straight from %s to %s.\n", face[s[0]],
face[s[4]]);
}
}
Però nel momento in cui vado ad eseguirlo la funzione flushHand me lo manda in crash e non riesco a capire perchè.
Qualcuno saprebbe aiutarmi?
Vi ringrazio