C++ Poker - Winning Conditions

Hi guys,

I need some help with 2 things:

1. Guidance for winning conditions, ex flush etc.
2. A way to store every used card that has been pulled up so far.

Please refrain from using advanced code, I need guidance with simple, easy to understand code so I can master the basics. (Avoiding vectors, pointers, etc).

I know I might need a bubble sort for my current hand, but not sure how that works with suits etc.

I want to be able to finish this code with basic code, ty in advance.

ps. I got a few errors commented and not sure why im getting said error. ty.
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
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
 
//5. Write a program to play poker! You can provide 5 cards to the player, let that player choose new
//cards, and then determine how good the hand is. Think about whether this is easy to do? What
//problems might you have in terms of keeping track of cards that have been drawn already? Was this
//easier or harder than the slot machine?
 
string cards[13] = {"2", "3", "4", "5", "6","7", "8", "9","10", "j", "q", "k","Ace"};
string suits[4] = {" of Hearts", " of Spades", " of Diamond", " of Clubs" };

 
int randcard() {
	return rand() % 13;
}
 
int randsuits(){
	return rand() % 4;
}
 
string* drawHand()
{
	string* hand = new string[5];
	for(int i=0; i<5; i++){
	hand[i] = cards[randcard()] + suits[randsuits()];
	}
	return hand;
}
 
string* replaceHand(int i)
{
	string* hand = drawHand();
	string* newhand; /// if i remove the pointer, I cant send new hand to hand.
	newhand[i] = cards[randcard()] + suits[randsuits()];
	newhand[i] = hand[i];
return newhand;
}

void displayHand(){
	string* hand = drawHand();
	cout << "1." << hand[1] << endl;
	cout << "2." << hand[2] << endl;
	cout << "3." << hand[3] << endl;
	cout << "4." << hand[4] << endl;
	cout << "5." << hand[5] << endl;
}

 
string winningHands(){
 
string* hand = drawHand();
 
if(hand[1] += 1) == hand[2] && (hand[2] += 1) == hand[3] && (hand[3] += 1) == hand[4] && (hand[4] +=1) == hand[5]) 
{
	return "You have a straight flush!!";
}
/* TODO: Change this if statement to check the hand that was drawn.
You'll probably need a for loop.
if(hand() == "j of Clubs" && hand() == "10 of Clubs" && hand() == "9 of Clubs" && hand() == "8 of Clubs" && hand() == "7 of Clubs"){
return "You have a straight flush!!";
}
if(hand() == " of Clubs" && hand() == " of Hearts" && hand() == " of Spades" && hand() == " of Diamond" ){
return "You have four of a kind!!";/// comparing a partial string, not sure if this is correct
}
*/
return 0;
 
delete[] hand;
}
 
int main() {
srand( time( NULL ) );
int newCard(-1);
string* hand = drawHand();
char confirmHand;


do{
	cout << "Input the card number that you wish to replace" << endl;
	displayHand();
	cin <<  /* << Error?? */ newCard; // inputs 2
	replaceHand(newCard);
	displayHand();
	cout << "Are you pleased with your hand? y/n" << endl;
	cin <<  /* << Error?? */ confirmHand;

	if(confirmHand == y){
		cout << winningHands() << endl; //// incomplete
	}else{
		cout << " input the card that you wish to replace" << endl;
		cin <<  /* << Error?? */newCard;  
		replaceHand(newCard);
	}
}while(!winningHands);




system ("PAUSE");
}
Please refrain from using advanced code, I need guidance with simple, easy to understand code so I can master the basics. (Avoiding vectors, pointers, etc).

Code using standard containers, such as std::vector, is more simple and easier to understand (and easier to get correct) than code using manually managed dynamic memory.

Your function replaceHand leaks memory and bludgeons other memory it has no business accessing.


1. Guidance for winning conditions, ex flush etc.

Describe to yourself the steps you would use to do that for a real hand for yourself. Convert that to code. You might find it easier to write separate functions for each winning combination.


2. A way to store every used card that has been pulled up so far.

The usual way is to use a deck of cards and draw from it. The cards left in the deck haven't been pulled. You might implement this using an array of cards and a variable to track how many cards have been drawn from the deck.


Line 56: You're missing a left paren.
Lines 84, 88 and 94: istreams use the insertion operator (>>) not the extraction operator (<<).
wow the cin error is so noob.

So the way I have the deck now wouldnt work?I would have to make an array of 52 cards?

As for replaceHand, you recommend I learn about vectors and go about it that way, but just for learning purposes why does it leak memory?
As for replaceHand, you recommend I learn about vectors and go about it that way, but just for learning purposes why does it leak memory?


1
2
3
4
5
6
7
8
string* replaceHand(int i)
{
    string* hand = drawHand();   // variable named hand points to dynamically allocated memory.
    string* newhand;             // variable named newhand points to random place in memory.
    newhand[i] = cards[randcard()] + suits[randsuits()];  // write to the random place in memory.
    newhand[i] = hand[i];        // overwrite what was just written.
    return newhand;              // return the random memory address.
}


The variable hand stops existing when the function returns and the address it contains is lost. Since it was the only variable containing the address of the dynamic memory returned from drawHand that memory is lost to us, thus it is leaked.
Last edited on
how would I go about fixing that? I want the replace hand to go back to array hand[5].
Topic archived. No new replies allowed.