2 Replies - 622 Views - Last Post: 31 October 2013 - 03:27 PM Rate Topic: ***-- 2 Votes

#1 jshm415  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-October 13

Why is my code not running? C++

Posted 30 October 2013 - 04:12 PM

I am making a mastermind game that will have a user input a password or have the computer randomly generate it, it will then try to guess at the answer, and return how many tries it took. Below is my code up to this point. The problem is that my code would not run. What can I do to fix it. Thank you for your help.

#include <iostream>
using namespace std;

int generateAnswer();
void trim (int guesses[], int guess, int &length, int black, int white);
void evaluate( int guess, int answer, int &black, int &white);
int nthDigit( int combination, int position );			// identify a digit
void clearNthDigit( int &combination, int position );	// clear a digit
int methodicalEliminate (int answer);
char play;
char response;
int answer;
const int size = 4096;

int main() // Plays the game
{
	play = 'y';
	while (play == 'y')
	{
		int answer = generateAnswer();
		cout<<"It took methodicalEliminate "<< methodicalEliminate(answer)<<" tries";
		cout<<"Would you like to plasy Mastermind? ";
		cin >> play;
	}

return 0;
}

int generateAnswer() 
/*
Either lets the user at the keyboard choose the mystery combination,
or gives the option to have the computer generate a random combination.
*/

{ 
	cout<<"Do you want to create the mystery combination? Respond with y for yes or n for computer."<<endl;
    cin>>response;
    if(response=='y'){
        cout<<"Enter your combination.(4 digits ranging from 1-8.)"<<endl;
        cin>>answer;
    }
	else{
        answer=1000*(1+rand()%8)+100*(1+rand()%8)+10*(1+rand()%8)+1*(1+rand()%8);
        cout<<"The combination that is trying to be guessed is "<<answer;
    }
    return answer;
}

int generateSearchSpace(int guesses[], int& length)
/*
Populates an array with all possible combinations of four-digit
values in the range 1 to 8.
*/
{
	int a = 0;
	
		for (int b = 1; b <=8; b++)
			for (int c = 1; c <=8; c++)
				for (int d = 1; d <= 8; d++)
					for (int e = 1; e <=8; e++)
					{
					guesses[a] = 1000*b + 100*c + 10*d + 1*e;
					a++;
					}
		return length = a;
}

void trim (int guesses[], int guess, int &length, int black, int white)
/*
Trim function analyzes the response to a particular guess and then eliminates
any values from the list of possibilities that are no longer
possible answers.
*/
{
		int newLength = 0;
        int trimBlack = 0;
        int trimWhite = 0;
 
        for(int i = 0; i < length; i++)
        {
 
                evaluate(guesses[i], guess, trimBlack, trimWhite);
                if(trimBlack==black&&trimWhite==white)	// If they are matched, keep them because one of them could be the right answer.
				{
				guesses[newLength]=guesses[i];
				newLength++;
				
				}
		length=newLength;		
				
			
		}		
				
}				
				
int methodicalEliminate (int answer)
/*
beginning with a list of all possible candidate answers
continually guesses the first element in the list, and
trim answers accordingly, until an answer is found
*/
{
	int guesses[size];
    int length = 0;
    int black = 0;
    int white = 0;
    int count = 0;
    int currentGuess;
    generateSearchSpace(guesses, length);
    while(black< 4)
    {       
		currentGuess=guesses[0];
        evaluate(currentGuess,answer,black,white);
		for(int i = 0; i < length; i++)
        {
            trim(guesses, currentGuess, length, black, white);
            
        }
        count++;
    }
	return count; // returns how many times did this function take to guess the right answer.
}


void evaluate( int guess, int answer, int &black, int &white )
/*
evaluates a combination by comparing it with the answer
Correctness is indicated by black pegs (correct digit in correct position)
and white pegs (correct digit in incorrect position)
*/
{
	black = 0;			
	white = 0;
	for (int i=1; i<=4; i++)
		if (nthDigit( guess, i ) == nthDigit( answer, i ) )
		{
			black++;						
			clearNthDigit( guess, i );		
			clearNthDigit( answer, i );
		}
	for (int i=1; i<=4; i++)
		for (int j=1; j<=4 && nthDigit(guess,i)>0; j++)
											
			if (nthDigit( guess,i ) == nthDigit( answer, j ) )
			{
				white++;					
				clearNthDigit( guess, i );  
				clearNthDigit( answer, j );
			}
}
		
int nthDigit( int combination, int position )
/*
identified the n'th digit of a combination

*/

{
	int current;			
	while (position < 4)
	{
		combination /= 10;		
		position++;				
	}

	return (combination % 10);

}

void clearNthDigit( int &combination, int position )
/*
clears the n'th digit of a combination to zero, so it will no longer match
digits must be counted in the same manner as nthDigit above.
*/
{
	int current;			// position currently being examined
	int digitValue;			
	current = 4;			
	digitValue = 1;			
	while (position < current)
	{
		digitValue *= 10;	// previou digit has 10 times the value
		current--;			// digitValue refers to previous digit
	}
	// subtract the digit minus itself to make it into a zero digit
	combination -= digitValue * nthDigit( combination, position );
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my code not running? C++

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,664
  • Joined: 19-February 09

Re: Why is my code not running? C++

Posted 30 October 2013 - 05:44 PM

You can use cout statements to check how far the program is working. Also print out values to check they are correct. The functions could be tested individually to check they are correctly working.

Does the nthDigit function work correctly?
Was This Post Helpful? 0
  • +
  • -

#3 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Why is my code not running? C++

Posted 31 October 2013 - 03:27 PM

You should post your errors if you want help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1