(Updated December 5, 2022)
Getting Started
Ok. Now we have some basic abilities at our disposal. We can now begin roughing out the game – at least from the viewpoint of the initial setup.
First, we will begin with a basic template for nearly all basic C programs:
#include <stdio.h>
int main(int argc, char *argv[]) {
// code goes here!
}
Iteration 1: Basic C template for the new CodeBreaker game.
As you can see, we have a relatively basic C template. This won’t be enough, of course. There is much to do and we will need more header files to complete the work.
What Needs To Be Managed?
Since we need to have the color list available in multiple locations within the game, we have chosen a design item that involves a global variable – one whose scope is available everywhere. This is strictly a simplification of the overall design.
const char* COLORS = "ROYBGW"; // global list of colors
The choice to have the variable COLORS
represented as a string was based on design. We know that each character is uniquely addressable using the array access operator ([ ]
). But, more importantly, we know that since each position is represented as a number, we can eventually use rand()
to pick a random position in the string! All we need to know is the length.
At this point, we need more variables. We need a secret and the user’s input representing their guess. The global variable COLORS
will be used to choose four (4) random colors, but we also need the size of the color list. These are shown below.
size
to calculate the length of the color list once. Whenever possible, refrain from using the same library call repeatedly. Make one call and save the value returned.const char* COLORS = "ROYBGW"; // global list of colors
int main(int argc, char *argv[]) {
char secret[5], input[5];
int size;
If we had a variable r
that held the random number, we could use that value as an index into the string. Recall that rand()
producing an int
modulo 6 will yield a number in the range 0-5. This coincides with the indexes of COLORS
.
0 1 2 3 4 5 ------------------------- COLORS --> | R | O | Y | B | G | W | -------------------------
Illustration 1: Indexes representing the set of colors in COLORS
.
All we have left is to get the character and add it to secret
.
int r;
char c;
char secret[5], input[5];
int size;
size = strlen(COLORS); // get the size of the color list
r = rand() % size; // get random number
c = COLORS[r]; // use it to get a color
secret[0] = c; // store the selected color
The code first picks the random number and stores it in r
. Then, using r
, it selects a character c
from COLORS
as an index into the string. These are more steps than necessary, but this code helps illustrate the work required to go from random numbers to a selected color.
Now that being done, we have only selected one color. We need three more, but we do not know how to write loops yet. So, we have to do it the tedious way of copy-paste!
r = rand() % size;
c = COLORS[r];
secret[0] = c;
r = rand() % size;
c = COLORS[r];
secret[1] = c;
r = rand() % size;
c = COLORS[r];
secret[2] = c;
r = rand() % size;
c = COLORS[r];
secret[3] = c;
secret[4] = '\0';
Finally, we consider simplifying the code and eliminating the use of r
and c
since they seem to clutter the code. Reducing the work down, we can perform simple substitution for r
and c
such that
r = rand() % size;
c = COLORS[r];
secret[0] = c;
becomes
c = COLORS[rand() % size];
secret[0] = c;
and finally
secret[0] = COLORS[rand() % size];
Repeating the statements until we have four randomly selected colors would then look like the following:
size = strlen(COLORS);
// select 4 random colors for the secret.
secret[0] = COLORS[rand() % size];
secret[1] = COLORS[rand() % size];
secret[2] = COLORS[rand() % size];
secret[3] = COLORS[rand() % size];
secret[4] = '\0';
Instructions Anyone?
At this point, we can consider the importance of displaying some instructions to the user. If they have never played before, this is an excellent time to inform them of the gameplay. We also need to instruct them on how to make guesses so that we can interpret them later when we have more tools at our disposal.
// display instructions
puts("\n"
"The game of CodeBreaker:\n\n"
"The computer will choose 4 colors from the list RED, ORANGE, YELLOW,\n"
"BLUE, GREEN and WHITE. You will have 10 chances to guess the colors\n"
"from left to right. You will use the first letter of each color. Your\n"
"guess could be \"RWYY\". Spaces are not allowed in the guess. If\n"
"you make a mistake with color selection, you will be prompted to\n"
"reenter your guess.\n\n"
"Each guess is graded. You will be told how many colors are correct\n"
"and how many are in the correct place.\n");
Make a Guess!
Then, all that is left to do is to set the stage for reading a guess from the user. Since we still lack some tools, this will be a simple one-guess model without checking if they guessed correctly. This is achieved by prompting and reading a string using scanf
.
printf("Enter your guess: ");
scanf("%4s", input);
printf("The secret was %s\n", secret);
We now have the basis for creating a rudimentary program suitable for Interlude 3.
Putting It All Together
The final version of CodeBreaker for Interlude 2 is shown below.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
const char* COLORS = "ROYBGW"; // global list of colors
int main(int argc, char *argv[]) {
char secret[5], input[5];
int size;
// display instructions
puts("\n"
"The game of CodeBreaker:\n\n"
"The computer will choose 4 colors from the list RED, ORANGE, YELLOW,\n"
"BLUE, GREEN and WHITE. You will have 10 chances to guess the colors\n"
"from left to right. You will use the first letter of each color. Your\n"
"guess could be \"RWYY\". Spaces are not allowed in the guess. If\n"
"you make a mistake with color selection, you will be prompted to\n"
"reenter your guess.\n\n"
"Each guess is graded. You will be told how many colors are correct\n"
"and how many are in the correct place.\n");
srand(time(NULL)); // seed the PRNG
size = strlen(COLORS);
// select 4 random colors for the secret.
secret[0] = COLORS[rand() % size];
secret[1] = COLORS[rand() % size];
secret[2] = COLORS[rand() % size];
secret[3] = COLORS[rand() % size];
secret[4] = '\0';
printf("Enter your guess: ");
scanf("%4s", input);
printf("The secret was %s\n", secret);
return 0;
}
Iteration 2: Final version for this interlude.