Game implement

Hi
Am trying to implement a game where in I have a deck of 52 cards.
I need to draw 2 cards from it and search for the highest.

Here's the ranking of suits :
Reverse alphabetical order: clubs (lowest), followed by diamonds, hearts, and spades (highest).

Cards are always compared by rank first, and only then by suit. For example, using the "alphabetical order" ranking, the ace of clubs ranks higher than any king, but lower than the ace of diamonds.

I also need to support the ties.
The game should never end in a tie - A new card should be further dealt out to each player in case of a tie.
Last edited on
So where are you stuck? Nobody here is going to write the program for you, you have to try to write it yourself and tell us where you get stuck at so we can point you in the right direction.

Edit: You've clearly shown that you understand the logic for your program, now stop being lazy and make an attempt so we can help you.
Last edited on
#include <stdlib.h>
#include <stdio.h>


/*
Cards are compared by rank first, and only then by suit.
standard ranking of suits followed in the game.
Reverse alphabetical order: clubs (lowest), followed by diamonds, hearts, and spades (highest).
For example,the ace of clubs ranks higher than any king, but lower than the ace of diamonds.
*/

/* Random No generation:
Here we need a random no generation between 2 and 13 i.e 13 no's which are >=2
2,3,4,5,6,7,8,9,10,
11 - Jack
12 - Queen
13 - King
14 - Ace

For suits: Here's the ranking:
1 - Clubs
2 - Diamonds
3 - Hearts
4 - Ace
*/


class HighCard
{
public:
HighCard(){}
~HighCard(){}

bool Play()
{

int card1_suit = rand() % 4 + 1;
int card1_facevalue = rand() % 13 + 2;

int card2_suit = rand() % 4 + 1;
int card2_facevalue = rand() % 13 + 2;

return(card1_facevalue < card2_facevalue) // No need to check for the suit

}

};

int main()
{
HighCard card;
if ( card.Play() )
{
puts("Card2 wins");
}
else
{
puts("Card2 looses");
}

return 0;
}

This simply implements the first part of checking which card's facevalue is greater.
What can I do for a better design/
Last edited on
Confused about one thing. How is it supposed to support ties when no two cards are considered equal and we're only comparing two cards?
That's a case when the game Supports for Multiple Decks.
I need 2 cater to that as well.

