I just started to learn C++ and I decided to make a rock paper scissor game. It works very well but I would like to know how could I improve it further. Thank you very much in advance for your help.
#include <iostream>
#include <string>
#include <time.h>
int main()
{
using namespace std;
string playagain = "yes";
string computerchoice;
string playerChoice;
// computer
int computerNumber;
//rounds + statistics
string name;
int playedRounds =0;
int wonRounds = 0;
int lostRounds = 0;
int tieRounds = 0;
// game text
cout << "_______________________________________________" << endl;
cout << "welcome it's a rock paper scissor game" << endl;
cout << "Whats your name?" << endl;
cin >> name;
cout << "well hey " +name +" please Choose one "<< endl;
cout << "rock - paper - scissor" << endl;
cout << "_______________________________________________" << endl;
while (playagain == "yes") {
// generating a random number
srand(static_cast<unsigned int>(time(NULL)));
computerNumber = rand() % 3 + 1;
// ________________________________ computer picking a element
if (computerNumber == 1) {
computerchoice = "rock";
}
else if (computerNumber == 2) {
computerchoice = "paper";
}
else {
computerchoice = "scissor";
}
//_________________________________
// player choice + go to line
playerChoice: {
cin >> playerChoice;
}
if (playerChoice != "rock" && "paper" && "scissor") {
cout << "" << endl;
cout << "Please choose from one of them"<<endl;
cout << "ROCK - PAPER - SCISSOR" << endl;
cout << "" << endl;
goto playerChoice;
}
// player vs computer
cout << playerChoice + " VS " + computerchoice << endl;
// game rules
if (playerChoice == "rock" && computerchoice == "paper") {
cout << "you lost"; playedRounds++; lostRounds++;
}
else if (playerChoice == "rock" && computerchoice == "scissor") {
cout << "you won"; playedRounds++; wonRounds++;
}
else if (playerChoice == "scissor" && computerchoice == "paper") {
cout << "you won"; playedRounds++; wonRounds++;
}
else if (playerChoice == "scissor" && computerchoice == "rock") {
cout << "you lost"; playedRounds++; lostRounds++;
}
else if (playerChoice == "paper" && computerchoice == "scissor") {
cout << "you lost"; playedRounds++; lostRounds++;
}
else if (playerChoice == "paper" && computerchoice == "rock") {
cout << "you won"; playedRounds++; wonRounds++;
}
else {
cout << "tie"; playedRounds++; tieRounds++;
}
//__________________________________________________________________
cout << "" << endl;
cout << "" << endl;
cout << "_______________________________________________" << endl;
cout << "would u like to play again? or if u wanna check out your statistics u can just type = sta" << endl;
cout << "yes - no - sta" << endl;
cout << "_______________________________________________" << endl;
cout << "" << endl;
cout << "" << endl;
playagain:{ // GO TO LINE
cin >> playagain;
}
if (playagain == "yes") {
cout << "_______________________________________________" << endl;
cout << "Choose wisely, rock - paper - scissor" << endl;
cout << "rock - paper - scissor" << endl;
cout << "_______________________________________________" << endl;
}
else if (playagain == "no") {
cout << "have a nice day! now the program is going to shutdown";
}
else if (playagain == "sta") {
cout << "" << endl;
cout << "" << endl;
cout << "Player name:" +name <<endl;
cout << "Played Rounds: "; cout << +playedRounds << endl;
cout << "Won Rounds: "; cout << +wonRounds << endl;
cout << "lost Round: "; cout << +lostRounds << endl;
cout << "Tie Rounds: "; cout << +tieRounds << endl;
cout << "would u like to continue ? " << endl;
cout << "yes for continue no for exit " << endl;
goto playagain;
}
else {
cout << "i couldn't understand could u repeat please?" << endl;
goto playagain;
}
}
}
using namespace std;
shouldn't be used, and how a monolithic sequence of executable instructions can be turned into small, specialized procedures that do nothing more than what their name says - I hope you learn as much on this site as I did! In the meantime feel free to browse other rock-paper-scissors posts for more ideas and tips, as each post contains a working implementation and very valuable inputs from reviewers. \$\endgroup\$ – Mathieu Guindon♦ Sep 17 '19 at 22:56