Tag Archives: educational games

Full code for the mastermind game

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{

alert(“number is out of range.”);




//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{

alert(“number is out of range.”);

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

var guessedAlready=[];

var secretedAlready = [];

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

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

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

guessedAlready[i] = false;

secretedAlready [i]= false;


//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] &&

guessedAlready[i] === false && secretedAlready[i] === false){

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


guessedAlready[i] = true;

secretedAlready[i] = true;



//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] &&

guessedAlready[i] === false && secretedAlready[j] === false){


guessedAlready[i] = true;

secretedAlready[j] = true;




//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!!”);


Tags: , , , , , ,

Cool site about RNA folding

A friend of mine just made me aware of this site that makes RNA folding into a game. The educational aspect focuses on teaching students/ players that RNA is not just a simple, linear molecule (like people think of mRNA most of the time) that carries information from DNA about making proteins. RNA is also a very dynamic molecule that has a number of functions not directly related to carrying information about building proteins. In fact, these molecules are so multifunctional, there is a pretty believable theory out there that the first life on this planet was wholly RNA-based.

This game illuminates just why the RNA-first theory makes so much sense: Because RNA can actually ‘do things’. It can fold on itself and form structures very similar to proteins and these structures can have functions very much as proteins do. In fact, many of us think about RNA as functional all the time. Ribosomes are perfect examples of this: RNA that acts as an enzyme.

I can’t wait to show this to my students in the Fall and see if it helps them get a better grasp on DNA/RNA base-pairing as well as the idea that folding of molecules dictates functions.


here’s the link, I hope you enjoy:

Leave a comment

Posted by on June 29, 2012 in Education, Uncategorized


Tags: , , , , , , ,