Sign in to follow this  
Chrono1081

Completely Stumped

Recommended Posts

Sorry for posting the same program twice on here but I am completely stuck. Ive searched the internet high and low for answers but could find none. I have the following code for a blackjack game:
#include <iostream>
#include <time.h>
#include <string>
#include <algorithm>

using namespace std;

void wait(int);
void titleScreen(void);
void cardGenerate(int&, int&);
int  cardDeal(int, int);


void main()
{

	int yCard1;
	int yCard2;

	titleScreen();

	cout<<"Your Cards:\n";
	
	cardGenerate(yCard1,yCard2);
	cout<<endl;
	wait(2);
	cardGenerate(yCard1,yCard2);
	

	
}

//------------------------------------------------------------------------------------

void titleScreen(void)
{
	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";	
}

//----------------------------------------------------------------------------------------

void cardGenerate(int &x, int &y)
{
	srand(time(0));

	x = rand() % 12+2; //Generates random value on the card
    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;
			break;

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

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

		default:
			break;
		}
	}

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

//----------------------------------------------------------------------------------------------

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

//----------------------------------------------------------------------------------------------


Now the problem is I would like the output to look like this:

Your Hand:                 House Hand:
 ---   ---                 ---   ---
|   | |   |               |   | |   |
|   | |   |               |   | |   |
 ---   ---                 ---   ---

Is there any way out there that I can line these cards up and still keep the random card/number generator that I have created? Any help would be greatly appreciated.

The card images should be under the owners of the cards and the cards should be side by side.
[\source]
[Edited by - Chrono1081 on January 28, 2008 10:26:03 PM]

Share this post


Link to post
Share on other sites
Each time you print a card, you are outputting several lines of text. You can't really go back to a previous line to stick stuff onto it; that ship has sailed. The output is a *stream*. There are platform-specific ways to get around that (i.e. treating the console as a grid of characters instead of just a place to output text), but you should first learn how to handle this logically.

Try to assemble each line of text that you want to output, first, and then output all the lines. The natural way to represent a line of text in C++ is with std::string. You can collect several strings in a vector: std::vector<std::string>, and each time you determine a card, you can append the appropriate text to each line (you can "add" to a std::string with +=, to join the strings up). Then output them all at the end.

Notice that because each "row" of a card text is the same length, this will correctly align everything. Trace through the process on a piece of paper if you aren't convinced.




By the way, do not put the srand() call into cardGenerate(). Put it at the beginning of main(). You want to call it only once, except for specific reasons that you don't yet have to worry about. Otherwise, you get very un-random results.

Share this post


Link to post
Share on other sites
Thank you smitty for telling me about the source tags, I knew they were there but I did not know what they were.

Thank you Zahlman for your suggestions. I moved the srand and it does make the numbers alot more random. Before if I clicked execute too fast I would get the same cards (hence the wait() ).

Im going to research what you said about vectors and give that a try. Its not the end of the world if they dont align (since in the project we are only to use numbers but I wanted to take it a step farther :D )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this