Oppure

Loading
27/07/16 22:25
salve, sto cercando di svolgere un' esercizio abbastanza complesso del manuale C Deitel, l'esercizio chiede di modificare un programma mostrato negli esempi, esso dovrebbe estrarre una mano di poker randomicamente e attraverso delle funzioni stabilire se si è in presenza di poker, scala, coppia ecc...
il mio codice è questo:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

void shuffle(int wDeck[][13]);
void deal(const int wDeck[][13], const char *wFace[], const char *wSuit[], bool hand[][13]);
bool pair(bool hand[][13]);
bool two_pairs(bool hand[][13]);
bool tris(bool hand[][13]);
bool poker(bool hand[][13]);
bool color(bool hand[][13]);
bool flush(bool hand[][13]);

int main(){
    const char *suit[4]={"Hearts", "Diamond", "Clubs", "Spades"};
    const char *face[13]={"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    int deck[4][13]={0};
    bool hand[4][13]={false};

    srand(time(NULL));
    shuffle(deck);
    deal(deck, face, suit, hand);
    if(poker(hand)){
        printf("Poker!");
    }else if(tris(hand)){
             printf("Tris!");
        }else if(two_pairs(hand)){
                printf("Two pairs!");
            }else if(pair(hand))
                    printf("\nPair!\n");
    if(color(hand))
        printf("Color!");
    if(flush(hand))
        printf("Flush!");
    return 0;
}

void shuffle(int wDeck[][13]){
    int row, column, card;

    for(card=1; card<=52; card++){
        do{
            row=rand()%4;
            column=rand()%13;
        }while(wDeck[row][column] != 0);
        wDeck[row][column]=card;
    }
}

void deal(const int wDeck[][13], const char *wFace[], const char *wSuit[], bool hand[][13]){
    int card, row, column;

    for(card=1; card<=5; card++){
        for(row=0; row<=3; row++){
            for(column=0; column<=12; column++){
                if(wDeck[row][column]==card){
                    printf("%5s of %-8s%c", wFace[column], wSuit[row], card%2==0 ? '\n' : '\t');
                    hand[row][column]=true;
                }
            }
        }
    }
}

bool pair(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=2 ; column++){
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count!= 2)
            count=0;
    }
    if(count==2)
        r=true;
    return r;
}
bool two_pairs(bool hand[][13]){
    int row, column, acc=0, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=4 ; column++){
        count=0;
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count == 2)
            acc+=count;
    }
    if(acc==4)
        r=true;
    return r;
}
bool tris(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=3 ; column++){
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count!= 3)
            count=0;
    }
    if(count==3)
        r=true;
    return r;
}
bool poker(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=4 ; column++){
        for(row=0; row<=4; row++){
            if(hand[row][column])
                count++;
        }
        if(count!= 4)
            count=0;
    }
    if(count==4)
        r=true;
    return r;
}
bool color(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(row=0; row<=3 && count!=2 ; row++){
        for(column=0; column<=3; column++){
            if(hand[row][column])
                count++;
        }
        if(count!= 5)
            count=0;
    }
    if(count==5)
        r=true;
    return r;
}
bool flush(bool hand[][13]){
    int row, column, v[5], i=0;
    bool r=true;
    void exchange_sort(int v[], int dim);

    for(row=0; row<=3; row++){
        for(column=0; column<=3; column++){
            if(hand[row][column]){
                v[i]=column;
                i++;
            }
        }
    }
    exchange_sort(v, 5);
    for(i=0; i<5 && r==true; i++){
        if(v[i+1]-v[i]!=1)
            r=false;
    }
    return r;
}
void exchange_sort(int v[], int dim){
	int i,j;
	void swap(int *aPtr, int *bPtr);

	for(i=0; i<dim; i++)
		for(j=i+1; j<dim; j++)
			if(v[i]>v[j])
				swap(&v[i], &v[j]);
}
void swap(int *aPtr, int *bPtr){
    int t;
	t = *aPtr;
	*aPtr = *bPtr;
	*bPtr = t;
}

tutto sembra andare bene fino a quando non capita un tris, ma il programma lo riconosce come poker!
A parte l'ottimizzazione e il rischio di differimento indefinito (di cui sono a conoscenza), cosa sbaglio?
30/07/16 7:29
problema risolto, chiedo l'eliminazione del post
30/07/16 8:02
lumo
Sarebbe più utile se condividessi la soluzione, rimarrebbe ai posteri.
aaa
30/07/16 8:14
Postato originariamente da lumo:

Sarebbe più utile se condividessi la soluzione, rimarrebbe ai posteri.


bhe, allora appena posso provvederò subito a mettere la versione senza errori e commentata. :k:
30/07/16 13:12
Come potete vedere ho aggiunto i commenti, l'errore era causato da un ciclo for con condizioni errate.
Spero che sarà utile a futuri apprendisti!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

