Pairs card game

ssss
Last edited on
i'd say the first issue is that this seems ridiculously overly complicated for what you're trying to accomplish.

Second, it looks like you shuffle the deck and print it out to view. If its working to that point the deck is already random. So just deal out the last card in the deck and pop it off....

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

I been playing around with this idea.  This is my deck struct.  So you can kinda see how i shuffled and labeled the cards in the vector.

[code]
struct deck {

	int card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13;
	string _deck[4][13];
vector < pair<string, int>> shuffledDeck;
	int deck1[4][13];

	deck()
		: card1(1),card2(2),card3(3),card4(4),card5(5), card6(6), card7(7), card8(8), card9(9), card10(10), card11(11), card12(12), card13(13),deck1{
		card1,card2,card3,card4,card5, card6, card7, card8, card9, card10, card11, card12, card13,card1,card2,card3,card4,card5, card6, card7, card8, 
		card9, card10, card11, card12, card13,card1,card2,card3,card4,card5, card6, card7, card8, card9, card10, card11, card12, card13,card1,card2,card3,
		card4,card5, card6, card7, card8, card9, card10, card11, card12, card13 } {}

	
	void shuffle() {
		srand(time(0));
		
		while (!done()) {
		int rand2 = rand() % 13;
		int rand1 = rand() % 4;             //heart == 0, diamonds==1 spades == 2 clubs == 3

			if (rand1 == 0 && this->deck1[rand1][rand2] != 0) {
				this->shuffledDeck.push_back(make_pair("hearts", this->deck1[rand1][rand2] + 1));
				this->deck1[rand1][rand2] = 0;
			}
			else if (rand1 == 1 && this->deck1[rand1][rand2] != 0) {
				this->shuffledDeck.push_back(make_pair("diamonds", this->deck1[rand1][rand2] + 1));
				this->deck1[rand1][rand2] = 0;
			}
			else if (rand1 == 2 && this->deck1[rand1][rand2] != 0) {
				this->shuffledDeck.push_back(make_pair("spades", this->deck1[rand1][rand2] + 1));
				this->deck1[rand1][rand2] = 0;
			}
			else if (rand1 == 3 && this->deck1[rand1][rand2] != 0) {
				this->shuffledDeck.push_back(make_pair("clubs", this->deck1[rand1][rand2] + 1));
				this->deck1[rand1][rand2] = 0;
			}
		}
		//return this->shuffledDeck;
	}

	bool done() {
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 13; x++) {
				if (this->deck1[y][x] != 0) { return false; }
			}
		}
		return true;
	}
	deck deckNew() {
		deck d;
		d.shuffle();
		this->shuffledDeck = d.shuffledDeck;
		return *this;
	}
		
	void displaydeck() {
		for (int i = 0; i < this->shuffledDeck.size(); i++) {
			cout << this->shuffledDeck[i].second << " of " << this->shuffledDeck[i].first << "\n";
			
		}
		cout << "decksize " << this->shuffledDeck.size() << "\n";
	}
};



[/code]
Last edited on
i also have a player struct and a game struct that controls the whole game

the game struct instantiates the players and the deck. the cards get dealt out and hit like this

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
122
123
124
125
126
127
void hit(player &p) {
	p.hand.push_back(make_pair(this->dek.shuffledDeck.back().first, this->dek.shuffledDeck.back().second));
	this->dek.shuffledDeck.pop_back();

}
void deal() {
		for (int i = 0; i < 2; i++) {
			this->p1.hand.push_back(make_pair(this->dek.shuffledDeck.back().first, this->dek.shuffledDeck.back().second));
			this->dek.shuffledDeck.pop_back();
			this->p2.hand.push_back(make_pair(this->dek.shuffledDeck.back().first, this->dek.shuffledDeck.back().second));
			this->dek.shuffledDeck.pop_back();
			this->p3.hand.push_back(make_pair(this->dek.shuffledDeck.back().first, this->dek.shuffledDeck.back().second));
			this->dek.shuffledDeck.pop_back();
		}
	}

