game

can anyone tell how to fix this program. my prblem is when iclick y in you want to play again the question/word repeat and when it reach the max of 10 you can not play anymore..
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <cstdlib>
#include <windows.h>
using namespace std;

char highDiff(), lowDiff();
int checkNum(), toupper();
void checkAnswer();
void askGuess();
void askToPlay();

char temp, answ;
char diff;
char choices[12] = {'A', 'B', 'C', 'D', 'E', 'G', 'L', 'M', 'P', 'R', 'S', 'T'};
char randomGenerated[4];
char userInput[4];

int choicesFlags[12];
int difficultyFlag = 0;
int gameFlag = 0;
int correctInPlace = 0;
int correctLetter = 0;
int trs = 0;
int checkFlag = 0;
int i, j, x, y, length, position, num;

main(){

system("cls");
cout<<"Welcome to the Mastermind Game!\n";
puts("");
cout<<"I got a secret code.\n";
cout<<"\nCHOICE: 'A', 'B', 'C', 'D', 'E', 'G', 'L', 'M', 'P', 'R', 'S', 'T' \n";


for(i = 0; i < 12; i++) {
choicesFlags[i] = 0;
}

srand(time(NULL));
for(i = 0; i < 4; i++) {
position = rand() % 12;
if(choicesFlags[position] == 0) {
randomGenerated[i] = choices[position];
choicesFlags[position] = 1;
} else {
i--;
}
}

askGuess();

getch();
return 0;
}

void askGuess(){

for(i = 0; i < 12; i++) {
choicesFlags[i] = 0;
}

while(gameFlag == 0) {
//printf("%s\n", randomGenerated);

checkFlag = 0;
while(checkFlag == 0 || checkFlag == 1 || checkFlag == 2) {
checkFlag = 3;
//trs++;
while(trs<10){
trs++;
cout<<"Enter guess "<<trs<<":";
cin>>userInput;
checkNum();
if (checkFlag == 2)
askGuess();
else (difficultyFlag == 1);
lowDiff();


}
}
randomGenerated[i] = 0;
cout<<"the secret code is "<<randomGenerated<<"\n";
cout<<"Sorry! You have exceeded the maximum number of tries\n";
cout<<"Thank you for playing!";
askToPlay();
}

}



int checkNum(){
length = strlen(userInput); // strlen - counts the num of char on a string

if(length != 4) {
cout<<"Input is not 4 characters\n";
cout<<"\n";
checkFlag = 2;
trs--;
return checkFlag;
}
else checkAnswer();
}

void checkAnswer(){

for(i = 0; i < 4; i++)
switch(toupper(userInput[i])) {
case 'A': choicesFlags[0]++; break;
case 'B': choicesFlags[1]++; break;
case 'C': choicesFlags[2]++; break;
case 'D': choicesFlags[3]++; break;
case 'E': choicesFlags[4]++; break;
case 'G': choicesFlags[5]++; break;
case 'L': choicesFlags[6]++; break;
case 'M': choicesFlags[7]++; break;
case 'P': choicesFlags[8]++; break;
case 'R': choicesFlags[9]++; break;
case 'S': choicesFlags[10]++; break;
case 'T': choicesFlags[11]++; break;
default: cout<<"Invalid Animal!\n\n"; trs--; askGuess(); break;
}
for(i = 0; i < 12; i++) {
if(choicesFlags[i] > 1) {
checkFlag = 0;
}
choicesFlags[i] = 0;
}
if(checkFlag == 0) {
cout<<"Invalid input: Repeated Animal\n\n";
trs--;
askGuess();
//trs--;
}
}


char lowDiff(){

char code[24]=" ";

for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(randomGenerated[i] == toupper(userInput[j]))
correctLetter++;
}
switch(toupper(userInput[i])) { //strcat - inserts a copy of the source string to the destination string
case 'A': strcat(code, "Ant"); break;
case 'B': strcat(code, "Bear"); break;
case 'C': strcat(code, "Cat"); break;
case 'D': strcat(code, "Dog"); break;
case 'E': strcat(code, "Emu"); break;
case 'G': strcat(code, "Goat"); break;
case 'L': strcat(code, "Lizard"); break;
case 'M': strcat(code, "Monkey"); break;
case 'P': strcat(code, "Parrot"); break;
case 'R': strcat(code, "Rat"); break;
case 'S': strcat(code, "Snake"); break;
case 'T': strcat(code, "Tiger"); break;
}
if (i>=0 && i<=2)
strcat(code, ", ");
else strcat(code, "; ");
}

printf("Your guess is%s", code);
if (correctLetter == 4){
cout<<"\nCongratulations, you win! You got the code in "<<trs<<"guess(es)\n";
askToPlay();
}
else {
cout<<"Your score is "<<correctLetter<<"\n\n";
correctLetter = 0;
}
}

void askToPlay(){
do {
cout<<"\nDo you want to play again? [Y]/[N] \n\n\t\t\t [B]ack to Main Menu "<<endl;
cin>>answ;
if ((answ=='y')||(answ=='Y'))
{
cout<<"\t\t\tWelcome back!"<<endl;
}
else if ((answ=='b')||(answ=='B'))
{
main(); askGuess(); checkAnswer(); askToPlay();
}
else if ((answ=='n')||(answ=='N'))
{
cout<<"\nThank You for Playing!!!"<<endl; exit(EXIT_FAILURE);
}
else
{
cout<<"Invalid Input!!!"; askToPlay();
}
}while((answ !='Y')&&(answ !='N')&&(answ !='y')&&(answ !='n')&&(answ !='b')&&(answ!='B'));
}
can you clarify on your question please? This game runs Great
Last edited on
Topic archived. No new replies allowed.