BFOIT - Introduction to Computer Programming

Mastermind Project - FAQs

How Do I Choose a Secret Code

It's a RANDOM Event

The problem to be solved here is to choose four unique colors out of a pallete of six.  Actually it is a bit more complicated than this since the order of the colors must also be random and we are not allowing any duplication of colors.

Since I used words composed of characters (each representing a color) in my program, that is what I will be working with in this FAQ.  If you decided to go with sentence of words, you should have no problem making the needed substitutions.

How to Solve It - Understanding the Problem

Let's go through the three steps that will help focus us on solving the problem we are faced with.  The first is always Understanding the Problem.

Here's what we know:

  1. Whatever we do is going to be iterative.  We need to pick four colors.  So we are going to do something at least four times.
  2. Since I'm going with characters representing colors, I'll be using the word operator to generate a value for secretCode.
  3. The palette of colors to choose from needs to change after each choice is made since duplicate colors are not allowed in the secretCode.
  4. In the last lesson (Word and Sentence Iteration) we wrote an operator, acronym, which outputs a word composed character by character.
  5. The basic skeleton of a recursive procedure that works its way through a collection, a word in our case, is:
    
            to wordOperator :wd
              if empty? :wd [output "]
              output word <opr1> :wd wordOperator <opr2> :wd
              end
    			
    where <opr1> is either first or last and <opr2> is either butfirst or butlast.  This means that we need to work with the first or last character of a word.  Which... leads me to believe we need to get a random character into the first or last position in the word of color choices.
  6. In the previous lesson we wrote a simple command that changed the first word of a sentence by rotating the sentence.  Can this could be converted to an operator that rotates words?

How to Solve It - Devising a Plan

The easiest way for me to demonstrate how using a word will work is to show you how my version of the program generates the secret code.  There are quite a few different ways in which to select four unique letters from our set of six.  I'm going to show you the one I like the best.

The basic steps in the approach I'm going to take are listed in Table 15.1.

  1. pick a random letter from the set: r, o, y, g, b, v.
  2. append the letter to the secret code being built.
  3. remove the letter from the set so that it is not picked again.  
  4. repeat steps 1 - 3 until the secret code has four letters.
Table 15.1

The algorithm I'm using comes from Brian Harvey's book "Computer Science Logo Style."  It's quite clever and definitely a technique worth learning.  Writing code is an art and so it's best to learn from the masters when you have the chance.

*NOTE*
To make the most out of the following hints you should stop at the end of paragraphs.  Do not peek ahead at the code that I give next until you've at least pondered how you would write the code on your own.  You should really try to write your own version of the code before checking mine out.

In the last lesson you learned the primitives that are available to assemble/disassemble words: word, first, butfirst, last, and butlast.  And, the first example in the last lesson showed you how to write an operator (rotateSentence) which takes the leading word off of a sentence and appends it onto the end.  You should now modify rotateSentence so that it takes two inputs: a number of times to rotate, and a word instead of a sentence.

    to rotate :num :wd
      if equal? :num 0 [output :wd]
      output rotate (difference :num 1) (word butfirst :wd first :wd)
      end
			

This new version can be used to get a random letter.  Test it...

    ? println rotate random 6 "roygbv
    bvroyg
    ? println rotate random 6 "roygbv
    oygbvr
    ? println rotate random 6 "roygbv
    gbvroy
			

Got this working?  Ok, then add the declaration of a global variable which will hold the secret code to the top of your Mastermind code.  Then add your the procedure(s) to fill it with four random characters from the six choices.  Here is most of my code.  I've put comments in place of code for all of the steps from Table 15.1.

    to pickSecretCode
      ; stop-rule: secretCode has all four characters? [yes - output it]
      ; rotate choices a random amount
      ; append first character of choices on to secretCode
      ; recursive invocation - removes chosen character from choices
      end
			

Carrying out the Plan


Back to Mastermind Project

Public Domain Mark
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.