0% found this document useful (0 votes)
42 views8 pages

Project 1 Solution

ابو هدهود

Uploaded by

khaledmamdooh77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

Project 1 Solution

ابو هدهود

Uploaded by

khaledmamdooh77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Project 1 Solution Using C++

#include<iostream>
#include<cstdlib>

using namespace std;

enum enGameChoice { Stone = 1, Paper = 2, ◌


ٍScissors = 3 };
enum enWinner { Player1 = 1, Computer = 2, Draw = 3 };

struct stRoundInfo
{
short RoundNumber = 0;
enGameChoice Player1Choice;
enGameChoice ComputerChoice;
enWinner Winner;
string WinnerName;
};

struct stGameResults
{
short GameRounds = 0;
short Player1WinTimes = 0;
short Computer2WinTimes = 0;
short DrawTimes = 0;
enWinner GameWinner;
string WinnerName = "";
};

int RandomNumber(int From, int To)


{
//Function to generate a random number
int randNum = rand() % (To - From + 1) + From;
return randNum;
}

string WinnerName(enWinner Winner)


{
string arrWinnerName[3] = { "Player1","Computer","No Winner"
};
return arrWinnerName[Winner - 1];

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

enWinner WhoWonTheRound(stRoundInfo RoundInfo)


{

if (RoundInfo.Player1Choice == RoundInfo.ComputerChoice)
{
return enWinner::Draw;
}

switch (RoundInfo.Player1Choice)
{
case enGameChoice::Stone:
if (RoundInfo.ComputerChoice == enGameChoice::Paper)
{
return enWinner::Computer;
}
break;

case enGameChoice::Paper:
if (RoundInfo.ComputerChoice == enGameChoice::◌
ٍScissors)
{
return enWinner::Computer;
}
break;

case enGameChoice::◌
ٍScissors:
if (RoundInfo.ComputerChoice == enGameChoice::Stone)
{
return enWinner::Computer;
}
break;

//if you reach here then player1 is the winner.


return enWinner::Player1;

string ChoiceName(enGameChoice Choice)


{
string arrGameChoices[3] = { "Stone","Paper","Scissors" };

return arrGameChoices[Choice - 1];

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

void SetWinnerScreenColor(enWinner Winner)


{
switch (Winner)
{
case enWinner::Player1:
system("color 2F"); //turn screen to Green
break;

case enWinner::Computer:
system("color 4F"); //turn screen to Red
cout << "\a";
break;
default:
system("color 6F"); //turn screen to Yellow
break;
}
}

void PrintRoundResults(stRoundInfo RoundInfo)


{

cout << "\n____________Round [" << RoundInfo.RoundNumber << "]


____________\n\n";
cout << "Player1 Choice: " <<
ChoiceName(RoundInfo.Player1Choice) << endl;
cout << "Computer Choice: " <<
ChoiceName(RoundInfo.ComputerChoice) << endl;
cout << "Round Winner : [" << RoundInfo.WinnerName << "]
\n";
cout << "__________________________________\n" << endl;

SetWinnerScreenColor(RoundInfo.Winner);

enWinner WhoWonTheGame(short Player1WinTimes, short


ComputerWinTimes)
{

if (Player1WinTimes > ComputerWinTimes)


return enWinner::Player1;
else if (ComputerWinTimes > Player1WinTimes)
return enWinner::Computer;
else
return enWinner::Draw;

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

stGameResults FillGameResults(int GameRounds, short


Player1WinTimes, short ComputerWinTimes, short DrawTimes)
{

stGameResults GameResults;

GameResults.GameRounds = GameRounds;
GameResults.Player1WinTimes = Player1WinTimes;
GameResults.Computer2WinTimes = ComputerWinTimes;
GameResults.DrawTimes = DrawTimes;
GameResults.GameWinner = WhoWonTheGame(Player1WinTimes,
ComputerWinTimes);
GameResults.WinnerName = WinnerName(GameResults.GameWinner);

return GameResults;
}

enGameChoice ReadPlayer1Choice()
{

short Choice = 1;

do
{

cout << "\nYour Choice: [1]:Stone, [2]:Paper, [3]:Scissors


? ";
cin >> Choice;

} while (Choice < 1 || Choice >3);

return (enGameChoice)Choice;

enGameChoice GetComputerChoice()
{
return (enGameChoice) RandomNumber(1, 3);
}

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

stGameResults PlayGame(short HowManyRounds)

{
stRoundInfo RoundInfo;
short Player1WinTimes = 0, ComputerWinTimes = 0, DrawTimes = 0;

for (short GameRound = 1; GameRound <= HowManyRounds; GameRound++)


{
cout << "\nRound [" << GameRound << "] begins:\n";
RoundInfo.RoundNumber = GameRound;
RoundInfo.Player1Choice = ReadPlayer1Choice();
RoundInfo.ComputerChoice = GetComputerChoice();
RoundInfo.Winner = WhoWonTheRound(RoundInfo);
RoundInfo.WinnerName = WinnerName(RoundInfo.Winner);

//Increase win/Draw counters


if (RoundInfo.Winner == enWinner::Player1)
Player1WinTimes++;
else if (RoundInfo.Winner == enWinner::Computer)
ComputerWinTimes++;
else
DrawTimes++;

PrintRoundResults(RoundInfo);
}

return FillGameResults(HowManyRounds, Player1WinTimes,


ComputerWinTimes, DrawTimes);

string Tabs(short NumberOfTabs)


{
string t = "";

for (int i = 1; i < NumberOfTabs; i++)


{
t = t + "\t";
cout << t;
}
return t;

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

void ShowGameOverScreen()
{
cout << Tabs(2) <<
"__________________________________________________________\n\n";
cout << Tabs(2) << " +++ G a m e O v e r
+++\n";
cout << Tabs(2) <<
"__________________________________________________________\n\n";

void ShowFinalGameResults(stGameResults GameResults)


{

cout << Tabs(2) << "_____________________ [Game Results


]_____________________\n\n";
cout << Tabs(2) << "Game Rounds : " <<
GameResults.GameRounds << endl;
cout << Tabs(2) << "Player1 won times : " <<
GameResults.Player1WinTimes << endl;
cout << Tabs(2) << "Computer won times : " <<
GameResults.Computer2WinTimes << endl;
cout << Tabs(2) << "Draw times : " <<
GameResults.DrawTimes << endl;
cout << Tabs(2) << "Final Winner : " <<
GameResults.WinnerName << endl;
cout << Tabs(2) <<
"___________________________________________________________\n";

SetWinnerScreenColor(GameResults.GameWinner);
}

short ReadHowManyRounds()
{
short GameRounds = 1;

do
{
cout << "How Many Rounds 1 to 10 ? \n";
cin >> GameRounds;

} while (GameRounds < 1 || GameRounds >10);

return GameRounds;
}

ProgrammingAdvices.com
© Copyright 2022
Project 1 Solution Using C++

void ResetScreen()
{
system("cls");
system("color 0F");

void StartGame()
{
char PlayAgain = 'Y';

do
{
ResetScreen();
stGameResults GameResults = PlayGame(ReadHowManyRounds());
ShowGameOverScreen();
ShowFinalGameResults(GameResults);

cout << endl << Tabs(3) << "Do you want to play again? Y/N? ";

cin >> PlayAgain;

} while (PlayAgain == 'Y' || PlayAgain == 'y');

int main()
{
//Seeds the random number generator in C++, called only once
srand((unsigned)time(NULL));

StartGame();

return 0;
}

ProgrammingAdvices.com
© Copyright 2022

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy