High Low Card Game Question

Hi everyone,
This is my first post. This is homework for an intro c++ class
that I'm taking. I have to write a high low card game and I'm
stuck. I can't figure out how to divide the void deal function
so that each player receives one card or how to compare the two
hands. Any advice would be greatly appreciated.

Mike

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct Card 
{
	char *face;
	char *suit;
};

void filldeck(Card * const, char *[], char *[]);
void shuffle(Card * const);
void deal(Card * const);

int main()
{
	Card deck[ 52 ];

	char *face[] =
	{"Ace","Deuce", "Three", "Four", "Five",
	 "Six", "Seven", "Eight", "Nine", "Ten",
	 "Jack", "Queen", "King"};


	char *suit[] =
	{"Hearts", "Diamonds", "Clubs", "Spades"};

	
	srand( time(0) );

	filldeck( deck, face, suit);
	shuffle( deck );
	deal( deck );
	
	system("Pause");
	return 0;
}

void filldeck(Card * const wDeck, char * wFace[], char *wSuit[])
{
	for (int i = 0; i < 52; i++)
	{
		wDeck[i].face = wFace[ i % 13 ];
		wDeck[i].suit = wSuit[ i / 13 ];
	}
}
void shuffle( Card * const wDeck )
{
	for (int i = 0; i < 52; i++)
	{
		int j = rand() % 52;
		Card temp = wDeck[ i ];
		wDeck[ i ] = wDeck[ j ];
		wDeck[ j ] = temp;
	}
}	

void deal(Card * const wDeck )
{
	for (int i = 0; i < 52; i++)
            
            cout << wDeck[ i ].face << " of "  << wDeck[i].suit;
			
}
Last edited on
My thoughts would be to have the deal function return a single card structure back to main. You could return the first card within the array and then move that card to the back of the array. You would have to move all the cards in the array one position to the front. You can then call the deal function twice from main and have a card or each player.

As far as comparison, you may want to add an integer to the card structure to represent face value. This will make comparison much easier.
i did this http://www.cplusplus.com/forum/beginner/69420/ for an assignment. see how things are done there. here is the working main.cpp
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
//
//  main.cpp
//  dealer2
//
//  Created by Home on 4/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include "Header.h"
#include <fstream>
#include <exception>
#include <string> 
#include <stdio.h>
#include <stdlib.h>

#define STRAIGHT_FLUSH 6
#define FLUSH 5
#define FOUR_OF_A_KIND 4
#define STRAIGHT 3
#define THREE_OF_A_KIND 2
#define PAIR 1
#define EMPTY 0 

void wait(int); 
void newshift(aCard,int,DeckofCards&);  
void swap(aCard&,aCard &); 
void sort(aCard[]);   
void replace(aCard[],DeckofCards&); 
int determinate(aCard[]); 
int quat();
std::string won(int);
bool sortorder(const aCard&,const aCard&); 

aCard operator+(aCard a, int i){
    aCard temp;//creates returned card
    temp.suit = a.suit; 
    temp.value = a.value + i; 
    return temp; 
}
bool operator==(aCard a, aCard b){
    return ((a.suit == b.suit) && (b.value == a.value)); 
}

int main(){
    int wager = 0; 
    int bank = quat();
    char re = 'y';
     DeckofCards deck; deck.shuffle(); aCard hand[5];
    if(bank == EXIT_FAILURE){ bank = 100; 
        std::cout << "\nNo bank was found. Starting with " << bank << " Quatloos\n";
    }
    //starts the IO exchange 
start: {
    for(int i = 0; i < 5; i++){hand[i] = deck.deal();}//deal first hand
    sort(hand); //sorts hand
    std::cout << "Your bank has " << bank << " quatloos.\n"; 
    wait(1);
    displayHand(hand, "Your cards so far..."); //displays hand
    
    while(wager > bank || wager == 0){ 
        std::cout << "What is your wager?";
        std::cin >> wager; 
        std::cin.ignore(std::numeric_limits<int>::max(), '\n'); //clears the \n
    }
    bank -= wager; 
    
    replace(hand, deck); 
    sort(hand); 
    displayHand(hand, "Your Cards"); 
    int profit = determinate(hand) * wager;
    bank += profit; 
    
    //deposits 
    std::ofstream deposit; 
    deposit.open("/Users/home/Desktop/dealer2/dealer2/bank.txt"); 
    deposit << bank; 
    deposit.close(); 
    
    if(profit == 0) std::cout << "You lost\n"; 
    else { std::cout << "You Have a " << won(profit/wager) << " And made " << profit << "$\n"; }
    
    std::cout << "Would you like to play again?";
    std::cin >> re; 
    std::cin.ignore(std::numeric_limits<int>::max(), '\n' ); 
    wager = 0; 

}
    if((tolower(re) == 'y') && !(bank == 0)) goto start; 
    std::cout << "Thank you for giving us your money"; 
    
    return EXIT_SUCCESS;
}

