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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
#include "pch.h"
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
enum Suit { Spade, Heart, Diamond, Club };
class Card {
private:
int number;
string description;
public:
Suit suit;
void setNumber(int cNumber) {
number = cNumber;
}
int getNumber() {
return number;
}
void setSuit(Suit cSuit) {
suit = cSuit;
}
Suit getSuit() {
return suit;
}
void setDescription() {
string temp = to_string(number);
switch (number) {
case 1:
temp = "Ace";
break;
case 11:
temp = "Jack";
break;
case 12:
temp = "Queen";
break;
case 13:
temp = "King";
break;
default:
temp = to_string(number);
}
switch (suit) {
case Spade:
description = "Spade " + temp;
//cout << description;
break;
case Heart:
description = "Heart " + temp;
//cout << description;
break;
case Diamond:
description = "Diamond " + temp;
//cout << description;
break;
case Club:
description = "Club " + temp;
//cout << description;
break;
default:
break;
}
}
string getDescription() {
return description;
}
bool operator ==(const Card &c) {
if (suit == c.suit && number == c.number)
return true;
return false;
}
bool operator <(const Card &c) {
if (suit < c.suit)
return true;
if (suit == c.suit && c.number == 1)
return true;
else if (suit == c.suit && c.number != 1 && number < c.number)
return true;
return false;
}
};
class Deck {
private:
Card deck[52];
public:
void shuffleDeck(Card deck[]) {
int seed = time(0);
srand(seed);
for (int i = 0; i < 52; i++) {
int r = rand() % 52;
Card temp = deck[i];
deck[i] = deck[r];
deck[r] = temp;
}
}
};
class Player {
private:
Card hand[13];
string name;
public:
void setName(string n) {
name = n;
}
string getName() {
return name;
}
void sort(Card array[], int size) {
Card temp;
bool swap;
do {
swap = false;
for (int count = 0; count < (size - 1); count++) {
if (array[count + 1] < array[count]) {
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
int clubTwo(Card array[], int size, Card value) {
int first = 0;
int last = size - 1;
int middle;
int position = -1;
bool found = false;
while (!found && first <= last) {
middle = (first + last) / 2;
if (array[middle] == value) {
found = true;
position = middle;
}
else if (value < array[middle])
last = middle - 1;
else
first = middle + 1;
}
return position;
}
};
int main() {
int m, i, j, results;
Deck blackJack;
Player p1, p2, p3, p4;
p1.setName("Lady Gaga");
p2.setName("Albert Einstein");
p3.setName("Muhammed Ali");
p4.setName("Diego Maradona");
Card deck[52];
Card hand1[13];
Card hand2[13];
Card hand3[13];
Card hand4[13];
for (int i = 1; i < 14; i++) {
for (int j = 1; j < 5; j++) {
int pos = i * 4 + j - 5;
switch (j) {
case 1:
deck[pos].setSuit(Club);
deck[pos].getSuit();
break;
case 2:
deck[pos].setSuit(Diamond);
deck[pos].getSuit();
break;
case 3:
deck[pos].setSuit(Heart);
deck[pos].getSuit();
break;
case 4:
deck[pos].setSuit(Spade);
deck[pos].getSuit();
break;
default:
break;
}
deck[pos].setNumber(i);
deck[pos].setDescription();
//cout << left << setw(15) << deck[pos].getDescription() << endl;
}
}
Card clubTwo = deck[4];
do {
cout << "Enter 1 to shuffle" << endl << "Enter 2 to deal" << endl << "Enter 3 to end";
cin >> m;
switch (m) {
case 1:
blackJack.shuffleDeck(deck);
//for (i = 0; i < 52; i++) {
//cout << left << setw(15) << deck[i].getDescription() << endl;
//}
break;
case 2:
j = 0;
for (i = 0; i < 13; i++) {
hand1[j] = deck[i];
j++;
}
j = 0;
for (i = 13; i < 26; i++) {
hand2[j] = deck[i];
j++;
}
j = 0;
for (i = 26; i < 39; i++) {
hand3[j] = deck[i];
j++;
}
j = 0;
for (i = 39; i < 52; i++) {
hand4[j] = deck[i];
j++;
}
p1.sort(hand1, 13);
p2.sort(hand2, 13);
p3.sort(hand3, 13);
p4.sort(hand4, 13);
cout << left << setw(15) << p1.getName() << left << setw(20) << p2.getName() << left << setw(25) << p3.getName() << left << setw(30) << p4.getName();
cout << endl;
cout << endl;
for (i = 0; i < 13; i++) {
cout << left << setw(15) << hand1[i].getDescription() << left << setw(20) << hand2[i].getDescription() << left << setw(25) << hand3[i].getDescription() << left << setw(30) << hand4[i].getDescription();
cout << endl;
}
/*results = p1.clubTwo(hand1, 13, clubTwo);
if (results == -1)
cout << "Not in hand 1";
else
cout << "ClubTwo in hand 1";
cout << endl;
results = p2.clubTwo(hand2, 13, clubTwo);
if (results == -1)
cout << "Not in hand 2";
else
cout << "ClubTwo in hand 2";
cout << endl;
results = p3.clubTwo(hand3, 13, clubTwo);
if (results == -1)
cout << "Not in hand 3";
else
cout << "ClubTwo in hand 3";
cout << endl;
results = p4.clubTwo(hand4, 13, clubTwo);
if (results == -1)
cout << "Not in hand 4";
else
cout << "ClubTwo in hand 4";
cout << endl;
break;*/
case 3:
exit(1);
}
} while (m != 3);
return 0;
}
|