Solved

help finishing code

Posted on 2007-08-03
235 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

LVL 25

Accepted Solution

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

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

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

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

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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a â€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.