More Card Game Problems

Hello Everyone, I'm working on a WAR game and it's sort of a work in progress. The basic idea is that a single array holds values 1-52 with their positions randomized, representing a shuffled card deck, and each group of 13 values represents each suit. The first half of the array is the player's cards, and the second half the computer's (at least initially). As the game plays out, where the deck splits changes. Other variables are used to track where each player is drawing from in the deck, and the position of each player's draw moves outward from the center. When reaching the end of the array, regardless of which player it is, their cards are shuffled and position reset.
Mostly, the program seems to work, but sometimes it is falling outside of the array, resulting in incorrect card appearances and invalid card totals. Any help in pinpointing this problem would be appreciated.
basic.h
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
#ifndef BASIC_H_INCLUDED
#define BASIC_H_INCLUDED
#endif // BASIC_H_INCLUDED

#include<iostream>//required for cout
#include<windows.h>//required for rand
#include<time.h>//required for time
#include<conio.h>
using namespace std;
void Color(int color)
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon,color);
}

void shuffle(int array[])
{//randomly assign values 0-51 to the array
    int x=1;int y=0;//variable initialization
        array[0]=rand()%52+1;//first number assigned to 0
    do
    {
        array[x]=rand()%52+1;//value generated for x's position
        y=0;//re-initialization to ensure check of all positions
        while (y<x)
        {//iterate through array while checking for duplicate values
            if (array[y]==array[x])
            {//if duplicate is found, generate another value for this position
                --x;//repeat this position of the array for new number
                break;//by breaking out of the loop after decrementing
            }
            ++y;//increment y to check additional array positions
        }
        ++x;//increment x to assign values to additional array positions
    } while (x<52);//continue loop until whole deck is assigned
}

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
#include "basic.h"

void War(int array[],int &player,int &computer, int &split);
void playerShuffle(int array[],int startPoint,int endPoint);
void Card(int array[],int x)
{
    if (array[x]<27)
        Color(12);//assign red to lower values for hearts/diamonds
    else
        Color(8);//assign dark text to higher values for clubs/spades
    int temp=x;//output suit symbols based on value
    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)//subtract as needed to get true value
        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);
}

int main()
{
    int array[52]={0};//array of cards
    int player=25,computer=26;//tracks starting positions of players
    int psum=0,csum=0,rounds=0;
    int split=26;//tracks where deck splits
    srand(time(NULL));//set random # seed
    shuffle(array);//randomized shuffle

    while (split>0 && split<52)
    {
    cout<<"Your card:\tValue:\n   ";
    Card(array,player);
    psum=((array[player]>39) ? array[player]-39 : (array[player]>13 && array[player]<27) ? array[player]-13 :
          (array[player]>26 && array[player]<40) ? array[player]-26 : array[player]);
    player--;
    cout<<"\t\t  "<<psum<<endl;
    cout<<"My card:\n   ";
    Card(array,computer);
    csum=((array[computer]>39) ? array[computer]-39 : (array[computer]>13 && array[computer]<27) ? array[computer]-13 :
          (array[computer]>26 && array[computer]<40) ? array[computer]-26 :  array[computer]);
    computer++;
    cout<<"\t\t  "<<csum<<endl;
    cout<<((psum>csum) ? "You " : (csum>psum) ? "I " : "");
    cout<<((psum!=csum) ? "won this round.\n" : "");
    split=((psum>csum) ? split+1 : (csum>psum) ? split-1 : split);
    if (psum==csum)
        War(array,player,computer,split);
    cout<<"You now have "<<split<<" cards.\n";
        if (split==0 || split==52)
            break;
        if (player<0)
        {
            playerShuffle(array,0,split-1);
            player=split-1;
            cout<<"You shuffled your cards.\n";
        }
        if (computer>51)
        {
            playerShuffle(array,split,51);
            computer=split;
            cout<<"I shuffled my cards.\n";
        }
    cout<<"Push a key to play another round.";
    getch();
    cout<<endl;
    rounds++;//count each round played
    }
    cout<<((split!=0) ? "I " : "You ")<<"ran out of cards.\n";
    cout<<((split==0) ? "I " : "You ")<<"win!\n";
    cout<<"We played "<<rounds<<" rounds.\n";
}//end of main

void War(int array[],int &player,int &computer, int &split)
{
    int psum=0;int csum=0;//re-initialization
    cout<<"\nWAR!!!!!!!!!!!!!!!!!!!\n";
    cout<<"Your cards: \tTotal:\n";
    for (int i=0;i<3;i++)
    {
        Card(array,player);
        psum=((array[player]>39) ? psum+array[player]-39 : (array[player]>13 && array[player]<27) ?
        psum+array[player]-13 : (array[player]>26 && array[player]<40) ? psum+array[player]-26 : psum+array[player]);
        player--;
    }
        cout<<"\t  "<<psum<<endl<<"My cards:\n";
    for (int i=0;i<3;i++)
    {
        Card(array,computer);
        csum=((array[computer]>39) ? csum+array[computer]-39 : (array[computer]>13 && array[computer]<27) ?
              csum+array[computer]-13 : (array[computer]>26 && array[computer]<40) ? csum+array[computer]-26 : csum+array[computer]);
        computer++;
    }
    cout<<"\t  "<<csum<<endl;
    cout<<((psum>csum) ? "You " : (csum>psum) ? "I " : "");
    cout<<((psum!=csum)? "won this battle.\n" : "");
    split=((psum>csum) ? split+3 : (csum>psum) ? split-3 : split);
    if (psum==csum)
        cout<<"The battle was a draw.\n";
}

void playerShuffle(int array[],int startPoint,int endPoint)
{
    for (int i=startPoint;i<endPoint+1;i++)
    {
      int temp=array[i];//use temp to hold value being swapped
      int temp2=rand()%(endPoint+1);//randomize where to swap with
      array[i]=array[temp2];//copy from randomized position
      array[temp2]=temp;//replace randomized position with temp's value
    }//repeat loop until player's cards are shuffled
}
Last edited on
Topic archived. No new replies allowed.