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
|
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int suitCount(int C, int D, int H, int S){
int points = 0;
if (C > 4){
points += C - 4;
}
else if (C == 0){
points += 3;
}
else if (C == 1){
points += 2;
}
else if (C == 2){
points += 1;
}
else if (D > 4){
points += D - 4;
}
else if (D == 0){
points += 3;
}
else if (D == 1){
points += 2;
}
else if (D == 2){
points += 1;
}
else if (H > 4){
points += H - 4;
}
else if (H == 0){
points += 3;
}
else if (H == 1){
points += 2;
}
else if (H == 2){
points += 1;
}
else if (S > 4){
points += S - 4;
}
else if (S == 0){
points += 3;
}
else if (S == 1){
points += 2;
}
else if (S == 2){
points += 1;
}
return points;
}
int main(){
string input;
istringstream sso;
vector <string> hand;
int match = 0;
bool badFormat = false;
bool duplicate = false;
int J;
int Q;
int K;
int C;
int D;
int A;
int H;
int S;
int points = 0;
string handConcatenate = "";
string cardsArray[52] = {"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"};
vector<string> cards(&cardsArray[0], &cardsArray[0]+52);
/* initializing vector--requires g++ "-std=c++11" flag)
vector <string> cards {"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"};
*/
cout << "Enter your string: " << endl;
while (cin >> input){
if (input != "."){
sso.str(input);
while(getline(sso, input, ',')) {
hand.push_back(input);
}
sso.clear();
}
else {
// BAD FORMAT
for (int i=0;i<hand.size();i++){
if (find(cards.begin(), cards.end(), hand.at(i)) != cards.end()){
match++;
}
}
if (match != hand.size()){
cout << "BAD FORMAT" << endl;
badFormat = true;
}
// WRONG NUMBER OF CARDS
else if (hand.size() != 13){
cout << "WRONG NUMBER OF CARDS" << endl;
}
// DUPLICATE CARD
else{
for(int i = 0; i < hand.size(); i++){
for(int j = i+1; j < hand.size(); j++){
if(hand.at(i) == hand.at(j)){
duplicate = true;
}
}
}
if (duplicate){
cout << "DUPLICATE CARDS" << endl;
}
}
if ((!badFormat) && (hand.size() == 13) && (!duplicate)){
for(int i=0;i<hand.size();i++){
handConcatenate += hand.at(i);
}
//cout << handConcatenate << endl;
for(int i=0;i<handConcatenate.length();i++){
if (handConcatenate[i]== 'J'){
points += 1;
J++;
}
if (handConcatenate[i] == 'Q'){
points += 2;
Q++;
}
if (handConcatenate[i] == 'K'){
points += 3;
K++;
}
if (handConcatenate[i] == 'A'){
points += 4;
A++;
}
if (handConcatenate[i] == 'C'){
C++;
}
if (handConcatenate[i] == 'D'){
D++;
}
if (handConcatenate[i] == 'H'){
H++;
}
if (handConcatenate[i] == 'S'){
S++;
}
if (A == 0){
points -= 1;
}
else if (A == 4){
points += 1;
}
else if ((J + Q + K) == 1){
points -= 1;
}
}
cout << points << endl;
cout << points + suitCount(C, D, H, S) << endl;
}
// Empty hand vector to build a new one
hand.clear();
handConcatenate = "";
// Reset match count
match = 0;
points = 0;
}
}
return 0;
}
|