Could you give me your opinion about this simple rock, paper, and scissors game code and tell me tips to improve this code?
#include<iostream>
#include<cstdlib>
using namespace std;
enum element {rock = 1, scissors = 2, paper = 3};
enum result {lose = 4, draw = 5, win = 6};
short random (short from, short to)
{
return rand() % (to - from + 1) + from;
}
short read (string msg)
{
short num;
cout << msg;
cin >> num;
return num;
}
result roundresult(element userchoice, element computerchoice)
{
switch(userchoice)
{
case rock:
{
if (computerchoice == scissors)
return win;
else if (computerchoice == rock)
return draw;
else if (computerchoice == paper)
return lose;
}
case scissors:
{
if(computerchoice == paper)
return win;
else if (computerchoice == scissors)
return draw;
else if (computerchoice == rock)
return lose;
}
case paper:
{
if (computerchoice == rock)
return win;
else if (computerchoice == paper)
return draw;
else if (computerchoice == scissors)
return lose;
}
}
}
short userscore(result currentresult, short userresult)
{
if (currentresult == win)
userresult++;
return userresult;
}
short computerscore(result currentresult, short computerresult)
{
if (currentresult == lose)
computerresult++;
return computerresult;
}
short drawscore(result currentresult, short drawtimes)
{
if (currentresult == draw)
drawtimes++;
return drawtimes;
}
void printchoice(element userchoice, element computerchoice)
{
switch (userchoice)
{
case(rock):
{
cout << "\nUser choice is rock\n";
break;
}
case (paper):
{
cout << "\nUser choice is paper\n";
break;
}
case (scissors):
{
cout << "\nUser choice is scissors\n";
break;
}
}
switch (computerchoice)
{
case(rock):
{
cout << "computer choice is rock\n";
break;
}
case (paper):
{
cout << "computer choice is paper\n";
break;
}
case (scissors):
{
cout << "computer choice is scissors\n";
break;
}
}
}
void printresult(short length, element userchoice, element computerchoice, short& computerresult, short& userresult, short& drawtimes)
{
computerresult = 0;
userresult = 0;
drawtimes = 0;
for(short i = 0; i < length; i++)
{
cout << "Round [" << i + 1 << "] begins\n";
userchoice = element(read("Enter your choice: \n1. Rock\n2. Scissors\n3. Paper\nYour choice is: "));
cout << "-----------------------------" << endl;
computerchoice = element(random(1, 3));
printchoice(userchoice, computerchoice);
if (userchoice == computerchoice)
{
cout << "Result is draw\n";
cout << system("color 6f");
}
else if (roundresult(userchoice, computerchoice) == win)
{
cout << "User won\n";
cout << system("color 2f");
}
else if (roundresult(userchoice, computerchoice) == lose)
{
cout << "Computer won\n";
cout << system("color 4f");
}
userresult = userscore(roundresult(userchoice, computerchoice), userresult);
computerresult = computerscore(roundresult(userchoice, computerchoice), computerresult);
drawtimes = drawscore(roundresult(userchoice, computerchoice), drawtimes);
cout << "\nresult is\nUser: " << userresult << "\nComputer: " << computerresult << endl;
cout << "\n--------------------------------------------\n" << endl;
}
}
void printfinalresult(short length, short computerresult, short userresult, short drawtimes)
{
cout << "_______________G A M E O V E R________________\n";
cout << "User winning times : " << userresult << endl;
cout << "Computer winning times: " << computerresult << endl;
cout << "Drawing times : " << drawtimes << endl;
cout << "Final winner : ";
if (userresult > computerresult)
cout << "User" << endl;
else if (userresult < computerresult)
cout << "Computer" << endl;
else
cout << "No winner" << endl;
cout << "Total rounds : " << length << endl;
cout << "_______________________________________________" << endl << endl;
}
void singlegame()
{
short computerresult, userresult, drawtimes = 0;
element userchoice, computerchoice;
srand((unsigned)time(NULL));
short length = read("How many rounds? ");
cout << endl << endl;
printresult(length, userchoice, computerchoice, computerresult, userresult, drawtimes);
printfinalresult(length, computerresult, userresult, drawtimes);
}
bool newgame()
{
return read("Do you want to play again? [1] Yes, [0] No: ");
}
void startgame()
{
bool askingnewgame = true;
do
{
singlegame();
askingnewgame = newgame();
}
while(askingnewgame);
}
int main ()
{
startgame();
}