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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
//-------------Mastermind--------------
//Libraries
#include <iostream>
#include <cstdlib>
#include <ctime>
//No Global Constants
//Function Prototypes here
void displayInstructions();
void getInput (char [], char [], int &, char [][4], int, int [], int [], int);
char colorconversion (int);
int numCorrect (char [], char []);
int wrongSpot (char [], char [], int);
//Execution begins here
using namespace std;
int main(int argc, char *argv[]) {
//Declare Variables
int randNum1, randNum2, randNum3, randNum4; //Variable to convert input to integer
char correct[4], guess[4]; //Variable for numbers/colors generated
char again; //Input by user to play again
int numguess=0, guessleft=10; //Number of user guesses
int wrongS=0, totright=0; //Variable to display the users right guesses and right colors but in wrong spot
char history[10][4];
int counter=0, hCorrect[10], hWrongS[10];
do{
displayInstructions(); //Function call to display instructions and begin program.
srand(time(0)); //RNG function to generate the correct sequence
randNum1=rand()%5+1; //Range the random numbers 1-5
randNum2=rand()%5+1;
randNum3=rand()%5+1;
randNum4=rand()%5+1;
//Get the Correct Sequence
correct[0]= colorconversion(randNum1);
correct[1]= colorconversion(randNum2);
correct[2]= colorconversion(randNum3);
correct[3]= colorconversion(randNum4);
do{
getInput(guess, correct, guessleft, history, counter, hCorrect, hWrongS, totright);
//Break out if user wins
if (guess[0]==correct[0] && guess[1]==correct[1] && guess[2]==correct[2] && guess[3]==correct[3]) {
numguess=10;
}
numguess++; //Counter for user guesses
counter++;
} while (numguess<10); //Limit user guesses to 10
//Determine if user wins
if (guess[0]==correct[0] && guess[1]==correct[1] && guess[2]==correct[2] && guess[3]==correct[3]) {
cout<<"Congratulations you win!";
} else {
//If user loses output correct sequence
cout<<"Sorry try again. Correct sequence was "<<correct[0]<<" "<<correct[1]<<
" "<<correct[2]<<" "<<correct[3]<<" "<<endl;
}
//Prompt user to play again
cout<<"GOOD GAME"<<endl;
cin>>again;
//If user wants to play again reset the guesses counter to zero
if (again=='Y' || again=='y'){
numguess=0;
guessleft=10;
}
} while (again=='Y' || again=='y');
return 0;
}
//Function to display instructions
void displayInstructions () {
cout<<"I will generate 4 random colors that can each be"
<<" 1 of 5 colors allowing duplicates."<<endl;
cout<<"You will get 10 guesses of the colors and I will tell you after\n"
"each guess the number of colors that are correct and in the right position"<<endl;
cout<<"You have 5 colors to pick from: Green Yellow Red Blue Purple"<<endl;
cout<<"enter first letter of the color so it could be gyrb = green yellow red blue =D have fun"<<endl;
}
//Function to get users guesses
void getInput (char guess[], char correct[], int &guessleft, char history[][4], int counter, int hCorrect [], int hWrongS[], int totright) {
cout<<"Good luck"<<endl; //Output for guesses
for(int i =0;i<4;i++){
cin >> guess[i];
if (guess[i]>91)
guess[i]= guess[i]-32;
history[counter][i]=guess[i];
}
hCorrect[counter]=numCorrect(guess, correct);
hWrongS[counter]=wrongSpot(guess, correct, hCorrect[counter]);
for (int i=0; i<counter+1; i++){
cout<<"History "<<i+1 << " ";
for(int j =0;j<4;j++){
cout << history[i][j];
}
cout<<" Correct= "<<hCorrect[i]<<" Wrong spot= "<<hWrongS[i];
cout<<endl;
}
guessleft--;
cout<<endl;
cout<<"Guesses left "<<guessleft<<endl;
cout<<endl;
if (guessleft==3)
cout<<"Warning 3 more tries!!!"<<endl;
if(guessleft==2)
cout<<"Warning two tries left"<<endl;
if(guessleft==1)
cout<<"Last chance, choose wisely!"<<endl;
}
//Convert random number to a character representing a color
char colorconversion (int passNum) {
switch (passNum) {
case 1:
return 'G';
break;
case 2:
return 'Y';
break;
case 3:
return 'R';
break;
case 4:
return 'B';
break;
case 5:
return 'P';
break;
}
}
//Function to determine how many colors were correct and in the right spot.
int numCorrect (char guess[], char correct[]){
int totRight=0;
if (guess[0]==correct[0]) //Determine if 1st color guess is right
totRight++; //If true add 1 to counter
if (guess[1]==correct[1]) //Determine if 2nd color guess is right
totRight++; //If true add 1 to counter
if (guess[2]==correct[2]) //Determine if 3rd color guess is right
totRight++; //If true add 1 to counter
if (guess[3]==correct[3]) //Determine if 4th color guess is right
totRight++; //If true add 1 to counter
return totRight; //Return the number right
}
int wrongSpot(char correct[], char guess[],int totright){
int greenC=0;
int sum=0, keepG, keepY, keepR, keepB, keepP;
for(int i=0; i<4; i++)
if (correct[i]=='G'){
greenC++;
}
int green=0;
for(int i=0; i<4; i++)
if (guess[i]=='G'){
green++;
}
if (greenC<=green)
keepG=greenC;
else keepG=green;
int yellowC=0;
for(int i=0; i<4; i++)
if (correct[i]=='Y'){
yellowC++;
}
int yellow=0;
for(int i=0; i<4; i++)
if (guess[i]=='Y'){
yellow++;
}
if (yellowC<=yellow)
keepY= yellowC;
else keepY= yellow;
int redC=0;
for(int i=0; i<4; i++)
if (correct[i]=='R'){
redC++;
}
int red=0;
for(int i=0; i<4; i++)
if (guess[i]=='R'){
red++;
}
if (redC<=red)
keepR= redC;
else keepR= red;
int blueC=0;
for(int i=0; i<4; i++)
if (correct[i]=='B'){
blueC++;
}
int blue=0;
for(int i=0; i<4; i++)
if (guess[i]=='B'){
blue++;
}
if (blueC<=blue)
keepB= blueC;
else keepB= blue;
int purpleC=0;
for(int i=0; i<4; i++)
if (correct[i]=='P'){
purpleC++;
}
int purple=0;
for(int i=0; i<4; i++)
if (guess[i]=='P'){
purple++;
}
if (purpleC<=purple)
keepP= purpleC;
else keepP= purple;
sum=keepG+keepY+keepR+keepB+keepP;
return sum-totright;
}
|