# Category Archives: Programming and Collaborations

## CDC Urges Preparedness for Zombie Attack!

Coming Soon from DownHouse Software

DownHouse Software announces the coming release of the new eBook, “In Part: A Tale of Fractional Zombies.” This is the story of a zombie attack on an elementary school where children in the math class are learning fractions – Hey Zombies are Fractions!  – What a perfect opportunity to put their new skills to work in the real world.

In Part… A Tale of Fractional Zombies – coming soon

## Objects and Methods made my head swim – but not any more!

I’ve really been struggling with understanding objects and methods from my work on codecademy. Although it is a great site, because it is crowdsourced teaching, some topics just don’t get the best treatment. It’s not all that common to have someone who knows how to do something well and have them also be able to explain it well to a student (just ask some of my poor victim / students from my first several semesters teaching).

Nevertheless, I trolled through some of the Q&A sessions looking for a good explanation of the material I was struggling with and couldn’t find anything that answered my question very well. What I did find, though, were some decent pieces of ideas that I thought I could combine into a more comprehensive bolus of work that reviewed the topics I just covered, provided some practice working with objects and methods and even a switch statement.

Here’s the setup:

Shermer High school. A couple famous students go there and we can make up Objects representing each of these students using a constructor. Also, each of these students has a brand name jacket that they cherish and these are also created as objects. So the twist is: we’re creating objects(Persons) who have other objects(jackets) as one of their properties. Furthermore, to make this exercise include methods as well, I’ve added ‘wash’ and ‘sew’ methods. Each time one of these methods is carried out, the jackets are changed (e.g. when you wash a jacket, you lose a button. When you sew a jacket, you add a button).

What I wanted to accomplish was to make these objects and methods and learn how to call them so that they carry out their work. Presented below is this program with some annotation and console.log statements that should clear up what’s going on.

Please let me know if this is helpful to anyone – or if this could be cleaned up and done in a more clear and concise way.

// Constructor to make jackets
function Jacket(brand){
switch(brand){
case jordache:
this.make = “Jordache”;
this.buttons = 5;
this.color = “blue”;
break;

case gap:
this.make = “Gap”;
this.buttons = 2;
this.color = “brown”;
break;

case oldNavy:
this.make = “Old Navy”;
this.buttons = 3;
this.color = “white”;
break;
}

}

//constructor to make people
function Person(name,age,brand) {
this.name = name; //unique
this.age = age; //unique
this.brand = new Jacket(brand); //object
this.school = “Shermer High”;    //constant
this.sayHello = function(){
console.log(“Say Hello to ” +this.name+”, student at “+this.school);
};
//method that washes jacket – loses a button each time
this.wash = function(){
console.log(“You just washed “+this.name +”‘s Jacket.”);
this.brand.buttons = this.brand.buttons-1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);
return this.brand.buttons;
};
this.sew = function(){
console.log(“You just mended “+this.name+”‘s jacket.”);
this.brand.buttons = this.brand.buttons +1;
console.log(“That jacket now has “+this.brand.buttons+” buttons.”);
};

}

// Make three people using a constructor
var geek = new Person(“Anthony Michael Hall”, 14, oldNavy);
var girl = new Person(“Molly Ringwald”, 15, gap);
var caroline = new Person(“Caroline”, 17, jordache);

//now complete with uses of these objects and methods
console.log(“Getting started”);
girl.sayHello();

//console.log(girl.brand.make);
console.log(girl.name+ ” has an awesome “+girl.brand.make +” jacket.”);
girl.wash();
girl.wash();
geek.sayHello();
geek.wash();
girl.sew();
girl.sew();

console.log(“finished”);

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

}

}

};

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

## function: compareGuess – the hardest one yet

First – I’m playing with some themes, so sorry about the fact that the site doesn’t look the same. I’m actually having a hell of a time getting things set up in a way that I am happy with and feel like I have sufficient control over.

Second -the new function: compareGuess. This one was a bear! I was wrestling with it forever before I finally got it to work correctly. Well, I hope it works correctly – I’ll post the whole program in the next post so that you can copy and paste it into a Javascript editor / compiler /whatever it’s called.

