Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------
- * Program 2: Mastermind Game
- *
- * Class: CS 141, Summer 2019
- * System: Windows 10, Codio.com
- * Author: Travis Lee
- *
- * --------------------------------------------------------
- */
- #include <iostream>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- int main()
- {
- //Declare Variables
- int origValue[3];
- int userInput;
- int inpArray[3];
- int turnCap = 11;
- int correctCount = 0;
- int wrongCount = 0;
- int zeroCount = 0;
- int nineCount = 0;
- //Print out ID information (class, author)
- cout << "Class: CS 141, Summer 2019" << endl;
- cout << "Author: Travis Lee" << endl;
- cout << endl;
- // Print out the program instructions and name of program
- cout << "Program 2: Mastermind" << endl;
- cout << "The program selects 3 distinct random digits 0..9." << endl;
- cout << "On each turn you guess 3 digits. The program indicates" << endl;
- cout << "how many are correct. You have 10 moves to guess the number." << endl;
- cout << "Good luck!" << endl;
- cout << endl;
- cout << "Input of 000 displays the hidden digits. Input of 999 exits the program" << endl;
- //choose 3 digits from 0..9 at random and store them
- for(int i = 0; i < 3; ++i)
- {
- origValue[i] = rand() % 10;
- }
- //Display top of "Board"
- cout << "\t\t\tIn place Out of place" << endl;
- cout << "\t\t\t-------- --------" << endl;
- //Main loop to run through the 10 turns
- for(int i = 1; i < turnCap; ++i)
- {
- cout << i << ". Your guess: ";
- //Take in the user's input
- cin >> userInput;
- //splits up the input and puts them into an array
- inpArray[0] = (userInput / 100) % 10;
- inpArray[1] = (userInput / 10) % 10;
- inpArray[2] = userInput % 10;
- cout << "\tYou entered: ";
- //Print user's input and check compares original value
- //with input value increments Count variables based off of result
- for(int c = 0; c < 3; ++c)
- {
- cout << inpArray[c];
- if(inpArray[c] == 9)
- {
- nineCount = nineCount + 1;
- }
- if(inpArray[c] == 0)
- {
- zeroCount = zeroCount + 1;
- }
- if(inpArray[c] == origValue[c])
- {
- correctCount = correctCount + 1;
- }
- else if(inpArray[0] == origValue[c])
- {
- wrongCount = wrongCount +1;
- }
- else if(inpArray[1] == origValue[c])
- {
- wrongCount = wrongCount +1;
- }
- else if(inpArray[2] == origValue[c])
- {
- wrongCount = wrongCount +1;
- }
- }
- //Exits program if user enters 999
- if(nineCount == 3)
- {
- cout << endl;
- cout << "Exiting program..." << endl;
- exit(0);
- }
- //Prints the in place and out of place spots
- cout << " ";
- cout << correctCount << "\t\t";
- cout << wrongCount << endl;
- //Exit the program and print out exiting statements if user guessed correctly
- if(correctCount == 3)
- {
- cout << endl;
- cout << "*** Congratulations! ***" << endl;
- cout << "Exiting program..." << endl;
- exit(0);
- }
- //Resets correctCount and wrongCount
- if(zeroCount == 3)
- {
- cout << "\t\t\t Hidden digits: ";
- for(int i = 0; i < 3; ++i)
- {
- cout << origValue[i];
- }
- cout << endl;
- }
- correctCount = 0;
- wrongCount = 0;
- //Resets nineCount so the program only ends when user
- //types 3 nines at the same time
- nineCount = 0;
- //Resets zeroCount so the program only prints hidden digits
- //when user types three zeros at the same time
- zeroCount = 0;
- }
- //Prints program exit after the 10 turns from the loop above
- cout << " Better luck next time." << endl;
- cout << endl;
- cout << "Exiting program..." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement