mardi 29 décembre 2020

C++ Casino game - How to create player and dealer hands? [closed]

I am working on a rather simple Casino program in C++. I am somewhat newer to C++ but since my college uses it, I am trying to tackle this project with that language. This was inspired to me by a blackjack program I made with Java, however, classes seem much easier and make way more sense to me in Java compared to C++.

Code will be below and will post my questions below it.

main.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cctype>
#include "Card.h"
#include "Deck.h"
using namespace std;

void showMenu(int &choice);
int blackjackGame(int betAmount, int userBalance);
int threeCardGame(int betAmount, int userBalance);
int highCardGame(int betAmount, int userBalance);
int getValidBet(int betAmount, int userBalance);

enum cardSuits{HEART, SPADES, DIAMONDS, CLUBS};
enum cardValues{TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};

int main()
{
    int userBalance;
    int userChoice;
    int betAmount;
    string userName;
    bool keepGoing = true;


    // Creates playing deck.
    Deck playingDeck;
    playingDeck.printDeck();
    cout << "Shuffled Deck..." << endl;
    playingDeck.shuffle();
    playingDeck.printDeck();


    cout << "Welcome to the casino program!" << endl;
    cout << "Please enter your name." << endl;
    cin >> userName;
    cout << "Please enter your starting balance." << endl;
    cin >> userBalance;

    while(keepGoing)
    {
        showMenu(userChoice);

        switch(userChoice)
        {
            case 1:
                blackjackGame(betAmount,userBalance);
                break;
            case 2:
                threeCardGame(betAmount,userBalance);
                break;
            case 3:
                highCardGame(betAmount,userBalance);
                break;
            case 4:
                break;
            default:
                break;
        }
    }
    return 0;
}
void showMenu(int &choice)
{
    // User Menu
    cout << "Please select a game" << endl;
    cout << "1) Blackjack" << endl;
    cout << "2) Three Card Poker" << endl;
    cout << "3) High Card Flush" << endl;
    cout << "4) Exit" << endl;
    cin >> choice;

    // Validates input
    while(choice < 1 || choice > 4)
    {
        cout << "You must enter either 1,2,3 or 4" << endl;
        cout << "Select an option (1,2,3,4)" << endl;
        cin >> choice;
    }
}
int getValidBet(int betAmount, int userBalance)
{
    cout << "Please enter the amount you would like to bet" << endl;
    cin >> betAmount;

    while(betAmount > userBalance || betAmount < 1)
    {
        cout << "Invalid entry." << endl;
        cout << "Please enter the amount you would like to bet" << endl;
        cin >> betAmount;
        cout << endl;
    }

    return betAmount;
}
int blackjackGame(int betAmount, int userBalance)
{
    cout << "Your current balance is $" << userBalance << endl;
    betAmount = getValidBet(betAmount, userBalance);
    cout << betAmount;
}
int threeCardGame(int betAmount, int userBalance)
{
    cout << "Your current balance is $" << userBalance << endl;
    betAmount = getValidBet(betAmount, userBalance);
    cout << betAmount;
}
int highCardGame(int betAmount, int userBalance)
{
    cout << "Your current balance is $" << userBalance << endl;
    betAmount = getValidBet(betAmount, userBalance);
    cout << betAmount;
}

Card.cpp

#include "Card.h"

Card::Card()
{
    cardNumber = "";
    cardSuit = "";
    cardValue = 0;
}

Card::Card(string num, string suit, int value)
{
    cardNumber = num;
    cardSuit = suit;
    cardValue = value;
}
void Card::setNumber(string num)
{
    cardNumber = num;
}
void Card::setSuit(string suit)
{
    cardSuit = suit;
}
void Card::setValue(int value)
{
    cardValue = value;
}
const string Card::getNumber() const
{
    return cardNumber;
}
const string Card::getSuit() const
{
    return cardSuit;
}
const int Card::getValue() const
{
    return cardValue;
}
string Card::print() const
{
    return (cardNumber + " of " + cardSuit);
}

Card.h

#ifndef CASINO_CARD_H
#define CASINO_CARD_H
#include <string>
using namespace std;

class Card
{
private:
    string cardNumber;
    string cardSuit;
    int cardValue;
public:
    Card();
    Card(string num, string suit, int value);

    void setNumber(string num);
    void setSuit(string suit);
    void setValue(int value);

    const string getNumber() const;
    const string getSuit() const;
    const int getValue() const;
    string print() const;

};


#endif //CASINO_CARD_H

Deck.cpp

#include "Deck.h"
#include "Card.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>


Deck::Deck()
{
    string cardNumbers[13] = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};
    string cardSuits[4] = {"HEARTS", "DIAMONDS", "SPADES", "CLUBS"};
    int cardValues[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
    deck = new Card[CARDS_PER_DECK];
    int suitCounter = 0;
    for (int i=0; i < 52; i++)
    {
        deck[i] = Card(cardNumbers[i % 13], cardSuits[suitCounter], cardValues[i%13]);
        if ((i+1) % 13 == 0)
        {
            suitCounter = suitCounter + 1;
        }
    }
}
void Deck::printDeck() const
{
    for (int i=0; i < 52; i++)
    {
        cout << deck[i].print() << endl;
    }

}
void Deck::shuffle()
{
    srand(time(NULL));

    for (int original = 0; original < 52; original++)
    {
        int r = rand() % 52;

        Card temp = deck[original];
        deck[original] = deck[r];
        deck[r] = temp;

    }
}

Deck.h

#ifndef CASINO_DECK_H
#define CASINO_DECK_H
#include "Card.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>

using namespace std;

const int CARDS_PER_DECK = 52;

class Deck
{
public:
    Deck();
    Card dealCard();
    void shuffle();
    void printDeck() const;
private:
    Card *deck;
    int currentCard;

};


#endif //CASINO_DECK_H

Questions

  1. So what I need to do next is have a way to create a playerHand and a dealerHand. In Java I was able to do this within the Deck class itself. I feel like that would not be possible to do in C++. What would be the best way to handle this?
  2. If another class like Hand.cpp for example is needed, in C++ how do I populate the deck, shuffle it, and then use that shuffled deck within Hand.Cpp where I could create the players hand and dealers hand?

Aucun commentaire:

Enregistrer un commentaire