Simple poker code

This is a discussion on Simple poker code within the C++ Programming forums, part of the General Programming Boards category; Hello again everyone I'm designing a code now that just deals a five card poker hand when the user presses ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    Simple poker code

    Hello again everyone I'm designing a code now that just deals a five card poker hand when the user presses D. I have to use pass by reference for this which I have pretty much no understanding of. The books isn't to clear and I haven't found too many useful tutorials online. Some guidance would be excellent. Just to forewarn I'm aware that there is lots of errors in this i get somewhere around 50 when i compile but they all seem to be similar.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime> // allows rand num gen to choose a "random" number from the clock
    #include <string>
    using namespace std;
    
    
    void get_card(int&,int&); 
    int main() 
    { 
    	int card, suit, val;
    	int ansr;
    	srand((unsigned) time(0)); // "seed" the random number generator 
    	
    	cout << "\n\nTo deal yourself a poker hand please press D." << endl;
    	cin << ansr;
    	switch (ansr)
    	{
    	case 'd':
    	case 'D':
    	
    	int n = 5; // deal this many cards 
    	for (int i = 0; i < n; i++)	
    	{ 
        card=get_card(suit,val) 
    
    	}
    	default:
    	cout << "\n\nThanks for playing." << endl << endl;
    	}
    
    	return 0; 
    }
    
    void get_card(int& suit,int& val)
    {	
      int suit = rand() % 4;
      int val = 1 + rand() % 13;
      switch (val) 
      { 
          case 1: 
            cout << "Ace"; 
          break; 
          case 11: 
            cout << "Jack"; 
          break; 
          case 12: 
           cout << "Queen"; 
          break; 
          case 13: 
            cout << "King"; 
          break; 
          default: 
            cout << value; 
        }
    
       switch (suit) 
        { 
          case 0: 
            cout << " of Spades" << endl; 
            break; 
          case 1: 
            cout << " of Hearts" << endl; 
            break; 
          case 2: 
            cout << " of Diamonds" << endl; 
            break; 
          case 3: 
            cout << " of Clubs" << endl; 
        } 
    return;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    A reference is simply a means by which to pass an object or variable to another function so that function can modify the original object or variable in the calling function.
    Example:

    Code:
    void foo(int& n)
    {
    	n = 5;
    }
    
    int main()
    {
    	int n = 0;
    	cout << n << endl; // 0 is output
    	foo(n);
    	cout << n << endl; // 5 is output
    	return 0;
    }
    It wouldn't hurt for you to learn some about indenting either.
    http://cpwiki.sf.net/Indentation
    And don't mix tabs and spaces - indentation will look messed up outside your editor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    Looks like you are passing by reference ok, but you are assigning card=get_card(suit,val) when get_card returns void.

    By the looks of things you are passing by reference for no purpose at all because you are not using the changed values after you call get_card.

    In the get_card function you should not be redeclaring suit and val seeing as they are the values you are passing in, just set the values.

    After your for loop within the case 'D' you should have a break;

    Hope this clears up some of your errors.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    BTW, in real poker hands you obviously cannot be dealt the same card twice. I don't know if you care about that now (there's more important things to work on), but if you did want to make a program plays poker then you'd want to use a different strategy for dealing cards. The typical strategy is to create a container of cards, then shuffle the container and deal by going through the container one at a time.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i thought you set it to void though BECAUSE it doesn't return a value?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by got1sleeve View Post
    i thought you set it to void though BECAUSE it doesn't return a value?
    Yes, if you don't return a value, you use "void" to indicate that the function is not returning a value.

    But in your case, you are then assigning a variable from the result of a "no return value" function - you shouldn't do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Ok so i made some major modifciations here IMO. I changed it from void to int. As well as I have to use strings to display the card. Also i came up with the process to prevent drawing another duplicate card. Any suggestions are greatly appreciated. I'm still at about 25 errors mostly about the pass by reference not working and not being able to use a + in the string code?

    Code:
     #include "stdafx.h"
    #include <iostream>
    #include <ctime> // allows rand num gen to choose a "random" number from the clock
    #include <string>
    using namespace std;
    
    void get_val(int&);
    void get_suit(int&);
    int main() 
    { 
    	int card1, card2, card3, card4, card5, suit, val;
    	int ansr;
    	string suit;
    	string of;
    	string val;
    	srand((unsigned) time(0)); // "seed" the random number generator 
    	
    	cout << "\n\nTo deal yourself a poker hand please press D. " ;
    	cin >> ansr;
    	switch (ansr)
    	{
    	case 'd':
    	case 'D':
    		{
    			string val=get_val(val);
    			sting suit= get_suit(suit);
    			string of= " of ";
    	                	card1 = val+of+suit;
    			do
    			{
    				string val=get_val(val);
    				sting suit= get_suit(suit);
    				string of= " of ";
    				card2=val+of+suit;
    			}
    			while (card2 == card1);
    			do
    			{
    				string val=get_val(val);
    				sting suit= get_suit(suit);
    				string of= " of ";
    				card3=val+of+suit;
    			}
    			while (card1==card3 || card2==card3);
    			do
    			{
    				string val=get_val(val);
    				sting suit= get_suit(suit);
    				string of= " of ";
    				card4==val+of+suit;
    			}
    			while (card1==card4 || card2==card4 || card3==card4);
    			do
    			{
    				string val=get_val(val);
    				sting suit= get_suit(suit);
    				string of= " of ";
    				card5=val+of+suit;
    			}
    			while (card1==card5 || card2==card5 || card3==card5 || card4==card5);
    		}
    
    	
        
    	}
    	default:
    		cout << "\n\nThanks for playing." << endl << endl;
    	}
    
    	return; 
    }
    
    
    int get_val(int&)
    {	
    	int val = 1 + rand() % 13;
    	switch (val) 
    	 { 
    		case 1: 
    			 cout << "Ace"; 
    		break; 
    		case 11: 
    			cout << "Jack"; 
            break; 
    		case 12: 
    			cout << "Queen"; 
            break; 
    		case 13: 
    			cout << "King"; 
            break; 
    		default: 
    			cout << value; 
        }
    	return;
    }
    
    
    int get_suit(int&)
    {
    	int suit = rand() % 4;
    	switch (suit)
        { 
          case 0: 
            cout << " of Spades" << endl; 
            break; 
          case 1: 
            cout << " of Hearts" << endl; 
            break; 
          case 2: 
            cout << " of Diamonds" << endl; 
            break; 
          case 3: 
            cout << " of Clubs" << endl; 
        } 
    return;
    }
    I have no idea why my tabs are all screwed in the second function i copied and pasted from VS....i got yelled at earlier for spacing out to make it look cleaner so i'll just leave it as is.....tab doesn't tab....i hate comptuers sometimes haha....i envy you all

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    No, no. It's because you use both tabs and spaces. If you use either just spaces or tabs, you'll be fine! Spaces are perfectly acceptable.
    You get compile errors too? If you do, then you should probably post them, as well.

    OK, so compile errors:
    Code:
    string suit;
    While this isn't wrong, you already previously defined the variable as int:
    Code:
    int card1, card2, card3, card4, card5, suit, val;
    You can't have two variables with the same name.

    Code:
    string val;
    Same thing here - already defined.

    Code:
    sting suit= get_suit(suit);
    How do you propose to pass a std::string to a function expecting an int?
    If the function is supposed to take a string, then you need to change the function.

    Code:
    sting suit= get_suit(suit);
    No such thing as "sting". A little typo, since it should be "string".

    Code:
    card1 = val+of+suit;
    std::string doesn't seem to like the + operator it seems. That plus you can't store the result in an int!

    Code:
    string val=get_val(val);
    get_val expects an int.

    Code:
    sting suit= get_suit(suit);
    Syntax error.

    Code:
    card2=val+of+suit;
    Same error as previous of same line.

    Plus you've just duplicated code over and over. Very bad. Extract that code into a function of its own.
    Now you have something to ponder about.
    Last edited by Elysia; 01-29-2008 at 02:32 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Ok fixed everything...code compiles and builds. Only problem is it does not display the cards. It just asks user to press D to deal and then says thanks for playing.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime> // allows rand num gen to choose a "random" number from the clock
    #include <string>
    using namespace std;
    
    
    void poker(string, string, string, string, string);
    void get_card(string);
    int main() 
    { 
    	string card1, card2, card3, card4, card5;
    	srand((unsigned) time(0)); // "seed" the random number generator 
    	int n=5;
    	int ansr;
    
    	cout << "\n\nTo deal yourself a poker hand please press D. " ;
    	cin >> ansr;
    	switch (ansr)
    	{
    	case 'd':
    	case 'D':
    		 poker(card1, card2, card3, card4, card5);
    		 cout << card1 << endl << card2 << endl << card3 << endl << card4 << endl << card5 << endl << endl;
    			 
    	default:
    		cout << "\n\nThanks for playing." << endl << endl;
    	}
    
    	return 0;
    }
    
    
    void poker(string card1, string card2, string card3, string card4, string card5)
    {
    	get_card(card1);
    	do
    	get_card(card2);
    	while (card2 == card1);
    	do
    	get_card(card3);
    	while ((card1==card3) || (card2==card3));
    	do
    	get_card(card4);
    	while ((card1==card4) || (card2==card4) || (card3==card4));
    	do
    	get_card(card5);
    	while ((card1==card5) || (card2==card5) || (card3==card5) || (card4==card5));
    
    	return;
    }
    
    
    void get_card(string cards)
    {
        int value = 1 + rand() % 13;
        int suit = rand() % 4;
    	string val;
    	string type;
    	string of = " of ";
        switch (value)
        {
          case 1:
            val="Ace";
            break;
          case 11:
            val="Jack";
            break;
          case 12:
            val="Queen";
            break;
          case 13:
            val="King";
            break;
          default:
            val=value;
        }
    
        switch (suit)
        {
          case 0:
            type="Spades";
            break;
          case 1:
            type="Hearts";
            break;
          case 2:
            type="Diamonds";
            break;
          case 3:
            type="Clubs";
        }
    	cards=val+of+type;
    
    	return;
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    Code:
    void poker(string card1, string card2, string card3, string card4, string card5)
    {
    	get_card(card1);
    	do
    	get_card(card2);
    	while (card2 == card1);
    	do
    	get_card(card3);
    	while ((card1==card3) || (card2==card3));
    	do
    	get_card(card4);
    	while ((card1==card4) || (card2==card4) || (card3==card4));
    	do
    	get_card(card5);
    	while ((card1==card5) || (card2==card5) || (card3==card5) || (card4==card5));
    
    	return;
    }
    This is poorly indented.
    Maybe this will explain it? http://cpwiki.sourceforge.net/User:E...ne_indentation

    As for your problem is that you're passing by-value. You should know the difference between passing by value and by reference. Do you know this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Elysia View Post
    [code]
    As for your problem is that you're passing by-value. You should know the difference between passing by value and by reference. Do you know this?
    I was told that passing strings pass by reference and not value.

    and if i change the header to
    Code:
    void poker (string &card1, string &card2, string &card3, string &card4, string &card5)
    I get the following fail.

    Code:
    Error	1	error LNK2019: unresolved external symbol "void __cdecl poker(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?poker@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0000@Z) referenced in function _main	Poker Lab.obj

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    Something is passed by reference only if you use & after the type. Otherwise it's by value. If it's a * after the type, it's a pointer. That's important to remember.
    Your problem is that your function declaration and definition doesn't match. Always keep them the same:

    Code:
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5); // Declaration
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5) // Definition
    {
    	// ...
    }
    On a side note, don't remove names for variables in the declarations either.
    Last edited by Elysia; 01-29-2008 at 09:51 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Something is passed by value only if you use & after the type. Otherwise it's by value.
    That is a typographical error. It should be:
    Something is passed by reference only if you use & after the type. Otherwise it's by value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    Oops. Haha. Fixed. Thanks for the catch.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Elysia View Post
    Something is passed by reference only if you use & after the type. Otherwise it's by value. If it's a * after the type, it's a pointer. That's important to remember.
    Your problem is that your function declaration and definition doesn't match. Always keep them the same:

    Code:
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5); // Declaration
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5) // Definition
    {
    	// ...
    }
    On a side note, don't remove names for variables in the declarations either.
    My teacher must have confused me then. I originally had the & in there and he said strings are pass by reference so i didn't need them in there and thats why they were causing the link error. I changed it to what you said above but i still have no output of the cards themselves. Could it be something in the switch statement? My default is "thanks for playing" and it always says that when it shouldn't right? Heres what i have so you can see the changes made.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime> // allows rand num gen to choose a "random" number from the clock
    #include <string>
    using namespace std;
    
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5);
    void get_card(string& cards);
    int main() 
    { 
    	string card1, card2, card3, card4, card5;
    	srand((unsigned) time(0)); // "seed" the random number generator 
    	int n=5;
    	int ansr;
    
    	cout << "\n\nTo deal yourself a poker hand please press D. " ;
    	cin >> ansr;
    	switch (ansr)
    	{	
    		case 'd':
    		case 'D':
    			poker(card1, card2, card3, card4, card5);
    			cout << card1 << endl << card2 << endl << card3 << endl << card4 << endl << card5 << endl << endl;
    
    		default:
    			cout << "\n\nThanks for playing." << endl << endl;
    	}
    
    	return 0;
    }
    
    
    void poker(string& card1, string& card2, string& card3, string& card4, string& card5)
    {
    	get_card(card1);
    	do
    		get_card(card2);
    	while (card2 == card1);
    	do
    		get_card(card3);
    	while ((card1==card3) || (card2==card3));
    	do
    		get_card(card4);
    	while ((card1==card4) || (card2==card4) || (card3==card4));
    	do
    		get_card(card5);
    	while ((card1==card5) || (card2==card5) || (card3==card5) || (card4==card5));
    
    	return;
    }
    
    
    void get_card(string& cards)
    {
        int value = 1 + rand() % 13;
        int suit = rand() % 4;
    	string val;
    	string type;
    	string of = " of ";
        switch (value)
        {
          case 1:
            val="Ace";
            break;
          case 11:
            val="Jack";
            break;
          case 12:
            val="Queen";
            break;
          case 13:
            val="King";
            break;
          default:
            val=value;
        }
    
        switch (suit)
        {
          case 0:
            type="Spades";
            break;
          case 1:
            type="Hearts";
            break;
          case 2:
            type="Diamonds";
            break;
          case 3:
            type="Clubs";
        }
    	cards=val+of+type;
    	return;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 07:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

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