Poker Game Help

So I tried going on SO and got flamed and insulted. My question wasn't answered though, I guess the trolls on there decided to skip that part.

I am making a poker game for a final. I have it displaying cards and I have values set for whatever value the card is. I got it to find if the player has a pair, but have been unable to proceed any further.

Basically my code is fine right now, but I need to make it more efficient to be able to compare hands. I think instead of string arrays, making the value and suit a vector? I've tried doing this and failed multiple times, always running into errors.

question1: How can I make my code more efficient
question2: From an experts perspective, how should I proceed with my project? Like I'm sorry if this is a bad question, but I don't know any other way to ask it.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  // random_shuffle example
	#include "stdafx.h"  
	#include <cstdlib>
	#include <iostream>
	#include <string>
	#include <sstream>
	#include <conio.h>
	#include <windows.h>
	#include <algorithm> 
	#include <stdio.h>
	#include <vector>
	#include <ctime>
#include <iomanip>

using namespace std;

void shuffle(vector<string>& deck);
void createdeck(vector<string>& deck, int suits, int values);
void choosecard(vector<string>& deck);
void draw1card(vector<string> hand,string value,vector<string> suit,int col,int x);
void _pair(bool pair);

string card1;
string card2;
string card3;
string card4;
string card5;
bool pair = false;

const int suits = 4;
const int values = 13;

vector<string> deck;

HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);

void gotoxy(int x,int y)
{
	COORD Coord;
	Coord.X=x;
	Coord.Y=y;

	SetConsoleCursorPosition(con,Coord);
}
void textattr(int color)
{
	SetConsoleTextAttribute(con, color);
}


