Jump to content
  • Advertisement
Sign in to follow this  

Jacks or Better Program

This topic is 4436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am reading the book Beginning C++ Game Programing. And from what I have learned so far I think I should be able to write this program. Just run into a little road block. I can make it deal to a hand from a deck and also discard cards and redraw cards. But analizing the hand is where I am having an issue. I kind of got an Idea of how to see if there is a flush but anything else seems a little hard lol. Anyone have any ideas how I can achieve my goal. (Analyzing a hand) Or any other advice would be greatly appreciated. Thanks!
/* Jacks Or Better */

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main() {
//Creating Deck
    string value[13] = { "A", "2", "3", "4", "5", "6", "7",
                         "8", "9", "T", "J", "Q", "K" };
    string suite[4] = { "h", "s", "c", "d" };
    vector<string> deck;
    vector<string> hand;
    for(int x = 0; x != 13; ++x) {
        for(int i = 0; i != 4; ++i) {
            deck.push_back(value[x] + suite);

//Shuffling Deck    
    for(int x = 0; x != 52; ++x){
        int index1 = (rand() % 52);
        int index2 = (rand() % 52);
        string temp;
        temp = deck[index1];
        deck[index1] = deck[index2];
        deck[index2] = temp;

//Drawing Cards from deck
    const int CARDS_IN_HAND = 5;
    for(int x = 0; x != CARDS_IN_HAND; ++x) {

//Show Cards
    for(int x = 0; x != 5; ++x) {
        cout << hand[x] << " ";
    cout << endl;
    cout << endl;
    for(int x = 0; x != 47; ++x) {
        cout << deck[x] << " ";
        if((x+1) % 5 == 0) {
            cout << endl;

//Ask if want to discard and Draw new card when appropriate
    string discard;
    cout << "\n\nDo you want to keep? (y/n)" << endl;    
    for(int x = 0; x!= CARDS_IN_HAND; ++x) {
        cout << "Card " << x+1 << ": "; cin >> discard[x];
        while(discard[x] != 'y' && discard[x] != 'n') {
            cout << "Card " << x+1 << ": "; cin >> discard[x];
        if(discard[x] == 'n') {
            hand[x] = deck[0];

//Show new Hand
    cout << endl;
    for(int x = 0; x != 5; ++x) {
        cout << hand[x] << " ";
    bool pair = false; //pair pays nothing
    bool jorb = false; //jacks or better pays 1x
    bool twopair = false; //Two Pair pays 2x
    bool threekind = false; //Three of a Kind pays 3x
    bool straight = false; //Straight pays 4x
    bool flush = false; //Flush pays 5x
    bool fullhouse = false; //Full House pays 10x
    bool straightflush = false; //Straight Flush pays 50x
    bool royalflush = false;  //Royal Flush pays 100x

//Checks For Flush 
    if(hand[0][1] == hand[1][1], hand[0][1] == hand[2][1],
     hand[0][1] == hand[3][1], hand[0][1] == hand[4][1]) {
         flush == true;
//checks For Straight       
//checks For Pair
    hand.find(hand[0][0], 1)

    return 0;
[Edited by - Danikar on March 29, 2006 4:51:50 PM]

Share this post

Link to post
Share on other sites
For anything other than flush, your first step should be to sort the hand by rank. That way, you know that 2-, 3-, and 4-of-a-kinds (including in two pair and full house) will be consecutive, and that straights and straight flushes will be in order. Also, you don't need to check separately for royal flush (it's just an ace-high straight flush).

Share this post

Link to post
Share on other sites
Cool thanks,

for straight flush I figured I would jsut say

if(straight == true && flush == true) {
straightflush == true;

and the reason I have diffrent thing for royal flush is because I want it to pay out higher.

How do you do the code brackets thing? I type the tag I thought it was but I guess it didnt work.

I see other people use it, it looks like
but has a white background.


Share this post

Link to post
Share on other sites
FYI you forgot 4 of a kind which is right under Straight Flush.
It looks good. But something to sort them would work, the problem is, is that your average sorter does it by numbers or alphebets but not both. You could also have two sorters or something but I was thinking change J to 11, Q to 12, and K to 13. (and A to 14) Then when printing out on the screen change them back to J, Q, K and A. This way sorting is easier, as is seeing if they got straights with J, Q, K and A's.

Share this post

Link to post
Share on other sites
you need an array that has a count of cards by rank(r).

you need an array that has a count of cards by suit(s).

you need to keep a value that is the lowest ranked card in the hand(l).

you need to keep a value that is the highest ranked card in the hand(h).

have an enum for ranks


have an enum for suits


initialize the rank array(r) to zeros

initialize the suit array(s) to zeros

initialize the lowest ranked card to ACE

initialize the highest ranked card to TWO

go through each card in the hand.

increment the array value corresponding to each card's rank.

increment the array value coresponding to each card's suit.

if the rank of the current card is higher than the current high value, set a new current high value

if the rank of the current card is lower than the current low value, set a new current low value

once you are done doing all of this, start to analyze the resulting data:

check the suits array.

if a 5 is found, you have either a flush or a straight flush. you have no need to check the rank array, provided you have a single deck.

look at the highest ranked card minus the lowest ranked card. (h-l)==4, you have a straight flush, otherwise it is just a flush. telling whether or not a straight flush is a royal flush is trivial.

if it is NOT a flush, look at the rank array, and find the highest number.

if the highest number in the rank array is 4, you have four of a kind.

if the highest number in the rank array is 3, you either have a full house or three of a kind. search the rank array for a 2. if a 2 is found, it is a full house, otherwise it is three of a kind.

if the highest number in the rank array is a 2, you either have one or two pair. count the number of 2s in the rank array, and that's how many pairs you've got. if only one pair is present, make certain it is at least a JACK.

if the highest number in the rank array is a 1, the hand is either a straight or junk. if (h-l)==4, you have a straight. otherwise, you have junk.


there is one exception of which you should be aware...

you can have a straight or straight-flush that numbers A-2-3-4-5. The above algorithm will not detect it as is, as it considers Ace to be high. you may simply decide to do a check specifically for it during straight and straight flush detection.

Share this post

Link to post
Share on other sites
I made a console poker game a while back. I don't really remember how I did the hand checking now but I could dig it up if you want. Though, I was even more of a newb than I am now so I don't know how well I coded it but I think it worked fine. You probably have enough info from TANSTAAFL though. Goodluck getting it sorted out :)

Share this post

Link to post
Share on other sites
Tan that confuses the hell out of me. How does that prove they are flush if 5 is the highest? Could it not be you have, Ace, ace, Queen, Jack, and 10. And that 10 would make it a 5. Or maybe i just get the concept. It seems that you are doing a check high and low, but the low is really the high (ace) and ur high is really your low (two) Maybe it's the enum.. I have never heard of it.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!