Simple Blackjack

Simple Blackjack program

Page 1 of 1

6 Replies - 3709 Views - Last Post: 25 March 2010 - 04:25 AM Rate Topic: -----

#1 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Simple Blackjack

Posted 24 March 2010 - 03:22 AM

All card values are between 1 and 10. No cards are worth 11.
- If you get 21, you win at that point and the house does not draw (game over).
- If you bust (go over 21), you lose at that point and the house does not draw (game over).
- If you hold with less than 21, the house will keep drawing until his score is bigger
than yours (he wins), or his score is 21 (he wins), or his score is over 21 (you win).

**** I left the notes in and filled in the code i though needed according the the parameters given within the notes, but I am missing something or doing something wrong. Help or even just a point in the right direction would be appreciated.

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

void play21();
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random();

int main()
{
	char keepPlaying = 'n';
	keepPlaying = toupper(keepPlaying);
   
	do
	{
		play21();

		cout << "Do you want to play another hand? (y/n): ";
		cin >> keepPlaying;
	} 
	
	while (keepPlaying == 'y' || keepPlaying == 'Y');
}

void play21()
{
	int person = 0;   // your score
	int house = 0;    // computer's score
	
	// radomize the cards
	srand(static_cast<int>(time(0)));

	// Your first two cards are dealt
	// and your score is displayed. The cards
	// that were dealt will be displayed
	// in the dealCards function.
	person = dealCards(2, "Your cards: ");
	cout << " = " << person << endl;

	// The house's first two cards are dealt
	// and house's score is displayed. The cards
	// that were dealt will be displayed in
	// the dealCards function.
	house = dealCards(2, "Computer cards: ");
	cout << " = " << house << endl;

	// Now we see if you want another card.
	// Inside the hit function, your score will
	// be updated and we will see if you busted
	hit(person);
	cout << endl;

	// We use this while loop to see if the house has to take another card.
	// If you have not busted and are holding with less than 21, house has to hit.
	while (house < person && house < 21 && person < 21)
	{
		house = house + dealCards(1, "The house takes another card");
		cout << "The house now has " << house << endl;
	}

	// Now we find out who won
	determineWinner(person, house);
}

int dealCards(int numberOfCards, string message)
{
	int total = 0;

	cout >> "Your cards: ";

	for (x+ 1; x <= numberOfCards; x += 1)
	{	
		random();
		cout << total
		
		total = total + x;
	}
	
	return total; 
		
	// First, you need to display the message that was passed in.
	// Then you need to deal the number of cards passed in.
	// This can be done using a loop (for loop would be easier).
	// Within the loop, call the Random() function to deal the card.
	// This card must be displayed using a cout.
	// It then needs to be added to a total variable.
	// When the loop is finished, the total is returned.


}

