Thread: First program (Black Jack game)

  1. #1
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29

    First program (Black Jack game)

    I am busy building this black jack game, and been struggling with something...

    I know its not programmed in perfect language, but i do try to only use the functions i know.

    Now the problem i have is that, this part
    Code:
    #include<iostream>
    #include<stdlib>
    #include<time>
    #include<string>
    using namespace std;
    
    int main()
    {
    	char Choice, Play;
    	int Card1, Card2, Card3, Card4, Card5, CardTotal, PCTotal;
    	int Cash, Bet;
    	string Name;
    	srand(time(0));
    	
    	
    	cout<<"\n		*******************"
    	      "\n		*BlackJack v0.001 *"
    	      "\n		*By RHiNO         *"
    	      "\n		*Started 20/02/06 *"
    	      "\n		*******************"<<endl;
    	cout<<"\n\n\n";
    	while(true)
    	{
    		cout<<" 		WELCOME \n 		MAIN MENU\n"<<endl;
    		cout<<"Please select from the following:\n";
    		cout<<"p: Play Game.\n";
    		cout<<"q: Quit.\n";
    		cin>>Choice;
    		cin.ignore();
    		if(Choice=='q')
    		{
    			cout<<"Thank you for playing!";
    			break;
    		}
    		if(Choice=='p')
    		{
    			cout<<"You selected to start a game.\nl: Load Game.		n: New Game\n";
    			cout<<"Choice: ";
    			cin>>Play;
    			if(Play=='n')
    			{
    				cout<<"What is your name?\n";
    				cin>>Name;
    				Cash=100;
    			}
    			if(Play=='l')
    			{
    				Name="loaded name";
    				Cash=132;			//loaded amount
    				cin.get();
    			}
    			cout<<"Hey "<<Name<<". You have R"<<Cash<<" in your pocket\n";
    			while(true)
    			{
    				if(Cash<1)
    				{
    					cout<<Name<<", you are bancrupt. \nGAME OVER\n";
    					break;
    				}
    				cout<<Name<<" you have R"<<Cash<<" left... \nWhat would you like to do?\n";
    				cout<<"p: Play.		s: Save		m: Main Menu\n";
    				cout<<"Selection: ";
    				cin>>Choice;
    				cin.ignore();
    				if(Choice=='m')
    				{
    					break;
    				}
    				if(Choice=='s')
    				{
    					cout<<"Feature disabled... Press enter to continue.\n";
    					cin.get();
    					continue;
    				}
    				if(Choice=='p')
    				{
    					NoMoney:
    					cout<<"How much would you like to bet: ";
    					cin>>Bet;
    					if(Bet>Cash+1)
    					{
    						cout<<"You do not have that much money.\n";
    						goto NoMoney;
    					}
    					if(Bet<1)
    					{
    						cout<<"Sorry you cant bet less than R1!\n";
    						goto NoMoney;
    					}
    					if(Bet<Cash+1&&Bet>0)
    					{
    						Card1=1+rand()%11;
    						Card2=1+rand()%10;
    						Card3=1+rand()%11;
    						Card4=1+rand()%10;
    						Card5=1+rand()%10;
    						Cash=Cash-Bet;
    						CardTotal=Card1+Card2;
    						PCTotal=Card3+Card4;
    						cout<<Name<<" place a bet of R"<<Bet<<" and have "<<Cash<<" left\n";
    						cout<<Name<<" gets two cards which are "<<Card1<<" and "<<Card2<<". You have "<<CardTotal<<"\n";
    						cout<<"The computer gets "<<Card3<<" and "<<Card4<<". The computer has "<<PCTotal<<"\n";
    						if(CardTotal==21)
    						{
    							if(PCTotal==21)
    							{
    								cout<<"Its a draw.\n";
    								Cash=Cash+Bet;
    							}
    							else
    							{
    								cout<<"BLACKJACK "<<Name<<" wins R"<<Bet+Bet<<"!\n";
    								Cash=Cash+Bet+Bet+Bet;
    							}
    						}
    						if(PCTotal==21)
    						{
    							cout<<"The computer has BLACKJACK";
    						}
    						if(CardTotal<21)
    						{
    							while(true)
    							{ 
    								cout<<"Would you like another card?\ny: Yes		n: No\n";
    								cin>>Choice;
    								cin.ignore();
    								if(Choice=='n')
    								{
    									break;
    								}
    								if(Choice=='y')
    								{
    									cout<<Name<<" gets a "<<Card5<<"\n";
    									CardTotal=CardTotal+Card5;
    									cout<<Name<<" got a score of "<<CardTotal<<" now.\n";
    									if(CardTotal>21)
    									{
    										cout<<Name<<" looses!!\n";
    										break;
    									}
    									
    									continue;
    								}
    							}
    						}
    						if(PCTotal<17)
    						{
    							while(true)
    							{
    								cout<<"The computer takes another card, "<<Card5<<"!\n";
    								PCTotal=PCTotal+Card5;
    								cout<<"PC now has "<<PCTotal<<" points\n";
    								if(PCTotal>17)
    								{
    									break;
    								}
    								if(PCTotal<17)
    								{
    									continue;
    								}
    							}
    						}
    						
    					}
    
    					
    					continue;
    				}
    			}
    				
    			
    			continue;
    		}
    		
    		cout<<"Invalid choice!!\n";
    	}
    	cin.get();
    }
    on line 105 does not randomise everytime i use it...

    Any help icw the specific problem will be appreciated, other bugs ill try find and fix myself.

    Thanks alot

    PS:: How do i add attachments?

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Telling us line 105 doesn't help any. There are no line numbers and I'm not counting them. But I'm guessing that you use rand() without seeding the randomizer. Once in the program you should call srand() before the calls to rand().
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Nope, he seeds rand. The only problem I have, without looking thoroughly, is line 105 doesn't have rand() in it at all. It's just an if statement. So telling us line 105 *really* doesn't mean anything.

    ...and secondly <stdlib> and <time> are not libraries. Try <cstdlib> and <ctime>.

    If you're talking about where you ask for a new card, then you should look at your code (ln. 134). Perhaps you should actually give them a random card not the same predetermined card.
    Last edited by SlyMaelstrom; 02-21-2006 at 01:03 PM.
    Sent from my iPad®

  4. #4
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Sorry, been on bout line 96
    Code:
    Card5=1+rand()%10;
    when i call it later in a loop, it gives me the same value.
    i use it here
    Code:
    while(true)
    							{ 
    								cout<<"Would you like another card?\ny: Yes		n: No\n";
    								cin>>Choice;
    								cin.ignore();
    								if(Choice=='n')
    								{
    									break;
    								}
    								if(Choice=='y')
    								{
    									cout<<Name<<" gets a "<<Card5<<"\n";
    									CardTotal=CardTotal+Card5;
    									cout<<Name<<" got a score of "<<CardTotal<<" now.\n";
    									if(CardTotal>21)
    									{
    										cout<<Name<<" looses!!\n";
    										break;
    									}
    									
    									continue;
    								}
    							}
    and here
    Code:
    while(true)
    							{
    								cout<<"The computer takes another card, "<<Card5<<"!\n";
    								PCTotal=PCTotal+Card5;
    								cout<<"PC now has "<<PCTotal<<" points\n";
    								if(PCTotal>17)
    								{
    									break;
    								}
    								if(PCTotal<17)
    								{
    									continue;
    								}
    							}
    Or do i have to use different random cards? i just dont know anymore...

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    When you assign a random value to a variable

    Code:
    int var1 = rand() % 20 + 3;
    It calls the rand function which returns a number that you then perform a mathematical operation on to get your range. It REMAINS that value forever. It doesn't generate a random number everytime you access the data. The variable doesn't contain rand() in it. It's just a number at that point. If you want it to generate a new number you have to reassign the variable with the rand function.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It REMAINS that value forever
    What a ripoff! C++ sucks.
    Last edited by 7stud; 02-22-2006 at 01:07 AM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Forever until you change it, that is.
    Sent from my iPad®

  8. #8
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Quote Originally Posted by SlyMaelstrom
    Forever until you change it, that is.
    Even i knew that... okay, so now i moved the
    Code:
    Card5=1+rand()%10;
    into the loop i want to use it in, and it works,

    Now a nother question (I should probably have found this out earlier, sinse i started c++ a month ago) but is there any keyword/command i can use to clear the screen?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    No. But, you can clear the screen. Here's a tutorial:

    http://www.adrianxw.dk/SoftwareSite/...Consoles1.html

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    None that are portable.

    Look at this: FAQ > How do I... (Level 1) > Clear the screen?
    Sent from my iPad®

  11. #11
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Thank you

  12. #12
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Now the next thing i am havin trouble with, something u gonna probably laugh about, but i always say, ask and be the fool, while learning something than keep your mouth and always wonder...

    The program/game works fine now, i added a save game and load game option...

    Now 2 problems..

    1: How do i tell the program to look for a saved game (savegame.dat) and if it does not find it, tell the player that the saved game can not be found?

    2: At the moment i am using the following for the save game and load game:
    LOAD:
    Code:
    ifstream Rdn("name.dat");
    				Rdn>>Name;
    				Rdn.close();
    				ifstream Rds("score.dat");
    				Rds>>Cash;
    				Rds.close();
    				cout<<"Welcome back "<<Name<<" You have R"<<Cash<<" left!\n";
    				cin.get();
    SAVE
    Code:
    ofstream Wrtn("name.dat");
    					Wrtn<<Name;
    					Wrtn.close();
    					ofstream Wrts("score.dat");
    					Wrts<<Cash;
    					Wrts.close();
    					cout<<"Thank you "<<Name<<", your R"<<Cash<<" has been saved.\n";
    					cin.get();
    As you can see i am using 2 files to store data, name.dat and score.dat... now, how can i save both score and name in one file? and how can i encrypt it so that players cant open the .dat file with notepad and change their score?
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    > 1: How do i tell the program to look for a saved game (savegame.dat) and if it does not
    > find it, tell the player that the saved game can not be found?

    Code:
    ifstream Rdn(“fname”, ios_base::in);// Open to read
    if (!Rdn) { // If open fails
      // File doesn't exist and don't create it. This is where your error message goes.
    }
    > As you can see i am using 2 files to store data, name.dat and score.dat... now, how can i
    > save both score and name in one file? and how can i encrypt it so that players cant open
    > the .dat file with notepad and change their score?

    Simply save them both to the same file one after the next. O_o
    If you're having trouble identifying the name from the score when you load. Then put identifying keywords in the .dat that you can look for and get the appropriate information.

    For encryption, look into bitwise operators. Those are always a good choice for encryption algorithms without having to go too serious with it.
    Sent from my iPad®

  14. #14
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Quote Originally Posted by SlyMaelstrom

    >Simply save them both to the same file one after the next. O_o
    >If you're having trouble identifying the name from the score >when you load. Then put identifying keywords in the .dat that >you can look for and get the appropriate information.
    .

    How do i do it?
    guess it will be something like :
    Code:
    ofstream wrt("namenscore.dat");
        wrt>>Name>>Cash;
    and to recall
    Code:
    ifstream rd("namenscore");
       rd<<Name<<Cash;
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not such a great idea to post two variables RIGHT NEXT to each other in text files. Especially when the first thing you're reading is a string. Anyway, start writing your encryption algorithm. Most likely you're going to have something that looks like this when you save:

    Code:
    ofstream wrt("namenscore.dat", ios_base::out);
     if (wrt) {
       wrt << ":: This file contains encrypted player information - Editting it could permently ruin your save game ::\n\n"
       << "Name: "  << encrypt(Name) << '\n'
       << "Money: " << encrypt(Cash);
    
       wrt.close();
    }
     else
       cout << "Save game does not exist.";
    Last edited by SlyMaelstrom; 02-24-2006 at 02:10 AM.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this Game Maker-like c program look
    By hahaguy in forum Game Programming
    Replies: 0
    Last Post: 04-23-2009, 06:32 AM
  2. Replies: 9
    Last Post: 11-11-2007, 02:31 PM
  3. Problems in a game program
    By ChronoSquare in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 07:32 AM
  4. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM
  5. need help with a guessing game program
    By davidnj732 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 11:59 AM