Thread: Wierd problem

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    39

    Wierd problem

    I am working on a poker game. I have figured out what code I need, but it has 1 problem.

    I am working on the code that chooses a random card for each player (2 players) plus all the cards on the table (5). However, the part the problem appears at is the code for making sure that no two cards are identical.

    Here is the code...Scroll down beneath the code the see what problems I get.

    Code:
    #include <iostream>
    using namespace std;
    int main ( int argc , char** argv ) 
    {
        // -- Variables -- //
        int totalpoints1=0;
        int suit1=0;
        int totalpoints2=0;
        int suit2=0;
        int randnum1; // Opponents first card
        string card1;
        int randnum2; // Your first card
        string card2;
        int randnum3; // Opponents second card
        string card3;
        int randnum4; // Your second card
        string card4;
        string communitycard1;
        int suitcc1;
        string communitycard2;
        int suitcc2;
        string communitycard3;
        int suitcc3;
        string theturn;
        int suittt;
        string theriver;
        int suittr;
        int quit=1; // if quit is 0 then game over
        // -- No More Variables -- //
        // -- Begin Main Game Loop -- //
        while(quit==1)
        {
          randnum1 = rand() % 52 ;
          randnum2 = rand() % 52 ;
          randnum3 = rand() % 52 ;
          randnum4 = rand() % 52 ;
          communitycard1 = rand() % 52 ;
          communitycard2 = rand() % 52 ;
          communitycard3 = rand() % 52 ;
          theturn = rand() % 52 ;
          theriver = rand() % 52 ;
          // -- Begin Card Choosing System -- //
          if(randnum1=1)
          {card1="Ace of Hearts"; suit1=1;}
          else if(randnum1=2)
          {card1="Ace of Diamonds"; suit1=2;}
          else if(randnum1=3)
          {card1="Ace of Spades"; suit1=3;}
          else if(randnum1=4)
          {card1="Ace of Clovers"; suit1=4;}
          else if(randnum1=5)
          {card1="Two of Hearts"; suit1=1;}
          else if(randnum1=6)
          {card1="Two of Diamonds"; suit1=2;}
          else if(randnum1=7)
          {card1="Two of Spades"; suit1=3;}
          else if(randnum1=8)
          {card1="Two of Clovers"; suit1=4;}
          else if(randnum1=9)
          {card1="Three of Hearts"; suit1=1;}
          else if(randnum1=10)
          {card1="Three of Diamonds"; suit1=2;}
          else if(randnum1=11)
          {card1="Three of Spades"; suit1=3;}
          else if(randnum1=12)
          {card1="Three of Clovers"; suit1=4;}
          else if(randnum1=13)
          {card1="Four of Hearts"; suit1=1;}
          else if(randnum1=14)
          {card1="Four of Diamonds"; suit1=2;}
          else if(randnum1=15)
          {card1="Four of Spades"; suit1=3;}
          else if(randnum1=16)
          {card1="Four of Clovers"; suit1=4;}
          else if(randnum1=17)
          {card1="Five of Hearts"; suit1=1;}
          else if(randnum1=18)
          {card1="Five of Diamonds"; suit1=2;}
          else if(randnum1=19)
          {card1="Five of Spades"; suit1=3;}
          else if(randnum1=20)
          {card1="Five of Clovers"; suit1=4;}
          else if(randnum1=21)
          {card1="Six of Hearts"; suit1=1;}
          else if(randnum1=22)
          {card1="Six of Diamonds"; suit1=2;}
          else if(randnum1=23)
          {card1="Six of Spades"; suit1=3;}
          else if(randnum1=24)
          {card1="Six of Clovers"; suit1=4;}
          else if(randnum1=25)
          {card1="Seven of Hearts"; suit1=1;}
          else if(randnum1=26)
          {card1="Seven of Diamonds"; suit1=2;}
          else if(randnum1=27)
          {card1="Seven of Spades"; suit1=3;}
          else if(randnum1=28)
          {card1="Seven of Clovers"; suit1=4;}
          else if(randnum1=29)
          {card1="Eight of Hearts"; suit1=1;}
          else if(randnum1=30)
          {card1="Eight of Diamonds"; suit1=2;}
          else if(randnum1=31)
          {card1="Eight of Spades"; suit1=3;}
          else if(randnum1=32)
          {card1="Eight of Clovers"; suit1=4;}
          else if(randnum1=33)
          {card1="Nine of Hearts"; suit1=1;}
          else if(randnum1=34)
          {card1="Nine of Diamonds"; suit1=2;}
          else if(randnum1=35)
          {card1="Nine of Spades"; suit1=3;}
          else if(randnum1=36)
          {card1="Nine of Clovers"; suit1=4;}
          else if(randnum1=37)
          {card1="Ten of Hearts"; suit1=1;}
          else if(randnum1=38)
          {card1="Ten of Diamonds"; suit1=2;}
          else if(randnum1=39)
          {card1="Ten of Spades"; suit1=3;}
          else if(randnum1=40)
          {card1="Ten of Clovers"; suit1=4;}
          else if(randnum1=41)
          {card1="Jack of Hearts"; suit1=1;}
          else if(randnum1=42)
          {card1="Jack of Diamonds"; suit1=2;}
          else if(randnum1=43)
          {card1="Jack of Spades"; suit1=3;}
          else if(randnum1=44)
          {card1="Jack of Clovers"; suit1=4;}
          else if(randnum1=45)
          {card1="Queen of Hearts"; suit1=1;}
          else if(randnum1=46)
          {card1="Queen of Diamonds"; suit1=2;}
          else if(randnum1=47)
          {card1="Queen of Spades"; suit1=3;}
          else if(randnum1=48)
          {card1="Queen of Clovers"; suit1=4;}
          else if(randnum1=49)
          {card1="King of Hearts"; suit1=1;}
          else if(randnum1=50)
          {card1="King of Diamonds"; suit1=2;}
          else if(randnum1=51)
          {card1="King of Spades"; suit1=3;}
          else if(randnum1=52)
          {card1="King of Clovers"; suit1=4;}
          ///////////////////////////////////////////
          while(1==1)
          {
          if(randnum2=1)
          {card2="Ace of Hearts"; suit2=1;}
          else if(randnum2=2)
          {card2="Ace of Diamonds"; suit2=2;}
          else if(randnum2=3)
          {card2="Ace of Spades"; suit2=3;}
          else if(randnum2=4)
          {card1="Ace of Clovers"; suit1=4;}
          else if(randnum2=5)
          {card2="Two of Hearts"; suit2=1;}
          else if(randnum2=6)
          {card2="Two of Diamonds"; suit2=2;}
          else if(randnum2=7)
          {card2="Two of Spades"; suit2=3;}
          else if(randnum2=8)
          {card2="Two of Clovers"; suit2=4;}
          else if(randnum2=9)
          {card2="Three of Hearts"; suit2=1;}
          else if(randnum2=10)
          {card2="Three of Diamonds"; suit2=2;}
          else if(randnum2=11)
          {card2="Three of Spades"; suit2=3;}
          else if(randnum2=12)
          {card2="Three of Clovers"; suit2=4;}
          else if(randnum2=13)
          {card2="Four of Hearts"; suit2=1;}
          else if(randnum2=14)
          {card2="Four of Diamonds"; suit2=2;}
          else if(randnum2=15)
          {card2="Four of Spades"; suit2=3;}
          else if(randnum2=16)
          {card2="Four of Clovers"; suit2=4;}
          else if(randnum2=17)
          {card2="Five of Hearts"; suit2=1;}
          else if(randnum2=18)
          {card2="Five of Diamonds"; suit2=2;}
          else if(randnum2=19)
          {card2="Five of Spades"; suit2=3;}
          else if(randnum2=20)
          {card2="Five of Clovers"; suit2=4;}
          else if(randnum2=21)
          {card2="Six of Hearts"; suit2=1;}
          else if(randnum2=22)
          {card2="Six of Diamonds"; suit2=2;}
          else if(randnum2=23)
          {card2="Six of Spades"; suit2=3;}
          else if(randnum2=24)
          {card2="Six of Clovers"; suit2=4;}
          else if(randnum2=25)
          {card2="Seven of Hearts"; suit2=1;}
          else if(randnum2=26)
          {card2="Seven of Diamonds"; suit2=2;}
          else if(randnum2=27)
          {card2="Seven of Spades"; suit2=3;}
          else if(randnum2=28)
          {card2="Seven of Clovers"; suit2=4;}
          else if(randnum2=29)
          {card2="Eight of Hearts"; suit2=1;}
          else if(randnum2=30)
          {card2="Eight of Diamonds"; suit2=2;}
          else if(randnum2=31)
          {card2="Eight of Spades"; suit2=3;}
          else if(randnum2=32)
          {card2="Eight of Clovers"; suit2=4;}
          else if(randnum2=33)
          {card2="Nine of Hearts"; suit2=1;}
          else if(randnum2=34)
          {card2="Nine of Diamonds"; suit2=2;}
          else if(randnum2=35)
          {card2="Nine of Spades"; suit2=3;}
          else if(randnum2=36)
          {card2="Nine of Clovers"; suit2=4;}
          else if(randnum2=37)
          {card2="Ten of Hearts"; suit2=1;}
          else if(randnum2=38)
          {card2="Ten of Diamonds"; suit2=2;}
          else if(randnum2=39)
          {card2="Ten of Spades"; suit2=3;}
          else if(randnum2=40)
          {card2="Ten of Clovers"; suit2=4;}
          else if(randnum2=41)
          {card2="Jack of Hearts"; suit2=1;}
          else if(randnum2=42)
          {card2="Jack of Diamonds"; suit2=2;}
          else if(randnum2=43)
          {card2="Jack of Spades"; suit2=3;}
          else if(randnum2=44)
          {card2="Jack of Clovers"; suit2=4;}
          else if(randnum2=45)
          {card2="Queen of Hearts"; suit2=1;}
          else if(randnum2=46)
          {card2="Queen of Diamonds"; suit2=2;}
          else if(randnum2=47)
          {card2="Queen of Spades"; suit2=3;}
          else if(randnum2=48)
          {card2="Queen of Clovers"; suit2=4;}
          else if(randnum2=49)
          {card2="King of Hearts"; suit2=1;}
          else if(randnum2=50)
          {card2="King of Diamonds"; suit2=2;}
          else if(randnum2=51)
          {card2="King of Spades"; suit2=3;}
          else if(randnum2=52)
          {card2="King of Clovers"; suit2=4;}
          if(card1==card2 && suit1==suit2)
          {} // Rechoose the card until they are different
          else
          {break;}
          }
          // -- End Card Choosing System -- //
          cout << "The dealer hands your opponent a card.\n";
          cout << "The dealer hands you a card.\n";
          cout << "You got a \"";
          cout << card2;
          cout << "\"\n";
          cout << "The dealer hands your opponent a card.\n";
          cout << "The dealer hands you a card.\n";
          cout << "You got a \"";
          cout << card4;
          cout << "\"\n";
        }
          // -- End Main Game Loop -- //
    }
    The Error appears at the lines:
    Code:
          if(card1==card2 && suit1==suit2)
          {}
          else
          {break;}
    If I have
    Code:
    if(card1==card2 && suit1==suit2)
    then no compile errors appear, but the program does not function. It opens the console window and then nothing happens. Hitting any key does nothing whatsoever.

    If I have
    Code:
    if(card1=card2 && suit1=suit2)
    then the compile error:
    Code:
    int function 'int main(int, char**)':
    no match for 'operator&&' in 'card2 && suit1'
    candidates are: operator&&(bool, bool) <built-in>
    I don't know what to do since I am relativly new to C++.

    Also, another question. Is there anyway to make this code better? As in decreasing the amount of code or anything along those lines?

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well my advice to you is learn how to overload the operator <<, here is an example code:

    Code:
    ostream& operator<< (ostream &Out, cardType card)
    {
       if (card == ace) Out << "Ace";
       ...
       ...
       ...
    
       return Out;
    }
    And you can create a cardsType using an enumeration, for example:

    Code:
    enum cardType {ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
    
    enum cardSuit { /*suits...*/ };
    Also I suggest using either a struct or a matrix and first (not randomly) insert all cards into a full deck, then either shuffle or call random to pull a random card from the deck.
    Last edited by JoshR; 05-04-2005 at 10:25 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)You need to learn the difference between assignment(=) and comparison(==). You've got at least 104 errors in your program.
    Last edited by 7stud; 05-04-2005 at 10:28 PM.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Quote Originally Posted by 7stud
    1)You need to learn the difference between assignment(=) and comparison(==). You've got at least 104 errors in your program.
    Um, that really helped me...
    I tried using == (gives no compile errors) but then nothing happens.
    Saying I have 104 errors in my program doesn't help me either. Could you maybe help by providing advice on how do eliminate those errors?

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Ok I fixed the code so it works now, but there still seems to be some sort of error.

    New Code:
    Code:
    #include <iostream>
    using namespace std;
    int main ( int argc , char** argv ) 
    {
        // -- Variables -- //
        //int totalpoints1=0;
        //int totalpoints2=0;
        int suit=0;
        int randnum1; // Opponents first card
        string card1;
        int randnum2; // Your first card
        string card2;
        //int randnum3; // Opponents second card
        string card3;
        //int randnum4; // Your second card
        string card4;
        //string communitycard1;
        //string communitycard2;
        //string communitycard3;
        //string theturn;
        //string theriver;
        int quit=1; // if quit is 0 then game over
        // -- No More Variables -- //
        // -- Begin Main Game Loop -- //
        while(quit==1)
        {
          while(1==1)
          {
          randnum1 = rand() % 52 ;
          randnum2 = rand() % 52 ;
          //randnum3 = rand() % 52 ;
          //randnum4 = rand() % 52 ;
          suit = rand() % 4 ;
          //communitycard1 = rand() % 52 ;
          //communitycard2 = rand() % 52 ;
          //communitycard3 = rand() % 52 ;
          //theturn = rand() % 52 ;
          //theriver = rand() % 52 ;
          // -- Begin Card Choosing System -- //
          if(randnum1==1 && suit==1)
          {card1="Ace of Hearts";}
          else if(randnum1==2 && suit==2)
          {card1="Ace of Diamonds";}
          else if(randnum1==3 && suit==3)
          {card1="Ace of Spades";}
          else if(randnum1==4 && suit==4)
          {card1="Ace of Clovers";}
          else if(randnum1==5 && suit==1)
          {card1="Two of Hearts";}
          else if(randnum1==6 && suit==2)
          {card1="Two of Diamonds";}
          else if(randnum1==7 && suit==3)
          {card1="Two of Spades";}
          else if(randnum1==8 && suit==4)
          {card1="Two of Clovers";}
          else if(randnum1==9 && suit==1)
          {card1="Three of Hearts";}
          else if(randnum1==10 && suit==2)
          {card1="Three of Diamonds";}
          else if(randnum1==11 && suit==3)
          {card1="Three of Spades";}
          else if(randnum1==12 && suit==4)
          {card1="Three of Clovers";}
          else if(randnum1==13 && suit==1)
          {card1="Four of Hearts";}
          else if(randnum1==14 && suit==2)
          {card1="Four of Diamonds";}
          else if(randnum1==15 && suit==3)
          {card1="Four of Spades";}
          else if(randnum1==16 && suit==4)
          {card1="Four of Clovers";}
          else if(randnum1==17 && suit==1)
          {card1="Five of Hearts";}
          else if(randnum1==18 && suit==2)
          {card1="Five of Diamonds";}
          else if(randnum1==19 && suit==3)
          {card1="Five of Spades";}
          else if(randnum1==20 && suit==4)
          {card1="Five of Clovers";}
          else if(randnum1==21 && suit==1)
          {card1="Six of Hearts";}
          else if(randnum1==22 && suit==2)
          {card1="Six of Diamonds";}
          else if(randnum1==23 && suit==3)
          {card1="Six of Spades";}
          else if(randnum1==24 && suit==4)
          {card1="Six of Clovers";}
          else if(randnum1==25 && suit==1)
          {card1="Seven of Hearts";}
          else if(randnum1==26 && suit==2)
          {card1="Seven of Diamonds";}
          else if(randnum1==27 && suit==3)
          {card1="Seven of Spades";}
          else if(randnum1==28 && suit==4)
          {card1="Seven of Clovers";}
          else if(randnum1==29 && suit==1)
          {card1="Eight of Hearts";}
          else if(randnum1==30 && suit==2)
          {card1="Eight of Diamonds";}
          else if(randnum1==31 && suit==3)
          {card1="Eight of Spades";}
          else if(randnum1==32 && suit==4)
          {card1="Eight of Clovers";}
          else if(randnum1==33 && suit==1)
          {card1="Nine of Hearts";}
          else if(randnum1==34 && suit==2)
          {card1="Nine of Diamonds";}
          else if(randnum1==35 && suit==3)
          {card1="Nine of Spades";}
          else if(randnum1==36 && suit==4)
          {card1="Nine of Clovers";}
          else if(randnum1==37 && suit==1)
          {card1="Ten of Hearts";}
          else if(randnum1==38 && suit==2)
          {card1="Ten of Diamonds";}
          else if(randnum1==39 && suit==3)
          {card1="Ten of Spades";}
          else if(randnum1==40 && suit==4)
          {card1="Ten of Clovers";}
          else if(randnum1==41 && suit==1)
          {card1="Jack of Hearts";}
          else if(randnum1==42 && suit==2)
          {card1="Jack of Diamonds";}
          else if(randnum1==43 && suit==3)
          {card1="Jack of Spades";}
          else if(randnum1==44 && suit==4)
          {card1="Jack of Clovers";}
          else if(randnum1==45 && suit==1)
          {card1="Queen of Hearts";}
          else if(randnum1==46 && suit==2)
          {card1="Queen of Diamonds";}
          else if(randnum1==47 && suit==3)
          {card1="Queen of Spades";}
          else if(randnum1==48 && suit==4)
          {card1="Queen of Clovers";}
          else if(randnum1==49 && suit==1)
          {card1="King of Hearts";}
          else if(randnum1==50 && suit==2)
          {card1="King of Diamonds";}
          else if(randnum1==51 && suit==3)
          {card1="King of Spades";}
          else if(randnum1==52 && suit==4)
          {card1="King of Clovers";}
          ///////////////////////////////////////////
          if(randnum2==1 && suit==1)
          {card2="Ace of Hearts";}
          else if(randnum2==2 && suit==2)
          {card2="Ace of Diamonds";}
          else if(randnum2==3 && suit==3)
          {card2="Ace of Spades";}
          else if(randnum2==4 && suit==4)
          {card1="Ace of Clovers";}
          else if(randnum2==5 && suit==1)
          {card2="Two of Hearts";}
          else if(randnum2==6 && suit==2)
          {card2="Two of Diamonds";}
          else if(randnum2==7 && suit==3)
          {card2="Two of Spades";}
          else if(randnum2==8 && suit==4)
          {card2="Two of Clovers";}
          else if(randnum2==9 && suit==1)
          {card2="Three of Hearts";}
          else if(randnum2==10 && suit==2)
          {card2="Three of Diamonds";}
          else if(randnum2==11 && suit==3)
          {card2="Three of Spades";}
          else if(randnum2==12 && suit==4)
          {card2="Three of Clovers";}
          else if(randnum2==13 && suit==1)
          {card2="Four of Hearts";}
          else if(randnum2==14 && suit==2)
          {card2="Four of Diamonds";}
          else if(randnum2==15 && suit==3)
          {card2="Four of Spades";}
          else if(randnum2==16 && suit==4)
          {card2="Four of Clovers";}
          else if(randnum2==17 && suit==1)
          {card2="Five of Hearts";}
          else if(randnum2==18 && suit==2)
          {card2="Five of Diamonds";}
          else if(randnum2==19 && suit==3)
          {card2="Five of Spades";}
          else if(randnum2==20 && suit==4)
          {card2="Five of Clovers";}
          else if(randnum2==21 && suit==1)
          {card2="Six of Hearts";}
          else if(randnum2==22 && suit==2)
          {card2="Six of Diamonds";}
          else if(randnum2==23 && suit==3)
          {card2="Six of Spades";}
          else if(randnum2==24 && suit==4)
          {card2="Six of Clovers";}
          else if(randnum2==25 && suit==1)
          {card2="Seven of Hearts";}
          else if(randnum2==26 && suit==2)
          {card2="Seven of Diamonds";}
          else if(randnum2==27 && suit==3)
          {card2="Seven of Spades";}
          else if(randnum2==28 && suit==4)
          {card2="Seven of Clovers";}
          else if(randnum2==29 && suit==1)
          {card2="Eight of Hearts";}
          else if(randnum2==30 && suit==2)
          {card2="Eight of Diamonds";}
          else if(randnum2==31 && suit==3)
          {card2="Eight of Spades";}
          else if(randnum2==32 && suit==4)
          {card2="Eight of Clovers";}
          else if(randnum2==33 && suit==1)
          {card2="Nine of Hearts";}
          else if(randnum2==34 && suit==2)
          {card2="Nine of Diamonds";}
          else if(randnum2==35 && suit==3)
          {card2="Nine of Spades";}
          else if(randnum2==36 && suit==4)
          {card2="Nine of Clovers";}
          else if(randnum2==37 && suit==1)
          {card2="Ten of Hearts";}
          else if(randnum2==38 && suit==2)
          {card2="Ten of Diamonds";}
          else if(randnum2==39 && suit==3)
          {card2="Ten of Spades";}
          else if(randnum2==40 && suit==4)
          {card2="Ten of Clovers";}
          else if(randnum2==41 && suit==1)
          {card2="Jack of Hearts";}
          else if(randnum2==42 && suit==2)
          {card2="Jack of Diamonds";}
          else if(randnum2==43 && suit==3)
          {card2="Jack of Spades";}
          else if(randnum2==44 && suit==4)
          {card2="Jack of Clovers";}
          else if(randnum2==45 && suit==1)
          {card2="Queen of Hearts";}
          else if(randnum2==46 && suit==2)
          {card2="Queen of Diamonds";}
          else if(randnum2==47 && suit==3)
          {card2="Queen of Spades";}
          else if(randnum2==48 && suit==4)
          {card2="Queen of Clovers";}
          else if(randnum2==49 && suit==1)
          {card2="King of Hearts";}
          else if(randnum2==50 && suit==2)
          {card2="King of Diamonds";}
          else if(randnum2==51 && suit==3)
          {card2="King of Spades";}
          else if(randnum2==52 && suit==4)
          {card2="King of Clovers";}
          if(randnum1==randnum2)
          {} // rechoose the card until they are different
          else if(randnum1!=randnum2)
          {break;}
          }
          // -- End Card Choosing System -- //
          cout << "The Cards choosen are:\n";
          cout << card1;
          cout << "\n";
          cout << card2;
          cout << "\nHit Enter twice to continue...\n";
          cin.ignore(80,'\n');
          cin.get();
        }
          // -- End Main Game Loop -- //
    }
    Whenever you run this, it is supposed to choose two cards randomly and check if they are the same or not. However, when you do run it, the first and second time it goes through (after it says "Hit enter twice to continue") it doesn't display any chosen cards. The third time it does, but if you keep hitting enter, the cards stay the same or very similar. Sometimes the second card doesn't change at all. So, how would I go about fixing this?

    I don't want you people to start changing the way I wrote my code (unless its needed). I want to make this code work before I look for ways to improve it.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    You should read the faq concerning random number generator, you need to seed the generator. If you want it different each time you must include time into the seed.

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I believe the reason you are having problems is because you are randomizing the number from 1-52 AND the suit from 1-4. Think about what happens if randnum1 is 52 and suit is 1, then all of the if statements fail, therefore the card doesn't get picked.

    If you choose to do it this way, either JUST do the randnum1 and randnum2, or make randnum1 and randnum2 be between 1 and 13 and keep the suit variable.

    Oh, and use srand() at the beginning of main to see the random number generator, else every time you run the program you will get the same results. Read the faq for instruction how to use it.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Quote Originally Posted by PJYelton
    I believe the reason you are having problems is because you are randomizing the number from 1-52 AND the suit from 1-4. Think about what happens if randnum1 is 52 and suit is 1, then all of the if statements fail, therefore the card doesn't get picked.

    If you choose to do it this way, either JUST do the randnum1 and randnum2, or make randnum1 and randnum2 be between 1 and 13 and keep the suit variable.

    Oh, and use srand() at the beginning of main to see the random number generator, else every time you run the program you will get the same results. Read the faq for instruction how to use it.
    Ugh, I get it...
    Thanks for pointing that out, cause I thought something was wrong with it...

    EDIT: So should the srand() go after the int and before the choosing?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    I have a new problem now. If I run the program over and over and over (ect.) again, then it doesn't seem to stay random. Sometimes it repeats the same card over and over. Is there some problem in my code?

    Also, I believe that the code I added to make it so the cards are not the same is incorrect. Is it? To make it work (I think) you would have to add a while loop, with the brackets around the choosing of the second card. However, when I add that, the program opens the console window, but then nothing happens. I can't do anything. No keys have any effect whatsoever.

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    About the problem of generating the same card twice.
    I would recommend using a linked list of a simple card type. When you use a card, you remove it from the deck (one list) and place it into a hand or on the table (other lists). This way, you will have a list of all the cards, and you are just moving them around instead of hoping to not randomly get the same card twice. You could also use arrays for this. I have a blackjack game I wrote using this method if you would like to see it.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Quote Originally Posted by neandrake
    About the problem of generating the same card twice.
    I would recommend using a linked list of a simple card type. When you use a card, you remove it from the deck (one list) and place it into a hand or on the table (other lists). This way, you will have a list of all the cards, and you are just moving them around instead of hoping to not randomly get the same card twice. You could also use arrays for this. I have a blackjack game I wrote using this method if you would like to see it.
    I'd like to see your blackjack game, because I don't really understand this linked list business. Does the list reset after the game is over (the poker game ends and you restart with new random cards, not as in closing the window and restarting the exe.)

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Olidivera
    I'd like to see your blackjack game, because I don't really understand this linked list business. Does the list reset after the game is over (the poker game ends and you restart with new random cards, not as in closing the window and restarting the exe.)

    I took a look at my blackjack game; it's a little old and uses quite a bit of linked lists. If you haven't gotten in to using classes and pointers, it's probably not a good idea start off the bat with this. I was thinking instead, you should make a simple struct called card. Make an array of size 52, and put all the cards into the array. Randomly swap a bunch of the cards in the array, and then randomly pick cards to put into a hand (or on the table). Have a second array of size 52 as well (except make it an array of bools) which will tell you if the card is in the deck or not.

    Code:
    struct Card
    {
        int face;
        int suit;
    };
    
    Card deck[52];
    bool indeck[52];
    
    //this should generate all the cards and put them into the array
    //however I think there is something wrong with calculating the 
    //temp face and suit (I'm doing this off memory).
    Card temp;
    for (int i=0; i<52; i++)
    {
        temp.face = i / 13;
        temp.suit = i / 4;
        deck[i] = temp;
        indeck[i] = true;
    }
    
    //This will randonly swap two cards in the array 4000 times
    //(should be enough shuffling)
    srand(time(0));
    for (i=0; i<4000; i++)
    {
        int x = rand() % 52;
        int y = rand() % 52;
        temp = deck[x];
        deck[x] = deck[y];
        deck[y] = temp;
    }
    Hopefully that can get you started. Before you copy/move cards out of the deck array and into a hand/table array, you should check if the corresponding index in indeck[] is true or false (don't move the card if it's false). Then, if you do move the card, set the indeck[] to false. This way you won't use cards twice. When you want to start a new hand, just run through the indeck[] array and set all to true.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Wow I was thinking of implementing something very similar, but why not just add a bool to each struct that is initially false, then when dealt becomes true? it would be so much easier.

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by JoshR
    Wow I was thinking of implementing something very similar, but why not just add a bool to each struct that is initially false, then when dealt becomes true? it would be so much easier.
    That idea did come to my mind, but for some reason that I honestly don't know, I didn't put that in, even though it's a better implementation. Yea, just have a bool canDeal variable in the struct. I think I had already started writing out the second array thing and didn't feel like changing it (working on a project).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do this very very easily with a simple array holding the numbers 0 through 51, which you simply shuffle.

    A card's suit is the card's number divided by 13.
    A card's face value is it's number modulus 13.
    Code:
    int main( void )
    {
        const char *suits[] = { "Spades", "Hearts", "Clubs", "Diamonds", NULL };
        const char *faces[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        int x;
    
        for( x = 0; x < 52; x++ )
        {
            printf( "This card is the %s of %s.\n", faces[ x % 13 ], suits[ x / 13 ] );
        }
    
        return 0;
    }
    This was a C example, but including the appropriate header should do the trick for your C++ compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM