jeudi 31 décembre 2020

C++ Casino Game Deck and Card classes questions [closed]

I got some great feedback the other day about how I could improve and implement my Casino game further. I had some follow up questions to this. Questions will be below and code will follow.

Questions - Card Class

  1. When creating Card() is there a reason I am setting the strings to "" and the int to 0? This is something I had to do in my intro class and I really don't get the purpose.
  2. For my setNumber/setSuit/setvalue, are these actually being used? If so, what is the purpose of them exactly?
  3. The same as above for getNumber/getSuit/getValue, I know that these return the value but as of right now it doesn't seem like I need those values returned right?
  4. print() was something I copied from another blackjack game someone else was making on YouTube since I was stuck a bit. However, I was told print() shouldn't be used like this. So how would I cout the card/deck without this?

Questions - Deck Class

  1. Mostly on this one, it's the same question as 4 for the card class. I don't want to use print() but how else do I output the cards in my main class?

Questions - Main Class (May seem like very basic questions but just want to confirm my thinking)

  1. Deck playingDeck is declaring the object playingDeck in my main class. I am then printing that deck. Then I am shuffling that deck. The below questions all revolve around this.
    1. In my line that says "Deck playingDeck", is this creating the deck of cards with the 52 cards?
    2. Since I want to remove printDeck(), how can I achieve the same thing without using printDeck()? I don't know of a way to cout an object so to speak.
    3. This one may be a stupid question, but I just want to confirm. When I do playingDeck.shuffle() that is shuffling my playingDeck object right? That is now permanently changed to a shuffled deck? ​

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);


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();

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;

    }
}

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);
}

Aucun commentaire:

Enregistrer un commentaire