1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Mastermind Game
// Programmer: Edward Chen
// CMPSC 121
// Date: 10/08/2013
#include <iostream> // belongs to the beginning of every program
#include <math.h> // header
#include <ctime>
using namespace std;
void playMastermind() // play Mastermind function for the whole game
{
int rollDie ( int sides ); // declaring the functions used
void evaluate ( int answer, int combo );
bool play = true; // bool setting play to true
while ( play == true ) // resets while loop if play is equal to true
{
int digitOne; // initialize digit variables
int digitTwo;
int digitThree;
int digitFour;
digitOne = rollDie (6); // finding random values for the digits by calling the rollDie function
digitTwo = rollDie (6);
digitThree = rollDie (6);
digitFour = rollDie (6);
digitOne *= 1000; // finding the actual number with the digits combined
digitTwo *= 100;
digitThree *= 10;
digitFour *= 1;
int correct; // initializing the correct answer that the computer decides on
correct = digitOne + digitTwo + digitThree + digitFour; // the correct answer
int combo; // initializing the variable that the player guesses
int tries = 0; // initializing the number of tries to 0
bool answer = false; // bool for when the answer is false
while ( tries < 12 && answer == false ) // while loop asking for the player to guess a number
{
cout << "Please enter your 4-digit guess: " << endl; // asking the user to input his/her guess
cin >> combo; // storing the guess as combo
tries++; // adding up the tries for each time
evaluate ( correct, combo ); // letting people know how many pegs they got
}
if ( tries >= 12 ) // if-statement for when the user reaches his limit of tries
{
cout << "Sorry! You did not guess my number." << endl; // telling the user he is done
cout << "The number was " << correct << endl; // letting the user know the number
}
cout << "Play another game? (y/n)"; // asking the user if he/she wants to play again
char reply; // initializing the reply
cin >> reply; // storing the input
if ( reply == 'y' || reply == 'Y' ) // if-statement for when the user replies with a yes
{
play = true; // keeps the bool set to true
}
else // if anything else is input
{
play = false; // sets the bool to false so that the while loop does not repeat
}
}
}
int rollDie ( int sides ) // rollDie function to calculate the random digits
{
srand(time(NULL));
int randNum; // initializing the random numbers
randNum = 1 + rand() % sides; // calling a random number between 1 and 6
return randNum; // returning the value
}
int nthDigit ( int combo, int position ) // nthDigit function to find the digits in their respective positions
{
int digit; // initializing the digit variable
digit = ( combo ) / ( pow( 10, 4 - position )); // finding the digit based on the position
int value; // initializing the value variable
value = digit % 10; // finding the actual value of the digit
return value; // returning the value
}
void evaluate ( int answer, int combo ) // evaluate function to determine the number of correct digits guessed
{
int nthDigit ( int combo, int position ); // declaring the function to be used
int clearNthDigit ( int combo, int position );
int digitOne; // initializing variables for the digits
int digitTwo;
int digitThree;
int digitFour;
int pegsBlack = 0; // initializing the different colored pegs
int pegsWhite = 0;
digitOne = answer / ( pow( 10, 3 )); // finding each respective digit
digitOne = digitOne % 10;
digitTwo = answer / ( pow( 10, 2 ));
digitTwo = digitTwo % 10;
digitThree = answer / ( pow( 10, 1 ));
digitThree = digitThree % 10;
digitFour = answer / ( pow( 10, 0 ));
digitFour = digitFour % 10;
int i = 1; // initializing the counter
while ( i <= 4 ) // while statement acting until i is equal to 4
{
if ( nthDigit ( combo, i ) == nthDigit ( answer, i ) ) // if the one digit of the combo is equal to that of the answer
{
pegsBlack++; // add one black peg
clearNthDigit ( answer, i ); // function used to clear the digits that are correct
// clearNthDigit ( combo, i );
}
i++; // adds 1 to i each loop
}
int j = 1; // initializing variable
int k = 1;
while ( j < 4 ) // while-statement for the white pegs
{
while ( k <= 4 )
{
k++;
if ( nthDigit ( combo, j ) == nthDigit ( answer, k ))
{
pegsWhite++;
clearNthDigit ( answer, k );
}
}
j++;
}
/*
if ( nthDigit ( combo, 1 ) == digitTwo ) // otherwise giving white peg if in different position
{
pegsWhite++;
}
if ( nthDigit ( combo, 1 ) == digitThree )
{
pegsWhite++;
}
if ( nthDigit ( combo, 1 ) == digitFour )
{
pegsWhite++;
}
if ( nthDigit ( combo, 2 ) == digitOne )
{
pegsWhite++;
}
if ( nthDigit ( combo, 2 ) == digitThree )
{
pegsWhite++;
}
if ( nthDigit ( combo, 2 ) == digitFour )
{
pegsWhite++;
}
if ( nthDigit ( combo, 3 ) == digitOne )
{
pegsWhite++;
}
if ( nthDigit ( combo, 3 ) == digitTwo )
{
pegsWhite++;
}
if ( nthDigit ( combo, 3 ) == digitFour )
{
pegsWhite++;
}
if ( nthDigit ( combo, 4 ) == digitOne )
{
pegsWhite++;
}
if ( nthDigit ( combo, 4 ) == digitTwo )
{
pegsWhite++;
}
if ( nthDigit ( combo, 4 ) == digitThree )
{
pegsWhite++;
} */
cout << "You have earned " << pegsBlack << " black pegs and " << pegsWhite << " white pegs." << endl; // letting the user his number of pegs
if ( pegsBlack == 4 ) // if-statement for when the user reached 4 black pegs
{
cout << "Congratulations! You have guessed the correct combination!" << endl; // stating that he/she then wins
}
}
int clearNthDigit ( int combo, int position ) // clearNthDigit function to change the digit to 0
{
int nthDigit ( int combo, int position ); // declaring the function to be used
int value; // initializing the variable
value = nthDigit ( combo, position ) * ( pow ( 10, 4 - position ) ); // finding the value of the digit and then multiplying that by its position value
int zero; // initializing variable
zero = combo - value; // subtracting the previous value to "zero" that digit
return zero; // return the value
}
int main() // main function that runs the program
{
cout << "Welcome to Mastermind!" << endl; // introduction of the program
cout << "The computer will choose a 4-digit number." << endl;
cout << "Your objective is to guess that number!" << endl;
cout << "If you get one digit correct, but in the wrong position ... " << endl;
cout << "You will be given a white peg." << endl;
cout << "If you get one digit correct, and in the correct position ... " << endl;
cout << "You will be given a black peg." << endl;
playMastermind(); // function that actually starts the mastermind game
return 0; // ends the program
}
|