This is the first piece of code I wrote on my own. I tried to use everything I know, like functions, loops and variables. Is it good or could it be simpler?
Please be hard as you can be with me because I feel my programming knowledge is quite poor and I end up using Google for solving my problems.
// RPS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int nPlayerScore=0;
int nComputerScore=0;
int nPlayerMove=0;
int nComputerMove=0;
void DisplayScore()
{
cout << "Player: " << nPlayerScore << endl;
cout << "Computer: " << nComputerScore << endl << endl;
}
int GetMove()
{
cout << "Please choose a move: \n1.Rock\n2.Paper\n3.Scissors\n";
int x;
cin >> x;
cout << endl;
return x;
}
void CalculateResults()
{
if (nPlayerMove==1) //player is Rock
{
if (nComputerMove==1) {cout << "player Rock vs computer Rock: IT'S A TIE!";}
if (nComputerMove==2) {cout << "player Rock vs computer Paper: COMPUTER WON!"; nComputerScore=nComputerScore+1;}
if (nComputerMove==3) {cout << "player Rock vs computer Scissors: PLAYER WON!"; nPlayerScore=nPlayerScore+1;}
}
if (nPlayerMove==2) //player is Paper
{
if (nComputerMove==1) {cout << "player Paper vs computer Rock: PLAYER WON!"; nPlayerScore=nPlayerScore+1;}
if (nComputerMove==2) {cout << "player Paper vs computer Paper: IT'S A TIE!";}
if (nComputerMove==3) {cout << "player Paper vs computer Scissors: COMPUTER WON!"; nComputerScore=nComputerScore+1;}
}
if (nPlayerMove==3) //player is Scissors
{
if (nComputerMove==1) {cout << "player Scissors vs computer Rock: COMPUTER WON!"; nComputerScore=nComputerScore+1;}
if (nComputerMove==2) {cout << "player Scissors vs computer Paper: PLAYER WON!"; nPlayerScore=nPlayerScore+1;}
if (nComputerMove==3) {cout << "player Scissors vs computer Scissors: IT'S A TIE!";}
}
cout << endl << endl;
}
int main()
{
do
{
cout << "Welcome to RPS 1.0a \tby GeTAFIX \n\n";
DisplayScore();
nPlayerMove=GetMove();
srand((unsigned)time(0));
nComputerMove=rand()%(3)+1;
CalculateResults();
//cout << "Press any key to continue...";
system("pause");
system("cls");
} while (1==1);
}