int main () {
	createdeck(deck,suits,values);
	choosecard(deck);
	cin.get();
	main();
	
}
void createdeck(vector<string>& deck, int suits, int values)
{
	string cardValue[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
	string cardSuit[] = {"H","D","S","C"};
	string card;

	for(int i = 0; i < suits; ++i)
	{
		for(int j = 0; j < values; ++j)
		{
			card = cardValue[j] + cardSuit[i];
			deck.push_back(card);
		}
	}
}
void shuffle(vector<string>& deck)
{
	srand(static_cast<unsigned int>(time(0)));
	random_shuffle(deck.begin(), deck.end());
}
void choosecard(vector<string>& deck)
{

	vector<string> spade;
	spade.push_back("    ,    ");
	spade.push_back("   / \\   ");
	spade.push_back("  (_ _)  ");
	spade.push_back("   /_\\   ");
	spade.push_back("         ");

	vector<string> heart;
	heart.push_back("   _ _   ");
	heart.push_back("  / ^ \\  ");
	heart.push_back("  \\   /  ");
	heart.push_back("   \\ /   ");
	heart.push_back("    `    ");

	vector<string> clover;
	clover.push_back("    _    ");
	clover.push_back("   (_)   ");
	clover.push_back("  (_)_)  ");
	clover.push_back("   /_\\   ");
	clover.push_back("         ");

	vector<string> diamond;
	diamond.push_back("         ");
	diamond.push_back("    /\\   ");
	diamond.push_back("   <  >  ");
	diamond.push_back("    \\/   ");
	diamond.push_back("         ");

	vector<string> cards;

	shuffle(deck);


	vector<string>hand;
	hand.push_back(deck[0]);
	card1 = deck[0];
	hand.push_back(deck[1]);
	card2 = deck[1];
	hand.push_back(deck[2]);
	card3 = deck[2];
	hand.push_back(deck[3]);
	card4 = deck[3];
	hand.push_back(deck[4]);
	card5 = deck[4];

	vector<string>playerhand;
	playerhand.push_back(deck[5]);
	playerhand.push_back(deck[6]);

	vector<string>cpuhand;
	cpuhand.push_back(deck[7]);
	cpuhand.push_back(deck[8]);


	int x = 5;
	int color;
	string val;
	

	for(string item : hand)
	{

	
	if(item.at(1) == *"H")
	{
		cards = heart;
		color = 252;
		val = item.at(0);
	}
	else if(item.at(1) == *"S")
	{
		cards = spade;
		color = 240;
		val = item.at(0);
	}
	else if(item.at(1) == *"D")
	{
		cards = diamond;
		color = 252;
		val = item.at(0);
	}
	else if(item.at(1) == *"C")
	{
		cards = clover;
		color = 240;
		val = item.at(0);
	}
	
	
				draw1card(hand,val,cards,color,x);
				x = x+15;
	}
	int y = 20;
	for(string item : playerhand)
	{

		gotoxy(0,y );
		cout<<item<<endl;
		y = y+1;
	}


}




void draw1card(vector<string> hand, string value,vector<string> card,int color, int x)
{
	int col = color;
	bool pair = false;
   // int x = 5;

	{gotoxy(x,5 );	 textattr(15);printf("  ");textattr(col);printf("         "); textattr(15); printf(" ");}
	{gotoxy(x,6 );  textattr(15);printf("  ");textattr(col);printf("%s",value.c_str());textattr(240);printf("        ");textattr(15);printf(" ");}
	{gotoxy(x,7 ); textattr(15);printf("  ");textattr(col);printf("%s",card[0].c_str());textattr(15);printf(" ");}
	{gotoxy(x,8 ); textattr(15);printf("  ");textattr(col);printf("%s",card[1].c_str());textattr(15);printf(" ");}
	{gotoxy(x,9 ); textattr(15);printf("  ");textattr(col);printf("%s",card[2].c_str());textattr(15);printf(" ");}
	{gotoxy(x,10 ); textattr(15);printf("  ");textattr(col);printf("%s",card[3].c_str());textattr(15);printf(" ");}
	{gotoxy(x,11 ); textattr(15);printf("  ");textattr(col);printf("%s",card[4].c_str());textattr(15);printf(" ");}
	{gotoxy(x,12 ); textattr(15);printf("  ");textattr(col);printf("        ");textattr(col);printf("%s",value.c_str());textattr(15);printf(" ");}
	{gotoxy(x,13 );textattr(15);printf("  ");textattr(col);printf("         ");textattr(15);printf(" ");}

	_pair(pair);

};

void _pair(bool pair)
{
		if(card1 == card2){
		pair = true;
	}

	if(card2 == card3){
		pair = true;
	}

	if(card3 == card4){
		pair = true;
	}

	if(card4 == card5){
		pair = true;
	}

	if(card2 == card4){
		pair = true;
	}

	if(card2 == card5){
		pair = true;
	}

	if(card3 == card5){
		pair = true;
	}

	if(card1 == card5){
		pair = true;
	}
	if(card1 == card4){
		pair = true;
	}
	if(pair == true)
	{
		cout << "\n\n" << endl;
		cout << "pair!"; //need to figure out how to clear this after each hand
		pair = false;
	}

	if(pair == false)
	{
		cout << "\n\n" << endl;
		cout << "";
	}
}


There's a lot in your code that shouldn't be in a final project.

Your _pair() function does nothing. Sometimes you pass a vector by reference, other times you don't. Sometimes you pass parameters that you don't even use. You seed the pseudo-random number generator every time you shuffle.
You're calling main() explicitly. The names of your vectors are not intuitive, and your use of white space is hard to understand. You've got unnecessary semi-colons in some places. The way you initialize some vectors is questionable. using the using namespace directive in the global namespace is not recommended. Some of your naming conventions are misleading, such as in the case of the draw1card() function.

I suggest you start over, wrap the console/win32 stuff in a class, and make Card class and go from there.
Whoa man. Lol, so yeah I'm def up for criticism, just a little confused…

-the "using namespace" is to use the std library. Why would that not be recommended in the global?

-I guess I am using some parameters that I don't use..

-I am working on this with another person, he's calling the main.. I learned that was a programmer no-no a couple weeks ago lol.

-Not sure how the names of my vectors have anything to do with the code.

-Which vectors being initialized are questionable? (so i can fix them)

-the draw1card is def not misleading. But again, names are the least of my worries.

I'm also not going to redo my entire final because some of the names of variables don't make as much sense as code changes.

Thanks for the start of help!
Last edited on
About the using namespace std; thing, it's just that some people prefer not to use it and instead prefix everything in the std namespace with std:: (like std::cout, std::vector, etc.).

I won't give any arguments for or against that, but there's a nice discussion about it here:
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Ok man I mean I'm in intro to C++ so honestly that's a little ridiculous anyone would bring it up lol. I'm just looking for advice on my code man, not about specific social norms of coding, that don't have any affect on the program running.
Yes, some of my suggestions are stylistic in nature. What beginners sometimes don't realize is that just cleaning things up and making things consistent makes everything a lot more manageable - Its irks me, that's all.

Tell me, has your instructor covered classes yet? I'm asking so that I may present you with some fitting pseudo-code to help you with the program flow.
Topic archived. No new replies allowed.