RSS

Tag Archives: noob

Now What?

A number of software development courses, such as Codecademy or even traditional university classes, prepares the student to solve problems using their language of choice, but leaves them unprepared to go to the next step.  This next step is to take what you have created and fold it into something free-standing and usable; Something you can share with friends; Something attractively packaged.

My own dilemma is that I have been taking classes and practicing programming C++ on my mac using XCode as my IDE. Now that I have actually created something (a personal summertime project between classes), I want a good way to present my work. Something more attractive and user friendly than the Terminal environment I currently have.

According to apple, my next step is Cocoa. But, what is Cocoa?

Apple defines Cocoa as “Cocoa is an application environment for both the OS X operating system and iOS, the operating system used on Multi-Touch devices such as iPhone, iPad, and iPod touch.” Which could be meaningful… but not very descriptive to me.

Image

                            Hot Cocoa

For anyone familiar with Apple lore, Cocoa was first introduced as NeXTSTEP in 1989: A product of Steve Jobs’ successor company NexT. “You can use several programming languages when developing Cocoa software, but the essential, required language is Objective-C … You can even mix C++ code with your Cocoa code and link the compiled code into the same executable.”  It is this last phrase that gives me hope that I can figure out how to possibly use the work I have don’t so far and weave it (somehow) together with cocoa to add a friendlier appearance to my work. Furthermore, it looks like I could also use Ruby or Python with Cocoa, two languages that I have been working with and find to be somewhat less awkward at times than C++.

So, how to actually put this together: C++ and Cocoa?

            -I’ve been sifting through resources for a couple days now trying to identify some starting point to this without much luck.

-Perhaps this is the wrong way to go about using my C++ code, but is there another way?

 
Leave a comment

Posted by on August 4, 2013 in Uncategorized

 

Tags: , , , , , , , , , ,

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

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

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

yellow++;

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;

}

 

};

 

 

 

 

Tags: , , , , , , , ,

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….

 

Tags: , , , , , ,