We would like to write a program to play a number guessing game with the computer. The computer will "think" of a number from 1 to 100, and give the player six guesses to try to find the number. With each guess, the computer will give the player a hint, telling them if their guess is too high or too low. The player should be given the option of playing the game as many times as they like.
This time we will take a top down approach to designing the program. We will break the problem down into smaller chunks, or modules, and attack each one, one at a time.
We still begin with the five step process.
What is this program supposed to do?
The problem statement is pretty clear. The program should play this guessing game.
What kind of information is it given? (What is the input?)
The input is the set of player guesses, one at a time, until the player either gets the number, or runs out of guesses.
What kind of results is it to produce? (What is the output?)
The output is a hint after each wrong guess telling the player if their guess is too high or too low.
What formulas or techniques will be needed?
Most of the operation of the game is straightforward - we compare the player's guess with the stored number. One tricky thing we will have to worry about is generating the random number.
We can try a small hand example.
Here is where our approach differs from bottom up. Instead of trying to write the algorithm for the entire program, we begin by breaking up the program into modules to handle the details of different aspects of solving the task.
Our modules are: Driver, Pick, and Play.
The algorithms are shown in the Driver Algorithm, the Pick Algorithm, and the Play Algorithm.
The code for the modules are
Driver - guess.c - a driver includes a main function
Pick - guess.h
Play - sequence.c and test.c
Let's run the program in class.