blackjack help c++..stuck

Started by
3 comments, last by visitor 15 years, 9 months ago
I have code to my blackjack project. I am stuck right now and was wanting to see if the code I have now is correct, what I should do next and information dealing with what to do in main. Cards can be displayed in a shorthand. For example 2-S for the 2 of spades card, K-D for the king of diamonds. To help the user display the total for his current hand every time a decision is required. In our game the dealer will always stand on values of 17 or more. For simplicity in our game aces will always be worth 11 points. We will not implement betting, splitting or more complicated rules. You will need to define and implement a base class (does not have to be abstract) called Card. This class should store the cards name, suit, and point value. There should be functions for getting the name, suit, and value. There should be a class for each suit (Spade, Club, Diamond, Heart) that inherits from the Card class. There should a class for each card value (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace) that inherits from the suit classes. There should be a Deck class that includes 52 Card objects. Other classes can be added to fit your design. So I have created a class for every card, all 4 suits, Card and Deck. Ex. aceOfSpades.h , twoOfClubs.h and they all inherit from my from there suit Class and the suits inherit from Card. In each .h file of each card I have assigned the number for it already. Ex.

aceOfClubs::aceOfClubs()
{
	number = 11;
}
Here is my Card class:

#include "Card.h"


	Card::~Card() {}

	Card::Card(int SUIT, int num)
	{
		number=num;
		suit=SUIT;
	}

	int Card::getValue(const)
	{
		return number;
	}

	int Card::getSuit()
	{
		return suit;
	}

	void Card::setValue(int x)
	{
		number = x;
	}

	void Card::setSuit(int x)
	{
		suit = x;
	}

	void Card::print()
	{
		cout << "Suit:" << suit << "Number:" << number << endl;
	}

And my Deck class..I have not added all 52 card objects as I am stuck on knowing how to do it. Instead I used enum in the main.cpp for each but I am not sure if I am supposed to do that.

#include "Card.h"


	Card::~Card() {}

	Card::Card(int SUIT, int num)
	{
		number=num;
		suit=SUIT;
	}

	int Card::getValue(const)
	{
		return number;
	}

	int Card::getSuit()
	{
		return suit;
	}

	void Card::setValue(int x)
	{
		number = x;
	}

	void Card::setSuit(int x)
	{
		suit = x;
	}

	void Card::print()
	{
		cout << "Suit:" << suit << "Number:" << number << endl;
	}

And this is all I have in main.cpp

#include <iostream>
#include <ctime>
using namespace std;

	void shuffleDeck()
	{
				
		for (int i=0;i<52; i++)
		{
			swap Deck, Deck[RAND_MAX % 52];
		}
	}

int main()
{
	enum Cards { one=1, two=2, three=3, four=4, five=5, six=6, seven=7, eight=8, nine=9, ten=10, Jack=10, Queen=10, King=10, Ace=11 }
	enum Suits { Clubs, Diamonds, Hearts, Spades }







	char input;
	cout << "Do you want to play again (Y/N) ? "  ;
	cin >> input;

	if ((input == "y") || (input == "Y"))
	{
		shuffleDeck();
		break;
	}
	
	if ((input == "n") || (input =="N")
	{
		break;
	}

	return 0;
}

Advertisement
You do not need a sub-class for each card.

enum card_suit{clubs, diamonds, spades, hearts};class Card{    card_suit suit;    int value;    Card(card_suit new_suit, int new_value){        suit = new_suit;        value = new_value;    }};


Then add methods that return the type, number, or more importantly, one that returns a shorthand string.

You're missing a loop in main. You're missing a lot of things, too. I'm assuming 'swap' is a function? If that's the case, you're missing parentheses. And if you're only including <ctime> for rand, it's in <cstdlib>. Have you compiled this?
this is what I have now for my card and deck class:

class Card{public:    Card(){}    Card(int SUIT, int num)    {        number = num;        suit = SUIT;    }    int getNum()	{		return number;	}    int getSuit()	{		return suit;	}    void setNum(int x)	{		number = x;	}    void setSuit(int x)	{		suit = x;	}    void print()    {        cout &lt;&lt; "Suit: " &lt;&lt; suit &lt;&lt; " Number: " &lt;&lt; number &lt;&lt;endl;    }private:    int number;	int suit;};class Deck{public:    Deck()    {        int temp = 0;        int count = 0;        for (int i = 1; i &lt; 53; i++)        {            if ( count % 13 == 0) count = 0;			{				cardArray[i-1] = new Card(temp, count);			}            if ( i % 13 == 0) temp++;			{				count++;			}        }         //end i    }             //end constructor    void shuffle(){}    void deal(){}    void output()    {        for (int i = 0; i &lt; 52; i++)        {            cardArray-&gt;print();        }    }private:    Card * cardArray[52];};
That's better, but you're going to have to define constants for each suit, or use an enum. Seriously, this is what enums are destined for; variables that are only allowed to have a certain small set of values.

I'm not sure what those braces are for after your if statements. It seems you only want the if to apply to the statement immediately after the if, before the braces. If that's the case, then the braces are extraneous. You should really put them around the statement you want to be affected by the if.
You might also do something about this:

private:     Card * cardArray[52];cardArray[i-1] = new Card(temp, count);


There is no reason to enter the land of memory leaks or double deletes over something as simple as that.

Instead use automatic memory management of stack instances:
private:     Card cardArray[52];cardArray[i-1] = Card(temp, count);

This topic is closed to new replies.

Advertisement