Solved

help finishing code

Posted on 2007-08-03
Medium Priority
236 Views
Well-I have been trying to update my straight function to handle- ace low and ace high straights...I almost have it..it works for normal and ace low but not high..any comments to fix appreciated...

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

//const int SUITS = 4;
//const int RANKS = 13;
//const int N = SUITS * RANKS;
const int NUMCARDS = 5;

// const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
// const string suit[] = {"C","D","H","S"};

bool isStraight(int numInRank[]);
void initialize (int numInRank[], int numInSuit[], bool cardExists[][4]);
void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4]);
int main()
{
int numInRank[13];
int numInSuit[4];

// bool straight, flush, four, three, royalFlush;
// int pairs;

bool cardExists[13][4];

initialize (numInRank, numInSuit, cardExists);

if (isStraight(numInRank))
cout << "is a straight" << endl;
else
cout << "is not a straight" << endl;

return 0;
}

void initialize (int numInRank[], int numInSuit[], bool cardExists[][4])
{
int rank, suit;
for (rank = 0; rank < 13; rank++)
{
numInRank[rank] = 0;
for (suit = 0; suit < 4; suit++)
cardExists[rank][suit] = false;
}

for (suit = 0; suit < 4; suit++)
numInSuit[suit] = 0;
}
void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4])
{

int numConsec = 0;
int rank, suit;
char ch, rankCh, suitCh;

{

bool highflag = false;  //flag to determine if Ace is high or low
cout << "Enter a card: ";

cin.get(rankCh);
switch (toupper(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': rank = 9; break;
case 'J': rank = 10; break;
case 'Q': rank = 11; break;
case 'K': rank = 12; break;
case 'A':
if (highflag == true)
rank = 13;
else
rank = 0;
break;
}

cin.get(suitCh);
switch (toupper(suitCh))
{
case 'C': suit = 0; break;
case 'D': suit = 1; break;
case 'H': suit = 2; break;
case 'S': suit = 3; break;
}

while ((ch = cin.get())!= '\n')
if (ch != ' ')
cout << "Bad card; ignored." << endl;
else if (cardExists[rank][suit])
cout << "Duplicate card; ignored." << endl;
else
{
numInRank[rank]++;
numInSuit[suit]++;
cardExists[rank][suit] = true;
}
}
}

bool isStraight(int numInRank[])
{

int count = 0;

for (int i = 0; i < 13; i++)
{
if (numInRank[i] != 0)
{
count++;
if(i == 13 && numInRank[1] != 0)
count++;
}
else
count = 0;
if (count == 5)
return true;
}
return false;
}
0
Question by:CPlusJavaCSharp
• 3
• 2

LVL 25

Accepted Solution

clockwatcher earned 500 total points
ID: 19629807
Let the ace fill two spots (0 and 13) in your rank array.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

//const int SUITS = 4;
//const int RANKS = 13;
//const int N = SUITS * RANKS;
const int NUMCARDS = 5;

// const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
// const string suit[] = {"C","D","H","S"};

bool isStraight(int numInRank[]);
void initialize (int numInRank[], int numInSuit[], bool cardExists[][4]);
void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4]);
int main()
{
int numInRank[14];
int numInSuit[4];

// bool straight, flush, four, three, royalFlush;
// int pairs;

bool cardExists[13][4];

initialize (numInRank, numInSuit, cardExists);

if (isStraight(numInRank))
cout << "is a straight" << endl;
else
cout << "is not a straight" << endl;

return 0;
}

void initialize (int numInRank[], int numInSuit[], bool cardExists[][4])
{
int rank, suit;
for (rank = 0; rank < 13; rank++)
{
numInRank[rank] = 0;
for (suit = 0; suit < 4; suit++)
cardExists[rank][suit] = false;
}

for (suit = 0; suit < 4; suit++)
numInSuit[suit] = 0;
}
void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4])
{

int numConsec = 0;
int rank, suit;
char ch, rankCh, suitCh;

{

bool highflag = false;  //flag to determine if Ace is high or low
cout << "Enter a card: ";

cin.get(rankCh);
switch (toupper(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': rank = 9; break;
case 'J': rank = 10; break;
case 'Q': rank = 11; break;
case 'K': rank = 12; break;
case 'A': rank = 0; break;
}

cin.get(suitCh);
switch (toupper(suitCh))
{
case 'C': suit = 0; break;
case 'D': suit = 1; break;
case 'H': suit = 2; break;
case 'S': suit = 3; break;
}

while ((ch = cin.get())!= '\n')
if (ch != ' ')
cout << "Bad card; ignored." << endl;
else if (cardExists[rank][suit])
cout << "Duplicate card; ignored." << endl;
else
{
if (rank == 0) { numInRank[13] = 1; }  // ace can be either 0 or 13
numInRank[rank]++;
numInSuit[suit]++;
cardExists[rank][suit] = true;
}
}
}

bool isStraight(int numInRank[])
{

int count = 0;

for (int i = 0; i <= 13; i++)
{
if (numInRank[i] != 0)
{
count++;
if(i == 13 && numInRank[1] != 0)
count++;
}
else
count = 0;
if (count == 5)
return true;
}
return false;
}

0

LVL 25

Expert Comment

ID: 19629829
If you don't want to fill two spots (although in the long run when you get into checking other hands I think you'll find it makes more sense to do that, but if you don't want to) then you can change isStraight to this:

bool isStraight(int numInRank[])
{

int count = 0;

for (int i = 0; i <= 13; i++)
{
if (i < 13) {
if (numInRank[i] != 0) {
count++;
}
else {
count = 0;
}
}
else { // i == 13
if (numInRank[0]) {
count++;
}
}
if (count == 5)
return true;
}
return false;
}
0

LVL 3

Author Comment

ID: 19635157
What do you mean by this:

you don't want to fill two spots (although in the long run when you get into checking other hands I think you'll find it makes more sense to do that, but if you don't want to) then you can change isStraight to this:

like when checking straight flush, etc..
0

LVL 25

Expert Comment

ID: 19635544
It really depends on where you're going with the program.  If you end up needing to compare hands against each other (calculating a hand's strength), personally think it will probably be easier if the ace simply fills two spots in your rank array.   It seems like there'd be less of "is there an ace in the hand" exception checking if the ace was allowed two spots in your rank.  But, it could cause more headaches than it saves.  Really depends on where you're going with the program.
0

LVL 3

Author Comment

ID: 19635771
Yes I will eventually compare hands...I was going to assign a point value for the hand and then the individual cards- to handle tiebreaks...

Like
royal flush = 10
strigh flush = 9

then by hand
Ace = 13
King = 12

Ohh- wait I would problably need to sort to check whether the ace is high or low in the hand if it is a straight...because ace high beats an ace low straight...
0

LVL 53

Expert Comment

ID: 19636788
>> Ohh- wait I would problably need to sort to check whether the ace is high or low in the hand if it is a straight...because ace high beats an ace low straight...

Ordering straights is easy - just watch the value of the highest card (ranges from 5 to ACE).
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll