Advertisement

blackjack game

Started by July 21, 2018 10:50 PM
2 comments, last by ICanC 6 years, 7 months ago

well I am making a text based blackjack game I am unsure of how to progress. here is the skeleton of my program. I am  going to work on this more, and do some research on oop design. I don't want code. I am just trying to be a better programmer. I am also learning how to program in oop paradigm.


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

using namespace std;

enum suit { S, H, D, C };
enum face { J = 11, Q, K, A };

class card
{
private:

public:

};

class deck
{
private:

public:

};

int main()
{



	system("pause");
	return 0;
}

 

There is almost no code, so there's almost nothing to say. This is like saying "I'm going to write a book, and so far I have a main character called John. Is my book any good?".

The only real comments I have on your code is that the enums could have more descriptive names (Hearts instead of H, etc.), and that currently your face enum doesn't support values of 1-10. And that you're including time.h, when no part of your current code uses it.

Oh, and "using std" is generally not a good idea, because it can cause naming conflicts in larger projects. For such a tiny project it's probably not a huge deal, but I thought I could mention it.

 

If you want proper feedback, you're going to have a lot more to show. Preferably a working version of the game.

Hello to all my stalkers.

Advertisement

I'm a complete amateur but just in case you know less than I do, here is some example code from a text based blackjack I attempted to make which may give you some ideas:

 


class blackjack {
private:
	std::vector<card> deck;
	int minBet;
	int maxBet;
	int decks;
public:
	blackjack(int minBet, int maxBet, int decks);
	~blackjack() {};
	void shuffle();
	void reshuffle();
	int getValue(std::vector<card> hand) const;
	card deal();
	void play(std::shared_ptr<player> p);
	bool check_for_blackjack(std::vector<card> hand);
	bool bust(std::vector<card> hand);
};
      
blackjack::blackjack(int minBet, int maxBet, int decks)
{
	this->minBet = minBet;
	this->maxBet = maxBet;
	this->decks = decks;
	this->reshuffle();
}

void blackjack::shuffle()
{
	static std::random_device rd;
	std::shuffle(this->deck.begin(), this->deck.end(), std::mt19937(rd()));
}

void blackjack::reshuffle()
{
	this->deck.clear();
	for (int i = 1; i < 14; ++i)
	{
		for (int j = 1; j < 5; ++j)
		{
			if (i == 1)
			{
				card c1("Ace", i, j);
				this->deck.push_back(c1);
			}
			else if (i == 11)
			{
				card c1("Jack", i - 1, j);
				this->deck.push_back(c1);
			}
			else if (i == 12)
			{
				card c1("Queen", i - 2, j);
				this->deck.push_back(c1);
			}
			else if (i == 13)
			{
				card c1("King", i - 3, j);
				this->deck.push_back(c1);
			}
			else
			{
				card c1(std::to_string(i), i, j);
				this->deck.push_back(c1);
			}
		}
	}
}

card blackjack::deal()
{
	card dealCard = this->deck[this->deck.size() - 1];
	this->deck.pop_back();

	return dealCard;
}

int blackjack::getValue(std::vector<card> hand) const
{
	int value = 0;
	for (card &c : hand)
	{
		value += c.value;
	}
	return value;
}

bool blackjack::check_for_blackjack(std::vector<card> hand)
{
	int value = 0;
	for (auto &i : hand)
	{
		value += i.value;
	}
	if (value == 21)
	{
		return true;
	}
	else
		return false;
}

bool blackjack::bust(std::vector<card> hand)
{
	int value = 0;
	for (auto &i : hand)
	{
		value += i.value;
	}
	if (value > 21)
	{
		return true;
	}
	else
		return false;
}

/*-------------------------------------------------------------------------------------------------------*/

class card {
public:
	std::string name;
	int value;
	int suit;
	card(std::string name, int value, int suit);
	card() {};
	~card() {};
	std::string getSuit() const;
	void display() const;
};

card::card(std::string name, int value, int suit)
{
	this->name = name;
	this->value = value;
	this->suit = suit;
}

void card::display() const
{
	print(this->name);
	if (suit == 1) { print(" of Hearts\n"); }
	if (suit == 2) { print(" of Diamonds\n"); }
	if (suit == 3) { print(" of Clubs\n"); }
	if (suit == 4) { print(" of Spades\n"); }
}

std::string card::getSuit() const
{
	if (this->suit == 1)
	{
		return "Hearts";
	}
	if (this->suit == 2)
	{
		return "Diamonds";
	}
	if (this->suit == 3)
	{
		return "Clubs";
	}
	if (this->suit == 4)
	{
		return "Spades";
	}
	else
		return "Something went wrong";
}

 

This topic is closed to new replies.

Advertisement