std::string won(int i){
    switch (i) {
        case 6:
            return "Straight Flush"; 
            break;
        case 5:
            return "Flush"; 
            break;
        case 4:
            return "Four of a Kind"; 
            break;
        case 3:
            return "Straight"; 
            break;
        case 2:
            return "Three of a kind"; 
            break;
        case 1:
            return "Pair"; 
            break;
            
        default:
            break;
    }
    return NULL; 
    }
    
bool sortorder(const aCard &a, const aCard &b){
    if(a.suit < b.suit) 
        return true; 
    if(a.suit == b.suit){
        if(a.value < b.value)
            return true;
    }
    return false; 
}

int quat(){
    std::ifstream bank; int i; 
    bank.exceptions(std::ifstream::failbit | std::ifstream::badbit); 
    try{bank.open("/Users/home/Desktop/dealer2/dealer2/bank.txt");}
    catch(std::ifstream::failure &e){
         
        return EXIT_FAILURE; 
    }
    bank >> i;
    bank.close(); 
    return i; 
}

void wait(int seconds){
    clock_t endwait;
    endwait = clock() + seconds * CLOCKS_PER_SEC; 
    while(clock() < endwait) {} 
}

void swap(aCard &a, aCard &b){
    aCard temp; 
    temp = b; 
    b = a;
    a = temp;
}

void sort(aCard x[]){
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            if(sortorder(x[i], x[j])) swap(x[i], x[j]); 
        }
    }
}

void replace(aCard x[], DeckofCards &y){ //error occurs here
    std::string s; 
    int r = 0; 
    std::cout << "      Card 1       Card 2       Card 3       Card 4       Card 5\n"; 
    std::cout << "Which cards would you like to replace: ";
    getline(std::cin, s); //this skips before i added 
    for(int i = 0; i < s.length() && r < 3; i++){
        if(!isspace(s[i])){
            int j = (atoi(&s[i]))%5;
            x[--j] = y.deal(); 
            r++;
        }
    }
}

int determinate(aCard x[]){
    int q = 1; 
    int suit_match = 1, value_match = 1, consecutive_value = 1; 
    for(int i = 0; i < 5; i++){
        for(int j = i+1; j < 5; j++){
            //evaluation 
            if(x[i].suit == x[j].suit) ++suit_match;
            if(x[i].value == x[j].value) ++value_match; 
            if(x[i] + q++ == x[j]) ++consecutive_value; 
            //evals needed per inner loop run. 
            if( suit_match == 5 && consecutive_value == 5) return STRAIGHT_FLUSH;
            if( suit_match == 5 ) return FLUSH;
            if( consecutive_value == 5) return STRAIGHT; 
        }
        q += 10; 
        suit_match = 0; 
        consecutive_value = 0; 
    }
        if( value_match == 4) return FOUR_OF_A_KIND; 
        if( value_match == 3) return THREE_OF_A_KIND; 
        if( value_match == 2) return PAIR; 
    return EMPTY; 
}

hope this gives you ideas.
Thanks for the quick reply...
I'm new to structs and they confuse me. How do I return a single
card structure back to main? Also how do I move the cards in an array
one position forward? Here is code I wrote for the comparison.. Is this
a start in the right direction or am I way off base with my logic?

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
void deal(Card * const deck)
{

    cout << "Player one's card";
    
    int x;
    int cards;
    int player1[26];
    int player2[26];
    
    for(cards = 0; cards < 26; cards++)
    {
        player1[cards] = deck[cards];

    }
    cout << "Player two's card";
    
    for(cards = 26, x =0; cards < 52, x < 26; cards++, x++)
    {
        player2[x] = deck[cards];
        
    }

    int card_number;

    for(card_number=0; card_number <26; card_number++)
    {
        if(player1[card_number] > player2[card_number])
        {
            cout << "You Win";
        }

        else if(player1[card_number] < player2[card_number])
        {
            cout << "You Loose";
        }
        else
        {
            cout << "DRAW";
        }

    }
    
}
Thanks UI,
I'm I'm going to check out your code right now.
In the void deal() function in the first program when I try to write an assignment of " int deck = wDeck " the compiler tells me that "the conversion of Card const to int is invalid" My question is how would I assign wDeck to a new variable of any type?
there are several ways to do this. you could make your own operator.
1
2
3
void operator=(int &i, wDeck){
    i = wDeck.integer;  
}


you could make deck a card pointer that just advances through the deck.(which is what i did.
Last edited on
Topic archived. No new replies allowed.