I have a program evaluating a straight hand in poker: Here is my code: -
cin.get(rankCh);
-
switch (toupper(rankCh))
-
{
-
case '-1': exit(0);
-
case '?': rank = 0; break;
-
case 'A': rank = 1; break;
-
case '2': rank = 2; break;
-
case '3': rank = 3; break;
-
case '4': rank = 4; break;
-
case '5': rank = 5; break;
-
case '6': rank = 6; break;
-
case '7': rank = 7; break;
-
case '8': rank = 8; break;
-
case '9': rank = 9; break;
-
case 'T': rank = 10; break;
-
case 'J': rank = 11; break;
-
case 'Q': rank = 12; break;
-
case 'K': rank = 13; break;
-
}
-
bool isStraight(int numInRank[]){
-
int count = 0;
-
for(int i = 1; i < 14; i++){
-
if(numInRank[i] != 0){
-
count++;
-
if(i == 13 && numInRank[1] != 0) count++;
-
}else{
-
count = 0;
-
}
-
if(count == 5) return true;
-
}
-
return false;
-
}
-
I would like to modify the above code to handle A high and A low straights- I sized my array to 14 instead of 13 to hold both...
Any suggestions...
8 1456
I suggest you inmplement a flag that derternmines whether the ace is high or low. In your switch: -
case 'A':
-
if (flag == HIGH)
-
{
-
rank = 13;
-
}
-
else
-
{
-
rank =1;
-
}
-
break;
-
You would do this for each case in your switch.
OK
When I think flag I think boolean...
Not sure what to set HIGH too...then I would have to modify function...
Here is what I fixed... - int numInRank[13];
-
-
bool flag = true; //flag to determine if Ace is high or low
-
-
cin.get(rankCh);
-
switch (toupper(rankCh))
-
{
-
case '-1': exit(0);
-
case 'A':
-
if (flag == HIGH)
-
rank = 13;
-
else
-
rank = 0;
-
break;
-
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;
-
}
-
-
-
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;
-
}
I suggest you to initialize the array to 14.. because you have 14 elements
case 'A':
if (flag == HIGH)
rank = 13;
else
rank = 0;
break;
Yes. Assuming HIGH is true. Personally, I would: -
case 'A':
-
if (HighFlag == true)
-
rank = 13;
-
else
-
rank = 0;
-
break;
-
Here I get the high part in the flag name.
Well almost- I created a test program- works for all straights except ace high..
I have to use the flag modification- just not sure how to modify my- I have to set the flag now somewhere in function not sure where... - #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[]);
-
-
int main()
-
{
-
int numInRank[13];
-
int numInSuit[4];
-
-
bool straight, flush, four, three, royalFlush;
-
int pairs;
-
-
bool cardExists[13][4];
-
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 < 13; rank++)
-
{
-
numInRank[rank] = 0;
-
for (suit = 0; suit < 4; suit++)
-
cardExists[rank][suit] = false;
-
}
-
-
for (suit = 0; suit < 4; suit++)
-
numInSuit[suit] = 0;
-
-
while (cardsRead < NUMCARDS)
-
{
-
-
badCard = false;
-
bool highflag = false; //flag to determine if Ace is high or low
-
cout << "Enter a card: ";
-
-
//rankCh = getchar();
-
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;
-
case 'A':
-
if (highflag == true)
-
rank = 13;
-
else
-
rank = 0;
-
break;
-
default: badCard = true;
-
}
-
//suitCh = getchar();
-
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;
-
default: badCard = true;
-
}
-
-
while ((ch = cin.get())!= '\n')
-
//while ((ch = getchar())!= '\n')
-
if (ch != ' ')
-
badCard = true;
-
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++;
-
}
-
}
-
-
if (isStraight(numInRank))
-
cout << "is a straight" << endl;
-
else
-
cout << "is not a straight" << endl;
-
-
return 0;
-
}
-
-
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;
-
}
You haven't written functions yet. Your logic is all in main().
Consider main() to have a menu.
Make a menu choice and switch on that choice to a function. When the function returns ask for another menu choice.
Try to get your logic partitioned into functional blocks.
Well I think there is a miscommunication here- I added functions but I don't want the user to choose Ace high or low- I want the function isStraight to handle it when I type in the values to test...
I will add more functions/clean code up once I get the isStraight function to work correctly... - #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];
-
int cardsRead = 0;
-
-
initialize (numInRank, numInSuit, cardExists);
-
checkCards(cardsRead, 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])
-
{
-
bool badCard;
-
-
int numConsec = 0;
-
int rank, suit;
-
char ch, rankCh, suitCh;
-
-
while (cardsRead < NUMCARDS)
-
{
-
-
badCard = false;
-
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;
-
default: badCard = true;
-
}
-
-
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;
-
default: badCard = true;
-
}
-
-
while ((ch = cin.get())!= '\n')
-
if (ch != ' ')
-
badCard = true;
-
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++;
-
}
-
}
-
}
-
-
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;
-
}
isStraight() will need to know about the flag.
Pass the flag in also.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Japcuh |
last post by:
How do you write self modifying code in Java?
Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
..0.
...0
000
|
by: qwweeeit |
last post by:
Hi all,
when I was young I programmed in an interpreted language that allowed
to modify itself.
Also Python can (writing and running a module, in-line):
fNew =open("newModule.py",'w')
lNew=...
|
by: Yavuz Bogazci |
last post by:
Hi,
i am reading the LastAccessTime, LastWriteTime and DateofCreation of a file.
The second and third are working well, but each time i want to read the
LastAccessTime, its set to "now". How can...
|
by: Robin Becker |
last post by:
When young I was warned repeatedly by more knowledgeable folk that self
modifying code was dangerous.
Is the following idiom dangerous or unpythonic?
def func(a):
global func, data
data =...
|
by: GaryB |
last post by:
Hi Guys,
I've been battling with this one for hours - I hope that you can help
me!
My code modifies the <aon a page, from a standard document link into
a link with a tailored onclick event.
...
|
by: AES |
last post by:
Can an HTML web page dynamically modify its own code?
(without resort to Java or any other non-HTML complexities)
That is, is there any provision in HTML such that a single
"Next" button on a...
|
by: subramanian100in |
last post by:
The standard allows that we can copy strings onto arg, arg, etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?
|
by: allpervasive |
last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be...
|
by: Arnshea |
last post by:
(apologies for the crosspost)
I'm working with an MFC based COM object. From C# I'd like to be able
to call a method on the COM object that takes a string array and
modifies the contents.
Is...
|
by: IUnknown |
last post by:
Ok, we are all aware of the situation where modifying the folder
structure (adding files, folders, deleting files, etc) will result in
ASP.NET triggering a recompilation/restart of the application....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |