This is my code for a simple rock-paper-scissors game. Users have the choice between playing against a computer or watching a "computer player" play another "computer player".
The code for the computer vs computer mode isn't finished yet, so you must chose human vs computer mode for the program to work as expected.
If you guys have any suggestions, corrections, recommendations, etc., please share them.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
class Player{
public:
void play(); //for the computer players only
std::string hand;
int hands_won;
std::string name;
};
void Player::play(){
std::string choices[3]{"rock", "paper", "scissors"};
hand = choices[std::rand()%3]; //select rock, paper, or scissors at random
}
std::vector<Player> players(2);
void print_winner(int mode);
bool GAME_OVER();
void game_start(){
std::cout << "-------------------------------------------\n";
std::cout << "Select a mode: \n";
std::cout << "1. human vs computer\n2. computer vs computer\n";
std::cout << "-------------------------------------------\n";
int mode;
std::cin >> mode;
if (mode == 1){
std::cout << "Enter your name: ";
std::cin >> players[0].name;
players[1].name = "Computer";
}
if (mode == 3){
players[0].name = "Computer 1";
players[1].name = "Computer 2";
}
while (mode == 1){ //game loop for mode 1
while (!GAME_OVER()){
std::cout << "\nWhat will you chose? rock, paper, or scissors? ";
std::cin >> players[0].hand;//player makes choice
players[1].play();//computer makes its choice
std::cout << std::endl;
std::cout << "You chose: " << players[0].hand << " Computer chose: " << players[1].hand << std::endl; //display each player's choice for the round
print_winner(1); //display the winner of the round
while (players[0].hand == players[1].hand){ //check to see if the round was a draw
std::cout << "\nWhat will you chose? rock, paper, or scissors? ";
std::cin >> players[0].hand;
players[1].play();
std::cout << endl;
std::cout << "You chose: " << players[0].hand << " Computer chose: " << players[1].hand << std::endl;
print_winner(1);
}
}
if (GAME_OVER()){ //display the winner of the game
if (players[0].hands_won == 2){
std::cout << "Congratulations, " << players[0].name << ". You win the game!\n";
return 1;
}else{
std::cout << "Game over. You lose.\n";
return 0;
}
}
}
}
void print_winner(int mode){ //determine which player won the round
if (mode == 1){
if (players[0].hand == "rock" && players[1].hand == "scissors"){
std::cout << " You win!";
players[0].hands_won += 1;
}else if (players[0].hand == "scissors" && players[1].hand == "paper"){
std::cout << " You win!";
players[0].hands_won += 1;
}else if (players[0].hand == "paper" && players[1].hand == "rock"){
std::cout << " You win!";
players[0].hands_won += 1;
}else if (players[0].hand == players[1].hand){
std::cout << " It's a draw!";
}else{
std::cout << " Computer wins!";
players[1].hands_won += 1;
}
}else{
if (players[0].hand == "rock" && players[1].hand == "scissors"){
std::cout << " Computer 1 wins!";
players[0].hands_won += 1;
}else if (players[0].hand == "scissors" && players[1].hand == "paper"){
std::cout << " Computer 1 wins!";
players[0].hands_won += 1;
}else if (players[0].hand == "paper" && players[1].hand == "rock"){
std::cout << " Computer 1 wins!";
players[0].hands_won += 1;
}else if (players[0].hand == players[1].hand){
std::cout << " It's a draw!";
}else{
std::cout << " Computer 2 wins!";
players[1].hands_won += 1;
}
}
}
int main()
{
std::srand(time(NULL));
std::cout << "---------------------------------------------------\n";
std::cout << "Welcome to Rock, Paper, Scissors.\nTo play, type '1'. \nTo exit, type '2'.\n";
std::cout << "---------------------------------------------------\n";
int choice;
std::cin >> choice;
switch (choice){
case 1:
game_start();
break;
case 2:
return 0;
default:
std::cout << "You made an invalid choice. Exiting game...";
return 0;
}
if (GAME_OVER()){ //ask the user if they would like to play again
std::cout << "Would you like to play again? <y/n> ";
char input;
std::cin >> input;
if (input == 'y'){
game_start();
}
return 0;
}
}
bool GAME_OVER(){
if(players[0].hands_won == 2 || players[1].hands_won == 2){ //best 2/3
return true;
}
return false;
}