## Full code for the mastermind game

03 Jul

Here’s the whole thing as I’ve written it – my first full program ever!

Mastermind code in Javascript

Took me about a week to set up properly

Finally succeeded July 2, 2012

Plays all basics of the game – doesn’t show a running list to help player, but shows all moves at finish.

//gets the number of digits used

var code = function(){

var enteredCode = 0;

var codeVeritas = false;

while (codeVeritas === false){

enteredCode = prompt(“How many digits shall the code be? (1-6)”);

//verify input

if (enteredCode > 0 && enteredCode <= 6){

console.log(“We will be using “+ enteredCode+ ” digits in our code”);

codeVeritas = true;

return enteredCode;

} else{

}

}

};

//gets numerals to be used in code

var numbers = function(){

var enteredNumber = 0;

var numbersVeritas = false;

while (numbersVeritas === false){

enteredNumber = prompt(“code will consist of numerals 1 – : (1-9)”);

if (enteredNumber>0 && enteredNumber <=9){

console.log(“We will be using numerals from 1 to “+ enteredNumber+ ” for our code”);

numbersVeritas = true;

return enteredNumber;

} else{

numbersVeritas = false;

}

}

};

//sets up an array of numbers that will be the secret code

// code is the number of digits, number is the integers used for each digit

setSecretCode = function(code,numbers){

var secretCode = “0”;

var secret = [];

for (i=0; i < code; i++){

secret[i] = Math.floor(Math.random()*numbers+1);

}

return secret; // returns result to be used going forward

};

//accepts guess from user and parses it into an array of ‘code’ length

acceptGuess = function(code,numbers){

var guess = 0;

var guessDigit = [];

guess = prompt(“Make a guess at the secret code (“+code+”digits/1-“+numbers+”)”);

var codeNum = code * 1; // to transform code into a ‘number’

if (guess.length === codeNum){

for (i=0;i<code; i++){

guessDigit[i] =guess.substring(i,i+1);

}

return guessDigit;

} else {

console.log(“invalid guess”);

}

};

var compareGuess = function(code,secrets,guesses){

var green = 1-1; //right color, right position

var yellow = 1-1; //right color, wrong position

var white = 1-1; //wrong color

code = code*1; //converts code to a number type

//part 0: set ‘Already’ arrays to false

for (i=0; i<code; i++){

};

//part 1: check for perfect matches, alter guesses and secrets

//array positions to eliminate recount – count greens

for (i=0; i<code; i++){

guesses[i] = guesses[i] *1;

//console.log(“I’m in the first loop”);

//console.log(“guess”+i+”=”+guesses[i]+” of type “+typeof guesses[i]);

//console.log(“secret”+i+”=”+secrets[i]+” of type “+typeof secrets[i]);

if (guesses[i] === secrets[i] &&

//console.log(“I’m in green”);

green++;

}

}

//part2: check for imperfect matches, alter guesses and secrets

//array positions to eliminate recount – count yellows

for(i=0; i<code; i++){

for (j=0; j<code; j++){

if (guesses[i] === secrets[j] &&

yellow++;

}

}

}

//part 3: set white to number of unmatched items

//provide feedback to player

white = code – (green+yellow);

alert(green+” green    ” + yellow+” yellow”+ white+” white”);

console.log(green+” green ” + yellow+” yellow”+ white+” white”);

if (green === code){

exitShowResultLoop = true;

}

};

//main function

var guessArray = [];

var exitShowResultLoop = false;

var enteredCode = code(); // brings the variable  enteredCode out of the function

var enteredNumber = numbers(); // brings the variable enteredNumber out of the function

var secret = setSecretCode(enteredCode,enteredNumber);

//printout of array for debugging

console.log(“secret code is: “+ secret);

//play the game using a while loop to loop until correct guess is made

while (exitShowResultLoop === false){

guessArray = acceptGuess(enteredCode, enteredNumber);

console.log(“guess is: “+guessArray);

compareGuess(enteredCode, secret, guessArray);

}

console.log(“You’ve done it!!! Congratulations!!”);