BFOIT - Introduction to Computer Programming

Mastermind Project - FAQs

How Do I Get Started?

You Already Have a Start

Back in lesson 12 (What If? - Predicates) there was a user-interface project that is a start at writing the game Mastermind.  Make a copy of the program you wrote, your solution, and name it Mastermind.jlogo. Go back and refresh your memory by scanning through all of the notes I provided along with this project.  So, getting started is easier than you thought!

How to Solve It

Think back, a long time ago, to lesson 3 (Psuedocode).  In its Summary, it lists the three phases that you should go through when writing a computer program:

  1. Understanding the Problem
  2. Devising a Plan
  3. Carrying out the Plan

Get out some paper and a pencil.  Now, go back up and play the game a couple more times.  As you click the mouse, write down what you did followed by what happened on the display.  Write one line for each thing you do.  Once you've played a game or two, you have what you need for the first step.

Spend a little bit of time organizing all the notes you've taken.  This is the understanding phase.  Once you have a pretty good feel for what needs to happen, it is time to write some pseudocode.  A good way to start is to add comments to your program.  These comments can be your plan and can be replaced with Logo instructions later.  Another way for you to do your planning is to start by defining a bunch of empty procedures that are strategically named such that together they represent an outline of the program, showing its structure, its hierarchy.

Start with the mouseClicked procedure.  Given all your notes, you are in a good position to write some pseudocode describing what it should do.  Then move on to some other thing that your notes say that the program must do.  Alternatively, write a skeleton of your program as a bunch of empty procedures.

When this is as complete as you can make it, move on to the implementation phase, carrying out your plan.  What usually happens is that you find something that you didn't anticipate during your planning and this forces you to go back to this phase and rework some stuff.  But, there is nothing wrong with this.  It is quite often the case that you will move back and forth a few times before you have your finished program.

What About a Little People Design?

Starting with the Mastermind UI Project in lesson 12 (Predicates - What If?), I approached program design with little people.  The idea was to think about the program as a box with little people in it.  I broke the program down into jobs performed by virtual workers.

Take this approach with the Mastermind game.  Think about how you would assign jobs out to your own group of little people.  Start with the Mastermind UI Project.  Here are some things to thing about:

  • The bigBox little person in it goes away and must be replaced with someone that handles a bunch of little boxes.
  • You are going to need a little person that knows the secret.
  • What about determining a win and the feedback hints?

Write your thoughts down.  If you need to, review the little people design of a four-function calculator.  There is also little people discussion for the Hangman program.

Program Structure Diagram

Sometimes it helps if you think about the program in the terms of a diagram of its components.  I've been providing program structure diagrams for many of the programs we've been writing.  Here are links to them.

You can start with the Mastermind UI Project diagram and extend it.

Getting Started - A Functioning GUI

Your experience with all of the programs you have written so far should have taught you that the best way to get started is to get something working, then extend it.  You have your start, as mentioned above.  You will have to change some symbolic constants to move around things.

Here is one way of completing the program.

  1. first draw the color choice strip and the buttons, then
  2. draw an initial set of guess box frames, then
  3. get mouse clicks on the color choice boxes to fill in guess boxes, left to right, then
  4. get a mouse click on the [ClearChoice] button to clear the rightmost box filled with a color, then
  5. get a mouse click on the [CheckGuess] button to draw a new set of guess boxes which became the current set, then
  6. get your procedure just written (that gets invoked when [CheckGuess] is clicked on) to check to see if some maximum number of guesses has been exceeded and if so, display "Sorry, You Lose", then
  7. get a mouse click on the [NewGame] button to clear the display and redraw the color choice strip, the buttons, and the initial set of guess boxes, then
  8. construct a word or sentence consisting of the colors chosen in the current guess. A variable (I named mine chosenColors) should start out empty and as each color is chosen, something representing the color should be appended onto the word or sentence, then
  9. get your procedure which gets invoked when the [ClearChoice] button is clicked on to remove the last color from your variable which represents chosen colors, then
  10. add a new global variable (I named mine secretCode) which contains the secret colors, the answer, and initialize it with a word or sentence (which depends on the way you are representing the current guess), then
  11. get your procedure which get invoked when the [CheckGuess] button is clicked on to compare the secret to the current guess, one color at a time, counting the number of colors that are correct an in the correct places. If all four are correct, report the WIN otherwise report the number; then,
  12. get your procedure which get invoked when the [CheckGuess] button is clicked on to count the total number of correct colors in the current guess and report the number of colors that are correct, but in an incorrect place; report this number; and finally,
  13. make construction of the secret colors random.

Back to Mastermind Project FAQs

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.