0

//**numHits and numHalfHits give incorrect values.I don't know why**

```
#include "Mastermind-game.hpp"
using namespace std;
int main()
{
bool choice = false;
cout << "This is a game of MASTERMIND v.0.1" << endl;
while(1)
{
cout << "Enter 1 if you want to solve game for your self" << endl;
cout << "or 2 if you want the computer to solve the game: " ;
cin >> choice;
if(choice){
player();
}
else{
computer();
}
}
return 0;
}
void player()
{
boardSetup();
unsigned int x;
cout << "Enter number of attempts: " ;
cin >> x;
bool hasSolution = false;
for(unsigned int i=0; i<x; i++){
cout << "Enter your " << i+1 << "/" << x << " attempt: (one number at the time)" << endl;
for(unsigned int k=0; k<ROW_LENGTH; k++){
cin >> userSolution[k];
}
hasSolution = checkUserAnswer();
if(hasSolution){
i = x+1;
}
else{
cout << "number of hits:" << numHits << ", number of hits not in place:" << numHalfHits << endl;
numHits = 0;
numHalfHits = 0;
}
}
if(hasSolution)
{
cout << "that is the correct solution!!!" << endl;
}
else{
cout << "you have no more attempts remaining :(" << endl;
}
system("pause");
system("cls");
}
void boardSetup()
{
cout << "\n" ;
srand (time(NULL));
for(unsigned int i=0; i<ROW_LENGTH; i++){
correctAnswer[i] = rand()%NUMBER_OF_PIECES+1;
cout << correctAnswer[i] ;//TEMP,this line will be deleted when program works correctly.It gives correct answer
}
}
int checkUserAnswer()
{
bool hasSolution = false;
for(unsigned int i=0; i<ROW_LENGTH; i++){
if(correctAnswer[i] == userSolution[i]){
numHits++;
if(numHits == ROW_LENGTH){
hasSolution = true;//solution found
}
}
}
int alreadyCompared[ROW_LENGTH] = {0};
for(unsigned int j=0; j<ROW_LENGTH; j++){
for(unsigned int k=0; k<ROW_LENGTH; k++){
if(correctAnswer[k] == userSolution[j]){//check userSolution with every correctAnswer
if(k != j)//if not in right place
{
if(alreadyCompared[j] == 0)//if userSolution not already compared
{
alreadyCompared[j] = 1;
numHalfHits++;
}
}
}
}
}
return hasSolution;
}
int computer()//not done
{
int x, y;
cout << "Enter Length of the board:" << endl;
cin >> x;
cout << "Enter number of attempts:" << endl;
cin >> y;
cout << "Enter characters 1122 as solution:" << endl;
system("pause");
cout << "Enter number of hits in correct places:" << endl;
cin >> numHits;
cout << "Enter number of hits which are not in place:" << endl;
cin >> numHalfHits;
//not done
return 0;
}
```

//this is hpp file

```
#ifndef MASTERMIND-GAME_HPP_INCLUDED
#define MASTERMIND-GAME_HPP_INCLUDED
#include <iostream>
#include <cctype>
#include <string>
#include <cmath>
#include <ctime>
#include <stdlib.h>
using namespace std;
#define ROW_LENGTH 4
#define NUMBER_OF_PIECES 6
void player();
int computer();
void boardSetup();
int checkUserAnswer();
//public
int correctAnswer[ROW_LENGTH-1];
int userSolution[ROW_LENGTH-1];
int numHits = 0, numHalfHits = 0;
#endif // MASTERMIND-GAME_HPP_INCLUDED
```