Card Game

Recently I have this assignment where I have create "Crazy Eight" program.

These are the requirements:
In this assignment, you will be playing with other 3 people whose card turns are automatically generated according to the next rules:
1.find the same number of any suit
2.find the same suit
3.use any eight and declare the suit of the most occurring suit in hand (e.g., select H if HHHCCDS in hand)
4.draw from the deck until finding any of the above

You need to implement the card selection in this order. Each player is given 6 cards at the beginning of the game. You are the first player, and anyone who used up all cards will win. Game also ends when all cards in the stockpile are used. You should check all the potential input errors. The first card in the pile may be a crazy eight, in which case use it as a selection condition of suit or reshuffle the pile to choose another non crazy eight card (choice is up to you).

You must display all kinds of transactions happening in the progress of the game. At your turn, typing "?" will show hands of others (cheating option). The next session shows an example game sessions:
Pile has S10 <--- your turn

(a) C3 (b) H3 (c) C8 (d) S3 (e) HK (f) DQ (g) draw

Which one to play? d

Pile has S3
Player 1 chose H8
Player 1 declared suite C
Pile has C*
Player 2 chose CJ
Pile has CJ
Player 3 chose CK
Pile has CK <--- your turn

(a) C3 (b) H3 (c) C8 (d) DQ (e) HK (f) draw

So far, I was able to learn how to create a deck of cards. I need to know how I can apply that deck of cards program to this problem. These are my codes so far.

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
  //Designing the cards
#ifndef H_cardtype
#define H_cardtype
#include <iostream>
#include <string>

using namespace std;

class card {
public: card(string cardFace, string cardSuit);
		string print() const;
		card();
private: string face; string suit;
};

card::card(){

}

card::card(string cardFace, string cardSuit){
	face=cardFace;
	suit=cardSuit;
}

string card::print() const{
	return face + "of" + suit;
}

#endif

//Designing the deck
#ifndef H_deckbuild
#define H_deckbuild
#include "cardtype.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

const int cards_per_deck = 52;

class deckOfCards {
public:
	deckOfCards();
	void shuffle();
	card dealCard();
	void printdeck()const;
private:
	card *deck;
	int currentCard;
};

void deckOfCards::printdeck() const {
	cout<<left;//print everything to the left
	for (int i=0; i < cards_per_deck; i++){
	cout<<setw(19)<<deck[i].print();
	if ((i + 1) % 4==0) cout<<endl;
	}
}

deckOfCards::deckOfCards(){
	string faces[] = {"Ace","Deuce","Three","Four","Five","Six","Seven",
					"Eight","Nine","Ten","Jack","Queen","King"};
	string suits[] = {"Hearts","Diamonds","Clubs","Spades"};
	deck = new card[cards_per_deck];//creates a new deck
	currentCard = 0;
	for(int count = 0; count < cards_per_deck; count++) //populates deck with cards in order
	deck[count]	= card(faces[count%13], suits[count/13]); //gives the cards variety
}

void deckOfCards::shuffle(){ //shuffles all 52 cards
	currentCard=0;
	for(int first = 0; first < cards_per_deck; first++){
		int second = (rand()+ time(0)) % cards_per_deck;
		card temp = deck[first];
		deck[first] = deck[second];
		deck[second] = temp;
	}
}

card deckOfCards::dealCard(){
	if (currentCard > cards_per_deck) shuffle(); //if we are out of cards, they must be shuffled
	if (currentCard < cards_per_deck) return (deck[currentCard++]);
	return deck[0]; // gives top card of deck
}

#endif

//Shows all the cards
#include "deckbuild.h"

using namespace std;

int main() {
	deckOfCards deck;
	card currentCard;
	deck.printdeck();
	deck.shuffle();
	cout<<endl<<endl;
	for (int i = 0; i < 52; i++){
		cout<<currentCard.print()<<endl;
	}
	system("PAUSE");
	return 0;
}
closed account (Sw07fSEw)
This is what I would do. First I would simplify your deck and declare it as a vector. Using a vector gives you a lot of options.

Let's say your using one deck. You could declare your deck as such...

std::vector<Card> deck

Now to actually construct it, I would use id's 0 thru 51. Before I'd add cards to the deck, let me explain why I use card IDs. Let's assume each suit corresponds to consecutive IDs. Since there's 13 cards of each suit, lets say card IDs 0-12 are hearts, 13-25 are clubs, 26-38 are diamonds, 39-51 are spades. Cool. Now about about we give each card a rank, like A, 5, 10, J, Q, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Rank Card
0          A
1          2
2          3
3          4
4          5
5          6
6          7
7          8
8          9
9          10
10        J
11        Q
12        K


By structuring our ID's in this manner, you'll kill a bunch of birds with one stone. Let me simplify your card constructor and add some members to your class card.

1
2
3
4
5
6
7
8
9
10
class card{
	int rank;
	enum suits {HEART, CLUB, DIAMOND, SPADE}; 
};


card::card(int _id):id(_id){
	rank = id % 13;
	suit = id / 13;
}


If we were to call card(23) what card would that be? Well, 23 % 13 is 10, so now we know we have a J of some suit. Then to find out the suit, we would use the divisor operator. So 23 / 13 is 1, which corresponds to CLUB. Therefore id(23) corresponds to a J of clubs. How about card(39)? Well, 39 % 13 is 0, and 39 / 13 is 3, so we'd have an A of spades. Make sense?

Now to actually construct our deck, which use a basic for loop.

1
2
3
4
5
for(int i = 0; i < 52 * numDecks; i++){
	deck.push_back(card(i)); // a sorted deck (Hearts->Clubs->Diamonds->Spades)
}

std::random_shuffle(deck.begin(), deck.end()); //shuffle them 


If you haven't already, I would create a class Player because each player is going to need their own hand of cards and other members. Then when you want start the game, you can implement a member function of class Player like such...
1
2
3
4
void Player::takeCard(std::vector<Card> &deck){
	//take the card off the top of the deck (splice or pop_back)
	//place that card onto the players hand
}


Now every time a player needs to take a card from the top of the deck, they'll actually remove it from the deck since you're passing it by reference. Within that function there are multiple ways to remove the card, but I'm just giving you a basic idea. At the beginning of the game, each player could call Player::takeCard() 6 times, to grab 6 cards.

1
2
3
4
5
6
7
const int handStartSize = 6;

for(auto &player : players){ //assumes a you have a container of Players
	for(int i = 0; i < handStartSize; i++){
		player.takeCard(deck);
	}
}


I don't know if players have to replace their cards back into the deck, but if so, you could create another member function of class Player like replaceCard(). This way you could add cards back to the deck and remove them from the players hand.


i seem to be having trouble compiling the class cards, can you explain a little more on this?
This is what I made of that, is this alright?
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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <random>
#include <chrono>
using namespace std;


class Card {
private:	int rank;
			enum suits{HEART, CLUB, DIAMOND, SPADE};
public:	Card(int id);
		Card();
};

Card::Card(int id){
	rank = id % 13;
	suits = id / 13;
}

vector<Card> deck;

void deckbuild(){
	for(int i = 0; i < 52; i++){
		deck.push_back(Card(i)); // a sorted deck (Hearts->Clubs->Diamonds->Spades)
	}

	std::random_shuffle(deck.begin(), deck.end()); //shuffle them
}


Also, is it alright if you elaborate a bit on the player class.
And also, I am having trouble when compiling, I get an error message at random_shuffle and suits in class Card.

And by the way, are there any other commands aside from splice and pop_back that can be applied to a vector if I were to vectorize the hand of each of the 4 players into seperate vectors?
Last edited on
Topic archived. No new replies allowed.