Hello!
I have written the following C++ program that serves as a game of rock, paper, scissors:
Note that since this project is required for a beginner’s course, the code is naturally not ideal as I have used the features I have learnt so far and not much else:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void DisplayTitle()
{
cout << "==================================================" << "n";
cout << " Rock, Paper, Scissors! " << "n";
cout << "==================================================" << "n";
cout << "n";
cout << "Hello and Welcome to "Rock, Paper, Scissors!", Please follow the instructions below:" << "nn";
}
int ReadNumberofRounds()
{
int NumberofRounds = 0;
do
{
cout << "Please enter the number of rounds you wish to play before the game is concluded." << "n";
cout << "* Input can be any positive integer." << "n";
cin >> NumberofRounds;
} while (NumberofRounds <= 0);
return NumberofRounds;
}
enum Item { Rock = 0, Scissor = 1, Paper = 2 };
string Items[3] = { "Rock", "Scissor", "Paper" };
enum Status { PlayerLost = 0, PlayerWon = 1, GameTied = 2 };
string Status_[3]{ "You have lost", "You have won", "You are at a draw with the computer for" };
struct RPSGameStats
{
unsigned RoundsWon, RoundsLost, RoundsTied;
};
Item ReadPlayerChoice()
{
unsigned short PlayerChoice = 0;
do
{
cout << "n" << "Please enter the number corresponding to the desired item from the list below:" << "n";
cout << "[0] Rock" << "n";
cout << "[1] Scissor" << "n";
cout << "[2] Paper" << "n";
cin >> PlayerChoice;
} while (PlayerChoice < 0 || PlayerChoice > 3);
return Item(PlayerChoice);
}
int GenerateRandomNumberBet(int start, int end)
{
return (rand() % (end - start + 1)) + start;
}
Item ReadComputerChoice()
{
cout << "n" << "The computer is choosing an item..." << "n";
unsigned short ComputerChoice = GenerateRandomNumberBet(1, 3);
return Item(ComputerChoice);
}
void RespondtoStatus(Status PlayerStatus)
{
switch (PlayerStatus)
{
case Status::PlayerLost:
system("Color 47");
cout << "a";
break;
case Status::PlayerWon:
system("Color 27");
break;
case Status::GameTied:
system("Color 67");
break;
}
}
Status Round()
{
Item PlayerChoice = ReadPlayerChoice(), ComputerChoice = ReadComputerChoice();
Status PlayerStatus = Status::GameTied;
if (PlayerChoice == ComputerChoice)
PlayerStatus = Status::GameTied;
else if (PlayerChoice == Item::Rock && ComputerChoice == Item::Paper)
PlayerStatus = Status::PlayerLost;
else if (PlayerChoice == Item::Paper && ComputerChoice == Item::Rock)
PlayerStatus = Status::PlayerWon;
else
{
if (int(PlayerChoice) < int(ComputerChoice))
PlayerStatus = Status::PlayerWon;
else
PlayerStatus = Status::PlayerLost;
}
cout << "n";
cout << "You have chosen: " << Items[int(PlayerChoice)] << "n";
cout << "The computer has chosen: " << Items[int(ComputerChoice)] << "nn";
cout << Status_[int(PlayerStatus)] << " this round." << "n";
RespondtoStatus(PlayerStatus);
return PlayerStatus;
}
RPSGameStats Game()
{
int NumberofRounds = ReadNumberofRounds();
RPSGameStats GameStats = { 0u, 0u, 0u };
for (int i = 1; i <= NumberofRounds; i++)
{
cout << "n";
cout << "==========" << "n";
cout << "Round " << i << "n";
cout << "==========" << "n";
cout << "n";
switch (Round())
{
case Status::PlayerLost:
GameStats.RoundsLost++;
continue;
case Status::PlayerWon:
GameStats.RoundsWon++;
continue;
case Status::GameTied:
GameStats.RoundsTied++;
continue;
}
}
return GameStats;
}
void RPS()
{
DisplayTitle();
bool PlayAgain = 0;
do
{
system("Color 07");
RPSGameStats GameStats = Game();
cout << "n" << "The specified number of rounds has been finished; Game Over!" << "n";
cout << "n";
cout << "==========" << "n";
cout << "Results" << "n";
cout << "==========" << "n";
cout << "n";
cout << "--------------------" << "+" << "--------------------" << "+" << "--------------------" << "n";
cout << setw(20) << left << "Rounds Won" << "|" << setw(20) << left << "Rounds Lost" << "|" << setw(20) << left << "Rounds Tied" << "n";
cout << "--------------------" << "+" << "--------------------" << "+" << "--------------------" << "n";
cout << setw(20) << left << GameStats.RoundsWon << "|" << setw(20) << left << GameStats.RoundsLost << "|" << setw(20) << left << GameStats.RoundsTied << "|" << "n";
cout << "--------------------" << "+" << "---------------------" << "+" << "-------------------" << "n";
Status PlayerStatus = Status::GameTied;
if (GameStats.RoundsWon < GameStats.RoundsLost)
PlayerStatus = Status::PlayerLost;
else if (GameStats.RoundsWon > GameStats.RoundsLost)
PlayerStatus = Status::PlayerWon;
cout << "n" << Status_[int(PlayerStatus)] << " this game." << "n";
cout << "n";
RespondtoStatus(PlayerStatus);
cout << "Do you wish to play again? Please enter the number corresponding to the desired choice from the list below:" << "n";
cout << "[1] Yes" << "n";
cout << "[2] No" << "n";
cin >> PlayAgain;
} while (PlayAgain);
}
int main()
{
srand(unsigned int(time(NULL)));
RPS();
return 0;
}
On running the program, everything worked as expected except for the misprinting of some character sequences, as illustrated in the following screenshots (the misprinted sequences are highlighted):
I have tried replacing the newline characters (n
) with std::endl
to flush the stream, but the problem still persists.