Beginner BlackJack Game [closed]
質問
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 about delete
. 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.
他のヒント
I'm approaching this from the "learning by doing is better" perspective... so the aim of this answer is to get you on track so you can start experimenting.
Design: Both languages are very similar, so the classes and class structure do not matter regardless of Java or C++, so you can continue thinking "in Java terms" when you're working with C++. You can just start with the same design.
organization of files: Think of a .java file as corresponding to a .h file and a .cpp file pair (not always true, and the extensions may differ according to preference, but this will do for now). Copy the Java code into the .h header file and isolate the function definitions into the corresponding .cpp file. The usual syntactic cleanup is necessary - subbing out imports into
#include<header.h>
definitions, prefixing function definitions with the class name (remember to use theClassName::
syntax). Also remember that including the header files does not mean you're including the context - you will still need to refer to it using the appropriate namespace. Note: This step will produce compilation errors, but that's probably the best way to learn and get used to the differences.memory management: On your Q pointers, good C++ programming convention denotes: pointers = no-no, stack variables and smart pointers=yes-yes. Use stack variables for normal / temporary variables. For parameters etc, use
boost::shared_pointer
(download and install the boost libraries). It's usage is just like a normal C++ pointer (i.e. usesnewlyDealtCard->member
syntax). The reason in a nutshell is that the shared pointer mimics the Java object model - object assignment creates references instead of copies, and the object is auto-deleted when all references go out of use. 99 out of 100 times, the behavior is the same and it will save you time that you can spend on understanding other things. Once you're comfortable with how C++ represents and handles memory, you can start using other smart pointer types. Syntax:Card newlydealtCard = new Card(randomSuite, randomValue); //Java
boost::shared_pointer newlydealtCard(new Card(randomSuite, randomValue)); //C++
Inheritance: For
interface
s, use the keywordclass
. And declare the functions pure virtual. When deriving from these 'interfaces' or other classes, use public inheritance for now. There are two other type in C++ that you can start looking up once you've become more comfortable.
The rest of the differences are mostly syntactic or are just differently-named standard functions, they don't involve a change of perspective. Hopefully, as you code more, you'll be able to explore outside these options.
Good luck!