Am still trying the first part.. :-(
Last edited on
What logic can I follow for building the game?
The user should be given a choice - I suppose for playing with a single deck or with multiple decks.
What logic can I follow for multiple decks?
Well I made some changes to your code to help put you in the right direction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

/*
Cards are compared by rank first, and only then by suit. 
standard ranking of suits followed in the game.
Reverse alphabetical order: clubs (lowest), followed by diamonds, hearts, and spades (highest). 
For example,the ace of clubs ranks higher than any king, but lower than the ace of diamonds. 
*/

/* Random No generation: 
Here we need a random no generation between 2 and 13 i.e 13 no's which are >=2
2,3,4,5,6,7,8,9,10,
11 - Jack
12 - Queen
13 - King
14 - Ace

For suits: Here's the ranking:
1 - Clubs
2 - Diamonds
3 - Hearts
4 - Ace
*/
using namespace std;

class HighCard
{
public: 
HighCard(){}
~HighCard(){}

int Suit;
int FaceValue;

void DrawCard() //No parameters necessary, use for first draw
{
	Suit = rand() % 4 + 1;
	FaceValue = rand() % 13 + 2;
}
void DrawCard(HighCard ComparisonCard) //pass card that was drawn, use for second draw for when there's only 1 deck
{
	Suit = rand() % 4 + 1;
	FaceValue = rand() % 13 + 2;
	while (Suit == ComparisonCard.Suit && FaceValue == ComparisonCard.FaceValue) //If card drawn is card already drawn, redraw
	{
		Suit = rand() % 4 + 1;
		FaceValue = rand() % 13 + 2;
	}
}

int CompareSize(HighCard ComparisonCard) //Returns 1 if the card calling the function is bigger, returns 0 if the other card is bigger, returns 2 if they're the same size
{
	if (FaceValue>ComparisonCard.FaceValue)
	{
		return 1; //card calling the function is bigger
	}
	if (FaceValue<ComparisonCard.FaceValue)
	{
		return 0; //Card that is being compared to is bigger
	}
	if (FaceValue==ComparisonCard.FaceValue) //If face values are equal, check suit
	{
		if (Suit>ComparisonCard.Suit)
		{
			return 1; //Suit of our card is bigger than comparison card
		}
		if (Suit<ComparisonCard.Suit)
		{
			return 0; //Suit of card being compared is bigger
		}
		if (Suit==ComparisonCard.Suit)
		{
			return 2; //Suits are the same
		}
	}
}


};

int main()
{
HighCard card1;
HighCard card2;
card1.DrawCard();
card2.DrawCard(card1); 

int ReturnValue = card1.CompareSize(card2);
//If returnvalue is 0 that means that card2>card1
//If returnvalue is 1 that means that card1>card2
//If return value is 2, that means they're equal
if (ReturnValue==0)
{
	cout << "Card 2 is bigger than card 1." << endl;
	cout << "Card 1 Suit:" << card1.Suit << endl;
	cout << "Card 1 FaceValue:" << card1.FaceValue << endl;
	cout << "Card 2 Suit:" << card2.Suit << endl;
	cout << "Card 2 FaceValue:" << card2.FaceValue << endl;
}
if (ReturnValue==1)
{
	cout << "Card 1 is bigger than card 2." << endl;
	cout << "Card 1 Suit:" << card1.Suit << endl;
	cout << "Card 1 FaceValue:" << card1.FaceValue << endl;
	cout << "Card 2 Suit:" << card2.Suit << endl;
	cout << "Card 2 FaceValue:" << card2.FaceValue << endl;
}
if (ReturnValue==0)
{
	cout << "Card 2 is equal to card 1." << endl;
	cout << "Card 1 Suit:" << card1.Suit << endl;
	cout << "Card 1 FaceValue:" << card1.FaceValue << endl;
	cout << "Card 2 Suit:" << card2.Suit << endl;
	cout << "Card 2 FaceValue:" << card2.FaceValue << endl;
}


return 0;
}


Try this out and let me understand if you get what's going on

Edit: About the multiple decks part - will it still only be drawing 2 cards total?
Last edited on
Yeah, We can only take out 2 cards at a time.
What confuses me is how will the game end??
Last edited on
I guess one way to play this game would be to score each round and tally up the total score at the end of the deck (so after 26 turns).
The winner should obviously be the one who got the highest over-all tally.

At the end of your program check if there is a tie, if true then state that the deck has been reshuffled and this is now sudden death, the next point wins the game. (since each card has it's own value [based on number and suit] there is no chance of a single-round tie.)
Last edited on
However, this is not currently being encoded as a deck, but instead as just quasi-random cards. Perhaps you should consider using a vector of 52 cards for the deck? You can then remove cards as they are played and the game will be scored when the vector is empty. If a tie occurs, simply refill the vector for one last round.
It makes more sense to use a std::set since you don't want duplicate cards and you will be removing them in arbitrary order.
Thank u All!

One way would be to ask the user how many decks the user wants 2 play with.
On that input, no of cards to be dealt with would be n*52.

While(n*52 > = 0)
{
Create 2 objects of the High_card class
Do all the process of determining the higher card.
Add the score to the appropriate winner (player 1 / Player 2)
Decrement the no of cards remaining by 2.
Call the destructors of the 2 objects created in the while loop
}

Wondering if this approach is right ?
Will the objects of the same name be repeated created and destroyed?
Can this lead to any further issues?
@LB - I was thinking of using std::vector as in shuffle it and then pop the cards from the top two by two and you may want duplicate cards (multiple decks / jokers).

@high card - I still think a std::vector is a better approach (it's how I've approached all card games and it's always worked). That's my two cents anyway.



Thanks Mats!
Can u guide me on how to use them as well?
To be honest, if you've never used vectors before, it is going to be a whole lot better for you to pick up a book (I take it you're learning from one already?) and start reading about them instead of me trying to thoroughly explain them through a few forum posts. Also, if you haven't already, before learning vectors learn about arrays.

Then you will be ready to apply this method to your game. Reference stuff like this if you get stuck http://www.cplusplus.com/reference/vector/vector/ and if you run into a more specific problem, then ask here on the forum.
Topic archived. No new replies allowed.