Need help for a card game.

This is my assignment.I'm very new to C++.So this is my code.Is there anything that I can fix.


There are four suits and each suit contains 13 card. For ease, an Ace is considered the card with smallest value in the deck.Develop a program for a card game that uses a deck of 52 cards. Distribute all the 52 cards randomly between two players and each player will discard the card to the center of the table.
Game rules:
If one card has a value less than or equal to 3, the other player has to give a corresponding number of cards to the number reported on the card.
Example:
Player 1:
King of Spades
Player 2:
Queen of Clubs
Player 1:
3 of Diamond (now Player 2 has to discard 3 cards)
Player 2:
Jack of Hearts
2 of Clubs (now Player 1 has to throw 2 cards although Player 2 supposed to discard 3 cards)
Player 1:
6 of Diamond
10 of Spades
The game ends when one of the players does not have any cards in his hand and this player is the winner. Display the winner at the end of the program. Beware that there might be a draw for this game.
Note:
 Ensure no duplicate card being assigned (only one set of cards).
 Ensure the randomness in the card distribution so that each run will give a different set of card.
Sample output:
Player 1: Jack of Diamonds
Player 2: 5 of Clubs
Player 1: 2 of Diamonds
Player 2: 9 of Hearts
Player 2: 10 of Spades


Player 2 has 3 cards left. Player 1 wins!

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


using namespace std;

int sumofarray(int d[]);
void restartgame();
void turnplayer1(int player1[],string deck[],int player2[]);
void turnplayer2(int player2[],string deck[],int player1[]);
void endgame();
char lol;
int main()
{
string deck[53]= {"---------------","Ace of Clubs   ","2 of Clubs    ","3 of Clubs     ","4 of Clubs    ","5 of Clubs    ","6 of Clubs     ","7 of Clubs     ","8 of Clubs    ","9 of Clubs    ","10 of Clubs   ","Jack of Clubs  ","Queen of Clubs ","King of Clubs  ","Ace of Diamonds","2 of Diamonds  ","3 of Diamonds  ","4 of Diamonds  ","5 of Diamonds","6 of Diamonds","7 of Diamonds","8 of Diamonds","9 of Diamonds","10 of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds","Ace of Hearts  ","2 of Hearts    ","3 of Hearts  ","4 of Hearts   ","5 of Hearts    ","6 of Hearts   ","7 of Hearts   ","8 of Hearts   ","9 of Hearts   ","10 of Hearts  ","Jack of Hearts ","Queen of Hearts","King of Hearts ","Ace of Spades  ","2 of Spades   ","3 of Spades   ","4 of Spades   ","5 of Spades   ","6 of Spades   ","7 of Spades   ","8 of Spades   "  ,"9 of Spades   ","10 of Spades  ","Jack of Spades ","Queen of Spades","King of Spades "};
int array[52]={0};
int x=1,y=0;
srand(time(0));
array[0]=rand()%52+1;
    do
    {
        array[x]=rand()%52+1;
        y=0;
        while (y<x)
        {
            if (array[y]==array[x])
            {
                --x;
                break;
            }
            ++y;
        }
        ++x;
    } while (x<52);

   	int *shuffle=array;
	int player1[29];
	int player2[29];
	for(int a=0;a<26;a++){
	    player1[a]=shuffle[a];
	}

	for (int i=0;i<26;  i++){
	    player2[i]=shuffle[i+26];
	}

	for (int c=0;c<26;c++){
	    cout <<player1[c]<<"*\t";
	}
	cout<<"\n\n";
	for (int e=26;e<52;e++){
	    cout <<player2[e]<<"^\t";
	}
	cout<<"\n\n";
	cout <<"Player 1 :\t\t\t\tPlayer 2 : \n";
	for (int d=0;d<26;d++){
	    int v=player1[d];
	    int c =player2[d];
		cout <<d<<" = " <<deck[v]<<"\t\t\t"<<d<<" = " <<deck[c]<<"\n";
	}

for (int u=0;u>0;u++){
int p1=sumofarray(player1);
int p2=sumofarray(player2);
if (p1==0 && p2==0){
    cout << "Draw\n";
    restartgame();
}
else if (p1==0){
    cout <<"Player 1 wins!\n";\
    restartgame();

}
else if (p2==0){
    cout <<"Player 2 wins!\n";
   restartgame();

}
else if (u%2==0){
    turnplayer1(player1,deck,player2);

}
else if (u%2==1){
    turnplayer2(player2,deck,player1);
}
}

}



