i am having a lot of problems trying to create a Mastermind game. The goal is to implement it and have it print out a game board on the screen that has the previous answer move up as new ones are entered. Also its supposed to keep track of whether you got the colors and position right on the right side of board. Im having problems just implementing the board itself. I cant get it to print out. I know i have to set up my class correctly but im not sure how to go about doing that. Please help. I think i may need to do a constructor for it but since its not actually doing anything but printing out with the users guesses im not sure where to go with it.
#include <iostream>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
using namespace std;
void promptUser(char guess[], int a );
bool endGame(char guess[], char answer[]);
char setColor(int num);
/*class drawBoard
{
public:
drawBoard();
int getgesses();
int (&getarray())[4];
}*/
int main()
{
char guess[4];
char answer[4];
bool playAgain = true;
bool success;
bool win;
int guesses;
char vote;
while(playAgain)
{
success = false;
guesses = 8;
cout << endl;
srand((unsigned int) time(0));
for (int x = 1; x < 5; x++)
{
int hold = (rand()% 100) + 1;
answer[x] = setColor(hold);
}
cout << endl;
while (!success && guesses > 0)
{
promptUser(guess, guesses);
success = endGame(guess, answer);
guesses--;
}
if(success)
{
cout << "You guessed the correct answer" << endl;
cout << "The correct answer was: " << answer[0] << answer[1] << answer[2] << answer[3];
}
else
{
cout << "Sorry!!! The correct answers were: " << answer[0] << answer[1] << answer[2] << answer[3];
}
cout << "Play agains? (Y or N): ";
cin >> vote;
playAgain = (vote == 'Y' || vote = 'y' ) ? true : false;
}
return 0;
}
void promptUser(char guess[], int a )
{
cout << "Please enter guess number " << a << " : ";
cin >> guess[0] >> guess[1] >> guess[2] >> guess[3];
for (int i = 0; i < 4; i++)
{
guess[i] = guess[i].toUpper;
}
}
char setColor(int num)
{
if (num > 0 && num < 18)
{
return static_cast<char>(82);
}
else if ( num >= 18 && num < 35)
{
return static_cast<char>(79);
}
else if ( num >= 35 && num < 52)
{
return static_cast<char>(89);
}
else if ( num >= 52 && num < 69)
{
return static_cast<char>(71);
}
else if ( num >= 69 && num < 86)
{
return static_cast<char>(66);
}
else if ( num >= 86 && num <= 100)
{
return static_cast<char>(86);
}
}
bool endGame(char guess[], char answer[])
{
int totalCorrect = 0;
int correctColor = 0;
bool status;
if(guess[0] == answer[1] || guess [0] == answer [2] || guess [0] == answer[3])
{
// insert an x
totalCorrect++;
}
else
{
//insert a dash
}
if(guess[1] == answer[0] || guess [1] == answer [2] || guess [1] == answer[3])
{
// insert an x
totalCorrect++;
}
else
{
//insert a dash
}
if(guess[2] == answer[1] || guess [2] == answer [0] || guess [2] == answer[3])
{
// insert an x
totalCorrect++;
}
else
{
//insert a dash
}
if(guess[3] == answer[1] || guess [3] == answer [2] || guess [3] == answer[0])
{
// insert an x
totalCorrect++;
}
else
{
//insert a dash
}
if(guess[0] == answer[0])
{
//insert an o
correctColor++;
}
else
{
//insert a dash
}
if(guess[1] == answer[1])
{
//insert an o
correctColor++;
}
else
{
//insert a dash
}
if(guess[2] == answer[2])
{
//insert an o
correctColor++;
}
else
{
//insert a dash
}
if(guess[3] == answer[3])
{
//insert an o
correctColor++;
}
if (correctColor = 4 && totalCorrect = 4)
{
status = true;
}
else
status = false;
return status;
}
class drawBoard(guess[4], guesses)
{
if (guesses = 0)
{
string a = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "5 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "4 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "3 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "2 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "1 | | | | | ----";
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
if (guesses = 1)
{
string a = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "5 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "4 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "3 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "2 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 2)
{
string b = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "5 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "4 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "3 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 3)
{
string c = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "5 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "4 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 4)
{
string d = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "5 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << d;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 5)
{
string e = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "6 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << d;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << e;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 6)
{
string f = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "7 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << d;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << e;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << f;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 7)
{
string g = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << "8 | | | | | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << d;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << e;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << f;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << g;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}
else if (guesses = 8)
{
string h = "1 |" << guess[0] << " |" << guess[1] << " |" << guess[2] << " |" << guess[3] << " | ----";
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << a;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << b;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << c;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << d;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << e;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << f;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << g;
cout << " +xxxxx+xxxxx+xxxxx+xxxxx+ ";
cout << h;
cout << "Guess by typing the letters for the colors you want.";
cout << "R:RED O:ORANGE Y:YELLOW G:GREEN B:BLUE V:VIOLET";
}