Wondering if anyone can help me, I've created a card guessing (higher or lower) but for some reason I can't get the comparisons 100%. For instance it will show a 10 of hearts, I'll say lower, and it was output "Wrong" even though the next card is lower. This doesn't always happen, but I'm really confused as to why it does.
#include <cstdlib>
#include <sys/time.h>
#include <string>
#include <iostream>
#include <ctime>
usingnamespace std;
#include "game.h"
#include "card.h"
Game::Game() { //initializes deck
playerscore=0;
housescore=0;
for (int i=0; i<52; i++) {
cards[i] = Card(i);
}
}
void Game::shuffle(){ //shuffles deck
srand ( time(NULL) );
for (int i=0; i<(51); i++) {
int r = rand() % 52;
Card temp = cards[i];
cards[i] = cards[r];
cards[r] = temp;
}
}
void Game::announceWinner(){} //Announces winner (not shown)
void Game::demo(){} //game demo (not shown)
void Game::menu(){} //game menu (not shown)
void Game::play(){ //plays game
string input;
char choice;
shuffle();
cout <<endl;
cout <<endl;
cout <<"The Computer will deal you a card one at a time"<<endl;
cout <<"Press 'H' if you think the next card will be higher"<<endl;
cout <<"Press 'L' if you think the next card will be lower"<<endl;
cout <<"Press 'Q' to return to Quit"<<endl;
for (int i=0; i<51; i++){
Card current = cards[i]; //initializes current card index to variable
Card next = cards[i+1]; //initializes next card index to variable
cout << "The computer draws "<< current.getRank() << " of " << current.getSuit()<<endl;
cout << next.getRank()<<" of " <<next.getSuit()<<endl;
cin >> input;
choice = input.at(0);
choice=tolower(choice);
if ((choice=='l'&& next.getRank()<current.getRank())){
cout <<"Correct!"<<endl;
playerscore=playerscore+2;
}
elseif ((choice=='l' && next.getRank()>current.getRank())){
cout <<"Wrong!"<<endl;
housescore=housescore+2;
}
elseif ((choice=='h' && next.getRank()>current.getRank())){
cout <<"Correct!"<<endl;
playerscore=playerscore+2;
}
elseif ((choice=='h' && (next.getRank()<current.getRank()))){
cout <<"Wrong!"<<endl;
housescore=housescore+2;
}
elseif ((((choice=='h') || (choice=='l')) && (current.getRank()==next.getRank()))){
cout << "The cards were equal, its a draw!"<<endl;
playerscore++;
playerscore++;
}
elseif (choice=='q'){
announceWinner(); //announces game winner if user quits early
}
else {
cout <<"Invalid input, please enter 'H' for Higher 'L' forlower or 'Q' to quit"<<endl;
i--;
}
} //end for
announceWinner(); //announces game winner if entire game is played
} //end function
You are comparing the ranks of the cards as strings against one another.
So when you are comparing say an Ace against a 3 the program looks at this as:
"Ace" < "3". When it compares 2 strings, it just goes by alphabetical order. Since numbers come before letters, the above will return False because "A" comes after "3".
This is the main reason 10 and the face cards have the problems. Say you are comparing "10" against "3" now. 3 should obviously be the lower card, but again they are stored as strings, so it goes by alphabetical order. "10" starts with "1", which is actually lower than "3", so "10" < "3" would actually come out true when this is obviously false.
You are going to have to convert each number to an integer rather than a string ("Jack" = 11, "Queen" = 12, and "King" = 13, and "Ace" = either 1 or 14 depending on how you are counting them as high or low) and then compare them.