A Number Guessing Game Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu
Learning Objectives • To read a word problem and create: • Correct Pseudocode, and • An accurate flow chart
The Problem: • The problem is an old number guessing game. • The computer picks a number from 1 to 100, and you have 7 tries to guess the number it picked. • Produce the pseudocode • Create an accurate flow chart using Visio
The Solution: • One, organized way to approach this problem uses the acronym D.I.P.O. • D = Declare • I = Input • P = Process • O = Output
D.I.P.O. • D = Declare(the variables) • The First Step • What numbers will need to be stored? • What names will need to be stored? • What other data needs to be stored? • Anything used or manipulated by the computer must first be stored in memory.
D = Declare • For the number guessing game we will need someplace to store… • the number the computer picks. (We’ll call this variable number) • the number of guesses made by the human. (We’ll call this variable howMany) • The number the human enters as a guess. (And call this variable guess)
I = Input • The input for this program is easy… • The human’s guess is the only input
P = Process • The process for this program is easy… • Read the human’s guess from the console • Compare the guess to the computer’s number • Route to “Guess again” (with a hint) or “Game over” depending on human guess
O = Output • During the guessing process, there will be four possible outputs: • “Your guess was too low!” • “Your guess was too high!” • “You Got IT!!!”,(You win!) and • “Too many guesses. (You lose.)” • …and at the end, the output: • “Play Again?”
The Chicken or the Egg? • Which comes first? • The pseudocode? or • The flow chart? • Actually, it doesn’t matter…. • Do one or the other first, or • Do them both, side-by-side, at the same time.
The Pseudocode • Because of this PowerPoint format, it would be cumbersome to do both together, so I’ll make the choice most programmers would pick: pseudocode first. • At this point, it would be good to have all the previous slides printed out and available, so that you can better see all the requirements and components.
The Pseudocode • The “Big Picture”, meaning lacking details or specifics of the pseudocode would look like this. Computer picks a number Human gets 7 guesses Possible responses: - Too high - To low - Correct! You win! - Too many guesses. You lose. Play again?
Modules/Subroutines • Although this program is too small to normally warrant the use of several modules or subroutines, I will use them here just to help you keep in mind the idea that separate tasks should be in individual modules.
Modules • Here is what the “big picture” flow chart looks like in Visio:
Modules Next, let’s try to write some pseudocode to match each of these shapes. (Look at the previous slides and apply what you know here.)
Modules Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7)
Modules Process: (Do this up to 7 times) Get human guess Compare guess with number Output message: Too high! (Repeat guess) Too low! (Repeat guess) You win! (Exit to Play Again) You lose. (Exit to Play Again)
Modules Play Again: If YES, return to start If NO, end program
Complete Pseudocode Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7) Process: (Do 7 times) Get human guess; Compare with number Based on Guess, Output message: Too high! (Repeat guess) Too low! (Repeat guess) You win! (Exit) You lose (Exit) Play Again: If YES, return to start If NO, end program
What’s Next? • So… We have the big picture and the major items of both the pseudocode and the flow chart • Now we need to go into more detail. • This is called “Top Down” design
Top Down? • Top Down means to start with the big picture, then step by step, provide more info and break down the tasks to smaller and smaller units. • The theory is that gradually, you will have a detailed, complete set of steps to perform the task. • Let’s continue our breakdown…
Initialize() From the previous slides, what needs to be declared here as places to store something?
In case the game is played again, we need to set the number of guesses to zero.
This is the complete flow chart for process(), but let’s start over and build each part of the chart with an explanation
First, we must have an input from the human, their guess. (We could verify this to be a number from 1 to 100 if desired.)
Next, we must ask several questions. If the human’s guess is less than the computer’s number, we print a hint that the guess is too low.
We’ll come back to “Too low” shortly, but let’s ask the next question: Is the human’s guess bigger than the computer’s number?
We’ll come back to “Too low” shortly, but let’s ask the next question: Is the human’s guess bigger than the computer’s number? Notice that we put “Yes” and “No” at the two outputs of the decision diamond to show the flow direction for each answer.
If the answer to both of these questions was “No”, what is the only possibility left?
If the guess was not too high, AND not too low, then the human guessed the computer’s number. Display the good news.
So the game is over. Let’s exit. (Which takes us to the “Play Again?” section of the code.
Now we need to go back and add the steps required after the “Too low” and “Too high” messages are displayed.
In either case, the human made the wrong guess, so add one to the number of guesses taken.
Did the human take too many guesses without getting the correct number?
And finally, if the human has any guesses left, send them back to guess again.
Now, You Finish Up the Chart And lastly, the “Play Again?” section needs to ask the question: Do you want to play again? (Input the human’s answer.) If the answer is “Yes” direct the flowchart back to the beginning of the program. If the answer is “No” direct the flowchart to “End”. You make the flowchart for this part.