How can I optimize this code? I want to use more C++11 features.
game_control.h
#ifndef GAME_CONTROL_H
#define GAME_CONTROL_H
#include <iostream>
#include <array>
#include <string>
class Game
{
unsigned int number_of_turns = 0;
unsigned int user_score = 0, comp_score = 0;
std::array<std::string, 3> data = {{"ROCK", "PAPER", "SCISSOR"}};
std::string user_name;
public:
Game() {};
~Game() = default;
void start_menu();
void start_game();
void game_play(unsigned int, unsigned int);
void exit_game();
};
#endif
game_control.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "game_control.h"
void Game::start_menu()
{
std::cout << "#### #### #### # # #### #### #### #### ####\n";
std::cout << "# # # # # # # # # # # # # # # #\n";
std::cout << "#### # # # ## #### #### #### #### ####\n";
std::cout << "# # # # # # # # # # # # # # \n";
std::cout << "# # #### #### # # # # # # #### # #\n";
std::cout << "\n\n";
std::cout << " #### #### # #### #### #### ####\n";
std::cout << " # # # # # # # # #\n";
std::cout << " #### # # #### #### # # ####\n";
std::cout << " # # # # # # # # # \n";
std::cout << " #### #### # #### #### #### # #\n";
std::cout << "\n\n";
unsigned int selection;
x:
std::cout << "\tPress\n";
std::cout << "\t1 to Enter\n";
std::cout << "\t2 to Exit\n";
std::cin >> selection;
std::cin.clear();
switch(selection)
{
case 1: start_game();
break;
case 2: exit_game();
break;
default: std::cout << "-------Enter valid choice--------\n";
goto x;
break;
}
}
void Game::exit_game()
{
std::cout << "Thanks for playing!!\n";
exit(EXIT_SUCCESS);
}
void Game::start_game()
{
std::cout << "\nEnter your name\n";
//std::getline(std::cin, user_name);
std::cin >> user_name;
std::cin.clear();
std::cout << "Enter number of turns for you want to play\n";
std::cin >> number_of_turns;
std::cin.clear();
while (number_of_turns--)
{
unsigned int user_choice, comp_choice;
std::cout << "\nEnter your choice\n";
y:
std::cout << "\nPress\n";
std::cout << "1 for ROCK\t2 for PAPER\t3 for SCISSOR\n";
std::cin >> user_choice;
if (user_choice > 3)
{
std::cout << "\n---------Please enter valid choice------------\n";
goto y;
}
user_choice--;
std::cin.clear();
srand(time(0));
comp_choice = rand() % 3 + 1;
game_play(user_choice, comp_choice);
std::cout << "The score is\t";
std::cout << user_name << " = " << user_score;
std::cout << "\tComputer = " << comp_score << "\n";
}
exit_game();
}
void Game::game_play(unsigned int user_choice, unsigned int comp_choice)
{
//User = Rock
if (user_choice == 0)
{
if (comp_choice == 1) // Computer = Paper
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "----Computer Wins!!----\n";
comp_score++;
}
else if (comp_choice == 2) // Computer = Scissor
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "----" << user_name << " Wins!!----\n";
user_score++;
}
else
{
std::cout << "\n";
std::cout << "Both " << user_name << " and Computer chooses ROCK\n";
}
}
//User = Paper
if (user_choice == 1)
{
if (comp_choice == 0) // Computer = Rock
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "----" << user_name << " Wins!!----\n";
user_score++;
}
else if (comp_choice == 2) // Computer = Scissor
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "-----Computer Wins!!-----\n";
comp_score++;
}
else
{
std::cout << "\n";
std::cout << "Both " << user_name << " and Computer chooses PAPER\n";
}
}
//User = Scissor
if (user_choice == 2)
{
if (comp_choice == 0) // Computer = Rock
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "----Computer Wins!!-----\n";
comp_score++;
}
else if (comp_choice == 1) // Computer = Paper
{
std::cout << "\n";
std::cout << user_name << " chooses " << data[user_choice];
std::cout << " and Computer chooses " << data[comp_choice] << "\n";
std::cout << "----" << user_name << " Wins!!----\n";
user_score++;
}
else
{
std::cout << "\n";
std::cout << "Both " << user_name << " and Computer chooses SCISSOR\n";
}
}
}
main.cpp
#include <iostream>
#include "game_control.h"
int main()
{
Game game;
game.start_menu();
game.start_game();
return 0;
}