Thanks for your time, I am new to programming and I spent some days making this rock, paper & scissor game. What other possible improvements could I make after the ones I've made myself?
I've tried explaining each step as the program goes on, but I essentially at first generate a computer pick (i.e. Rock, Paper or Scissor), then ask the user for their pick (i.e. rock
, paper
or scissor
), compare the two and depending on the game rules (Rock vs Paper results in a loss for the Rock), output the game result.
Again, this is my first try at it. Some improvements I've already added myself are: getline
instead of cin
, use of functions and switches, shortened the code and merged outputs in little space.
Thank you for helping a new-entry at coding.
main.cpp
#include "main.h"
int main(int argc, const char * argv[]) {
//variables.
string usrPick;
string stringResult;
char randPick = fnc.randPick(); //generates the first random pick.
int gameCountr = 0;
//beginning of the program.
cout << endl << "GAME " << gameCountr << " ~ ";
while (getline(cin, usrPick) && ++gameCountr) {
//variables.
char gameResult = fnc.gameResults(fnc.toChar(usrPick), randPick);
//converts game result to readable text.
switch (gameResult) {
case 'e': stringResult = "even";
break;
case 'w': stringResult = "won";
break;
case 'l': stringResult = "lost";
break;
default: stringResult = "?";
break;
}
//prints out the choices and who won the game.
cout << "you: " << fnc.toChar(usrPick) << " / computer: " << randPick << " / " << stringResult << endl << endl << endl;
//generates a new random choice.
randPick = fnc.randPick();
//prompts the user to input their choice to play again.
cout << "GAME " << gameCountr << " ~ ";
}
return 0;
}
main.h
#ifndef main_h
#define main_h
#include <iostream>
#include <vector>
using namespace std;
class exercise2 {
public:
char toChar(string word);
char randPick();
char gameResults(char usr_ch, char cmptr_ch);
private:
char outp_s;
};
exercise2 fnc;
//converts to char the user input.
char exercise2::toChar(string inpt_s) {
//reinitializes value.
outp_s = NULL;
if (inpt_s == "rock") {
outp_s = 'R';
} else if (inpt_s == "paper") {
outp_s = 'P';
} else if (inpt_s == "scissor") {
outp_s = 'S';
} else {
outp_s = '?';
}
return outp_s;
}
//generates a random choice for the computer to play.
char exercise2::randPick() {
//variables.
vector<char> vctrOptions = {'R','P','S'};
//chooses a random value in the given pool of values.
return vctrOptions[rand() % vctrOptions.size()];
}
//prints out the game results.
char exercise2::gameResults(char usr_ch, char cmptr_ch) {
//variables.
outp_s = NULL;
//checks user input and applies game rules.
switch (usr_ch) {
case 'R':
switch (cmptr_ch) {
case 'R': outp_s = 'e';
break;
case 'P': outp_s = 'l';
break;
case 'S': outp_s = 'w';
break;
}
break;
case 'P':
switch (cmptr_ch) {
case 'R': outp_s = 'w';
break;
case 'P': outp_s = 'e';
break;
case 'S': outp_s = 'l';
break;
}
break;
case 'S':
switch (cmptr_ch) {
case 'R': outp_s = 'l';
break;
case 'P': outp_s = 'w';
break;
case 'S': outp_s = 'e';
break;
}
break;
default:
outp_s = '0';
break;
}
return outp_s;
}
#endif