Hey guys.
How may I implement a high/low card game using inheritance, any ideas out there? Sorry I am a bit of a newbie to all this stuff.
Help very much appreciated !

Recommended Answers

All 16 Replies

I am not too sure on the methods involved in your game but looking at whats exactly needed.
52 card need to be shuffled, one card is displayed, guess next card higher or lower. If correct next card is turned over and so on.

You will need classes and methods to shuffle the cards, to dislpay a card in its suit and rank, to check each guess...
I hope i was of some help, this is a tricky enough program to implement .

Thanks for the help, but i am still no where near figuring this one out. I am trying to think of what classes and class members I will need for this program. I know that inheritance is involved because of the suit and rank. But how do I implement a base class and derived classes for this prgram ?

Perhaps you could define a base class, called "Card" that has the rank values contained within it, as cards of all suits have a rank value. Then, you could define 4 derived classes, "SpadeCard", "HeartCard", "DiamondCard" and "ClubCard".

Good luck!

>Then, you could define 4 derived classes, "SpadeCard",
>"HeartCard", "DiamondCard" and "ClubCard".
You really think a class for each suit is necessary? Do you suggest making a separate class for each value too? :icon_rolleyes: There's not enough complexity in a standard deck of cards to justify inheritance, so perhaps the players can use it. A player base class with AI and human derived classes, for example.

I agree, you could represent the suits with an enum; but since he was looking for a class/inheritance feature, I suggested that one. Certainly your approach works as well.

Adding inheritance for the sake of inheritance is silly. I'd argue that just throwing inheritance in wherever you can because the assignment requires it teaches you nothing about how to design good software.

For such a simple game I wouldn't create a whole class, it's maybe only 30 lines or so ...

In my opinion you really get benefit from classes when you've moderate - big C++ code ...

For such a simple game I wouldn't create a whole class, it's maybe only 30 lines or so ...

(the estimate above is just for a 'high/low game')

Sorry, I didn't see you meant a 'high/low card game' ...

So you can use classes if you want, you can also use normal functions, but don't put everything in your main function !!
I think inheritance is just overhead (in the code's length) in this case ...

> A player base class with AI and human derived classes, for example.

But what would these AI and human derived classes do ? Apparently in my assignment I must use inheritance i would rather do it an easier way but....

Here's one possible scenario. All players need to play a card, have a hand, keep a score, whatever, whether it is a computer or an enduser. Maybe you overload that you overload that so the computer outputs snide remarks like "Ha Ha, I won that one", or "Drat" or whatever. On the other hand the end user has a name, a record of won/lost, a rating, etc, much of which may be irrelevant to a computer.

Thanks for the help guys, so I need a player base class with an AI and human derived classes which both deal with the present card (rank and suit), the next guess,next card, the output result. So if the three card guesses are correct the human player wins but if the human player is incorrect then the computer wins. I know i will need a rand()function and a couple of constructors to implement this but does anyone have any idea to what other functions I may need ?

I don't think there is much need to overcomplicate this program with too many functions. For example a single function deal(int suit,int rank) could organise each card into its suit and rank, some switch commands could implement this.. this function would pretty much do most of the things that you want this program to do. Not sure how you might relate all this within an AI and human class though.

Thanks for the help guys, so I need a player base class with an AI and human derived classes which both deal with the present card (rank and suit), the next guess,next card, the output result. So if the three card guesses are correct the human player wins but if the human player is incorrect then the computer wins. I know i will need a rand()function and a couple of constructors to implement this but does anyone have any idea to what other functions I may need ?

We don't know what you need. Only you do. Does the assignment require Artificial Intelligence? Narue (I think) just brought Artificial Intelligence up as an example where inheritance might be useful. Does the specification demand that or does it just say "Write a high/low card game with inheritance", which seems like an awfully vague project specification.

I am not totally sure what the program needs in terms of implementation, i just know i need a game that can shuffle some cards, turn one card over, guess higher or lower, if correct move to the next card and so on.. it is a bit of vague project i suppose in terms of implementation as all i do know is that i have to use and explore inheritance and virtual methods (which could be totally unnecessary!),but if that means i must have an AI and human player class i will look into it. Again newbie here !

if that means i must have an AI and human player class i will look into it. Again newbie here !

I don't know that it means that. It sounds like it means that it is 100% up to you (confirm this with your instructor obviously) what your program does and what you implement, as long as it is a high-low card game and has inheritance/virtual methods, etc. So AI is one possibility, but if it's not required, it's not required (that doesn't mean you shouldn't do it if you want to). Presumably the inheritance should relate in some way to the high/low card game. Maybe you could force people to register in order to play the game (don't laugh. it happens all the time) and go by the old standard category "Person", then they pick "Faculty" or "Student" and there's your inheritance. Why that would be relevant to the card game is anyone's guess, but on the other hand, I don't see why this game would need inheritance in the first place. Basically it sounds like an assignment where you are supposed to prove you know how to do it, whether it's needed or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.