How to find Royal Flush on Poker C++

hello guys, this is my first post on cpluplus forum so i feel a little bit awkward. anyways..
i actually making a poker game on c++. but like my tittle above, i don't know how to find the royal flush. anybody can help me? thanks in advance ^^
oh and this is my program btw
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <time.h>

using namespace std;
int point[52];
int a[52];

int scoring(int a, int b, int c, int d, int e)
{
    int R[5];
    int S[5], x[5];

    x[0]=a; x[1]=b; x[2]=c;  x[3]=d;  x[4]=e;

    for (int o=0; o<5; o++)
    {
        R[o] = x[o]%13;
        S[o] = x[o]/13;
    }

    // Sort the R array
    bool swapped = false;
    do
    {
        // 1 pass of the bubble sort
        swapped = false;
        for(int o=0; o<4; o++)
        {
            if(R[o] > R[o+1])
            {
                int temp = R[o];
                R[o] = R[o+1];
                R[o+1] = temp;
                swapped = true;
            }
        }
    }
    while(swapped == true);

    int scoring= 0;
    // Check for a straight:
    if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
    {
        cout << "You got a Straight" << endl;
        scoring=(scoring+19);
    }
    else
    {
        scoring=(scoring+0);
    }

    // Check first 5 cards for any pair
    if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
    {
        // check for twopair
        if(R[0]==R[1]&&R[2]==R[3]||R[1]==R[2]&&R[3]==R[4]||R[0]==R[1]&&R[3]==R[4])
        {
            cout << "You got a Twopair" << endl;
            scoring=(scoring+17);
        }
        // check for 3 of a kind
        if(R[0] == R[1] == R[2] || R[1] == R[2] == R[3] || R[2] == R[3] == R[4])
        {
            cout << "You got a 3 of a kind" << endl;
            scoring=(scoring+18);
        }
        // Check for 4 of a kind
        if(R[0] == R[1] == R[2] == R[3] || R[1] == R[2] == R[3] == R[4])
        {
            cout << "You got a 4 of a kind" << endl;
            scoring=(scoring+22);
        }
        else
        {
            cout << "You got a Pair" << endl;
            scoring=(scoring+16);
        }
    }
    else
    {
        scoring=(scoring+0);
    }

    // Check for a flush (all the same suit)
    if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
    {
        cout << "You got a Flush" << endl;
        scoring=(scoring+20);
    }
    else
    {
        scoring=(scoring+0);
    }

    // Check for straight flush
    if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
    {
        if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
        {
            cout << "You got a Straight Flush" << endl;
            scoring=(scoring+23);
        }
        else
        {
            scoring=(scoring+0);
        }
    }

    // check for royal flush

    // Check for full house
    if(R[0] == R[1] == R[2] && R[3] == R[4] || R[0] == R[1] && R[2] == R[3] == R[4])
    {
        cout << "You got a Full House" << endl;
        scoring = (scoring + 21);
    }
    else
    {
        scoring=scoring+0;
    }

    //check for the highest score
 
}

