Program modified from C Programming book- into C++
Question: What would be the best way to handle ace high and ace low straights...without brute force way of doing it ...
I would like to just modify the code for the straight and royal flush right now- aces are low...
I know I could make other changes but now I just trying to get this part to go...
Any suggestions are appreciated...
Code:#include <iostream> #include <cstdlib> using namespace std; const int NUMCARDS = 5; const int NUMSUITS = 4; const int NUMRANKS = 13; int main() { int numInRank[NUMRANKS]; int numInSuit[NUMSUITS]; bool straight, flush, four, three, royalFlush; int pairs; bool cardExists[NUMRANKS][NUMSUITS]; char ch, rankCh, suitCh; int rank, suit; bool badCard; int cardsRead = 0; int numConsec = 0; royalFlush = false; straight = false; flush = false; four = false; three = false; pairs = 0; for (rank = 0; rank < NUMRANKS; rank++) { numInRank[rank] = 0; for (suit = 0; suit < NUMSUITS; suit++) cardExists[rank][suit] = false; } for (suit = 0; suit < NUMSUITS; suit++) numInSuit[suit] = 0; while (cardsRead < NUMCARDS) { badCard = false; cout << "Enter a card: "; rankCh = getchar(); //cin.get(rankCh); switch (rankCh) { case '0': exit(0); case '2': rank = 1; break; case '3': rank = 2; break; case '4': rank = 3; break; case '5': rank = 4; break; case '6': rank = 5; break; case '7': rank = 6; break; case '8': rank = 7; break; case '9': rank = 8; break; case 't': case 'T': rank = 9; break; case 'j': case 'J': rank = 10; break; case 'q': case 'Q': rank = 11; break; case 'k': case 'K': rank = 12; break; case 'a': case 'A': rank = 0; break; default: badCard = true; } suitCh = getchar(); //cin.get(suitCh); switch (suitCh) { case 'c': case 'C': suit = 0; break; case 'd': case 'D': suit = 1; break; case 'h': case 'H': suit = 2; break; case 's': case 'S': suit = 3; break; default: badCard = true; } //cin.get(ch); //while ((ch = cin.get())!= '\n') while ((ch = getchar())!= '\n') if (ch != ' ') badCard = true; //cin.ignore(); if (badCard) cout << "Bad card; ignored." << endl; else if (cardExists[rank][suit]) cout << "Duplicate card; ignored." << endl; else { numInRank[rank]++; numInSuit[suit]++; cardExists[rank][suit] = true; cardsRead++; } //cin.ignore(); } for (suit = 0; suit < NUMSUITS; suit++) if (numInSuit[suit] == NUMCARDS) flush = true; for (rank = 0; rank < NUMRANKS; rank++) if (numInRank[rank] == 0) numConsec = 0; else { numConsec++; if (numConsec == NUMCARDS) { straight = true; if (rank == NUMRANKS && flush) royalFlush = true; } } for (rank = 0; rank < NUMRANKS; rank++) { if (numInRank[rank] == 4) four = true; if (numInRank[rank] == 3) three = true; if (numInRank[rank] == 2) pairs++; } if (royalFlush) cout << "Royal flush\n\n"; else if (straight && flush) cout << "Straight flush\n\n"; else if (four) cout << "Four of a kind\n\n"; else if (three && pairs == 1) cout << "Full house\n\n"; else if (flush) cout << "Flush\n\n"; else if (straight) cout << "Straight\n\n"; else if (three) cout << "Three of a kind\n\n"; else if (pairs == 2) cout << "Two pairs\n\n"; else if (pairs == 1) cout << "Pair\n\n"; else cout << "High card\n\n"; return 0; }