I made my first C++ game! :D

Started by
12 comments, last by Chrono1081 16 years, 2 months ago
Ok you can all laugh when you see this but I was excited :D I made it for my programming class. Only thing is I cant figure out how to make the whole thing loop when you reach the bust function or the win function. Is there a way to make a function skip to the end of a loop? Here is my code.

#include <iostream>
#include <time.h>
#include <string>

using namespace std;

void wait(int);
void blackJack(void);
void bust(string);
void titleScreen(void);
void cardGenerate(int &);
void compare(int, int);
void win(string);

int  initialDeal (int &, int&);
int  playerTurn(int &);
int  houseTurn(int &, int);


void main()
{
	srand(time(0)); //Seed random number for the cards

	int pTotal=0;   //Sum of players cards
	int hTotal=0;   //Sum of house cards
	int card=0;     //Card value out of the cardGenerate()
    
	titleScreen();

	initialDeal(pTotal, hTotal);
	playerTurn(pTotal);
	houseTurn(hTotal, pTotal);
	compare(pTotal, hTotal);
}










//////////////////////////////////////////////////////////////////////////////////////////////////////////

void titleScreen()
{
	char userChoice;


	cout<<endl;
	cout<<"\t    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
	cout<<"\t    $$                                                   $$\n";
	cout<<"\t    $$ ***  *      *    **** *  *  ****   *    **** *  * $$\n";
	cout<<"\t    $$ *  * *     * *  *     * *      *  * *  *     * *  $$\n";
	cout<<"\t    $$ ***  *    *   * *     **       * *   * *     **   $$\n";
	cout<<"\t    $$ *  * *    ***** *     * *  *   * ***** *     * *  $$\n";
	cout<<"\t    $$ ***  **** *   *  **** *  *  ***  *   *  **** *  * $$\n";
	cout<<"\t    $$                                                   $$\n";
	cout<<"\t    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";	
	cout<<endl<<endl<<endl<<endl<<endl<<endl;
	cout<<"\t    Press 'S' to Start";
	cout<<endl<<endl;
	cout<<"\t    Press 'X' to Exit \n";
	cout<<endl<<endl;
	cout<<"\t    Selection: ";

	do
	{
		cin>>userChoice;
	    userChoice=toupper(userChoice);

		if(userChoice == 'S')
		{
		}

		else if(userChoice == 'X')
		{
			cout<<"\t    Thank you for playing!\n";
			exit(0);
		}

		else
		{
			cout<<endl;
			cout<<"\t    That is not a valid selection. Please enter S or X";
			cout<<endl<<endl;
			cout<<"\t    Selection: ";
		}
	}while(userChoice != 'S');
}

/////////////////////////////////////////////////////////////////////////////

void cardGenerate(int &x)
{
	x = rand() % 12+2; //Generates random value on the card
    int y = rand() % 4+3;  //Generates a random card

	if(x>9)
	{
		switch(x)
		{
		case 10:
			cout<<" --- "<<endl;
			cout<<"|"<<(char)y<<"  |"<<endl;
			cout<<"| "<<"J"<<" |"<<endl;
			cout<<"|  "<<(char)y<<"|"<<endl;
			cout<<" --- "<<endl;
			break;

		case 11:
			cout<<" --- "<<endl;
			cout<<"|"<<(char)y<<"  |"<<endl;
			cout<<"| "<<"Q"<<" |"<<endl;
			cout<<"|  "<<(char)y<<"|"<<endl;
			cout<<" --- "<<endl;
			x=10;
			break;

		case 12:
			cout<<" --- "<<endl;
			cout<<"|"<<(char)y<<"  |"<<endl;
			cout<<"| "<<"K"<<" |"<<endl;
			cout<<"|  "<<(char)y<<"|"<<endl;
			cout<<" --- "<<endl;
			x=10;
			break;

		case 13:
			cout<<" --- "<<endl;
			cout<<"|"<<(char)y<<"  |"<<endl;
			cout<<"| "<<"A"<<" |"<<endl;
			cout<<"|  "<<(char)y<<"|"<<endl;
			cout<<" --- "<<endl;
			x=11;
			break;

		default:
			break;
		}
	}

	else
		{		
		cout<<" --- "<<endl;
		cout<<"|"<<(char)y<<"  |"<<endl;
		cout<<"| "<<x<<" |"<<endl;
		cout<<"|  "<<(char)y<<"|"<<endl;
		cout<<" --- "<<endl;
		}
}

/////////////////////////////////////////////////////////////////////////////////////////

int initialDeal(int &pTotal, int &hTotal)
{
	
	int card;
	
	cout<<endl<<endl;
	cout<<"The house deals the cards...";

	wait(2);

	cout<<endl<<endl;
	cout<<"Your Cards:";
	
	wait(1);

	cout<<endl;
	cardGenerate(card);
	pTotal+=card;

	wait(1);
	
	cout<<endl;
	cardGenerate(card);
	pTotal+=card;

	if(card==11 && pTotal>21) //Tests for a second ace. If second ace is there then it will reduce ace value to 1.
	{
		pTotal=pTotal-10;
	}
	else
	{
	}

	wait(1);

	cout<<endl;

	cout<<"Total: "<<pTotal; //Tests for BlackJack

		if(pTotal == 21)
		{
			blackJack();
		}
		else
		{
		}
    ///Players deal ends, House deal begins

	wait(2);

	cout<<endl<<endl;
	cout<<"House Cards:";

	wait(1);
	
	cout<<endl;
	cardGenerate(card);
	hTotal+=card;

	wait(1);

	cout<<endl;
	cardGenerate(card);
	hTotal+=card;
	
	if(card==11 && hTotal>21) //Tests for the ace. If the ace throws the player over then ace value is 1 instead of 11.
	{
		hTotal=hTotal-10;
	}
	else
	{
	}

	wait(1);

	cout<<endl;
	cout<<"Total: "<<hTotal;
	cout<<endl<<endl;

	wait(1);

	if(hTotal == 21) //Tests for BlackJack
		{
			blackJack();
		}
		else
		{
		}

	return pTotal, hTotal; 

	
}

