mardi 7 avril 2015

Converting Integers to Strings and If.. Else Boolean Functions C++

I am working on this assignment that requires me to create a game of rock, paper scissors for my programming class. I have ran into a couple issues that I am not fully educated about as I am still learning the basics of this language. My professor wants me to take in the users choice and the computers choice and then change it from an int to a string and print it out as "You chose: Rock" instead of "You chose: 1" which is what it is doing now. This part would be in the getComputerChoice() and getPlayerChoice() functions. Another issue I am having trouble with is my professor wants us to check if it was a tie or if the player won and I am trying to put these functions in an If else statement but I am not exactly sure what the proper way to declare the function in the else statement is. (This is commented out in the else part of the if statement in main all the way at the bottom)


My code is as follows:



#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int getComputerChoice();

int getPlayerChoice();

bool isTie(int, int);

bool isPlayerWinner(int, int);


int getComputerChoice()
{
int comp;
string cpChoice;

comp = rand() % 3 + 1;

if (comp == 1)
{
cpChoice = "Rock";
}

else if (comp == 2)
{
cpChoice = "Paper";
}

else if (comp == 3)
{
cpChoice = "Scissors";
}
return comp;

}

int getPlayerChoice()
{
int userChoice;
string strChoice;

cout << "Rock, Paper, or Scissors?\n";
cout << "1) Rock\n";
cout << "2) Paper\n";
cout << "3) Scissors\n";
cout << "Please enter your choice : \n";
cin >> userChoice;
cout << '\n';

while(userChoice < 1 || userChoice > 3)
{
cout << "Invalid Selection\n";
cout << "Re-enter a number between 1 and 3\n";
cin >> userChoice;
}

if (userChoice == 1)
{
strChoice = "Rock";
}

else if (userChoice == 2)
{
strChoice = "Paper";
}

else if (userChoice == 3)
{
strChoice = "Scissors";
}
return userChoice;
}

bool isTie(string userChoice, string comp)
{
if (userChoice != comp)
return false;
else
return true;
}

bool isPlayerWinner(int userChoice, int comp)
{
if ((comp == 1 && userChoice == 2) || (comp == 3 && userChoice == 1) || (comp == 2 && userChoice == 3))
return true;
else
return false;
}



int main()
{
char selection;

int computerChoice;

int userChoice1;

string Rock;

string Paper;

string Scissors;

srand ((unsigned int)time(NULL));

do
{

cout << '\n';

cout << "ROCK PAPER SCISSORS MENU\n";

cout << "-------------------------\n";

cout << "p) Play Game\n";

cout << "q) Quit\n";

cout << "Please enter your choice : \n";

cin >> selection;

cout << '\n';

cout << '\n';

// cin >> selection;

if (selection == 'p' || selection == 'P')
{
computerChoice = getComputerChoice();
//string computerChoice = to_string(comp);
userChoice1 = getPlayerChoice();
//string userChoice1 = to_string(userChoice);

cout << "You chose: " << userChoice1 << '\n';
cout << "The computer chose: " << computerChoice << '\n';

if (isTie(computerChoice, userChoice1)== true)
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "It's a TIE!";
}
else //(isPlayerWinner(computerChoice, userChoice1));
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "You WIN!";
}
}

//else if (selection != 'p' || selection != 'q')
//{
// cout << "Invalid Selection. Try Again.\n";
// cout << '\n';
// cin >> selection;
//}


else if (selection == 'q' || selection == 'Q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
}

else if (selection != 'p' || selection != 'q')
{
cout << "Invalid Selection. Try Again.\n";
cout << '\n';
}

}while (selection != 'q');

}


ANOTHER NOTE: my professor doesn't want any void functions and doesn't want any global variables. She told me that my isTie function was fine but didn't mention anything about the isPlayerWinner function. I believe it is fine and has no issues, I am just not sure how to declare it in the main if else statement. Any help would be appreciated and if you guys have any questions or need more info please let me know. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire