I have an assignment to create a card game that compares to dealt cards between players to see who has the highest valued card. It must first be determined by the cards value, then only if it is tied, the suits value. The code below is what I have so far. It is working, but it isnt how I need to compare the two cards together.
string card(unsigned int card) //Assigns suits to values
{
if (card >= 52)
{
return "Invalid";
}
return values[card % 13] + " of " + suits[card / 13]; // where card ranges from 0 to 51.
This is the sections that seems to be the issue. Instead of both the value and the suit added together, I need just the value done, then only if it ties, to compare the higher suit to see who wins. Any help would be greatly appreciated.
#include <iostream> //Needed to run COUT
#include <ctime> //Needed to randomise
#include <string> //Needed to create strings
#include <cmath>
#include <sstream>
#include <cstdlib>
#include <stdlib.h> //Needed to run CLS
using namespace std;
const string values[13] = //Creates an array of cards
{
"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"
};
const string suits[4] = //Creates an array of suits
{
"Clubs", "Diamonds", "Hearts", "Spades"
};
string card(unsigned int card) //Assigns suits to values
{
if (card >= 52)
{
return "Invalid";
}
return values[card % 13] + " of " + suits[card / 13]; // where card ranges from 0 to 51.
}
int main()
{
string player1; //Creates string
string player2; //Creates string
cout << "Hello there! " << "\n" << endl; //Writing text
cout << "This is a cardgame where you win by chance, you pull a card out of the deck and if you get the highest card you win the round." << "\n" << endl; //Writing text
cout << "If you are lucky enought to be the first to win 3 rounds, you win the game!!! Sounds like fun!" << "\n" << endl; //Writing text
cout << "Let's start with getting to know eachother a bit better, what's the name of player 1? Type it down below and press enter!" << "\n" << endl; //Writing text
cin >> player1; //Player 1 input
cout << " " << endl; //Writing space
cout << "That's a great name! What's the name of player 2? Type it down below and press enter!" << "\n" << endl; //Writing text
cin >> player2; //Player 2 input
cout << " " << endl; //Writing space
cout << "Well, isn't that awesome." << "\n" << endl; //Writing text
cout << "Welcome to the most epic cardgame you have ever played, " << player1 << "and" << player1 << "!" << endl << endl; //Writing text
cout << "So the card you will get is totally random. The person with the highest card wins and it's first to 3 won rounds to win the game, got it?" << "\n" << endl << endl; //Writing text
cout << "Great! Let's get going. :)" << "\n" << endl; //Writing text
cout << "Press any key to draw a card, " << player1 << "!" << "\n" << endl; //Writing text
system("pause > nul"); //Press any button to continue... but the nul removes the text
bool loop = true; //Creates loop
int winsPlayer1 = 0; //Creates a counter
int winsPlayer2 = 0; //Creates a counter
int lossesPlayer1 = 0; //Creates a counter
int lossesPlayer2 = 0; //Creates a counter
bool anotherround = true; //Creates a boolean for playing another round
int roundswonPlayer1 = 0; //Creates a counter
int roundswonPlayer2 = 0; //Creates a counter
int gameswonPlayer1 = 0; //Creates a counter
int gameswonPlayer2 = 0; //Creates a counter
while (loop == true) //While the loop is active
{
bool loop3 = true; //Creates loop
while (loop3) //While the loop is active
{
srand(time(NULL));
int randomise = rand() % 52;
int randomise2 = rand() % 52;
int HighestCardPlayer1;
int HighestCardPlayer2;
HighestCardPlayer1 = randomise;
HighestCardPlayer2 = randomise2;
cout << player1 << " draws the card: " << card(randomise) << endl;
cout << " " << endl; //Writing space
cout << "So, your card is " << card(HighestCardPlayer1) << ", let's see what " << player2 << " gets! Press any key to draw a card, " << player2 << "!" << endl; //Presents drawn card
cout << " " << endl; //Writing space
system("pause > nul"); //Press any button to continue... but the nul removes the text
cout << player2 << " draws the card: " << card(randomise2) << endl;
cout << " " << endl; //Writing space
cout << "Just to be clear, " << player2 << "'s card is " << card(HighestCardPlayer2) << ", let's see who won the round! Press any key to continue." << endl; //Presents drawn card
cout << " " << endl; //Writing space
system("pause > nul"); //Press any button to continue... but the nul removes the text
if (card(HighestCardPlayer1) > card(HighestCardPlayer2)) //If player 1 has the highest card
{
cout << player1 << ", you won this round! Wohoo!!" << endl; //Writing text
cout << " " << endl; //Writing space
winsPlayer1++; //Adds value to selected int
lossesPlayer2++; //Adds value to selected int
roundswonPlayer1++; //Adds value to selected int
cout << player1 << ", your total rounds won are: " << winsPlayer1 << ", your total round lost are: " << lossesPlayer1 << "" << endl; //Writes results
cout << player2 << ", your total rounds won are: " << winsPlayer2 << ", your total round lost are: " << lossesPlayer2 << "" << endl; //Writes results
cout << "Press any key to draw another card!" << "\n" << endl; //Writing text
system("pause > nul"); //Press any button to continue... but the nul removes the text
}
else if (card(HighestCardPlayer1) < card(HighestCardPlayer2)) //If player 2 has the highest card
{
cout << player2 << ", you won this round! Wohoo!!" << endl; //Writing text
cout << " " << endl; //Writing space
winsPlayer2++; //Adds value to selected int
lossesPlayer1++; //Adds value to selected int
roundswonPlayer2++; //Adds value to selected int
cout << player1 << ", your total rounds won are: " << winsPlayer1 << ", your total round lost are: " << lossesPlayer1 << "" << endl; //Writes results
cout << player2 << ", your total rounds won are: " << winsPlayer2 << ", your total round lost are: " << lossesPlayer2 << "" << endl; //Writes results
cout << "Press any key to draw another card!" << "\n" << endl; //Writing text
system("pause > nul"); //Press any button to continue... but the nul removes the text
}
else if (card(HighestCardPlayer1) == card(HighestCardPlayer2)) //If player 1 and 2 has the same value cards
{
{
}
system("pause > nul"); //Press any button to continue... but the nul removes the text
}
if (roundswonPlayer1 == 3) //If player 1 has won 3 rounds
{
gameswonPlayer1++;
cout << player1 << ", congratulations!! You've won the game!! "; //Writing text
cout << player1 << ", your total games won are: " << gameswonPlayer1 << ", your total games lost are: " << gameswonPlayer2 << "" << "\n" << endl; //Writes results
cout << player2 << ", your total games won are: " << gameswonPlayer2 << ", your total games lost are: " << gameswonPlayer1 << "" << "\n" << endl; //Writes results
cout << "Press any key to continue!" << "\n" << endl; //Writing text
cout << " " << endl; //Writing space
roundswonPlayer1 = 0; //Brings int value back to 0
roundswonPlayer2 = 0; //Brings int value back to 0
}
if (roundswonPlayer2 == 3) //If player 2 has won 3 rounds
{
gameswonPlayer2++;
cout << player1 << ", sadly you lost the game this time, better luck next time!" << "\n" << endl; //Writing text
cout << player1 << ", your total games won are: " << gameswonPlayer1 << ", your total games lost are: " << gameswonPlayer2 << "" << "\n" << endl; //Writes results
cout << player2 << ", your total games won are: " << gameswonPlayer2 << ", your total games lost are: " << gameswonPlayer1 << "" << "\n" << endl; //Writes results
cout << "Press any key to continue!" << "\n" << endl; //Writing text
cout << " " << endl; //Writing space
roundswonPlayer1 = 0; //Brings int value back to 0
roundswonPlayer2 = 0; //Brings int value back to 0
}
}
bool loop2 = true;
while (loop2)
{
char anotherround; //Creates a character
cout << "Do you want to play another game? If you answer no, the game will end. Enter y for yes and n for no." << "\n" << endl; //Asks the player if it wants to play again or end game
cin >> anotherround; //Player input
if (anotherround == 'y') //If the player types y
{
loop2 = false; //Cancels loop
continue; //Continues
}
else if (anotherround == 'n') //If the player types n
{
cout << "Thank you for playing the most epic cardgame there is!" << endl; //Writing text
return 0; //Resets game
}
}
}
}