void hit(int &playerScore)
{
	char answer = ' ';

	cout >> "Do you want to hit: ";
	cin << answer;

		while (answer == "y" || answer == "Y" && playerScore < 21)
		{	
			cout << dealCards(1,string "Your cards =: );

			playersScore =playerScore + dealCards(1, "Your score is: ");
			
		}
		if (playerScore > 21)
			cout << "You Bust!!!";
		else 
		{	cout << "Do you want to hit: ";
			cin >> answer;
		}
	// Notice playerScore is passed in by reference.
	// That means it will have a new value when this function is done.
	// You need to ask the player if they want to hit (take another card).
	// So you need a cout/cin combination. The cin should put the 
	// player's response in a variable (probably a char variable).
	// Once the player has responded, you need to set up a loop to get the
	// the next card and add it to the player's score. This will probably
	// be a while loop because you want to keep asking the player if they want
	// to hit as long as the following are true: the player wants another card 
	// AND the player's score is less than 21. 
	// In the loop, call the dealCards function to deal 1 card. Then add that card
	// to the player's score and display the new score. If the player's score is less
	// than 21, ask the player if they want to hit again.
	// When the loop is finished, you need to check if the player's score is over 21
	// or exactly 21. In either case, display a message.
	

}

void determineWinner(int humanScore, int houseScore)
{
	if (humanScore == 21)
		cout << "Blackjack!! You win!!!" << endl;
	else if (humanScore < 21 && houseScore > 21)
		cout << "Dealer Bust!! You win!!" << endl;
	else if (humanScore > 21)
		cout << "You Bust....Dealer wins" << endl;
	else if (humanScore < 21 && houseScore == 21)
		cout << "Dealer has Blackjack...You loose" << endl;
	else (humanScore < 21 && houseScore > humanScore)
		cout << "Dealer wins." << endl;

	// This function will be a nested if-else statement, although it is
	// possible to have only one if-else combination. 
	// You need to display a message using a cout that says who won.
	// You win if:
	//  - you have 21
	//  - you have less than 21, but the house went over 21
	// The house wins if:
	//  - you go over 21
	//  - the house gets 21 after you hold with less than 21
	//  - neither of you has 21, but the house's score is bigger than your score
	// Out of all this, you have to figure out how your if-else statements work.

}

int Random()
{
	// returns a random number between 1 and 10
	return 1 + rand() % (10 - 1 + 1);
}




Is This A Good Question/Topic? 0
  • +

Replies To: Simple Blackjack

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Simple Blackjack

Posted 24 March 2010 - 03:47 AM

Uhh, well the srand() function should be called once for the lifetime of the program, and you should start using classes, it will make all of your code re-usable. I don't know how your card game works, but the normal way I do it is creating a deck of cards:

deque <card> deck(52);
The double ended queue will work
fine for a deck of cards
or
vector <card> deck(52); // 52 cards..

then fill the card structures out using some loops or something

and finally random_shuffle works prefectly for shuffling a deck of cards!
to make sure the cards aren't played more than once, they should be removed as they are used.
#include <algorithm>
//call random_shuffle on the vector
random_shuffle();




I don't really know what your professor wants, and what is it that you're wanting to improve in your code anyway?

This post has been edited by taylorc8: 24 March 2010 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Simple Blackjack

Posted 24 March 2010 - 03:52 AM

taylorc8, nice approach, but I think that implementing a full deck is a bit too much for this assignment.
The idea here is that an infinite amount of cards with value 1-10 is available, so a simple random function is sufficient.

By quickly scanning the code I think that it doesn't compile yet.
NoLove6, please try to fix your code so it compiles...
Then we can go to the next step: finding and fixing the bugs.
Was This Post Helpful? 0
  • +
  • -

#4 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: Simple Blackjack

Posted 24 March 2010 - 06:33 PM

That is what I'm looking for help with @Ferencn. I can't get it to compile, so I know something is wrong somewhere, I just kind figure out what. I am not looking for anyone to do it for my, just just pointing out where the mistakes are so I can go in and see what I did wrong and try and fix it.
Was This Post Helpful? 0
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Simple Blackjack

Posted 24 March 2010 - 09:23 PM

Tell us the error messages you are receiving and what compiler you're using!
Was This Post Helpful? 0
  • +
  • -

#6 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: Simple Blackjack

Posted 25 March 2010 - 01:03 AM

I am using Visual Studio 2008 and the error messages are:

: error C2143: syntax error : missing ',' before '+'
: warning C4552: '<=' : operator has no effect; expected operator with side-effect
: error C2143: syntax error : missing ';' before ')'
: error C2143: syntax error : missing ';' before ')'
: error C2143: syntax error : missing ';' before '{'
: error C3861: 'random': identifier not found
: error C2446: '==' : no conversion from 'const char *' to 'int'
There is no context in which this conversion is possible
: error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
: error C2446: '==' : no conversion from 'const char *' to 'int'
There is no context in which this conversion is possible
: error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
: error C2143: syntax error : missing ')' before ';'
: error C2065: 'playersScore' : undeclared identifier
: error C2146: syntax error : missing ';' before identifier 'cout'

- 12 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The errors I have tried to go in and fix I either couldn't find them or wasn't able to find what it was talking about.
Was This Post Helpful? 0
  • +
  • -

#7 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: Simple Blackjack

Posted 25 March 2010 - 04:25 AM

I have worked on it further and have been able to make some significant changes though I still have a few errors:
: error C2065: 'x' : undeclared identifier
: error C2065: 'x' : undeclared identifier
: error C2065: 'playersScore' : undeclared identifier

I swear some of these errors seem like they are simple but I can't figure them out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1