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";


	}

Recommended Answers

All 2 Replies

#1) Forget the game board. Just work on input of the guess and output of the result. Isn't that the important part of the game?
#2) I don't see a class defined anywhere. That's probably why you can't get it correct.

My suggestion:
a) Write the class and code to choose the colors to be guessed. Add to the class just the information you need at this point (the initial colors). Do a simple output just to be sure you've got that correct. Once you do...
b) Add a function to accept the player's guess. Simple output of the guess and keep fixing until you get this part correct. Then...
c) Add a function to test for colors in the correct position. Again, simple output to see that this is correct. Keep adding to the class as you find things you need to keep track of.
d) Now, start working on the actual game output function (no difficult 'board', just a simple display so you can follow the game).

Keep adding a piece at a time, as functions, until you get it working. The 'game board' is one single function. Once the game is running, you can then massage the output to look like a board.

Oh, and read up on where the srand() function should go. It's not where you have it.

WaltP is giving good advice.

To start one good possibility is to be computer yourself and to do one game on paper.
This is called use case and it is one of my favorite ways to program.

Code this game as program with fixed value for correct row. Even you have fixed value you use variable with the fixed value or simplified function returning always same value or doing simple print instead of fancy output.

Use debug prints before and after new actions you are adding and put assert statements to verify your assumptions.

Every time you should have only one last addition to worry as source of bug. In real life some bugs escape recognition immediately, if they occur in rare "corner cases", but then you just go back few steps and fix that bug. Do not forget in that case test every test done before that your correction can affect as it is more common than you would guess that fixing bug introduce new bug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.