Finally done. (well for now until I decide to do something else different with it…we’ll see)
I have changed how the game opens. Now there is a menu to choose to either start the game, display the high score, or exit the game.
The game will proceed as normal if you select start game. If you enter the choice for high score, the screen will clear, display the title, and then pull the highscore.txt if it exists, and if it doesn’t, the program will terminate. If you select to leave the game, the game will terminate immediately.
When playing the game, the player will be prompt to enter there name, and the story is catered to that person thereafter. When the player either wins the game or loses the game, the high score will be saved in highscore.txt (which it will create if it is not tested).
//Title: tripleXGame.cpp
//Course: Udemy Unreal Engine C++ Developer
//Author: Ryn Ellis
//Date: 2019-05-17
//Description: Code guessing game
//Libraries
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
//Function Definitions
void Opening(); //Title and story message function
int AdditionCalculations(int first, int second, int third); //Addition Calculations for code riddle function
int MultiplicationCalculations(int first, int second, int third); //Multiplication Calculations for code riddle function
int PlayerAdditionCalculations(int first, int second, int third); //Addition Calculations for code riddle function for the player
int PlayerMultiplicationCalculations(int first, int second, int third); //Multiplication Calculations for code riddle function for the player
int PlayerInput(); //Player code input function
void Boom(); //Game ending if wrong code guessed
void Win(); //Game ending if all the codes are guessed correctly
int Comparison(int codeSum, int playerSum, int codeProduct, int playerProduct); //Comparison function to compare sums and products
bool PlayGame(); //Actual game play function
bool ReplayGame(); //To ask player if they would like to play again
void LevelIncrease(); //To increase the level of difficulty if player gets the correct code
int RandomNumberGenerator(); //Generates random numbers for the code
void PlayerIntroduction(); //To get player's first name to use in the game
void Title(); //To print the main title of the game
int MainScreen(); //Main Screen
void HighscoreDisplay(); //Display high score
void WriteHighScore(); //Write high score
//Variables and Arrays
//Level Variables
int Difficulty = 1;
const int MaxDifficulty = 10;
bool bPlay = true;
//Code Number Array
int CodeNumber[3] = { 0, 0, 0 };
int Interval = 0;
//Mathematical Variables
int SumCodeTotal = 0;
int ProductCodeTotal = 0;
int PlayerSum = 0;
int PlayerProduct = 0;
//Player Information
int PlayersGuess[3] = { 0, 0, 0 };
string PlayerName;
//High Score File Information
ifstream inFile;
//Main Program
int main()
{
while (bPlay == true)
{
if (Difficulty == 1)
{
MainScreen();
}
else
{
PlayGame();
}
std::cin.clear(); //Clears any errors
std::cin.ignore(); //Discards the buffer
}
return 0;
}
//Functions
int RandomNumberGenerator()
{
//This function randomizes the number for the code
int Number = rand() % Difficulty + Difficulty;
return Number;
}
int MainScreen()
{
char Choice = 0;
Title();
std::cout << " ************************************************************************************************* \n";
std::cout << " * 1. Start Game *\n";
std::cout << " * 2. High Score *\n";
std::cout << " * X. Exit Game *\n";
std::cout << " ************************************************************************************************* \n";
std::cout << " Pick an option: ";
std::cin >> Choice;
if (Choice == '1')
{
PlayGame();
}
else if (Choice == '2')
{
HighscoreDisplay();
}
else
{
exit(0);
}
return 0;
}
bool PlayGame()
{
//This is the main gameplay function that calls other functions
srand(time(NULL));
Title();
Opening();
PlayerInput();
Comparison(SumCodeTotal, PlayerSum, ProductCodeTotal, PlayerProduct);
return true;
}
void Title()
{
//This function prints the title of the game to the screen
system("CLS");
system("color 1");
std::cout << " _________ _______ _________ _______ _ _______ \n";
std::cout << " \\__ __/( ____ )\\__ __/( ____ )( \\ ( ____ \\ |\\ /| \n";
std::cout << " ) ( | ( )| ) ( | ( )|| ( | ( \\/ ( \\ / ) \n";
std::cout << " | | | (____)| | | | (____)|| | | (__ \\ (_) / \n";
std::cout << " | | | __) | | | _____)| | | __) ) _ ( \n";
std::cout << " | | | (\\ ( | | | ( | | | ( / ( ) \\ \n";
std::cout << " | | | ) \\ \\_____) (___| ) | (____/\\| (____/\\ ( / \\ ) \n";
std::cout << " )_( |/ \\__/\\_______/|/ (_______/(_______/ |/ \\|\n\n\n\n";
}
void Opening()
{
PlayerProduct = 0;
PlayerSum = 0;
SumCodeTotal = 0;
ProductCodeTotal = 0;
if (Difficulty == 1)
{
PlayerIntroduction();
}
Title();
std::cout << " ************************************************************************************************* \n";
if (Difficulty == 1)
{
std::cout << " * Rookie " << PlayerName << ", you are our only bomb defuser. *\n";
std::cout << " * There is a crisis in the building with " << MaxDifficulty << " bombs to defuse. *\n";
std::cout << " * You need to enter the correct codes on the devices in order to defuse them! *\n";
std::cout << " * Hurry before time runs out! *\n";
}
else
{
std::cout << " * That's it Rookie " << PlayerName << "! You have defused the bomb! *\n";
std::cout << " * You only have " << MaxDifficulty - Difficulty + 1 << " left to defuse! *\n";
}
std::cout << " * * \n";
std::cout << " * * \n";
std::cout << " * This is bomb " << Difficulty << ". * \n";
std::cout << " * There are three numbers in the code in order to defuse the bomb. * \n";
std::cout << " * * \n";
std::cout << " * * \n";
while (Interval < 3)
{
CodeNumber[Interval] = RandomNumberGenerator();
++Interval;
}
Interval = 0;
SumCodeTotal = AdditionCalculations(CodeNumber[0], CodeNumber[1], CodeNumber[2]);
ProductCodeTotal = MultiplicationCalculations(CodeNumber[0], CodeNumber[1], CodeNumber[2]);
std::cout << " * The sum of the three numbers equal " << SumCodeTotal << ". *\n";
std::cout << " * The product of the three numbers equal " << ProductCodeTotal << ". *\n";
std::cout << " * *\n";
std::cout << " * You only have one chance to get this right and save everyone! *\n";
std::cout << " *************************************************************************************************" << std::endl;
std::cout << std::endl;
}
int AdditionCalculations(int First, int Second, int Third)
{
//This function calculates the sum and the product of the three numbers for the code sequence
SumCodeTotal = First + Second + Third;
return SumCodeTotal;
}
int MultiplicationCalculations(int First, int Second, int Third)
{
//This function calculates the product for the randomized code
ProductCodeTotal = First * Second * Third;
return ProductCodeTotal;
}
void PlayerIntroduction()
{
//This function prompts the user for their name
std::cout << " ************************************************************************************************* \n";
std::cout << " ********************** Hey YOU! Rookie! What's your name? ************************************\n ";
std::cout << " ";
std::cin >> PlayerName;
}
int PlayerInput()
{
//This function collects the player's guess
int I = 0; //Used for incrementing through the array
int Input = 0;
std::cout << " What is the code to defuse the bomb? ";
while (I< 3)
{
std::cin >> PlayersGuess[I];
I++;
}
PlayerSum = PlayerAdditionCalculations(PlayersGuess[0], PlayersGuess[1], PlayersGuess[2]);
PlayerProduct = PlayerMultiplicationCalculations(PlayersGuess[0], PlayersGuess[1], PlayersGuess[2]);
return 0;
}
int PlayerAdditionCalculations(int First, int Second, int Third)
{
//This function calculates the player sum of the three numbers for the code sequence
PlayerSum = First + Second + Third;
return PlayerSum;
}
int PlayerMultiplicationCalculations(int First, int Second, int Third)
{
//This function calculates the player product of the three numbers for the code sequence
PlayerProduct = First * Second * Third;
return PlayerProduct;
}
int Comparison(int CodeSum, int PlayerSum, int CodeProduct, int PlayerProduct)
{
//This function compares the sum of the code to the player's sum as well as the product of the code to the player's product
if ((CodeSum == PlayerSum) && (CodeProduct == PlayerProduct))
{
LevelIncrease();
}
else
{
Boom();
}
return 0;
}
bool ReplayGame()
{
//This function asks the player if they would like to replay the game
bool bPlayersChoice = true;
char PlayerReplay = 0;
std::cout << std::endl;
std::cout << " Would you like to play again? ";
std::cin >> PlayerReplay;
//checking to see if Y or y was selected in ASCII
if ((PlayerReplay == 89) || (PlayerReplay == 121))
{
bPlayersChoice = true;
Difficulty = 1;
}
else
{
bPlayersChoice = false;
}
return bPlayersChoice;
}
void LevelIncrease()
{
//This function either increases the difficulty per level or sends the program to the win function if the difficult level is reached
if (Difficulty == MaxDifficulty)
{
Win();
}
else
{
++Difficulty;
}
}
void Boom()
{
//This function is for when the player guesses the wrong code
system("CLS");
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " ======== ==== ==== ==== ==== \n";
std::cout << " || \\\\ // \\\\ // \\\\ || \\ / || \n";
std::cout << " ||_____// || || || || || V || \n";
std::cout << " ||~~~~~\\\\ || || || || || || \n";
std::cout << " || // \\\\ // \\\\ // || || \n";
std::cout << " ======== ==== ==== == == \n\n\n\n\n";
std::cout << " You guessed wrong.........\n";
std::cout << " You just killed yourself and everyone else in a five block radius.\n";
WriteHighScore();
bPlay = ReplayGame();
}
void Win()
{
//This function is for when the player guesses all the codes correctly
system("CLS");
std::cout << " |\\ /|\\__ __/( ( /|( ( /|( ____ \\( ____ )\n";
std::cout << " | ) ( | ) ( | \\ ( || \\ ( || ( \\/| ( )| \n";
std::cout << " | | _ | | | | | \\ | || \\ | || (__ | (____)| \n";
std::cout << " | |( )| | | | | (\\ \\) || (\\ \\) || __) | __) \n";
std::cout << " | || || | | | | | \\ || | \\ || ( | (\\ ( \n";
std::cout << " | () () |___) (___| ) \\ || ) \\ || (____/\\| ) \\ \\__ \n";
std::cout << " (_______)\\_______/|/ )_)|/ )_)(_______/|/ \\__/ \n\n\n";
std::cout << " *************************************************************************\n";
std::cout << " * Congradulations Rookie " << PlayerName << "! * \n";
std::cout << " * You have successfuly defused all the bombs and saved everyone! * \n";
std::cout << " * You will definitely be getting a promotion and raise for this. * \n";
std::cout << " ************************************************************************* \n\n";
WriteHighScore();
bPlay = ReplayGame();
}
void HighscoreDisplay()
{
int Increments = 0;
char Character = '#';
Title();
ifstream Myfile("highscore.txt", ios::in);
if (!Myfile)
{
std::cout << "Error opening file. Program will terminate.";
exit(0);
}
std::cout << " High Score\n";
std::cout << " *****************************************\n";
std::cout << " * Name Level *\n";
std::cout << " *****************************************\n";
for (Increments = 0; !Myfile.eof(); Increments++)
{
if (Character == '#')
{
std::cout << " ";
}
Myfile.get(Character);
std::cout << Character;
if (Character == '\n')
{
std::cout << " ";
}
}
Myfile.close();
system("pause");
}
void WriteHighScore()
{
ofstream Myfile("highscore.txt", ios::app);
if (!Myfile)
{
std::cout << "Error. Program closing." << std::endl;
exit(0);
}
Myfile << PlayerName << " " << Difficulty << "\n";
Myfile.close();
}