world of problems with poker game

This is a discussion on world of problems with poker game within the C++ Programming forums, part of the General Programming Boards category; hey all, hope you're doing better than i am today. this program, which i thought would be easy, is giving ...

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    world of problems with poker game

    hey all, hope you're doing better than i am today. this program, which i thought would be easy, is giving so many troubles. the thing is, it runs differently every time: sometimes it will work, sometimes not, and sometimes it xrashes and wants to send an error report to ms (but then the debugger stalls and doesn't respond).

    it's a simple program that after shuffling and dealing, prints 5 cards and then reports if there is a pair of like face cards. i think the root of the problem starts with the strcpy on line 88.

    also, does anyone know how to read these "debug" reports? is there a quick and easy tutorial out there? thanks a lot

    [edit code]
    Last edited by blight2c; 04-14-2002 at 10:02 PM.

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    ok, i made a little progress: the copied-to array face_card wasn't big enough for the \0 (NULL?). so it doesn't crash now

    but, it's not catching all the matches--i'd say about half of them. any ideas? thanks.

    new code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>
    
    #define draw 5
    
    using namespace std;
    
    
    void f_shuffle (int _deck [] [13]);
    void f_deal (int _hand [] [13], const int _deck [] [13]);
    void f_c_hand( const int _hand [] [13], const char *_face[], const char *_suit[] );
    void f_wins (const int _hand [] [13], const char *_face[], const char *_suit[]);
    void f_pair (char face_card[][6]);
    
    int main()
    {	const char *suit [4]={
    							"Hearts", "Diamonds", "Clubs", "Spades"};
    	const char *face [13]={
    							"Ace", "Deuce", "Three", "Four",
    							"Five", "Six", "Seven", "Eight",
    							"Nine", "Ten", "Jack", "Queen", "King"};
    	int deck [4] [13]={0};
    	int hand [4] [13]={0};
    	srand (time(0));
    
    	f_shuffle (deck);
    	f_deal (hand, deck);
    	f_c_hand(hand, face, suit);
    	f_wins (hand, face, suit);
    
    	return(0);
    }
    
    
    
    void f_shuffle (int _deck [] [13])
    {	int row, column;
    	for ( int card = 1; card <=52; card++)
    	{	do
    		{	row=rand() %4;
    			column=rand() %13;
    		}while(_deck [row] [column] != 0);
    		_deck [row] [column] = card;
    	}
    }
    
    
    
    void f_deal (int _hand[][13], const int _deck [][13])
    {	for (int card=1; card<=draw; card++)
    	{	for (int row=0; row<=3; row++)
    		{	for (int column=0; column <=12; column++)
    			{	if (_deck [row] [column] == card)
    				{	_hand [row] [column] = 1;
    				}
    			}
    		}
    	}
    }
    
    
    
    void f_c_hand( const int _hand [] [13], const char *_face[], const char *_suit[] )
    {	cout<<"Your Hand: "<<endl<<endl;
    	for (int row=0; row<=3; row++)
    	{	for (int column=0; column <=12; column++)
    		{	if (_hand [row] [column] == 1)
    			{	cout<<setw(5)<<setiosflags(ios::right)
    					<<_face[column]
    					<<" of "<<_suit[row]<<endl;
    			}
    		}
    	}
    }
    
    
    void f_wins (const int _hand [][13], const char *_face[], const char *_suit[])
    {	int counter=0; 
    	char face_card [5][6];
    	char suit_card [5][8]; 
    	for (int column=0; column<13; column++)
    	{	for (int row=0; row<4; row++)
    		{	if (_hand [row] [column] ==1)
    			{	counter++;
    				strcpy(face_card[counter], _face[column]);
    				strcpy(suit_card[counter], _suit[row]);
    			}
    		}
    	}f_pair(face_card);
    	
    }
    
    
    
    void f_pair (char face_card[5][6])
    {	for (int i=0; i<draw; i++)
    	{	for (int j=i+1; j<draw-1; ++j)
    		{	if (strcmp(face_card[i], face_card[j])==0)
    			{	cout<<"you have a match"<<endl;// :confused:
    				cout<<face_card[i]<<endl<<endl;
    			}}}}

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    . . . ok, i've isolated the problem to this little bit of code. and it only seems to happen in the first two loops through the if statment. anyone?

    Code:
    void f_wins (const int _hand [][13], const char *_face[], const char *_suit[])
    {	int counter=0; 
    	char face_card [5][6];
    	char suit_card [5][8]; 
    	for (int column=0; column<13; column++)
    	{	for (int row=0; row<4; row++)
    		{	if (_hand [row] [column] ==1)
    			{	counter++;
    				strcpy(face_card[counter], _face[column]);
    				strcpy(suit_card[counter], _suit[row]);
    			}
    		}
    	}

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Here's something i whipped up for ya during lunch... it compiles... not sure if it'll help you in your cause...

    Code:
    #include <time.h>
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    typedef enum Suit
    {
        HEARTS,
        DIAMONDS,
        CLUBS,
        SPADES,
        NUM_SUITS
    };
    
    const char* SuitStrings[NUM_SUITS] =
    {
        "Hearts", "Diamonds", "Clubs", "Spades"
    };
    
    typedef enum Value
    {
        ACE,
        DEUCE,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN,
        JACK,
        QUEEN,
        KING,
        NUM_VALUES
    };
    
    const char* ValueStrings[NUM_VALUES] =
    {
        "Ace", "Deuce", "Three", "Four",
        "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen",
        "King"
    };
    
    typedef struct _Card
    {
        Value value;
        Suit suit;
    } Card;
    
    static const int NUM_CARDS = NUM_VALUES * NUM_SUITS;
    
    static const int CARDS_PER_HAND = 5;
    
    int CreateCards(Card* cards)
    {
        int count = 0;
    
        for(int i = ACE; i < NUM_VALUES; i++)
        {
            for(int j = HEARTS; j < NUM_SUITS; j++)
            {
                cards[count].suit = (Suit)j;
                cards[count++].value = (Value)i;
            }
        }
    
        return(1);
    }
    
    int SwapCards(Card* card1, Card* card2)
    {
        Card temp;
    
        temp.suit = card1->suit;
        temp.value = card1->value;
    
        card1->suit = card2->suit;
        card1->value = card2->value;
    
        card2->suit = temp.suit;
        card2->value = temp.value;
    
        return(1);
    }
    
    int ShuffleCards(Card* cards)
    {
        srand(time(NULL));
    
        for(int i = 0; i < NUM_CARDS; i++)
        {
            SwapCards(&cards[i], &cards[rand() % NUM_CARDS]);
        }
    
        return(1);
    }
    
    int DealCards(Card* cards, Card* hand[CARDS_PER_HAND])
    {
        int flags[NUM_CARDS];
        memset(flags, 0, sizeof(flags));
    
        for(int i = 0; i < CARDS_PER_HAND; i++)
        {
            int card = rand() % NUM_CARDS;
            if(flags[card])
            {
                i--;
            }
            else
            {
                hand[i] = &cards[card];
                flags[card]++;
            }
        }
    
        return(1);
    }
    
    int ShowFacePairs(Card* hand[CARDS_PER_HAND])
    {
        for(int i = 0; i < CARDS_PER_HAND; i++)
        {
            for(int j = i + 1; j < CARDS_PER_HAND; j++)
            {
                if(hand[i]->value == hand[j]->value)
                {
                    cout << "You have a pair of " << ValueStrings[hand[i]->value] << "s\n";
                }
            }
        }
    
        return(1);
    }
    
    int ShowCards(Card* hand[CARDS_PER_HAND])
    {
        for(int i = 0; i < CARDS_PER_HAND; i++)
        {
            cout << "Card " << i + 1 << " is the ";
            cout << ValueStrings[hand[i]->value] << " of ";
            cout << SuitStrings[hand[i]->suit] << ".\n";
        }
    
        return(1);
    }
    
    int main(void)
    {
        Card cards[NUM_CARDS];
        Card *hand[CARDS_PER_HAND];
    
        CreateCards(cards);
        ShuffleCards(cards);
        DealCards(cards, hand);
        ShowCards(hand);
        ShowFacePairs(hand);
    
        return(1);
    }
    good luck
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>

    #define draw 5

    using namespace std;


    void f_shuffle (int _deck [] [13]);
    void f_deal (int _hand [] [13], const int _deck [] [13]);
    void f_c_hand( const int _hand [] [13], const char *_face[], const char *_suit[] );
    void f_wins (const int _hand [] [13], const char *_face[], const char *_suit[]);
    void f_pair (char face_card[][6]);

    int main()
    { const char *suit [4]={
    "Hearts", "Diamonds", "Clubs", "Spades"};
    const char *face [13]={
    "Ace", "Deuce", "Three", "Four",
    "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen", "King"};
    int deck [4] [13]={0};
    int hand [4] [13]={0};
    srand (time(0));

    f_shuffle (deck);
    f_deal (hand, deck);
    f_c_hand(hand, face, suit);
    f_wins (hand, face, suit);

    return(0);
    }



    void f_shuffle (int _deck [] [13])
    { int row, column;
    for ( int card = 1; card <=52; card++)
    { do
    { row=rand() %4;
    column=rand() %13;
    }while(_deck [row] [column] != 0);
    _deck [row] [column] = card;
    }
    }



    void f_deal (int _hand[][13], const int _deck [][13])
    { for (int card=1; card<=draw; card++)
    { for (int row=0; row<=3; row++)
    { for (int column=0; column <=12; column++)
    { if (_deck [row] [column] == card)
    { _hand [row] [column] = 1;
    }
    }
    }
    }
    }



    void f_c_hand( const int _hand [] [13], const char *_face[], const char *_suit[] )
    { cout<<"Your Hand: "<<endl<<endl;
    for (int row=0; row<=3; row++)
    { for (int column=0; column <=12; column++)
    { if (_hand [row] [column] == 1)
    { cout<<setw(5)<<setiosflags(ios::right)
    <<_face[column]
    <<" of "<<_suit[row]<<endl;

    }
    }
    }
    }


    void f_wins (const int _hand [][13], const char *_face[], const char *_suit[])
    { int counter=0;
    char face_card [5][6];
    char suit_card [5][9];
    for (int row=0; row<4; row++)
    { for (int column=0; column<13; column++)
    { if (_hand [row] [column] ==1 && counter<draw)
    { counter++;
    strcpy(face_card[counter], _face[column]);
    strcpy(suit_card[counter], _suit[row]);
    }
    }
    }
    cout<<"************"<<endl;
    for (int y=0; y<draw; y++)
    { cout<<face_card[y]<<endl;
    }
    cout<<"************"<<endl;
    f_pair(face_card);

    }



    void f_pair (char face_card[6][6])
    { int x=0;
    for (int i=0; i<draw; i++)
    { for (int j=i+1; j<draw-1; j++)
    { x++;
    if (strcmp(face_card[i], face_card[j])==0)
    { cout<<"you have a match"<<endl;//
    cout<<face_card[i]<<endl<<endl;
    }
    else
    cout<<"nothing "<<x<<endl;
    }}}

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    counter++;
    strcpy(face_card[counter], _face[column]);
    strcpy(suit_card[counter], _suit[row]);
    should be:

    Code:
    strcpy(face_card[counter], _face[column]);
    strcpy(suit_card[counter], _suit[row]);
    counter++;

    good luck
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. first opengl game, problems.
    By n3v in forum Game Programming
    Replies: 1
    Last Post: 07-11-2006, 08:22 PM
  2. Problems in a game program
    By ChronoSquare in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 07:32 AM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  4. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  5. Memory Problems w/My DX game
    By Stan100 in forum Game Programming
    Replies: 2
    Last Post: 10-03-2003, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21