int main()
{
    srand(time(0));
    int deck[52];
    int i;
    string suitnames[4]={"spades", "hearts", "clubs", "diamonds"};
    string ranknames[13]={"ace", "king", "queen", "jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

     // create a new deck, with cards in order, but unique
    for(i=0; i<52; i++)
    {
        deck[i] = i;
    }

    // shuffle the deck:
    for(i=0; i<52; i++)
    {
        // generate a random index to swap with the card at index i.
        int j = rand() % 52;
        int temp = deck[i];
        deck[i] = deck[j];
        deck[j] = temp;
    }

    // print all the cards
    for(i=0; i<52; i++)
    {
        int suitnumber = deck[i] / 13; // 0 - 3
        int rank = deck[i] % 13;
        cout << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
    }
    cout << endl;


    for(i=0; i<52; i++)
    {
        int temp = deck[i];
        deck[i]=a[i];
        a[i]=temp;
    }


    int f;
    cout << "your card :"<< endl;
    f=0;
    while(f<20)
    {
        cout << f << ". ";
        int suitnumber = a[f] / 13; // 0 - 3
        int rank = a[f] % 13;
        cout << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
        f=f+4;
    }

    int n;
    cout << "do you want to replace your card ? [type 1 for yes, 0 for no]" << endl;
    cin >> n;
    switch (n)
    {
        case 1:
        cout << "which card do you want to replace?[insert the number]";
        int o;
        cin >> o;
        int suitnumber = a[o] / 13; // 0 - 3
        int rank = a[o] % 13;
        cout << "you want to replace " << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
        int temp = a[o];
        a[o] = a[20];
        a[20] = temp;
        cout <<"your card now:" << endl;
        o=0;
        while(o<20)
        {
            int suitnumber = a[o] / 13; // 0 - 3
            int rank = a[o] % 13;
            cout << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
            o=o+4;
        }
    }

    //sorty(a[0], a[4], a[8], a[12], a[16]);
    cout << "your score : " << scoring(a[0], a[4], a[8], a[12], a[16]) << endl << endl;

    //sorty(a[1], a[5], a[9], a[13], a[17]);
    cout << "score P2 :"<< scoring(a[1], a[5], a[9], a[13], a[17]) << endl << endl;

    //sorty(a[2], a[6], a[10], a[14], a[18]);
    cout << "score P3 :"<< scoring(a[2], a[6], a[10], a[14], a[18]) << endl << endl;

    //sorty(a[3], a[7], a[11], a[15], a[19]);
    cout << "score P4 :"<< scoring(a[3], a[7], a[11], a[15], a[19]) << endl << endl;

    return 0;
}
1) check if all cards are of the same suit.
2) if so, sort the cards and check whether they are of consecutive rank.
3) if so, it's a royal flush when the last card is an ace.
well i don't really understand about the consecutive rank.
i've been search on google, and this is the way i make it (btw sorry if my language messed up, because i'm in process on learning it tho haha ^^v)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   // check for royal flush
    if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
    {
        if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
        {
            for (int o=0; o<5; o++)
            {
                int acerank = 0;
                int currentCardRank = x[o]%13;
                if(currentCardRank == acerank)
                {
                    cout << "You got a Royal Flush" << endl;
                    scoring=(scoring+24);
                }
            }
        }


it's hard to prove it anyway, because i'm using random number and to find royal flush it's almost impossible haha :(
it's hard to prove it anyway, because i'm using random number and to find royal flush it's almost impossible haha :(


So cheat! When you're testing conditions like this you're going to need to cheat a little to make sure they perform as expected.

Wherever you check your hand, hard-code your cards so that you have a royal flush.
Last edited on
It looks like your twos are the highest cards. Just for my post, consider the order: 2, 3, 4, ... 10, jack, queen, king, ace.

Sort the hand. If the first card is a 10 and the last card is 10 + 4 (which must be an ace of the same suit), then this is a royal flush.

Last edited on
thanks for the advice guys ^^
btw how to "hard-code" the cards?
and now i confused about the two pair and 3 of a kind, because when i run the program, i got 3 of a kind, but it shown that i got a pair instead.
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
30
31
    // Check first 5 cards for any pair
    if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
    {
        // check for twopair
        if(R[0]==R[1]&&R[2]==R[3]||R[1]==R[2]&&R[3]==R[4]||R[0]==R[1]&&R[3]==R[4])
        {
            cout << "You got a Twopair" << endl;
            scoring=(scoring+17);
        }
        // check for 3 of a kind
        if(R[0] == R[1] == R[2] || R[1] == R[2] == R[3] || R[2] == R[3] == R[4])
        {
            cout << "You got a 3 of a kind" << endl;
            scoring=(scoring+18);
        }
        // Check for 4 of a kind
        if(R[0] == R[1] == R[2] == R[3] || R[1] == R[2] == R[3] == R[4])
        {
            cout << "You got a 4 of a kind" << endl;
            scoring=(scoring+22);
        }
        else
        {
            cout << "You got a Pair" << endl;
            scoring=(scoring+16);
        }
    }
    else
    {
        scoring=(scoring+0);
    }


i'm not really sure with my code above. do i have to use else if rather than if?
btw how to "hard-code" the cards?
At line 209, call scoring() with 5 constants (scoring(0,1,2,3,4);).

These conditions don't work. For every comparison you need two operands:
1
2
3
if (x == y && x == z)

//Not if ( x == y == z) 


Figuring out pairs/three/four is the most complicated part, and you may want to consider doing something other than an if statement.
i think i've figured this pairs thing out. i try use this code below
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
30
31
32
33
34
    // Check first 5 cards for any pair
    if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
    {
        // check for 3 of a kind
        if(R[0] == R[1] == R[2] || R[1] == R[2] == R[3] || R[2] == R[3] == R[4])
        {
            // Check for 4 of a kind
            if(R[0] == R[1] == R[2] == R[3] || R[1] == R[2] == R[3] == R[4])
            {
                cout << "You got a 4 of a kind" << endl;
                scoring=(scoring+22);
            }
            else
            {
                cout << "You got a 3 of a kind" << endl;
                scoring=(scoring+18);
            }
        }
        // check for twopair
        else if(R[0]==R[1]&&R[2]==R[3]||R[1]==R[2]&&R[3]==R[4]||R[0]==R[1]&&R[3]==R[4])
        {
            cout << "You got a Twopair" << endl;
            scoring=(scoring+17);
        }
        else
        {
            cout << "You got a Pair" << endl;
            scoring=(scoring+16);
        }
    }
    else
    {
        scoring=(scoring+0);
    }

what do you think guys? i don't know if it's true or not, but it works when i run the program..
It looks like your twos are the highest cards. Just for my post, consider the order: 2, 3, 4, ... 10, jack, queen, king, ace.


i think this is the hardest part of making this game tho, that's why i try to figure it latter haha :p
anyway.. i made this code before but i don't know, it just can't work out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int score(int i)
{
    int  j, score;
    int ten=15;
    for (j=0; j<13; j++)
    {
        for(int k=j; k<52; k+=13)
        {
            point[k]=ten;
        }
        ten=ten-1;
    }
    score=point[i];

    return score;
}

the idea is make a new array to insert the point on every card. so, the ace (of all the suits) will got 15, king will got 14, and so on..
These conditions don't work. For every comparison you need two operands:

1
2
3
if (x == y && x == z)

//Not if ( x == y == z)  


aah i see.. i try to change this one
if(R[0] == R[1] == R[2] && R[3] == R[4] || R[0] == R[1] && R[2] == R[3] == R[4])

into this
if((R[0]==R[1]&&R[0]==R[2]&&R[3]==R[4])||(R[0]==R[1]&&R[2]==R[3]&&R[2]==R[4]))

hahaha thanks for the advice ^^
Last semester we made a poker program. This is a function of a class, and it's in Java, but I think helpful. First, a dummy hand is made from the real hand. The suits are taken off of the dummy hand, and it is sorted. Each card of the hand is looked at and compared to the next card, and then counts the matches. If there are matches, and the card isn't a match, the store the initial count and keep going. In the end, the two 'kind' variables store unique data depending upon the kinds.


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
30
31
32
33
34
35
  private int getKind(){
   
    int [] scaleValue = new int[SIZE_HAND];
    
    // Make all cards have the same card value ( every jack is equal to 10 now)
    for (int i = 0; i < SIZE_HAND; i++){
      scaleValue[i] = hand[i] % 13;
    }
    
    // This new array needs to be sorted: the array might look like this: 1,3,4,4,5
    scaleValue = sort(scaleValue);    
      
    int kind01 = 0, kind02 = 0;
    for (int i = 0; i < SIZE_HAND - 1; i ++){
      if (scaleValue[i] == scaleValue[i+1]){   // If the card = the next card
        kind02++;  // Add one to the counter
      }
      else if (kind02 > 0){  // Otherwise, if the counter counted something
        if (kind01 > 0) break; // And it was the first time: without this 1,1,2,2,3 would come back wrong
        kind01 = kind02;     // Store the count
        kind02 = 0;          // Set the count to 0
      }
    }
    
    int finalKind;
    
    if (kind01 == 3 || kind02 == 3)  finalKind = 5; // Four of a Kind
    else if (kind01 + kind02 == 3) finalKind = 4; // Full House
    else if (kind01 == 2 || kind02 == 2) finalKind = 3; // Three of a Kind
    else if (kind01 + kind02 == 2) finalKind = 2; // Two Pair
    else if (kind01 + kind02 == 1) finalKind = 1; // Pair
    else finalKind = 0; // Nothin'
    
    return finalKind;
  } 
Last edited on
wow i'll get java in the next semester tho but i think it's cool to learn it as soon as possible haha :p

btw i made it this way
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
30
31
32
33
34
35
36
37
38
39
40
    // Check first 5 cards for any pair
    if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
    {
        // check for 3 of a kind
        if((R[0]==R[1]&&R[0]==R[2])||(R[1]==R[2]&&R[1]==R[3])||(R[2]==R[3]&&R[2]==R[4]))
        {
            // Check for 4 of a kind
            if((R[0]==R[1]&&R[0]==R[2]&&R[0]==R[3])||(R[1]==R[2]&&R[1]==R[3]&&R[1]==R[4]))
            {
                cout << "You got a 4 of a kind" << endl;
                scoring=(scoring+22);
            }
            // Check for full house
            else if((R[0]==R[1]&&R[0]==R[2]&&R[3]==R[4])||(R[0]==R[1]&&R[2]==R[3]&&R[2]==R[4]))
            {
                cout << "You got a Full House" << endl;
                scoring = (scoring + 21);
            }
            else
            {
                cout << "You got a 3 of a kind" << endl;
                scoring=(scoring+18);
            }
        }
        // check for twopair
        else if((R[0]==R[1]&&R[2]==R[3])||(R[1]==R[2]&&R[3]==R[4])||(R[0]==R[1]&&R[3]==R[4]))
        {
            cout << "You got a Twopair" << endl;
            scoring=(scoring+17);
        }
        else
        {
            cout << "You got a Pair" << endl;
            scoring=(scoring+16);
        }
    }
    else
    {
        scoring=(scoring+0);
    }


what do you think?
Looks like it works :)

