Functions calculating unknown numbers of parameters with specific rules?

Hello fellow coders,
I've been working on various exercises and have attempted to create a blackjack game. I have created a function named Card that looks at the deck (an array of 1-52 shuffled at random) and views it as 4 differing suits of 1-13 before applying rules to follow that mimic blackjack. What I want to do is create a function for hitting that works regardless of the number of cards involved, instead of creating separate functions that apply to each card count. Here is what I have 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include<iostream>//required for cout
#include<windows.h>//required for rand
#include<time.h>//required for time
#include<conio.h>
using namespace std;

//function prototypes
void hit(int array[],int deck, int &q);
void shuffle(int array[],int startPoint,int endPoint);

void Color(int color)
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon,color);
}

int Card(int array[],int x)
{
    //change color and output card values based on numeric value pulled from deck
    if (array[x]<27)
        Color(12);
    else
        Color(8);
    int temp=x;//use temp to translate values 1-52 into 1-13 for each suit
    cout<<((array[x]<14) ? "\3" : (array[x]>13 && array[x]<27) ? "\4" : 
                 (array[x]>26 && array[x]<40) ? "\5" : "\6");
    temp=((array[x]>39) ? array[x]-39 : (array[x]>26 && array[x]<40) ? 
                 array[x]-26 : (array[x]<27 && array[x]>13) ? array[x]-13 : array[x]);
    if (temp<11 && temp!=1)
        cout<<temp<<" ";//just print the value unless it is 1 or above 10
    else//output A,J,Q, or K based on value
        cout<<((temp==11) ? "J" : (temp==12) ? "Q" : (temp==13) ? "K" : "A")<<" ";
    cout<<((temp==10) ? "" : " ");//add additional space for single digit values
    Color(7);
    //apply blackjack rules to card value
    //deal with face cards
    if (temp>10)
        temp=10;
    //deal with aces in main to get total card run
    //returns cards value when desired as well as outputting the card itself
    return temp;
}
int main()
{
    int array[52]={0};//array of cards
    int deck=0;//tracks position of draw
    int psum=0,csum=0,rounds=0,play=1,q=2;
    int split=26;//tracks where deck splits
    srand(time(NULL));//set random # seed
    //fill deck with values 1-52
    for (int i=0;i<52;i++)
        array[i]=i+1;

    shuffle(array,0,51);//randomized shuffle of whole deck

    //blackjack hand sample
    //output initial cards and assign values
    cout<<"Your cards\tTotal\n";
    int temp=Card(array,deck),temp2=Card(array,deck+2);

    //alters ace's value to reflect blackjack rules
    //aces to 11 when sum is under 12
    temp=((temp==1 && temp+temp2<12) ? 11 : temp);
    temp2=((temp2==1 && temp+temp2<12) ? 11 : temp2);
    //output sum of card values
    cout<<"\t "<<temp+temp2<<endl;
    //output house's first card
    cout<<"House cards\n";
    int temp0=Card(array,deck+1);
    cout<<" ?\t\t ?\n";

    if (temp+temp2==21)
    {
        cout<<"\nBlackjack!";
    }
    else
    {
        cout<<"Would you like to hit, or stand(H/S):";
        int c=getchar();
        if (c=='h' || c=='H')
        {
            hit(array,deck,q);
        }
        else if (c=='s' || c=='S')
        {
            cout<<"House cards\n";
            int temp3=Card(array,deck+1);
            int temp0=Card(array,deck+3);
            cout<<"\t  "<<temp0+temp3<<endl;
        }
    }
}//end of main


void shuffle(int array[],int startPoint,int endPoint)
{
    for (int i=startPoint;i<endPoint+1;i++)
    {
      int temp=array[i];//use temp to hold i's value
      int temp2=rand()%(endPoint+1);//use temp2 to randomize where to swap with
      array[i]=array[temp2];//copy from randomized position to i's position
      array[temp2]=temp;//replace randomized position with temp's value
    }//repeat loop until cards between start and endpoint are shuffled
}

void hit (int array[],int deck, int &q)
{
    //q is the quantity of cards in play
    q++;
    //start with repeat of initial cards
    cout<<"Your cards\t Total\n";
    int temp=Card(array,deck),temp2=Card(array,deck+2);
    int temp3=Card(array,deck+q);
    //alters ace's value to reflect blackjack rules
    //aces to 11 when sum is under 12
    temp=((temp==1 && temp+temp2+temp3<12) ? 11 : temp);
    temp2=((temp2==1 && temp+temp2+temp3<12) ? 11 : temp2);
    temp3=((temp3==1 && temp+temp2+temp3<12) ? 11 : temp3);
    cout<<"\t  "<<temp+temp2+temp3<<endl;
    //output house's first card
    cout<<"House cards\n";
    int temp0=Card(array,deck+1);
    if (temp+temp2+temp3<21)
    cout<<" ?\t\t  ?\n";
    else
        {
         int tmp=Card(array,deck+q+1);
         cout<<"\t  "<<temp0+tmp<<endl;
        }
}


I know that not all the code is finished and that alot of things are still missing, but instead of creating multiple repetitive functions to handle hits based on quantity of cards, I'd rather have a function that can do that regardless of the number of cards involved. Does anyone have any ideas how my hit or card function could be modified to accomplish this?
Last edited on
Interesting links. How would I go about summing the card values when I don't know how many there are, or feed the va_list the returned values from Card()? I've seen a couple examples of variadic functions, but don't entirely understand how they work, at least enough to interface it with my existing Card function. I thought I could somehow set it up by using q as the number of parameters, but don't see how unless I can plug the returned values from card into the va_list, while somehow accounting for the running total to determine whether an ace is equal to 1 or 11.
Last edited on
Looks like I was overthinking the solution. I found another way to do this, but it has it's own dilemmas and pitfalls.
Topic archived. No new replies allowed.