/*
A variation of the game 'Mastermind'.
This game allows two modes of play -
vs a computer and vs a friend (another
player). The game is designed with the
basics of the mastermind game function,
where a hidden sequence is given and it
is the opponent's job to find the sequence
given clues from feedback as fast as possible.
Instead of coloured pins, this game uses
a sequence of numbers in the theme of
cracking a safe.
*/
// Libraries and defines and global variables
#include <iostream>
#include <string>
#include <stdlib.h> // library with the function rand
#include <time.h> // library with time in
#include <windows.h> // library with clear screen in
#include <sstream> // library with string streams to save strings to file
#define MAXNUM 10 // Constant for making a random value between 0 and 9
int iChoice; // Global choice for menu
string sFirst; // Global name for first place
string sSecond; // Global name for second place
string sThird; // Global name for third place
int iFirstScore; // Score of first place
int iSecondScore; // Score of second place
int iThirdScore; // Score of third place
char cSequence[6]; // Variable to store player's code
char cGuess[6]; // Variable to store player's guess
char cCode[6]; // Variable to store inputs of codes
using namespace std;
void load(); // Get variables from file (program start)
void save(); // Save varaibles to file (program end)
int menu(); // Prototype to create menu
int computer(); // Prototype to play computer
int player(); // Prototype to play another player
int hiscore(); // Prototype to show hi-score
int gameengine(); // Prototype to perform main guessing function
void codeentry(); // Prototype to enter a 5 digit code
int main()
{
load(); // Gets the variables stored in "hi-score.bat"
cout << "Welcome to SafeCracker. Please choose an option from the menu.\n\n";
menu(); // Ask user what they want to do.
while ( iChoice != 4 ) // While user doesn't want to quit.
{
if ( iChoice == 1 ) // User chose to play vs computer.
computer(); // Run function to play computer.
if ( iChoice == 2 ) // User chose to play vs another player.
player(); // Run function to play another player.
if ( iChoice == 3 ) // User chose to view hi-scores
hiscore(); // Run function to show hi-score
System("cls"); // Clear the screen
menu(); // Ask what the user want's to do now.
}
save(); // Saves the varaibles to "hi-score.bat"
return 0; // End program (User typed "Quit")
}
void load() {
string data;
ostringstream temp;
ifstream fh ("scores.dat");
if (fh.is_open())
{
while (!fh.eof())
getline(fh, data);
fh.close();
}
else
cout << "Couldn't open file: scores.dat" << endl;
int del = 1;
int type = 0;
for (int i=0; i<data.size(); i++)
{
if (data[i] == chr(9))
{
if (del == 1)
{
if (type == 0)
{
sFirst = temp.str();
type = 1;
}
else
{
iFirstScore = atoi(temp.str().c_str());
type = 0;
del++;
}
}
if (del == 2)
{
if (type == 0)
{
sSecond = temp.str();
type = 1;
}
else
{
iSecondScore = atoi(temp.str().c_str());
type = 0;
del++;
}
}
if (del == 3)
{
if (type == 0)
{
sThird = temp.str();
type = 1;
}
else
{
iThirdScore = atoi(temp.str().c_str());
type = 0;
del++;
}
}
temp.str("");
}
else
temp << data[i];
}
}
void save() {
ostringstream str;
str << sFirst << chr(9) << iFirstScore << chr(9) << sSecond << chr(9) << iSecondScore << chr(9) << sThird << chr(9) << iThirdScore;
ofstream fh ("scores.dat");
if (fh.is_open())
{
fh << str.str();
fh.close();
}
else
cout << "Couldn't open file: scores.dat" << endl;
}
int menu() // Function to ask user what they would like to do
{
cout << "Menu:\n\n\t 1 - Vs. Computer\n\t 2 - Vs, Player\n\t 3 - Hi-Scores\n\t 4 - Quit\n\nChoice: ";
cin >> iChoice; // User enter's choice,
while ( ( iChoice < 1 ) || ( iChoice > 4 ) ) // If choice is invalid ask again
{
cout << iChoice << " is an invalid choice. Choose between option 1 and option 4.\n\nChoice: ";
cin >> iChoice;
}
return 0;
}
int computer() // Function to play the computer
{
srand ( (int) time( NULL )); // Seed the random number generator
for (int i=0; i<5; i++)
cSequence[i] = ( rand() % MAXNUM ) + '0'; // Set 5 digits to the sequence (0-9)
cSequence[5] = '\0';
gameengine(); // Run game engine function for user to guess the sequence.
return 0;
}
int player() // Function to play another player
{
// Player 1
cout << "Player 2: Enter a 5-number sequence between 00000 and 99999. (The entry will be masked.)\n\n Sequence: ";
codeentry(); // Function to create the sequence
for (int i=0; i<6; i++)
cSequence[i] = cCode[i]; // Assign sequence to variable
gameengine(); // Run game engine function for user to guess the sequence.
// Player 2
cout << "Player 1: Enter a 5-number sequence between 00000 and 99999. (This entry will be masked.)\n\n Sequence: ";
codeentry(); // Function to create the sequence
for (int i=0; i<6; i++)
cSequence[i] = cCode[i]; // Assign sequence to variable
gameengine(); // Run game enginge function for user to guess the sequence
return 0;
}
int gameengine() // Function for main engine
{
System("cls"); // Clear the screen
cout << "Armed with your 'Click Listener' and luck, attempt to crack the safe!";
char cGuessRight='n'; // Set the user's current status to "not guessed correctly."
int x=0; // Loop variable
int i=0; // Loop variable
int iCorrect; // Value to store whether guess is the same as sequence
while ( ( x<10 ) && ( cGuessRight != 'y' ) )
{
x++; // Increase attempt number
cout << "\n\nCrack The Safe: ";
codeentry(); // Function to create the sequence
for (i=0; i<6; i++)
cGuess[i] = cCode[i]; // Assign sequence to variable
iCorrect=0; // Reset value of iCorrect
for (i=0; i<5; i++ ) // While loop to decide whether or not the user got it right
{
if ( cGuess[i] == cSequence[i] ) // If a guess is the same as the sequence add one to iCorrect
iCorrect++;
}
if ( iCorrect == 5 )
{
cGuessRight = 'y';
cout << "\nCongratulations! You found to secret code. You finished it in " << x << " guesses.\n\n";
}
else
cout << "\nYour 'Click Listener' seems to have activated " << iCorrect << " times with that guess.\n\n";
}
if ( cGuessRight=='n' ) // Player failed
cout << "\n\nYou have failed to find the code in time and have been caught by the guards. Better luck next time.\n\n";
else // If player guess correctly, check to see if they qualify for the leader board.
{
if ( i < iFirstScore ) // First place
{
cout << "Your speed in cracking the code makes you 1st in the leader board!\nEnter initials: ";
cin >> sFirst;
iFirstScore = i;
hiscore(); // Display hiscore
}
else if ( i < iSecondScore ) // Second place
{
cout << "Your speed in cracking the code makes you 2nd in the leader board!\nEnter initials: ";
cin >> sSecond;
iSecondScore = i;
hiscore(); // Display hiscore
}
else if ( i < iThirdScore ) // Third place
{
cout << "Your speed in cracking the code makes you 3rd in the leader board! \nEnter initials: ";
cin >> sThird;
iThirdScore = i;
hiscore(); // Display hiscore
}
}
return 0;
}
void codeentry()
{
int trigger=1;
do
{
trigger=1;
cin >> cCode; // Enter code
for (int i=0; i<5; i++)
{
if ( ( ( cCode[i] < '0' || cCode[i] > '9' ) ) || ( cCode[5] != '\0' ) ) // Check each entry is a single digit
{
cout << "\nError with input, try again\n\n Input: ";
trigger=0;
break; // Force a break if not
}
}
} while (trigger != 1); // Repeat until all entries are one digit
}
int hiscore() // Function to show hiscore
{
System("cls"); // Clear the screen
cout << "Position\t\tPlayer Name\t\tHi-Score\n" << endl;
if ( iFirstScore < 11 )
cout << "1.\t\t\t" << sFirst << "\t\t\t" << iFirstScore << endl;
if ( iSecondScore < 11 )
cout << "2.\t\t\t" << sSecond << "\t\t\t" << iSecondScore << endl;
if ( iThirdScore < 11 )
cout << "3.\t\t\t" << sThird << "\t\t\t" << iThirdScore << endl;
char cDelete='n';
cout << "\n\nDo you wish to reset hi-scores?\n\n Reset? (y/n) ";
cin >> cDelete;
if ( cDelete == 'y' )
{
iFirstScore = iSecondScore = iThirdScore = 11;
cout << "\nScores deleted.";
getchar();
}
return 0;
}