Poker Hand

I need to write a program that can take a poker hand and store it in a 2-dimensional 4 rows by 13 columns array where the rows represent suits and columns represent denominations. The program has to take 5 cards from a usr and creates an array with 1's in the specified cells, and 0's everywhere else, and then pass the array to determine what type of hand it is (flsuh, straight. etc.)

If someone could explain to me where to start and what steps i need to do to complete this program i would really appreciate it.
Take it step by step.

Declare the array: bool[4][13];

At the start of your program, clear the array (set it all to zero), with a while loop, for example.

Make something to accept user input on numbers/suits and store them. e.g.

1
2
3
4
cout << "Enter suit: "; 
cin >> suit; 
cout << "Enter number: " 
cin >> num; 

Make sure you do not accept bad inputs, like '4' or 'a' or '}'. ;)

You can take the input in lots of ways. In chars, in numbers or in strings.

Then place the cards in the array. Something like if suit == diamonds, int x = 4;
if num == 9, y == 9 and you have x/y positions for the grid and then finally:

array[y][x] = 1;

Then you can pass the array, starting by looking at the pattern for a royal flush, then 4 of a kind etc until high card, making the pass stop at the first thing found.
Ok i got most of it to work. I now have an array with 1's in the correct places within the array. My next question would be how do i go about calculating totals for each row and column. And also how would i determine if two cards are next to eachother like in a straight.

Thanks.
i figured out how to sum the columns and rows, but i still dont know how to determine if its a straight, with five cards being adjacent in value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   d h s c 
1 
2    x
3 x
4 x
5       x
6          x
7
8
9
10
j
q
k      


So you have some situation like this. All you need to do is a quick while loop search. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int isstraight = 0;
int x = 0;
while(x < 13)
{
    int total = 0;
    int y = 0;
    while(y < 4)
    {
        total = total + array[x][y];
        ++y;
    }
    if(total > 1)
    {
         break; //If more than one card is the same number, there is no way we have a straight. 
    }
    else  if(total == 1) //If we found just one number, we may have a straight. 
    {
        ++isstraight; //If this total hits 5, we found a straight. 
        if(isstraight == 5)
        { 
              break; //And then tell the rest of the program you found a straight. 
        }
    }
    else if(total == 0) //If we did not find a card here.
    {
         isstraight = 0; //Then any straight we were accumulating is negated. 
    }
    ++x;
} 


I didn't test this code, but it's something like that you are going to want to do, although perhaps someone else will suggest a more elegant solution, this way will certainly work.
Last edited on
Hey all,

I read this post awhile back and was helping a friend design a 5 Card Poker Hand Evaluator, using a 2 dimensional array. From your few posts, I was able to get pointed in the right direction on how to proceed with writing this code. I just posted on my blog, how I accomplished evaluating a poker hand from a 2 dimensional array using C Sharp and Visual Basic.NET.

For anyone that this may help, please check out my blog post at http://wp.me/pw5Wb-1I.

Thanks to you all for your contributions.

Cheers!
Topic archived. No new replies allowed.