开发者

Problem with drawing same card in poker C++

开发者 https://www.devze.com 2023-02-16 20:06 出处:网络
I am trying to create a poker game with 2 players, each get 5 cards. The issue I am having is that when the first player draws his 5 cards, the second player draws the same 5 cards too. in Deck, there

I am trying to create a poker game with 2 players, each get 5 cards. The issue I am having is that when the first player draws his 5 cards, the second player draws the same 5 cards too. in Deck, there's a variable top_card that decrem开发者_Python百科ents every time for a draw. but it resets back to 51 rather then continuing from 46. Can anyone help me?

 Sample output
 nine of clubs 
 jack of diamonds 
 queen of clubs 
 queen of hearts
 joker of hearts

 nine of clubs 
 jack of diamonds 
 queen of clubs 
 queen of hearts
 joker of hearts

//Card 
#include <string>
using namespace std;

enum suits { none, diamond, club, heart, spade };

class card {
public:
    card( ){
        suit = none;
        rank = 0;
    }   
    card( suits sv, int rv) {
        rank = rv;
        suit = sv;
    }

    int spots ( ){
        return rank;
    } 
    suits kind ( ){
        return suit;
    }

    string toString() const {
        return rankString(rank) + " of " + suitString(suit);
    }

    string rankString(int r) const{
        if (1 == r)       return "ace";
        else if (2 == r)  return "two";
        else if (3 == r)  return "three";
        else if (4 == r)  return "four";
        else if (5 == r)  return "five";
        else if (6 == r)  return "six";
        else if (7 == r)  return "seven";
        else if (8 == r)  return "eight";
        else if (9 == r)  return "nine";
        else if (10 == r) return "ten";
        else if (11 == r) return "jack";
        else if (12 == r) return "queen";
        else if (13 == r) return "king";
        else return "joker";
    }

    string suitString(suits s) const {
        if (s == spade)         return "spades";
        else if (s == heart)    return "hearts";
        else if (s == diamond)  return "diamonds";
        else if (s == club)     return "clubs";
        else                    return "non-suit";
    }

protected:
    int   rank;
    suits suit;
};


//Deck
#include "card.h"
#include <ctime>
#include <cstdlib>
using namespace std;

class deck {
public:
    deck() {
        top_card = 0;
        for( int i = 2; i <= 14; i++ )
        {
            card c1( diamond, i ),
                c2( spade,   i ),
                c3( heart,   i ),
                c4( club,    i );

            cards[ top_card++ ] = c1;
            cards[ top_card++ ] = c2;
            cards[ top_card++ ] = c3;
            cards[ top_card++ ] = c4;
        }
    }    

    void shuffle() {
        srand(time(NULL));
        for(int i=0; i<52; i++) {
            int a = rand()%52;
            card temp = cards[i];
            cards[i] = cards[a];
            cards[a] = temp;
        }
    }

    bool is_empty() {
        return (top_card <= 0);
    }

    card draw() {
        if( !is_empty() ) {
            return cards[ --top_card ];
        } 
        else {
            card non_card( none, 0 );
            return non_card;
        }
    } 

protected:
    card cards[ 52 ];  
    int top_card;     
};


Your ctor for a hand takes a deck by value:

hand(deck a) {
    for(int i=0;i<5;i++)
        hands[i] = a.draw();
}

Therefore, when you draw from the deck, only the local copy of the deck gets modified.

Edit: It's probably also worth noting that right now, you're dealing 5 cards to one player, then 5 cards to the next player (Player 1 gets cards 1 to 5, player 2 gets cards 6 to 10, and so on). In the usual scheme, you deal round-robin, so with 2 players, player 1 would get cards 1, 3, 5, 7 and 9, and player 2 would get cards 2, 4, 6, 8 and 10.

Your shuffle algorithm is also flawed. Googling for "Fisher-Yates" should help you get it straightened out. You also do not want to call srand again each time you shuffle -- you want to call it only once at the beginning of the program.


You pass your deck in by value, so the hand decrements the top_card variable for a copy of the deck. Pass it in by reference.

hand(deck & a) { <-- CHANGE HERE
        for(int i=0;i<5;i++)
            hands[i] = a.draw();
    }


You are passing the deck by value to each hand. This means that each player gets his own copy of the deck!

    hand(deck a) {
        for(int i=0;i<5;i++)
            hands[i] = a.draw();
    }
   

Try passing by reference instead (deck& a).


Because when you pass "newDeck" to the hand constructor, you are passing a copy of it. Hence, each instance of hand (p1 and p2) are using different decks.

Change hand constructor to take a reference (or pointer) to a deck.

That is:

hand(deck &a)
{
    for(int i=0;i<5;i++)
        hands[i] = a.draw();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消