Why this one was hard is that, in the game, there are two ways you can ‘score’: if you get the right number in the right position, that’s the best(I’m calling that green). You can also get the right number in the wrong position (I’m calling that yellow). The problem is that these are mutually exclusive, with green trumping yellow in a manner of speaking. This is further complicated by the fact that once a number matches, you don’t want to use that same number to match again. This requires an example to illustrate…

secret code is: 112

guess is 123

The first position is a perfect match (green). But then the first position of the guess needs to be ignored when analyzing the rest of the code. If you just nest one loop inside another, you can easily get output of :

1 green

2 yellow

This is because position#1 of the guess matches position#1 of the code= green

but position #1 of the guess also matches position#2 of the code = yellow? -It’s not supposed to be, but how to fix?!?

then, position#2 of guess matches position#3 of the code = yellow (this one’s correctly attributed)

that gives us a total of three match signals when only two are a match!!

My solution (after a long time of playing around) was to add ‘guessedAlready’ and ‘secretedAlready’ Boolean Arrays within the function (It’s really important to keep these arrays private / privileged otherwise, you can run into problems when values get carried over from one guess to the next).

guessedAlready[0] refers to the first position of the guess, i.e. guess[0].

Then I set the arrays to false to start, added statements redefining them as true anytime a match was made and finally added qualifiers to my ‘if’ statements that check to see if each number was guessed already (or part of the secret code already) and therefore should be ignored.

This solution still ran into problems though until I separated my loops and looked for only exact matches (green) first, then went back and looked for inexact matches (yellow). Otherwise, if I found an inexact match in the secret code and then hid it, I couldn’t ‘see’ it for the exact match comparison later. Ughhh. example:

secret code: 112

guess : 212

if guess[0] matches code[2] as above, then we get a yellow peg for an inexact match and then we hide code[2] and don’t see that it should actually be an exact match with guess[2].

So, the function has two major parts. Part#1 loops and looks for greens. Part#2 loops and looks for yellows. Then I added a short Part#3 that sets the # of whites (no match) to white = code -(green+yellow), where code is the number of digits in the code.

I hope that makes sense. I tested this several times last night and it looked good. I will test it more later, but please feel free to make comments if you see a problem. Or ever better: if you can code and are still reading this for some reason, let me know if there is an easier way to solve this problem.

Here’s the code:

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;

}

};

## function: Generating a Secret Code

I knew this would be a more challenging function for me. From the outset I’ve been considering a couple different strategies for accomplishing this function. One idea was to have the secret code be an object – one thing I liked about this was that I’ve never used an Object in any coding before aside from the codecademy exercises where you have a lot of help in getting it right. The other idea was to make the guess an Array. I should say that I’ve never used an array before for anything that wasn’t a reformatted exercise either. I also thought that perhaps an array might be a bit easier too.

S0, I chose to go forward with the array idea. It makes sense to me. An array is a list of variables all collected together and in a fixed order (the first position is called [0], the second is [1] and so on. Some examples of arrays are:

var x = [1,2,3,5,8,13];

console.log(x[0]); would print the number in the ‘0’ position to the console, (i.e. 1)

console.log(x[3]); would print the number in the ‘3’ position to the console, (i.e. 5)

Array can also be lists of strings:

var cats = [“William”, “Oliver”, “Chloe”, “Anna”];

console.log(cats[1]); would print the number in the ‘1’ position to the console, (i.e. Oliver)

What I like about this is that the numbers have order – just what I’ll need to set and crack a combination.

Here’s the function I came up with to do this – It wasn’t easy for me, at one point I managed to make an array that had another nested array inside of it. I couldn’t make heads or tails of it, but thanks to some good people on stack overflow, the problem was identified and solved.

//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 = [];

console.log(“***reached setSecretCode function***”);

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

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

}

return secret; to return result to be used going forward

};

This function is called:

var secret = setSecretCode(enteredCode,enteredNumber);