//////////////////////////////////////////////////////////////////////////////////////////////////

void wait (int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

void blackJack()
{
	cout<<endl;
	cout<<"\t    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
	cout<<"\t    $$                                                   $$\n";
	cout<<"\t    $$ ***  *      *    **** *  *  ****   *    **** *  * $$\n";
	cout<<"\t    $$ *  * *     * *  *     * *      *  * *  *     * *  $$\n";
	cout<<"\t    $$ ***  *    *   * *     **       * *   * *     **   $$\n";
	cout<<"\t    $$ *  * *    ***** *     * *  *   * ***** *     * *  $$\n";
	cout<<"\t    $$ ***  **** *   *  **** *  *  ***  *   *  **** *  * $$\n";
	cout<<"\t    $$                                                   $$\n";
	cout<<"\t    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
	cout<<endl<<endl<<endl<<endl<<endl<<endl;
	cout<<"\t                             WIN!!!\n";
	exit(0);
}

///////////////////////////////////////////////////////////////////////////////////////////////////

void bust(string text)
{
	
	cout<<endl<<endl;
	cout<<"***  *   *  ***  *****\n";
	cout<<"*  * *   * *   *   *  \n";
	cout<<"***  *   *  ***    *  \n";
	cout<<"*  * *   * *   *   *  \n";
	cout<<"***   ***   ***    *  \n";
	cout<<endl<<endl<<endl;
	cout<<text<<" lost!!!";
	cout<<endl<<endl;
	exit(0);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

int playerTurn(int &pTotal)
{
	int  card;

	char userChoice;
	char player[20]="You";

	do
	{
		cout<<endl<<endl;
		cout<<"Would you like another card? Y or N";
		cin >> userChoice;
		userChoice=toupper(userChoice);

		if(userChoice == 'Y')
		{
			cardGenerate(card);
			pTotal=pTotal+card;
			
			if(card==11 && pTotal>21) //Tests for ace throwing player over 21
			{
				pTotal=pTotal-10;
			}
			else
			{
			}
			cout<<endl;
			cout<<"Total: "<<pTotal;

			if(pTotal>21)
			{
				cout<<endl;
				bust(player);
			}

			else
			{
			}
		}
	}while(userChoice != 'N');

	cout<<endl;
	cout<<"You chose to stay...";
	cout<<endl;

	return pTotal;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

int houseTurn(int &hTotal, int pTotal)
{
	int rndNum=rand()%4+14;
	int loop=1;
	int card;

	char house[20]="The House";

	do
	{
		if(hTotal>=rndNum && hTotal>pTotal)
		{
			cout<<endl;
			cout<<"The house chooses to stay...";
			cout<<endl;
			loop=0;
		}

		else
		{
			cout<<endl;
			cout<<"The house draws a card...";

			wait(1);
			
			cout<<endl;
			cardGenerate(card);
			cout<<endl<<endl;
			
			hTotal=hTotal+card;

			if(card==11 && hTotal>21)
			{
				hTotal=hTotal-10;
			}
			else
			{
			}
			cout<<endl;
			cout<<"Total: "<<hTotal;

			if(hTotal>21)
			{
				bust(house);
			}
			else
			{
			}
		}
	}while(loop != 0);

	return hTotal;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

void compare(int pTotal, int hTotal)
{
	char player[20]="You";
	char house[20] ="The House";

	if(pTotal>hTotal)
	{
		cout<<endl;
		win(player);
	}
	
	else if(pTotal<hTotal)
	{
		cout<<endl;
		win(house);
	}

	else
	{
		cout<<endl;
		cout<<"Theres a tie!!!";
		cout<<endl;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

void win(string text)
{
	cout<<endl;
	cout<<text<<" Wins!"<<endl;;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////


Advertisement
Congrats! [smile]

Quote:Original post by Chrono1081
Is there a way to make a function skip to the end of a loop?

Use the break key word, as in:
if (pTotal > 21){  cout << endl;  bust(player);  break; // I think we're done here!}

Along the same lines, you can use continue to skip the rest of the code in the loop and to proceed directly with the next itteration, as in:
while (true){  if (something)  {    cout << "I can't be bothered with the rest of the code.";    continue;  }  cout << "If 'something' was true, we shouldn't see this line.";}
Congrats and good job!
[cool]
Nice job!

You can break out of a loop by using the keyword break.

Im not sure if that is what you mean by skip to the end of a loop or not.


Good Job dude! it's always refreshing feeling after you complete a project :) now keep learning more C++ and don't give up ;)
Thanks guys :) Im gonna work on making the game loop tonight then after that work on making a gambling element :D
congrats!
nice job!

though it's not good to use 'goto',it can skip the loop.
I may have to use goto :/ Its the only way I can think of to skip to the end of the loop :/ The suggestions from other posters dont seem to work inside a function :'(
Quote:Original post by Chrono1081
I may have to use goto :/ Its the only way I can think of to skip to the end of the loop :/ The suggestions from other posters dont seem to work inside a function :'(


Are you trying to just skip some code inside of a loop and not break out of it? I'm trying to figure out why break or continue won't give you what you need.
Whoah don't use break, continue or goto. Properly structure your code with conditionals.
There is *never* a situation where you have to use break/continue/goto. They make the code unreadable and difficult to follow.

This topic is closed to new replies.

Advertisement