Please help me make sure this code can run on Ubuntu 14.0 and help me understand what I am doing wrong with my pointer "highScore" I need to be able to delete the pointer too. Please also give an explanation of how I went wrong.
My code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// My struct to control the game
struct Game {
int lo = 10;
int hi = 20;
int seed = 3;
int randNum = 0;
int randNum2 = 0;
int humanScore = 0;
int computerScore = 0;
int overallHumanScore = 0;
int overallComputerScore = 0;
bool humanTurn = true;
bool gameOver = false;
bool validChoice = false;
string playerName;
int choice = 0;
int* highScore = 0;
char yesno = ' ';
};
int D6(Game& game1);
int RandomNumber(Game &game1);
int ProtectRandomNumber(Game &game1);
int ProtectRandomNumber2(Game &game1);
int fixed_seed(Game &game1);
void get_name(Game &game1);
void program_greeting();
int get_game_choice(Game &game1);
int protect_choice(Game &game1);
void pig_game(Game &game1);
int high_score(Game &game1);
int main()
{
Game game1;
get_name(game1);
program_greeting();
cout << "C1 Spec: " << fixed_seed(game1)
<< endl;
cout << "A2 Spec: " <<
RandomNumber(game1) << endl;
cout << "A3 Spec: " <<
ProtectRandomNumber(game1) << endl;
cout << "A4 Spec: " <<
ProtectRandomNumber2(game1) << endl;
do {
while (!game1.gameOver)
{
pig_game(game1);
}
cout << "B3 & B4 Spec -
High score: " << high_score(game1) << endl;
delete game1.highScore;
cout << "Would you like to play
again? Press Y to continue or any ";
cout << "other key to quit:
";
cin >> game1.yesno;
if ((game1.yesno == 'y') ||
(game1.yesno == 'Y')) {
game1.yesno = 'Y';
game1.gameOver =
false;
}
else {
exit(0);
}
} while (game1.yesno != 'Y');
return 0;
}
// Spec A1 - random number generator & D6
int D6(Game &game1)
{
srand(time(NULL));
game1.randNum = rand() % 6 + 1;
return game1.randNum;
}
// Spec A2 - random number generator between lo and
high
int RandomNumber(Game &game1)
{
srand(time(NULL));
game1.randNum2 = rand() % (game1.hi - game1.lo) +
game1.lo;
return game1.randNum2;
}
// Spec A3 - random number generator between lo and high
with error checking -1
int ProtectRandomNumber(Game &game1)
{
game1.lo = 1;
game1.hi = 100;
if ((game1.lo >= game1.hi) || (game1.lo
< 1) || (game1.hi > 100))
return -1;
else
game1.randNum2 = rand() % (game1.hi -
game1.lo) + game1.lo;
return game1.randNum2;
}
// Spec A4 - random number generator between lo and high
with error checking -1 & -2
int ProtectRandomNumber2(Game &game1)
{
game1.lo = 1;
game1.hi = 100;
if ((game1.lo >= game1.hi) || (game1.lo
< 1) || (game1.hi > 100))
return -1;
else
game1.randNum2 = rand() % (game1.hi -
game1.lo) + game1.lo;
if (game1.randNum2 > 100)
return -2;
else
return game1.randNum2;
}
// Spec C1 - fixed seed function
int fixed_seed(Game &game1)
{
srand(game1.seed);
game1.randNum = rand() % 6 + 1;
return game1.randNum;
}
// C2 Spec - Student/player name
void get_name(Game &game1)
{
cout << "Please enter your first and last name
>> ";
getline(cin, game1.playerName);
}
// Spec B2 - game greeting with due date
void program_greeting()
{
cout << endl;
cout << "Welcome to Pig! ~ Due 09.04.2022"
<< endl;
cout << "--------------------------------"
<< endl;
}
// Specs C3 - Numeric menu
int get_game_choice(Game &game1)
{
cout << "-------------------------------"
<< endl;
cout << "1. Roll die" << endl;
cout << "2. Hold" << endl;
cout << "3. Quit" << endl;
cout << "-------------------------------"
<< endl;
cout << "Enter your choice: ";
cin >> game1.choice;
return game1.choice;
}
// Spec C4 - Bulletproof menu
int protect_choice(Game &game1)
{
if (game1.choice == 1)
{
cout << "You rolled a
" << D6(game1) << "." << endl;
if (game1.randNum != 1)
{
game1.humanScore +=
game1.randNum;
cout << "Your turn
score is " << game1.humanScore << "." <<
endl;
}
else
{
cout << "Your turn
score is " << game1.humanScore << "." <<
endl;
game1.humanTurn =
false;
game1.validChoice =
true;
}
}
else if (game1.choice == 2)
{
game1.overallHumanScore +=
game1.humanScore;
cout << "Your turn score is "
<< game1.humanScore << "." << endl;
cout << "Your overall score is "
<< game1.overallHumanScore << "." << endl;
game1.humanTurn = false;
game1.validChoice = true;
}
else if (game1.choice == 3)
{
exit(0);
}
else
{
cout << "Invalid choice. Please
enter 1, 2, or 3." << endl;
}
}
// The actual game
void pig_game(Game &game1)
{
if (game1.humanTurn)
{
cout << endl;
cout << game1.playerName <<
"'s game." << endl;
game1.humanScore = 0;
game1.validChoice = false;
while (!game1.validChoice)
{
get_game_choice(game1);
cout << endl;
protect_choice(game1);
}
}
else
{
cout << "Computer's turn."
<< endl;
game1.computerScore = 0;
game1.validChoice = false;
while (!game1.validChoice)
{
game1.randNum
= rand() % 3 + 1;
if (game1.randNum ==
1)
{
game1.overallComputerScore += game1.computerScore;
cout
<< "The computer's turn score is " <<
game1.computerScore << "." << endl;
cout
<< "The computer's overall score is " <<
game1.overallComputerScore << "." << endl;
game1.humanTurn = true;
game1.validChoice = true;
}
else
{
game1.randNum = rand() % 6 + 1;
cout
<< "The computer rolled a " << game1.randNum <<
"." << endl;
game1.computerScore += game1.randNum;
cout
<< "The computer's turn score is " <<
game1.computerScore << "." << endl;
}
}
}
if (game1.overallHumanScore >= 100)
{
game1.gameOver = true;
cout <<
"-------------------------------" << endl;
cout << game1.playerName <<
" wins!" << endl;
cout << "~ Final score ~ "
<< endl;
cout << game1.playerName <<
": " << game1.overallHumanScore << endl;
cout << "Computer: " <<
game1.overallComputerScore << endl;
}
else if (game1.overallComputerScore >= 100)
{
game1.gameOver = true;
cout <<
"-------------------------------" << endl;
cout << "The computer wins!"
<< endl;
cout << "~ Final score ~"
<< endl;
cout << game1.playerName <<
": " << game1.overallHumanScore << endl;
cout << "Computer: " <<
game1.overallComputerScore << endl;
}
}
// Spec B3 - Store high score on the heap and delete
afterwards.
int high_score(Game &game1)
{
game1.highScore = game1.overallHumanScore >
game1.overallComputerScore ?
new int(game1.overallHumanScore) : new
int(game1.overallComputerScore);
return *game1.highScore;
}