C++ I\'m having a problem with this C++ program I\'m working on. Here, I\'m work
ID: 641129 • Letter: C
Question
C++
I'm having a problem with this C++ program I'm working on. Here, I'm working on a variation of the Mastermind game that only uses numbers instead of colors.
The game also allows an infinite number of tries, but will output your score.
I'm having a hard time with the game itself. Here's my code. Please help!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Player
{
string name;
int highScores[6];
bool cheatOn;
};
struct Board
{
int numHoles;
int holes;
};
struct Guess
{
int count;
int numHoles;
int holes;
};
void printHighScores(Player);
void readHighScores(Player);
void updateHighScore(Player,Guess);
void getPlayer();
void getBoard();
void playGame(Player);
void menu(Player);
int main()
{
cout<<"Welcome to Mastermind."<<endl;
getPlayer();
system("PAUSE");
return 0;
}
void getPlayer(Player playerOne)
{
cout<<"Please enter your name. "<<endl;
cin>>playerOne.name;
menu(playerOne);
}
void menu(Player playerOne)
{
char choice;
cout<< playerOne.name << " ,Choose and option "<<endl;
cout<<"(P)lay"<<endl;
cout<<"(Q)uit"<<endl;
cout<<"Choice: "<<endl;
cin>>choice;
switch (choice)
{
case 'q':
case 'Q': cout<<"end"<<endl;
break;
case 'p':
case 'P': playGame(playerOne);
default: cout<<"not a valid option - try again."<<endl;
break;
}
}
void playGame(Player playerOne)
{
getBoard();
}
void getBoard()
{
//will create the game "board" but how do I do it?
}
void printHighScores(Player playerOne)
{
//This will print high score
}
void getHighScores(Player playerOne)
{
//This will retrieve high score
}
Explanation / Answer
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
// Declare variable numbers to store four integers for the random output
// Declare variable numbers to store four integers for the user input
int input[4],number[4];
int counter = 1;
int counter2;
int roundN = 1;
int rightnumber = 0;
int rightpostion = 0;
int wrongnumber = 0;
// Initialize random seed
srand( (unsigned int)time(NULL) );
// Generate four random numbers between 1 to 6
// and the four outputs are all distinct and in valid range
// because of the SWITCH
// if the numbers are the same then it will return to the FOR loop and SWITCH
// and get the new random number from the beginning
for(counter>0; counter<=4; counter++)
switch(counter)
{
case 1:
number[0] = rand() % 6 + 1;
break;
case 2:
number[1] = rand() % 6 + 1;
if (number[1] == number[0]){
counter=1;
continue;}
case 3:
number[2] = rand() % 6 + 1;
if (number[2] == number[0] || number[2] == number[1]){
counter=2;
continue;}
case 4:
number[3] = rand() % 6 + 1;
if (number[3] == number[0] || number[3] == number[1] || number[3] == number[2]){
counter=3;
continue;}
}
// Display the random four numbers
cout << "MasterMind (For checking: " << number[0] << " " << number[1] << " " << number[2] << " " << number[3] <<")" <<endl;
cout << "Enter four digits (1-6) separated by a space" <<endl;
cout << "--------------------------------------------" <<endl;
do
{
for(counter2=1; counter2<=1; counter2++){
cout << "Round "<< roundN <<":"<<endl;
roundN++;
cout << "Enter Guess: ";
cin >> input[0];
cin >> input[1];
cin >> input[2];
cin >> input[3];
if (input[0] == input[1] || input[0] == input[2] || input[0] == input[3] ||
input[1] == input[2] || input[1] == input[3] || input[2] == input[3])
{
counter2 = 0;
continue;
}
else if (input[0] > 6 || input[0] < 1 || input[1] > 6 || input[1] < 1 || input[2] > 6 || input[2] < 1 || input[3] > 6 || input[3] < 1 )
{
counter2 = 0;
continue;
}
else
break;
}
{for (int i = 0; i < 4; i++){
if (input[0] == number[0]){
rightnumber ++;
rightpostion ++;}
if (input[1] == number[1]){
rightnumber ++;
rightpostion ++;}
if (input[2] == number[2]){
rightnumber ++;
rightpostion ++;}
if (input[3] == number[3]){
rightnumber ++;
rightpostion ++;}
if ((input[0] == number[1]) || (input[0] == number[2]) || (input[0] == number[3]))
rightnumber ++;
else if ((input[1] == number[0]) || (input[1] == number[2]) || (input[1] == number[3]))
rightnumber ++;
else if ((input[2] == number[1]) || (input[2] == number[0]) || (input[2] == number[3]))
rightnumber ++;
else if ((input[3] == number[1]) || (input[3] == number[2]) || (input[3] == number[0]))
rightnumber ++;
if(rightnumber == 4 && rightpostion == 4)
cout << "O O O O" << endl;
else if(rightnumber == 4 && rightpostion == 2 )
cout << "O O # #" << endl;
else if(rightnumber == 4 && rightpostion == 1 )
cout << "O # # #" << endl;
else if(rightnumber == 4 && rightpostion == 0 )
cout << "# # # #" << endl;
else if(rightnumber == 3 && rightpostion == 3 )
cout << "O O O " << endl;
else if(rightnumber == 3 && rightpostion == 2 )
cout << "O O # " << endl;
else if(rightnumber == 3 && rightpostion == 1 )
cout << "O # # " << endl;
else if(rightnumber == 3 && rightpostion == 0 )
cout << "# # # " << endl;
else if(rightnumber == 2 && rightpostion == 2 )
cout << "O O " << endl;
else if(rightnumber == 2 && rightpostion == 1 )
cout << "O # " << endl;
else if(rightnumber == 2 && rightpostion == 0 )
cout << "# # " << endl;
else if(rightnumber == 1 && rightpostion == 1 )
cout << "O " << endl;
else if(rightnumber == 1 && rightpostion == 1 )
cout << "# " << endl;
else if(rightnumber == 0 && rightpostion == 0 )
cout << " " << endl;
cout << "--------------------------------------------" <<endl;
}
}
}while((number[0] == input[0] && number[1] == input[1] && number[2] == input[2] && number[3] == input[3])==false);
cout << "--------------------------------------------" << endl;
cout << "Congratulations! You win the game in " << roundN - 1<<"steps" << endl;
// Hold the command window
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.