void shuffle(int wDeck[][13]);
void deal(const int wDeck[][13], const char *wFace[], const char *wSuit[], bool hand[][13]);
/*Prototipi delle funzioni usate per stabilire le combinazioni*/
bool pair(bool hand[][13]);
bool two_pairs(bool hand[][13]);
bool tris(bool hand[][13]);
bool poker(bool hand[][13]);
bool color(bool hand[][13]);
bool flush(bool hand[][13]);

int main(){
    /*Vettori di stringhe contenenti i semi e i valori delle carte*/
    const char *suit[4]={"Hearts", "Diamond", "Clubs", "Spades"};
    const char *face[13]={"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    int deck[4][13]={0};    /*Matrice usata per mescolare le carte*/
    bool hand[4][13]={false};   /*Matrice usata per copiare la mano di 5 carte*/

    srand(time(NULL));
    shuffle(deck);
    deal(deck, face, suit, hand);
    /*If annidati e accatastati per stabilire le combinazioni create*/
    if(poker(hand)){
        printf("Poker!");
    }else if(tris(hand)){
             printf("Tris!");
        }else if(two_pairs(hand)){
                printf("Two pairs!");
            }else if(pair(hand))
                    printf("\nPair!\n");
    if(color(hand))
        printf("Color!");
    if(flush(hand))
        printf("Flush!");
    return 0;
}
/*NB questa funzione non è ben ottimizzata, si potrebbe riscontrare un differimento indefinito*/
void shuffle(int wDeck[][13]){
    int row, column, card;

    for(card=1; card<=52; card++){
        do{
            row=rand()%4;
            column=rand()%13;
        }while(wDeck[row][column] != 0);
        wDeck[row][column]=card;
    }
}
/*funzione di distribuzione modificata per distribuire una sola mano e copiare le carte in una matrice booleana*/
void deal(const int wDeck[][13], const char *wFace[], const char *wSuit[], bool hand[][13]){
    int card, row, column;

    for(card=1; card<=5; card++){
        for(row=0; row<=3; row++){
            for(column=0; column<=12; column++){
                if(wDeck[row][column]==card){
                    printf("%5s of %-8s%c", wFace[column], wSuit[row], card%2==0 ? '\n' : '\t');
                    hand[row][column]=true;
                }
            }
        }
    }
}

bool pair(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=2 ; column++){  /*I due cicli "scansionano" la matrice in cerca di due valori uguali*/
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count!= 2)       /*se nella colonna attuale non trova una coppia azzera il contatore*/
            count=0;
    }
    if(count==2)
        r=true;
    return r;
}
bool two_pairs(bool hand[][13]){
    int row, column, acc=0, count=0;
    bool r=false;

    for(column=0; column<=12 && acc!=4 ; column++){ /*è stato aggiunto un accumulatore per sommare le coppie*/
        count=0;
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count == 2) /*in presenza di una coppia il valore viene sommato all'accumulatore*/
            acc+=count;
    }
    if(acc==4)
        r=true;
    return r;
}
/*per tris(), poker() e color() ho usato lo stesso concetto delle precedenti funzioni*/
bool tris(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && count!=3 ; column++){
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count!= 3)
            count=0;
    }
    if(count==3)
        r=true;
    return r;
}
bool poker(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(column=0; column<=12 && r!=true ; column++){
        count=0;
        for(row=0; row<=3; row++){
            if(hand[row][column])
                count++;
        }
        if(count==4)
            r=true;
    }
    return r;
}
bool color(bool hand[][13]){
    int row, column, count=0;
    bool r=false;

    for(row=0; row<=3 && r!=true ; row++){
        count=0;
        for(column=0; column<=12; column++){
            if(hand[row][column])
                count++;
        }
        if(count== 5)
            r=true;
    }
    return r;
}

bool flush(bool hand[][13]){
    int row, column, v[5], i=0;
    bool r=true;
    void exchange_sort(int v[], int dim);

    for(row=0; row<=3; row++){              /*i cicli sono stati invertiti poichè la scala si basa sui valori e non sul seme*/
        for(column=0; column<=3; column++){
            if(hand[row][column]){
                v[i]=column;        /*in presenza di una carta si trasferisce il valore in un vettore di interi*/
                i++;
            }
        }
    }
    exchange_sort(v, 5);            /*il vettore viene riordinato per rendere possibile verificare se siamo in presenza di valori consecutivi*/
    for(i=0; i<5 && r==true; i++){
        if(v[i+1]-v[i]!=1)              /*if per trovare i valori non consecutivi*/
            r=false;                    /*se la condizione è rispettata viene interrotto il ciclo for e ritornerà false*/
    }
    return r;
}
void exchange_sort(int v[], int dim){
	int i,j;
	void swap(int *aPtr, int *bPtr);

	for(i=0; i<dim; i++)
		for(j=i+1; j<dim; j++)
			if(v[i]>v[j])
				swap(&v[i], &v[j]);
}
void swap(int *aPtr, int *bPtr){
    int t;
	t = *aPtr;
	*aPtr = *bPtr;
	*bPtr = t;
}

Ps: Se avete idee su come migliorare il codice delle funzioni per le combinazioni scrivete pure :)