![]() |
|
#1
|
|||
|
|||
Opinion on my code and a c++ class questionI just want you to look at what I have coded and tell me how you think I did and maybe give me some ideas on how to improve this code.
One thing I am thinking is to rename the "Card" class to "Cards" and make a new "Card" class that only defines a single card. Should I do this? If so, then how exactly? I don't want to create a bunch of card objects in the "Cards" class. Any comments would be great. Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
I changed this shuffle functioni changed the shuffle function. This makes the shuffle random each time the program is run.
Before, I forgot to seed the random generator CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
|
First, don't put the srand() call in the shuffle routine. Put it near the beginning of main(). It only needs to be called once.
If you have a method called getRemaining() that returns the value of remaining, make the value private. It doesn't need to be public. CPP / C++ / C Code:
I did a cursory overview and that's all I saw. Just a couple very minor things... Since you didn't include random_shuffle(), I can't comment on how you are doing it. But if you wish to replicate a real shuffle, "random" is not proper. Here's a couple thoughts... Shuffle deck into newdeck... Riffle Shuffle:
Overhand Shuffle:
And don't forget a cut function... :-) __________________
Age is unimportant -- except in cheese |
|
#4
|
|||
|
|||
|
random_shuffle (); is part of the standard lib of c++. I belive the <algorithm> include.
I have made a lot of changes and update to the Poker lib I am writing from what you have seen. I have re-written the shuffle function without random_shuffle() use. Basically, the new function that I wrote is my way of replacing the random_shuffle() use. Here is an example. Keep in mind the entire lib has changed from what I have posted above. You will get the idea though. CPP / C++ / C Code:
I will be adding a cut deck function and a hand over hand function, which you will see in the next posted version of the lib. Also, I have move srand(time(0)) to the constructor. Ony run it once, correct? 1) What is Riffle the deck? Code example, I'm a little confuzed by the +/- symbol you gave. 2)What do you mean by making a newDeck? Do you mean shuffle deck1 and move it to a new array? Please explain. Thanks for your comments and help! |
|
#5
|
||||
|
||||
|
Welcome to my 1000th post!!
Quote:
Quote:
Quote:
__________________
Age is unimportant -- except in cheese |
|
#6
|
|||
|
|||
|
Quote:
Okay, so.. Everytime someone uses the lib, they have to know to seed the random generator. Why don't I just use a check to see if srand() is seeded already. Possibly, there is a function along side srand() that does this? Or I can check create a boolean isSeeded and run a check each time the constructor is called. One last thing. It doesn't hurt to run srand() over and over. Doesn't it just re-seeds the generator with a different time? Oh, and most likly you won't instantiate more then one deck. right? |
|
#7
|
|||||
|
|||||
|
Quote:
Quote:
Quote:
Quote:
Quote:
__________________
Age is unimportant -- except in cheese |
|
#8
|
|||
|
|||
Re: Opinion on my code and a c++ class questionI realize that this thread is now quite dated, but there are many things "wrong" with this class design:
Card should, at most, be an abstract base class. (Of course, my implementation--for convenience--below is just a base with no virtuality at all!) In C++ we will define a Card class that can represent ANY kind of card, or else our code destroys the promise of reusability. With that in mind, one my start thinking of all of the kinds of cards. Greeting cards, 3x5 cards, flash cards and yes, playing cards...but also any kind of card, otherwise "Card" really isn't a Card except in a specific case. PlayingCard : public Card {}; ...might be something you want to consider. However, you may find that you don't need anything more than PlayingCard for your quick and dirty exploits into learning how to write what appears to be a game played only with playing cards. A PlayingCard class may have the enumerations of suit and face value, but storing them only as strings is probably not the best idea. It is far easier to store them as integers 1-13. Your public interfaces expose operations that are not "Card" but, as you indicate, "Cards" like. Your Card class tries to be a deck of cards. Your Deck class isn't. PlayingCardDeck is nothing more than a container and perhaps some rules for managing and manipulating the individual Card(s) stored in it. A well-written PCD would contain a std::vector<class PlayingCard> and expose the operations ::shuffle(); ...etc. I don't know where you really need "getRemaining()" but that is a poor naming convention. getRemaining what? While it may seem TOTALLY obvious to you at design time, who is going to use your library and wonder what it means? Even if everyone in the entire world can readily figure it out, what does it cost you to be specific? In fact, ::cardsLeft() is more descriptive and uses fewer letters, though I do like the idea of starting your operation name with a verb. You can't say getRemainingCards because it sounds like it is going to return a reference to the "rest" of the cards held inside of the deck. PlayingCardDeck::countOfCardsRemaining()? ...kind of ugly, but clear. I'd ask why you even need to know how many cards are remaining. It isn't like you can deal out more than you've got in the deck. Also, instead of Card initializing itself to some value of one unique card in the 52-card deck, your PlayingCardDeck should act as a Factory to instantiate and store the necessary cards that it knows must be in an entire PCD. Then you have a PlayingCard that is little more than a place holder for a face value and a suit and perhaps some operations to telegraph that information. EG: CPP / C++ / C Code:
It is easy enough to mask off the bits of the face value and the suit value... Also, you can comment in the friendship with the PCD (once you've written it) so that it can manipulate the card value directly, if somehow needed, though it is probably better not to use friendship for this purpose. You definitely do not want a public mutator for the value. Once a true PlayingCard instance exists, it is extremely unlikely that you'd ever want to change the face value of it. The main function is a quick and dirty example of what the factory operation in the PCD might do, albeit, you would push_back the PC (or just the value, as I've done) onto the PCD's internal vector upon instantiation. Then the PCD could random_shuffle (or other shuffle implementation, as seen fit) and be ready to cut. Output of execution of the goofy bit of code would look something like: Code:
Of course, this was a nasty hack done in a few minutes and is not likely very representative of "truly good" design. There are probably a number of important constraints that I've taken the liberty of avoiding in this "work." For example: You could, obviously, overload a ctor so that it takes an m_value. HTH... Take Care. Rob! Last edited by JdS : 07-Feb-2006 at 08:32.
Reason: Please insert your C code between [c] & [/c] tags
|
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Guidelines for posting requests for help - UPDATED! | WaltP | C++ Forum | 1 | 22-Aug-2008 00:07 |
| Introduction to .NET | LuciWiz | .NET Forum | 5 | 09-Aug-2007 04:53 |
| Problem with int mixed with char,... | leitz | C++ Forum | 17 | 07-Dec-2004 20:56 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The