I am currently switching languages from Java(beginner) to c++ and would like to replicate a BlackJack game I made in Java but am having difficulty with the set up in C++ using codeblocks.
Code Design:
-
enum’s of Rank and Suit.
-
The 52 variations of Rank and Suit are formed together to create 52 objects of Card
-
Store the objects in a vector
-
Randomise Vector
-
Pop two cards from Vector to Player
-
Pop one card to Dealer
-
When the player or dealer is dealt a card, the card retrieved is calculated and value += to int player/dealerValue;
I am lost as to how I can achieve this:
Deck.cpp:
for(Suit suit: Suit.values()) {
for (Rank rank : Rank.values()) {
add(new Card(rank, suit));
}
}
createDeck()
shuffleDeck()
dealUser()
dealerDealer()
Player.cpp
userVector
dealerVector
getUserVector()
getDealerVector()
addCardUser()
addCardDealer()
calcUserValue()
calcDealerValue()
Card.cpp
card(Rank rank, Suit suit) { }
getSuit()
getRank()
BlackJack.cpp
call deck constructor
Player user = new Player()
Player dealer = new Player()
—Game Code—
Could someone please cover or direct me to some good resources for:
-
Managing header files
-
Brief skeleton code blocks for some of the methods I require
-
But most importantly, will I be required to use pointers at all for this program? I’ve only had access to online YouTube tutorials for a few days now while my c++ books arrive and am not yet confident with memory management of any kind.
-
Any general c++ tips for this program would be fantastic.
Many thanks for your time and patience to read this.
To address your points:
Managing header files
Don’t bother. Just throw everything in one source file (you’re perfectly allowed to do this in C++, unlike Java). Maybe sometime later you can break it up into more than one source file, if you want.
Skeleton code blocks for methods
If you’ve already got the code written in Java, then there’s your skeleton blocks.
Do I have to use pointers?
Probably not. However, if you just want to get started with a program that looks like your Java code, you can always simply ignore manual memory management, call
new, and never worry aboutdelete. You’ll have memory leaks all over the place, but one thing at a time, right?Typical “modern” C++ style avoids the use of raw pointers almost entirely. You can work on that at some later time.