int sumofarray(int d[]){
int k;
for(int i=0;i<29;i++){
    k=k+d[i];
}
return k;
}
void restartgame(){
cout << "Restart? <Y/N> ";

if (lol=='Y'){
    main();
}
else if (lol=='N'){
    endgame();
}
else {
    restartgame();
}
}

void turnplayer1(int player1[],string deck[],int player2[]){
cout << "Enter the desired card to withdraw : ";
int yolo;
cin >> yolo;
cout << "Player 1 : "<< deck[player1[yolo]]<<endl;
player1[yolo]=0;
if (player1[yolo]%13==2){
    turnplayer2(player2,deck,player1);
}
else if(player1[yolo]%13==3){
     turnplayer2(player2,deck,player1);
     turnplayer2(player2,deck,player1);
}
}

void turnplayer2(int player2[],string deck[],int player1[]){
cout << "Enter the desired card to withdraw : ";
int yo;
cin >> yo;
cout << "Player 2 : "<< deck[player2[yo]]<<endl;
player2[yo]=0;
if (player2[yo]%13==2){
    turnplayer1(player1,deck,player2);
}
else if(player2[yo]%13==3){
     turnplayer1(player1,deck,player2);
    turnplayer1(player1,deck,player2);
}

}
void endgame(){
cout <<"Thank you";
}
I've not looked deeply at this, just a couple of points which were visible as I read through the code. It's likely that there are other issues. I didn't attempt to compile or run the code.

Function sumofarray()
Variable int k is not initialised - contains garbage. The result returned by the function is consequently meaningless.

Function restartgame()
Variable lol is global. Make it a local variable instead. There is no user input, so testing for a response doesn't make sense.

and this

In standard C++ you cannot call main()

line 104 is illegal and will not compile.

The usual way of restarting the program is to have a loop inside main which repeats or terminates as required.

Also in main, I'd make the code which shuffles the cards into a separate function, rather than having it all inline.

As for formatting, line 16, even with a wide-screen display, 945 characters is rather too long for a single line. Feel free to insert line breaks.




Line 4: You use std::string, but do not include the required <string> header. This causes errors in my compiler.

Your turnplayer1() and turnplayer2() functions are identical except for the player arrays which are passed as arguments. You should consider consolidating the two functions into a single function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void turnplayer(int player[],int player_num,string deck[],int other_player[])
{   cout << "Enter the desired card to withdraw : ";
    int yo;
    int other_playernum = player_num ^ 1;  // Invert the player number
    cin >> yo;
    cout << "Player " << player_num+1 << ": "<< deck[player[yo]] << endl;
    player[yo]=0;
    if (player[yo]%13==2)
    {   turnplayer (player ,player_num, deck, other_player);
    }
    else if (player[yo]%13==3)
    {   turnplayer (other_player, other_playernum, deck, player);
         turnplayer (other_player,  other_playernum, deck, player);
    }
}

Thank you guys for replying :)
This is a updated version of my code. It runs but it doesn'tmeet the repeating condition that if the card value is less than 3 the other player has to withdraw the respecting amount of cards.
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
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

int sumofarray(int d[]);
void restartgame();
void turnplayer1(int player1[],string deck[],int player2[]);
void turnplayer2(int player2[],string deck[],int player1[]);
void endgame();

