I have created a rock paper scissors game in C++.
#include <iostream>
#include <map>
#include <vector>
#include <time.h>
std::vector<std::string> gameOptions {"Rock", "Paper", "Scissors"};
// Map of Key as winner and Value as loser
std::map<std::string, std::string> gameRules {
{"Rock", "Scissors"},
{"Scissors", "Paper"},
{"Paper", "Rock"}
};
std::string checkWinner(const std::string& playerSelection, const std::string& pcSelection)
{
if (playerSelection == pcSelection) return "Draw!";
if (gameRules[playerSelection] == pcSelection) return "Player Wins!";
return "PC Wins!";
}
bool isUserInputValid(const std::string& playerSelection)
{
for (std::string option : gameOptions)
{
if (playerSelection == option) return true;
}
return false;
}
void promptUserInput()
{
std::cout << "Game Started! Enter your choice from below:" << std::endl;
std::cout << "Rock" << std::endl;
std::cout << "Paper" << std::endl;
std::cout << "Scissors" << std::endl;
}
int main()
{
promptUserInput();
std::string playerSelection;
std::cin >> playerSelection;
while (!isUserInputValid(playerSelection))
{
promptUserInput();
std::cin >> playerSelection;
}
std::string pcSelection;
srand(time(NULL));
int random = rand();
pcSelection = gameOptions[random % 3];
std::cout<< "The result is: " << checkWinner(playerSelection, pcSelection) << std::endl;
std::cout<< "Player Selection:\t" << playerSelection << std::endl;
std::cout<< "PC Selection:\t" << pcSelection << std::endl;
}
Are there any parts that can be improved on?