Member Avatar for 111100/11000

//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
Member Avatar for 111100/11000

i found that 2 arrays are created at the same memory address after adding this at line 6 of cpp file

    cout << "memory address of correctAnswer[3]: " << &correctAnswer[3] <<endl ;
    cout << "memory address of userSolution[0]: " << &userSolution[0] <<endl ;

but i don't know why that is?

In the hpp file you have:

#define ROW_LENGTH 4
int correctAnswer[ROW_LENGTH-1]; // correctAnswer[3]
int userSolution[ROW_LENGTH-1];  // userSolution[3]

Meaning that correctAnswer[3] is out of bounds for the array and therefore due to memory alignment has the same address as userSolution[0].

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.