//printout of array for debugging

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

## function#1: User provides number of digits in the secret code

Here’s my first function.

It prompts the user to provide a number of digits from 1-6. It then confirms this with the user – well, really just me for debugging purposes. I also use a Boolean ‘codeVeritas’ to ensure that the number of digits the user enters is within the parameters defined.

//gets the number of digits used
var code = function(){
var enteredCode = 0;
var codeVeritas = false;
//so far the codeVeritas is working fine
while (codeVeritas === false){
console.log(“***code function reached***”);
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{

}
}

};

## Outlining and early steps in coding mastermind

The only language that I have an even decent grasp on right now is javascript. So, even though I know I’ll have to redo most of my work in order to get this project into XCode, I’m starting there.

My first thought was to create an outline of how I think the program should function.

I’m not sure what will change over time, but to start I had this:

//gets the number of digits used

var code = function(){

console.log(“***code function reached***”);

};

//gets numerals to be used in code

var numbers = function(){

console.log(“***numbers function reached***”);

};

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

console.log(“***reached setSecretCode function***”);

};

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

acceptGuess = function(code,numbers){

console.log(“***acceptGuess function reached***”);

};

var compareGuess = function(){

console.log(“***compareGuess function reached***”);

};

var showResult = function(){

console.log(“***showResult function reached***”);

exitShowResultLoop = true;

};

var exitShowResultLoop = false;

//computer prompts users for parameters to build secret code

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

console.log(enteredCode); //checking variable

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

console.log(enteredNumber); //checking variable

//computer sets secret code

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

var guessArray = acceptGuess(enteredCode, enteredNumber);

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

compareGuess();

showResult(); //contains an exit clause

}

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

Really, this is just a list of functions and function calls that I want to build out. I think that if I can write each of these properly, I should have a functioning game. For starters, I have a ton of console.log()’s littered everywhere just to make sure that the program is going to each function in turn. Once I complete a section, I comment them out or completely delete then and replace them with notes about how things work.

Another important thing I learned was that it’s easiest to look at this in chunks. set variables myself and send them into the functions so they could be made independently. I’m sure this is not worth mentioning to anyone who has done any programming, but to someone like me it was an epiphany.

More later as I flesh things out….

## oDesk Contracting for Pushing Taoism

I say this because I, and I imagine many other people, turn to contracting sites like oDesk only for the access to programers / coders. However, one of the best experiences I have had with oDesk was in contracting an artist to create the art for the icon and main screen of the ‘Pushing Taoism’ app. I dealt with a fantastic artist named Catarina Garcia, located in Lisbon, Portugal. You can find her art at: http://catarinamgarcia.blogspot.com/

She worked with me to identify exactly what kind of final product I was interested in, provided several sample pieces and then produced a final work in just a couple weeks. I think it only took this long because our email communications were not always immediate – otherwise, I feel confident that she could have produced my art pieces in a matter of days.

This experience really opened my eyes as to a world of interesting possibilities working with professionals in a number of disciplines. I look forward to turning to oDesk again in the future for other art and design projects – I have even considered doing a small translation project as well.

## So, why provisioning?

I answered the what about provisioning last time. As far as ‘why’, I just attributed that to Apple’s control issues. I wanted to clarify what that means. After all, how are they actually controlling this?

The easy answer is that Apple controls provisioning by only allowing each provisioning file to work with 100 devices. 100 may sound like a lot if you’re just one person writing code for your machine and maybe that of two or three friends that are serving as your testers, but 100 can run out quickly if you’re a larger operation or if you give one person permission and then they stop working with you for whatever reason.

But what it really does is provides a limit to the number of times you can ‘work around’ their system. If there was no limit, then you are free to leave the walled garden and sell your apps to anyone and just provide them with the provisioning file along with your app. I’m sure it doesn’t matter what the number is to Apple, they just agree that you need some way to test on real devices, but want your ability to share to be limited to a number that is high enough for you, but low enough to never be a practical way for you to escape their control.

Tags: , , ,