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:
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?
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?