BFOIT - Introduction to Computer Programming

# Mastermind Project - FAQs

## Representing the Secret Code and a Guess

### What Kind of Data Will Make Coding the Easiest?

The way the state of the game is presented to us humans, for us to easily see what is going on, is very different from the way a computer prefers to have it.  We look at big spots of colors and our brain easily processes them and patterns stand out.  But, since a computer is based on 1s and 0s, the best way for it to process data is as binary numbers.  Thanks to Logo, we will be able to compromise and represent the secret code and guesses as symbols of our own choosing.

How do we choose how to represent our data?  A few years ago, when mobile phones were much more primitive than they are today, we would have had to worry about how much memory our representations would use.  But, today's mobile phones have lots of memory and fast processors in them.  Without any limitations to worry about, we will choose a representation that is easy to work with.  What does Logo excel at?  What did we learn about in the previous lesson?  Logo makes working with words and sentences easy.

The goal here is to make your program as easy to write as possible.

To a beginning programmer, the obvious way to represent the secret code and the guess is with a bunch of global variables with words in them, one variable for each color in the secret code and one for each box of a guess.  You can do this.  Try this if you want, but I'll warn you, you will be writing a lot of code, a lot of if commands.  Writing the code to compute the number of colors in the guess that are correct but in the wrong position is especially difficult.

The alternative is to use a four-item thing with each item representing one color.  If both the secret code and a guess are represented as sentences with one word for each color, you will be able to use one equal? operator to test for a correct guess.  The same idea applies to a word with each character representing a color.  If not correct, with either a word or a sentence, it's easy to count the number of correct colors that are in the correct position with iteration.  And, the member? operator is key to counting correct colors whether or not they are in the correct position.

### I'm Going With Characters/Words

I think writing the program will be pretty much the same whether we choose characters in a word or words in a sentence.  Since I know manipulating words is more efficient than sentences, I'm going with words.  Here are the definitions of the global variables I used including the comments which describe how they will be used.  I always try to provide very informative comments with my global variable definitions.  Since the code that accesses and changes global variables is spread out all over the program's text, I feel this makes reading my code easier.  You be the judge.

 ``` ; a word consisting of four characters, each representing the color ; of its corresponding box in the secret. "b for blue, "g for green,   ; "o for orange, "r for red, "v for violet, and "y for yellow global "secretCode ; a word consisting of zero to four characters, each representing ; the color that the player has chosen for the corresponding box ; in a guess. the character/color coorespondence is same as with ; the secretCode global variable declared above global "chosenColors ```

### Operations on chosenColors and secretCode

It's pretty hard to talk about why using a word makes everything you need to do with chosenColors and secretCode easy without showing you my code.  But this would not be right, it's like giving away the answer to a puzzle.

What I'll do instead is go through all of the parts of the program that deal with these two global variables and give an overview of what needs to be done.

• First of all you need to pick a value for the secret code.  This is essentially constructing a word by picking four characters representing colors, out of the six possible colors.  This is a lot like generating a sentence composed of random numbers .  The big difference is not that you will generate a word, that's no big deal.  The big difference is that your word can not have duplicate characters in it.  So coming up with the secret word will be covered as another FAQ.
• chosenColors should be initialized to be an empty word.  Just remember that it must be reset each time the program checks a guess and it's wrong.  This action is a simple Logo primitive command.
• Each time a color is chosen, a character is appended onto chosenColors and each time a choice is cleared a character needs to be removed from it.  Both of these operations can be performed with Logo primitives.
• Determining the number of correct colors (characters) in the correct positions involves both secretCode and chosenColors but it's a straight-forward iterative procedure.
• Finally, determining the number of correct colors in the wrong positions requires writing an iterative procedure, but Logo's member? primitive procedure helps a lot.  Just like with generating the secret code, the complicated part involves duplicate characters.  So, it will be covered as another FAQ.

That covers all of the operations that need to be performed that are concerned with the secret code and the color choices the player makes.  As you can see, words fit our needs.  My feelings are that sentences of words representing the colors would work as well.

Hope this helps...

Back to Mastermind Project

This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.