int main()
{
string deck[53]= {"---------------","Ace of Clubs   ","2 of Clubs    ","3 of Clubs     ","4 of Clubs    ","5 of Clubs    ","6 of Clubs     "
,"7 of Clubs     ","8 of Clubs    ","9 of Clubs    ","10 of Clubs   ","Jack of Clubs  ","Queen of Clubs ","King of Clubs  ","Ace of Diamonds"
,"2 of Diamonds  ","3 of Diamonds  ","4 of Diamonds  ","5 of Diamonds","6 of Diamonds","7 of Diamonds","8 of Diamonds","9 of Diamonds"
,"10 of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds","Ace of Hearts  ","2 of Hearts    ","3 of Hearts  ","4 of Hearts   "
,"5 of Hearts    ","6 of Hearts   ","7 of Hearts   ","8 of Hearts   ","9 of Hearts   ","10 of Hearts  ","Jack of Hearts ","Queen of Hearts"
,"King of Hearts ","Ace of Spades  ","2 of Spades   ","3 of Spades   ","4 of Spades   ","5 of Spades   ","6 of Spades   ","7 of Spades   "
,"8 of Spades   "  ,"9 of Spades   ","10 of Spades  ","Jack of Spades ","Queen of Spades","King of Spades "};
int array[52]={0};
int x=1,y=0;
srand(time(0));
array[0]=rand()%52+1;
    do
    {
        array[x]=rand()%52+1;
        y=0;
        while (y<x)
        {
            if (array[y]==array[x])
            {
                --x;
                break;
            }
            ++y;
        }
        ++x;
    } while (x<52);

   	int *shuffle=array;
	int player1[26];
	int player2[26];
	for(int a=0;a<26;a++){
	    player1[a]=shuffle[a];
	}

	for (int i=0;i<26;  i++){
	    player2[i]=shuffle[i+26];
	}

	for (int c=0;c<26;c++){
	    cout <<player1[c]<<"*\t";
	}
	cout<<"\n\n";
	for (int e=0;e<26;e++){
	    cout <<player2[e]<<"^\t";
	}
	cout<<"\n\n";
	cout <<"Player 1 :\t\t\t\tPlayer 2 : \n";
	for (int d=0;d<26;d++){
	    int v=player1[d];
	    int c =player2[d];
		cout <<d<<" = " <<deck[v]<<"\t\t\t"<<d<<" = " <<deck[c]<<"\n";
	}

do
{


 turnplayer1(player1,deck,player2);

}

while (-1);
}

int sumofarray(int d[]){
int k=0;
for(int i=0;i<26;i++){
    k=k+d[i];
}
return k;
}
void restartgame(){
cout << "Restart? <Y/N> ";
char lol;
cin>>lol;
if (lol=='Y'){
    main();
}
else if (lol=='N'){
    endgame();
}
else {
    restartgame();
}
}

void turnplayer1(int player1[],string deck[],int player2[]){
int e=sumofarray(player1);
cout <<e<<"!!!";
if(e==0){

    cout <<"Player 1 wins!\n";\
    restartgame();
}
else{
cout << "Enter the desired card to withdraw Player 1: ";
int yolo;
cin >> yolo;
cout << "Player 1 : "<< deck[player1[yolo]]<<endl;
int z = player1[yolo];
if (z==0){
     turnplayer1(player1,deck,player2);
}
else{
cout <<z<<" and "<<z%13<<endl;
player1[yolo]=0;
if (z%13==2){
    turnplayer2(player2,deck,player1);
     turnplayer2(player2,deck,player1);
}
else if(z%13==3){
     turnplayer2(player2,deck,player1);
     turnplayer2(player2,deck,player1);
    turnplayer2(player2,deck,player1);
}
else{
     turnplayer2(player2,deck,player1);
}
}
}
}

void turnplayer2(int player2[],string deck[],int player1[]){
int e=sumofarray(player2);
cout <<e<<"!!!";
if(e==0){

    cout <<"Player 2 wins!\n";\
    restartgame();
}
else{
cout << "Enter the desired card to withdraw Player 2: ";
int yo;
cin >> yo;
cout << "Player 2 : "<< deck[player2[yo]]<<endl;
int z = player2
[yo];
if (z==0){
     turnplayer2(player2,deck,player1);
}
else{

player2[yo]=0;
if (z%13==2){
    turnplayer1(player1,deck,player2);
       turnplayer1(player1,deck,player2);
}
else if(z%13==3){
       turnplayer1(player1,deck,player2);
          turnplayer1(player1,deck,player2);
             turnplayer1(player1,deck,player2);
}
else{
       turnplayer1(player1,deck,player2);
}
}
}
}
void endgame(){
cout <<"Thank you";
}



I'm still working on it. Feel free to help :D
Last edited on
Topic archived. No new replies allowed.