/* 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 #include #include // library with the function rand #include // library with time in #include // library with clear screen in #include // library with string streams to save strings to file #include #define MAXNUM 10 // Constant for making a random value between 0 and 9 using namespace std; int iChoice; // Global choice for menu string sFirst = "None"; // Global name for first place string sSecond = "None"; // Global name for second place string sThird = "None"; // Global name for third place int iFirstScore = 11; // Score of first place int iSecondScore = 11; // Score of second place int iThirdScore = 11; // 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 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() { // Set up the data storage variables string data; ostringstream temp; ifstream fh ("scores.dat"); // Open the file // Check if the file opened successfully if (fh.is_open()) { // Read through the file and store the data in the temporary variable while (!fh.eof()) getline(fh, data); fh.close(); } else cout << "Couldn't open file: scores.dat" << endl; // Display error if file can't be opened int del = 1; int type = 0; // Iterate through each byte in the file for (int i=0; i 0) { sFirst = temp.str(); } type = 1; } // Score else { iFirstScore = atoi(temp.str().c_str()); type = 0; del++; } } else if (del == 2) { if (type == 0) { if (temp.str().size() > 0) { sSecond = temp.str(); } type = 1; } else { iSecondScore = atoi(temp.str().c_str()); type = 0; del++; } } else if (del == 3) { if (type == 0) { if (temp.str().size() > 0) { sThird = temp.str(); } type = 1; } else { iThirdScore = atoi(temp.str().c_str()); type = 0; del++; } } temp.str(""); // Erase the temp data } else temp << data[i]; // Store byte into the temp variable } } void save() { // Generate the data string ostringstream str; str << sFirst << char(9) << iFirstScore << char(9) << sSecond << char(9) << iSecondScore << char(9) << sThird << char(9) << iThirdScore; ofstream fh ("scores.dat"); // Open the file // Check if the file opened successfully if (fh.is_open()) { // Write to the file fh << str.str(); fh.close(); } else cout << "Couldn't open file: scores.dat" << endl; // Display error if file can't be opened } 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; sFirst = sSecond = sThird = "None"; cout << "\nScores deleted."; getchar(); } return 0; }