Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int setCode(void);
char getGuess(void);
void gameOver(void);
void title(void);
void gameDirections(void);
int exactMatches(char x, char y, char a, char b, char c, char d,
char e, char f);
int inexactMatches(char x, char y, char a, char b, char c, char d,
char e, char f);
void printSequence(char c1, char c2, char c3, char c4);
int num, exact_count, inexact_counter, counter, count, code;
int X_exact_count,A_exact_count,C_exact_count,E_exact_count;
char code1, code2, code3, code4, color, entered_code1;
char entered_code2, entered_code3, entered_code4, guess;
int main(void)
{
counter = 0;
title();
gameDirections();
system("PAUSE");
system("CLS");
srand((long)time(NULL));
code1 = setCode();
code2 = setCode();
code3 = setCode();
code4 = setCode();
printSequence(code1, code2, code3, code4);
do{
do{
cout << "Please enter color sequence: ";
entered_code1 = getGuess();
entered_code2 = getGuess();
entered_code3 = getGuess();
entered_code4 = getGuess();
code=0;
if(entered_code1=='W'||entered_code1=='R'||entered_code1=='B'
||entered_code1=='G'||entered_code1=='O'||entered_code1=='Y')
code++;
if(entered_code2=='R'||entered_code2=='W'||entered_code2=='B'
||entered_code2=='G'||entered_code2=='O'||entered_code2=='Y')
code++;
if(entered_code3=='Y'||entered_code3=='O'||entered_code3=='G'
||entered_code3=='B'||entered_code3=='R'||entered_code3=='W')
code++;
if(entered_code4=='W'||entered_code4=='R'||entered_code4=='B'
||entered_code4=='G'||entered_code4=='O'||entered_code4=='Y')
code++;
}while(code!=4);
exactMatches(code1, entered_code1, code2, entered_code2,
code3, entered_code3, code4, entered_code4);
inexactMatches(code1, entered_code1, code2, entered_code2,
code3, entered_code3, code4, entered_code4);
if(exact_count==4)
{
gameOver();
return 0;
}
counter++;
}while(counter<8);
gameOver();
return 0;
}
int setCode(void)
{
num = 1+rand()%6;
if(num==1)
color = 'W';
else if(num==2)
color = 'R';
else if(num==3)
color = 'B';
else if(num==4)
color = 'G';
else if(num==5)
color = 'O';
else if(num==6)
color = 'Y';
return (color);
}
char getGuess(void)
{
cin >> guess;
return (guess);
}
int exactMatches(char x, char y, char a, char b, char c, char d,
char e, char f)
{
X_exact_count = 0,A_exact_count = 0,C_exact_count = 0,E_exact_count = 0;
if(x==y)
X_exact_count++;
if(a==b)
A_exact_count++;
if(c==d)
C_exact_count++;
if(e==f)
E_exact_count++;
exact_count=X_exact_count+A_exact_count+C_exact_count+E_exact_count;
cout << exact_count << " exact matche(s)\n";
return (exact_count,X_exact_count,A_exact_count,C_exact_count,E_exact_count);
}
int inexactMatches(char x, char y, char a, char b, char c, char d,
char e, char f)
{
int Y_count=0, B_count=0, D_count=0, F_count=0;
if(y==a||y==c||y==e&&y!=x)
Y_count++;
if(b==x||b==c||b==e&&b!=a)
B_count++;
if(d==a||d==x||d==e&&d!=c)
D_count++;
if(f==a||f==c||f==x&&f!=e)
F_count++;
if(Y_count>1)
Y_count=1;
if(X_exact_count==1)
Y_count=0;
if(B_count>1)
B_count=1;
if(A_exact_count==1)
B_count=0;
if(D_count>1)
D_count=1;
if(C_exact_count==1)
D_count=0;
if(F_count>1)
F_count=1;
if(E_exact_count==1)
F_count=0;
count =Y_count+B_count+D_count+F_count;
cout << count << " inexact matche(s)\n";
return (count);
}
void printSequence(char c1, char c2, char c3, char c4)
{
cout << c1 << " "<< c2 << " " << c3<< " " << c4;
return;
}
void gameOver(void)
{
system("CLS");
title();
cout << "Thanks for playing.\n";
cout << "The correct sequence was ";
printSequence(code1, code2, code3, code4);
cout << ". "<<endl;
return;
}
void title(void)
{
cout << "()()()()()()()()()()()()()()()\n";
cout << "()()()()()MASTERMI";
cout << "ND()()()()()\n";
cout << "()()()()()()()()()()()()()()()\n\n";
return;
}
void gameDirections(void)
{
cout << "Directions: A random code of four color pegs will\n";
cout << "be generate by the computer. Each peg is chosen\n";
cout << "from one of six colored pegs: White (W), Red (R),\n";
cout << "Blue (B), Green (G), Orange (O), and Yellow (Y).\n";
cout << "A player may choose duplicate colors when creating\n";
cout << "his/her sequence. The goal of the game is to guess\n";
cout << "the random code generate by the computer in 8 or\n";
cout << "less guesses. If the player cannot accomplish this\n";
cout << "task, the game will end, and the code will be shown.\n";
cout << "You may only use W, R, B, G, O, or Y when entering\n";
cout << "your selection.\n\n\n";
return;
}
its working so far. The inexact matches are still somewhat messed up.