The lines like line 11: scoring = (scoring + 22) confuse me a little. With a line like that, I expect that the original value of scoring is important. I think you can just do scoring = 22 and your program should still work properly.

As you can see, JAVA is not illegible. In fact, you could copy paste your previous post's code into a JAVA program and it would work just the same. There are little differences in the way things are set up (notice line 3, which is JAVA for int scaleValue[SIZE_HAND];, but most of the time spent coding is still in the problem solving part, the thinking part.
Last edited on
yeah my senior said that JAVA is very interesting :D

btw now i try to determined the winner. i have made the program just like this one, but it wouldn't works properly haha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   cout << "your score : " << scoring(a[0], a[4], a[8], a[12], a[16]) << endl << endl;
    //int q = scoring(a[0], a[4], a[8], a[12], a[16]);
    cout << "score P2 :"<< scoring(a[1], a[5], a[9], a[13], a[17]) << endl << endl;
    //int w = scoring(a[1], a[5], a[9], a[13], a[17]);
    cout << "score P3 :"<< scoring(a[2], a[6], a[10], a[14], a[18]) << endl << endl;
    //int y = scoring(a[2], a[6], a[10], a[14], a[18]);
    cout << "score P4 :"<< scoring(a[3], a[7], a[11], a[15], a[19]) << endl << endl;
    //int z = scoring(a[3], a[7], a[11], a[15], a[19]);

    /*if ((q>w)&&(q>y)&&(q>z))
    {
        cout << "YOU WIN!" << endl;
    }
    else
    {
        cout << "YOU LOSE!" << endl;
    }*/
Last edited on
Topic archived. No new replies allowed.