Help with card game in c++

I need some help figuring out the last part of my game code.
Some of it is in Swedish, hope it's still OK.

The one thins I'm missing is having different value for the card suit.

For example: if both player and computer gets a 5, the one with 5 of spades should win. (spades, hearts, diamonds, clubs)

I should have this in my code as a constructor, before int main.

Would really appreciate if someone could help me out and show me how I can do this.

Thank you! :)

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// ConsoleApplication11.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <ctime>
#include <string>
#include <sstream>

using namespace std;

struct card {
	string suite;
	int value;
	string name;
};



void printFunction()
{
	cout << "Welcome! This is a card dueling game." << "\n" << endl;
}

int main()
{
	card cards[52] = {
	{"hearts", 2, "Two of Hearts"},
	{"hearts", 3, "Three of Hearts"},
	{"hearts", 4, "Four of Hearts"},
	{"hearts", 5, "Five of Hearts"},
	{"hearts", 6, "Six of Hearts"},
	{"hearts", 7, "Seven of Hearts"},
	{"hearts", 8, "Eight of Hearts"},
	{"hearts", 9, "Nine of Hearts"},
	{"hearts", 10, "Ten of Hearts"},
	{"hearts", 11, "Jack of Hearts"},
	{"hearts", 12, "Queen of Hearts"},
	{"hearts", 13, "King of Hearts"},
	{"hearts", 14, "Ess of Hearts"},
	{"spades", 2, "Two of Spades"},
	{"spades", 3, "Three of Spades"},
	{"spades", 4, "Four of Spades"},
	{"spades", 5, "Five of Spades"},
	{"spades", 6, "Six of Spades"},
	{"spades", 7, "Seven of Spades"},
	{"spades", 8, "Eight of Spades"},
	{"spades", 9, "Nine of Spades"},
	{"spades", 10, "Ten of Spades"},
	{"spades", 11, "Jack of Spades"},
	{"spades", 12, "Queen of Spades"},
	{"spades", 13, "King of Spades"},
	{"spades", 14, "Ess of Spades"},
	{"diamonds", 2, "Two of Diamonds"},
	{"diamonds", 3, "Three of Diamonds"},
	{"diamonds", 4, "Four of Diamonds"},
	{"diamonds", 5, "Five of Diamonds"},
	{"diamonds", 6, "Six of Diamonds"},
	{"diamonds", 7, "Seven of Diamonds"},
	{"diamonds", 8, "Eight of Diamonds"},
	{"diamonds", 9, "Nine of Diamonds"},
	{"diamonds", 10, "Ten of Diamonds"},
	{"diamonds", 11, "Jack of Diamonds"},
	{"diamonds", 12, "Queen of Diamonds"},
	{"diamonds", 13, "King of Diamonds"},
	{"diamonds", 14, "Ess of Diamonds"},
	{"clubs", 2, "Two of Clubs"},
	{"clubs", 3, "Three of Clubs"},
	{"clubs", 4, "Four of Clubs"},
	{"clubs", 5, "Five of Clubs"},
	{"clubs", 6, "Six of Clubs"},
	{"clubs", 7, "Seven of Clubs"},
	{"clubs", 8, "Eight of Clubs"},
	{"clubs", 9, "Nine of Clubs"},
	{"clubs", 10, "Ten of Clubs"},
	{"clubs", 11, "Jack of Clubs"},
	{"clubs", 12, "Queen of Clubs"},
	{"clubs", 13, "King of Clubs"},
	{"clubs", 14, "Ess of Clubs"},
	};


	bool used[52]; //Försök till att slumpa kort och lägga undan dragna kort.
	for (int x = 0; x < 52; x++)
	{
		used[x] = false;
	}

	srand(time(NULL));
	int slumptal = rand() % 52;
	int slumptal2 = rand() % 52;

	int highestCardPlayer;
	int highestCardComputer;
	highestCardPlayer = slumptal;
	if (used[slumptal] == true)
	{

	}

	highestCardComputer = slumptal2;
	if (used[slumptal2] == true)
	{

	}

	string player1;
	string computer = "computer";

	printFunction();

	cout << "Player1 , type your name here;" << "\n" << endl;
	cin >> player1;
	cout << "Welcome " << player1 << "." << endl << endl;
	cout << "The game begins....." << endl << endl;
	cin.get();

	bool loop = true;
	int playerwins = 0;
	int computerwins = 0;
	int playerloses = 0;
	int computerloses = 0;
	bool omstartloop = true;

	while (omstartloop == true)
	{
		int drawnCardNumber = 0;
		// Blanda kort

		for (int i = 0; i < (52 - 1); i++) {
			int r = i + (rand() % (52 - i));
			card temp = cards[i];
			cards[i] = cards[r];
			cards[r] = temp;
		}

		while (loop == true)
		{

			highestCardPlayer = drawnCardNumber;

			cout << "Here's your card....." << "\n" << endl;
			cout << "Card: " << cards[drawnCardNumber].name << endl;
			cout << player1 << ", You got: " << cards[highestCardPlayer].name

				<< endl;

			cin.ignore();
			drawnCardNumber++;

			highestCardComputer = drawnCardNumber;

			cout << computer << ", here's your card..... " << "\n" << endl;
			cout << "Card:" << cards[drawnCardNumber].name << endl;
			cout << computer << "You got:" <<

				

				cards[highestCardComputer].name << endl;
			cin.ignore();
			drawnCardNumber++;
			cin.get();

			if (cards[highestCardPlayer].value > cards[highestCardComputer].value)
			{
				cout << player1 << "You win!" << endl;
				playerwins++;
				computerloses++;

				cin.ignore();
				cin.get();
			}
			else if (cards[highestCardPlayer].value < cards[highestCardComputer].value)
			{
				cout << player1 << "You lost!" << endl;

					computerwins++;
				playerloses++;

				cin.ignore();
				cin.get();
			}
			else
			{

				cout << player1 << "It's a draw!" << endl;

				cin.ignore();
				cin.get();
			}

			if (playerwins == 2)
			{
				cout << player1 << "Congratulations! You won the game.";
			}
			if (computerwins == 2)

			{
				cout << "Bad luck! You lost the game." << endl;
			}

			cout << player1 << "Your wins:" << playerwins << "" << endl;
			cout << player1 << "Your losses:" << playerloses << "" << endl;
			cout << computer << "Your wins:" << computerwins << "" << endl;
			cout << computer << "Your losses:" << computerloses << "" << endl;

			cout << "Do you want to play again? (y/n)" << endl;
			char omstartinput;
			cin >> omstartinput;

			if (omstartinput == 'y' || omstartinput == 'Y')
			{

			}
			else
			{
				return 0;
			}
		}
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
1
2
3
4
5
6
7
8
		else
			{

				cout << player1 << "It's a draw!" << endl;

				cin.ignore();
				cin.get();
			}


This needs to change. It cannot be a draw. You need to compare cards[highestCardPlayer].suite to cards[highestCardComputer].suite to decide which is higher.

For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
if (cards[highestCardPlayer].suite == "spades")
{
  // player has won - do  something
}
else if ( cards[highestCardPlayer].suite == "clubs")
{
  // play has lost - do something
}
else if (cards[highestCardPlayer].suite == "hearts" &&  cards[highestCardComputer].suite != "spades")
{
    // player has won - do  something
}
else ... and more to cover remains cases


This is very clumsy, though. It would be much better if you could simply compare the suites directly. Right now, they are strings, so that is difficult. Maybe you could change the type of suite to something that can be easily compared.
Topic archived. No new replies allowed.