Drag colors to form a target pattern trying to
match a "secret" pattern generated by the program. After
each guess the program will tell you the number of colors that are in the
correct position. The objective is to guess the pattern in the
fewest number of trys.
Background & Techniques
I received this email the other day:
I want to develop an application which generates a
random, three part code using three colours. These colours can be repeated
during the sequence. The main point im struggling with is that i want the user
to drag and drop the colours into a guess sequence.
This is basically the main features of the program
and i wondered if you can give me some feedback on how to code it.
I previously posted a Simple
drag/drop demo program in the Delphi Techniques section which shows
how to drag colors to targets. I decided to finish the program and
post it here.
Non-programmers are welcome to read on, but may
want to skip to the bottom of this page to
download executable version of the program.
I counted 46 lines of user written code in the
source for this program, so we'll put it in the "Beginners"
category. Here are a few tricks that a beginner might want to
- We use a "OnFormActivate" exit
procedure to initialize the random number generator with the "randomize"
statement so that secret codes do not repeat each time we start
program. FormActivate also calls the ResetBtnClick
procedure to set things up for a new game.
- Before giving a message to the user, a couple
of lines of code will maintain proper grammar by using singular verbs
and nouns in the text when when we are referencing a single guess or a
single item in the correct position.
- The ResetbtnClick procedure flashes
several sets of random patterns before selecting one, just to add a
little interest to the program. Inside the the loop that
generates the patterns, we will "sleep" for .05 seconds (50
milliseconds) for each set displayed. I put the Sleep
statement at the beginning of the loop so that no sharp-eyed player
will be able to pick out the final pattern before we clear the
display. Note that there a common variable "Shape"
is used to hold the particular shape being set (Shape1, Shape2,
Shape3). Note that for objects, statements like Shape:=Shape1
does not assign or move Shape1 to Shape. It merely
sets the pointer to Shape1 into the variable shape.
Before object oriented programming we needed a special pointer
reference symbol (^) to do this. In
fact this is one of the stumbling blocks to overcome when learning
object oriented programming.
I'm sure that there is more to be done with
this program. It is similar to Mastermind, but less feedback
information is provided to the user. I have not yet succeeded to
analyzing this version nor have I found an analysis of the game on the
web. That will be a first step to developing an automated
guesser strategy that will find the pattern in the fewest number of
guesses. We could then rank players skill levels at guessing,
track how fast users develop a guessing strategy based on experience,
Running/Exploring the Program
Suggestions for Further Explorations
control over number of colors to choose from and number of colors
in the guess pattern.
an "auto-solve" procedure that uses an optimal guess
strategy. I would probably use this to report the
"best score" to player as a target goal.
suggestion will require an analysis of the puzzle to determine the
best guess strategy.
|Original Date: March 27, 2004
Modified: February 18, 2016