/**
* PROGRAM: rps.cpp
* AUTHOR: <#Type your own name here#>
* UNIQNAME: <#Type your uniqname here#>
*
* EECS 183: Project 2, Fall 2014
*
* <#A description of the project here#>
*/
#include <iostream>
#include <string>
using namespace std;
/*************************************************************************
* The following functions have already been implemented for you.
* You should use them when writing the other functions.
*************************************************************************/
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects : Prints a pretty header to introduce the user to the game.
*/
void printInitialHeader();
/**
* Requires: error_number be either 1 or 2.
* Modifies: Nothing.
* Effects : if error_number is 1, prints an error message indicating
* an illegal name was entered.
* if error_number is 2, prints an error message indicating
* an illegal move was entered.
*/
void printErrorMessage(int error_number);
/*************************************************************************
* You must implement all of the following functions.
*************************************************************************/
/**
* Requires: player_number is either 1 or 2.
* Modifies: Nothing.
* Effects : Prompts the user to enter their name. Names may have spaces
* within them. Example: Kermit the Frog, Fozzie
* If an empty name is given, print error message 1, and
* return a default name.
* For player 1, the default name is: Rocky
* For player 2, the default name is: Hulk
* Prompt : Player [player_number], enter your name:
* Used In : main()
*/
string getName(int player_number);
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects : Returns true if and only if move is a valid move character.
* Used In : getMove()
*/
bool isMoveGood(char move);
/**
* Requires: player_name is the name of the player being prompted for a move.
* Modifies: Nothing.
* Effects : Prompts the player for their move and returns it.
* This function should accept the first non-whitespace character as
* the move
* If an illegal character is entered for their move, print error
* message 2 and return rock as a default.
* Prompt : [player_name], enter your move:
* Used In : doRound()
*/
char getMove(string player_name);
/**
* Requires: move is the move of the player being checked for a win.
* opponent_move is the move of the opponent.
* Modifies: Nothing.
* Effects : Returns true if and only if the player who made move won.
* Used In : doRound()
*/
bool isRoundWinner(char move, char opponent_move);
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects : If winner_name is empty, prints a message indicating the round is
* a draw. Otherwise, prints a congratulatory message to the winner.
* Prompt : This round is a draw!
* [winner_name] wins the round!
* Used In : doRound()
*/
void announceRoundWinner(string winner_name);
/**
* Requires: p1_name and p2_name are the names of the respective players.
* Modifies: Nothing.
* Effects : Simulates a complete round, returning 0 if the round was a draw;
* 1 if player 1 won; 2 if player 2 won.
* Used In : main()
*/
int doRound(string p1_name, string p2_name);
/**
* Requires: player_num is 1 or 2,
* r1_result, r2_result, r3_result is 0,1, or 2
* r1_result is result of round 1 play as determined by doRound(...)
* similar for r2_result and r3_result.
* Modifies: Nothing.
* Effects : Returns the number of times that player_num won.
* Used In : main()
*/
int getWinCount(int player_num, int r1_result, int r2_result, int r3_result);
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects : If winner_name is blank, prints that the game was a draw.
* Otherwise, prints a congratulatory message to the winner.
* Prompt : No winner!
* Congratulations [winner_name]!
* You won EECS 183 Rock-Paper-Scissors!
* Used In : main()
*/
void announceWinner(string winner_name);
int main() {
int player_number1 = 1;
int player_number2 = 2;
int round1;
int round2;
int round3 = 0;
int player1_count;
int player2_count;
string player_name1;
string player_name2;
string no_winner = "";
// Print the header
printInitialHeader();
// Get the player names
player_name1 = getName(player_number1);
player_name2 = getName(player_number2);
cout << endl;
// Simulate the first 2 rounds
round1 = doRound(player_name1, player_name2);
cout << endl << endl;
round2 = doRound(player_name1, player_name2);
// Check if either player already clinched a win:
player1_count = getWinCount(player_number1, round1, round2, round3);
player2_count = getWinCount(player_number2, round1, round2, round3);
// If they have, announce the winner.
if ( (player1_count == 2) ) {
cout << endl << endl;
announceWinner(player_name1);
}
else if (player2_count == 2) {
cout << endl << endl;
announceWinner(player_name2);
}
else { // Otherwise, simulate the third round, find who has more wins,
cout << endl << endl;
round3 = doRound(player_name1, player_name2);
if ( (round3 == 1) ) { // and announce the winner.
cout << endl << endl;
announceWinner(player_name1);
}
else if ( (round3 == 2) ) {
cout << endl << endl;
announceWinner(player_name2);
}
else if ( (round3 == 0) ) {
cout << endl << endl;
announceWinner(no_winner);
}
}
}
/*************************************************************************
* Put all function implementations below here.
*************************************************************************/
void printInitialHeader() {
cout << "----------------------------------------" << endl;
cout << " EECS 183 " << endl;
cout << " Rock-Paper-Scissors " << endl;
cout << "----------------------------------------" << endl << endl;
}
void printErrorMessage(int error_number) {
if (error_number == 1) {
cout << endl << "ERROR: Illegal name given, using default" << endl;
} else if (error_number == 2) {
cout << endl << "ERROR: Illegal move given, using default" << endl;
} else {
cout << "This should never print!";
}
}
string getName(int player_number) {
string player_name = "";
int error_number1 = 1;
if (player_number == 1) {
cout << "Player " << player_number << ", enter your name: ";
getline(cin, player_name);
if (player_name == "") {
printErrorMessage(error_number1);
return player_name = "Rocky";
}
else {
return player_name;
}
}
else if (player_number == 2) {
cout << "Player " << player_number << ", enter your name: ";
getline(cin, player_name);
if (player_name == "") {
cout << endl;
printErrorMessage(error_number1);
return player_name = "Hulk";
}
else {
return player_name;
}
}
return 0;
}
bool isMoveGood(char move) {
if ( (move == 'r') || (move == 'p') || (move == 's') ) {
return true;
}
else {
return false;
}
}
char getMove(string player_name) {
int error_number2 = 2;
char move;
cout << endl << player_name << ", enter your move: ";
cin >> move;
if (isMoveGood(move) == false) {
cout << endl;
printErrorMessage(error_number2);
return move = 'r';
}
else {
return move;
}
}
bool isRoundWinner(char move, char opponent_move) {
if ( move == 'r' && opponent_move == 's') {
return true;
}
else if (move == 'p' && opponent_move == 'r') {
return true;
}
else if (move == 's' && opponent_move == 'p') {
return true;
}
else {
return false;
}
}
void announceRoundWinner(string winner_name) {
if (winner_name == "") {
cout << "This round is a draw!";
}
else {
cout << endl << winner_name << " wins the round!";
}
}
int doRound(string p1_name, string p2_name) {
char p1_move = getMove(p1_name);
char p2_move = getMove(p2_name);
string no_winner = "";
if ( isRoundWinner(p1_move, p2_move) == false || isRoundWinner(p2_move, p1_move) ) {
announceRoundWinner(no_winner);
return 0;
}
else if ( isRoundWinner(p1_move, p2_move) == true ) {
announceRoundWinner(p1_name);
return 1;
}
else if ( isRoundWinner(p2_move, p1_move) == true) {
announceRoundWinner(p2_name);
return 2;
}
}
int getWinCount(int player_num, int r1_result, int r2_result, int r3_result) {
if (player_num == 1) {
if (r1_result == 1 && r2_result == 1) {
return 2;
}
}
else if (player_num == 2) {
if (r1_result == 2 && r2_result == 2) {
return 2;
}
}
return 0;
}
void announceWinner(string winner_name) {
if (winner_name == "") {
cout << "No winner!";
}
else {
cout << "Congratulations " << winner_name << "!" << endl;
cout << "You won EECS 183 Rock-Paper-Scissors!";
}
}