void clearhands() {
		while (!this->p1.hand.empty()) { this->p1.hand.pop_back(); }
		while (!this->p2.hand.empty()) { this->p2.hand.pop_back(); }
		while (!this->p3.hand.empty()) { this->p3.hand.pop_back(); }
	}

//the player struct.


struct player {

	string name;
	int wins, handvalue;
	vector<pair<string, int>> hand;
	player()
		:name(name), wins(0), handvalue(0){}

	void calcHandValue(int a) {
		this->handvalue = 0;
		int aceCount = 0;
		bool aceDone = false;
		for (int i = 0; i < this->hand.size(); i++) {

			if (this->hand[i].second < 14) {
				if (this->hand[i].second < 11) {
					this->handvalue += this->hand[i].second;
				}
				else if (this->hand[i].second > 10 && this->hand[i].second < 14) {
					this->handvalue += 10;
				}
			}
			else if (this->hand[i].second == 14 && i != this->hand.size() - 1) {  // push aces to the back...
				int temp = 14;
				string tempS = this->hand[i].first;
				this->hand.erase(this->hand.begin() + i);
				this->hand.push_back(make_pair(tempS, temp));
				i--;
				aceCount++;
			}
			else if (this->hand[i].second == 14 && i == this->hand.size() - 1 && aceDone == false && aceCount == 0) { //this deals with a single ace
				if (this->handvalue + 11 > 21) {
					this->handvalue += 1;
				}
				else if (this->handvalue + 11 < 22) {
					this->handvalue += 11;
				}
			}
			if (i == this->hand.size() - 1 && aceCount > 0 && aceDone == false) {
				 aceDone = true;
			}
		}
		if (aceDone == true && aceCount == 1) { //this deals with a single ace that was pushed
			if (this->handvalue + 11 > 21) {
				this->handvalue += 1;
			}
			else if (this->handvalue + 11 < 22) {
				this->handvalue += 11;
			}
			
		}
		if (aceDone == true && aceCount > 1) {  // this deals with more than one ace

			if (this->handvalue + 11 + (1 * (aceCount - 1)) > 21) {
				this->handvalue += (1 * aceCount);
			}
			else { this->handvalue += 11 + (1 * (aceCount - 1)); }
		}
		
	}

	void displayHand() {
		HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
		CONSOLE_SCREEN_BUFFER_INFO cbsi;
		GetConsoleScreenBufferInfo(console, &cbsi);
		int xx = cbsi.dwCursorPosition.X;
		cout << this->name << " hand    "<< "WINS = "<< this->wins;
		GetConsoleScreenBufferInfo(console, &cbsi);
		COORD coord{ xx,cbsi.dwCursorPosition.Y + 2 };
		SetConsoleCursorPosition(console, coord);
		this->calcHandValue(this->hand.size() > 2 ? 0 : 1);
		cout << this->name << " Hand Value = " << this->handvalue;
		 int b = 0;

		for (int i = 0; i < this->hand.size(); i++) {
			 COORD* coord1 = new COORD{ (SHORT)xx,cbsi.dwCursorPosition.Y+(SHORT)i+3 };
			
			SetConsoleCursorPosition(console, *coord1);
			if (this->hand[b].second < 11) {
				cout << this->hand[b].second << " of " << this->hand[b].first;
			}
			else if (this->hand[b].second == 11) {
				cout << "Jack" << " of " << this->hand[b].first;
			}
			else if (this->hand[b].second == 12) {
				cout << "Queen" << " of " << this->hand[b].first;
			}
			else if (this->hand[b].second == 13) {
				cout << "King" << " of " << this->hand[b].first;
			}
			else if (this->hand[b].second == 14) {
				cout << "Ace" << " of " << this->hand[b].first;
			}
			delete coord1;
			b++;
		}

	}

	
};


obviously b4 you call these functions you have to make sure that the shuffeledDeck actually has enough cards in it.

But i built a full on blackjack game with ai dealer and it really shows why the dealer has the advantage in vegas.... the dealer usually has the most wins.
Last edited on
Topic archived. No new replies allowed.