Guess the Number Game

Carolina Barriento
Embry Riddle Aeronautical University-Daytona Beach, Mechanical Engineering

Author Profile

Summary

The purpose of this activity is for students to practice the concepts listed below by writing code that simulates a guessing game between friends.
Keywords:
Loops, Random numbers, Programmer Defined Functions, Conditionals, Input/Output

Used this activity? Share your experiences and modifications

Learning Goals

1) Be able to understand the difference between while and for loops
2) Be able to write valid while loops (forced-exit commands are not allowed: break, return, etc.)
3) Be able to understand boolean and relational operators
4) Be able to use while loops for error-checking
5) Be able to properly set up a programmer defined function, as specified
6) Be able to properly use input  and output (fprintf())
7) Be able to properly use conditional statements 
8) Be able to properly use random numbers/random functions
9) Be able to write descriptive comments throughout and readable and organized code
Note: MATLAB is the programming language utilized for teaching programming concepts. The main purpose of the course is to learn programming logic.

Context for Use

Educational level: First Year College/First Programming Course (no programming experience)
This is a class activity, to be completed after students watch a video(s) on the topic (before coming to class) and after a classroom lecture. Approx. time to complete is 45 mins.
Instructor helps students as needed while completing this assignment. Can be easily moved to online only.
Upon completing this activity:
Students will practice and test understanding of:
1) Use of boolean and relational operators
2) Loops
3) Programmer defined functions (PDF)
4) input and output (command window)
5) conditionals
6) use of random numbers/random functions
7) Writing descriptive comments and applying coding standards

Description and Teaching Materials

Assignment is uploaded to Course CANVAS page. The use of MATLAB is required, University provides full license to students at no charge.
Assignment instructions:
Title: Simulate a guessing game between friends using MATLAB command window.
Main Topics: Loops, conditionals, random numbers, PDFs
Instructions:
Create a function randNum to generate the number to guess. The function will request all necessary data from the user to generate the number, and then it will pass it to the script.
You will add an error checking loop to allow the user to reenter a guess if out of range. 
The user will have only 5 chances to guess the number. If guess is lower or higher than the number to guess, display a message stating so.
The program must end when one of the following occurs:
1) The user guesses the number
2) The user runs out of tries
* Note that the use of return, quit, exit, break, etc., to end a loop is NOT allowed.
SAMPLE OUTPUT:
RUN #1:
Provide the lower interval edge: 1
Provide the upper interval edge: 5
Guess a number between 1 and 5: 9
Guess is out of range.
Guess a number between 1 and 5: 1
You guessed my number, 1!
>>
RUN #2:
Provide the lower interval edge: 1
Provide the upper interval edge: 10
Guess a number between 1 and 10: 1
Number is too low!
Guess a number between 1 and 10: 10
Number is too high!
Guess a number between 1 and 10: 5
Number is too low!
Guess a number between 1 and 10: 6
Number is too low!
Guess a number between 1 and 10: 7
You ran out of tries. The number was: 9
>>
OPTIONAL Challenge:
Every time the user enters number in the incorrect range, it will count as one try.
You can improve this code in any other way! For example, improve error checking using string functions. Give it a try!

Name your SCRIPT: CA10_YourLastName.m
Name your function: randNum.m
Your script must include the header below AND must be named as specified. It must also contain comments.
Your function must contain a comment stating its purpose AND must be named as specified.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ASSIGNMENT TYPE AND NUMBER: In-Class Activity #
% PROGRAM PURPOSE:
% AUTHOR:
% DATE:
% CREDIT TO(if applicable):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Post-Activity Discussion:
1) After completing this activity, are you able to determine when to use while or for loops?
2) What was the most challenging concept in this activity? Which of the resources provided helped you the most?
3) Did you write pseudocode, or a flowchart (or both) before coding? How did this help you? 
4) Did you try the optional challenge? If not, why not?



Teaching Notes and Tips

Loops and programmer defined functions are challenging topics for students. Emphasis is given to these two topics during the lecture preceding the class activity. A sample lecture on while loops is included as a reference.
Using break, quit, return, etc. is not allowed. Students usually try to use these as a lazy way to exit a repetition statement, so by prohibiting these, students must use logic instead.
A sample output should be provided to guide students as to what the expected outcome of the program is.
Students are required to submit pseudocode or a flowchart of the program (or both) besides the actual code file. The purpose of this is to encourage students to understand the problem and to plan the structure of the program before coding. Problem solving is the first topic taught in this course, showing students the necessary steps to approach and solve a problem, listed below:
1) Problem Definition (understand the problem)
2) Identify inputs and outputs
3) Theory (resources, equations, methodologies, etc.)
4) Assumptions and diagrams (if applicable)
5) Solution steps (algorithm)
6) Verify results
7) Coding

Assessment

The following RUBRIC is used for assessment:
Standards: Includes name, date, assignment title. Good use of white space. Organized work (i.e. repetition and conditional statements properly indented). Good use of variables (no unambiguous names)
1 pts
The program works and meets ALL of the specifications. No errors.
Program must run. Include code that does not run (commented out) so instructor can provide feedback on the error/issue.
Must set up PDF as specified in instructions.
Use of break, continue, exit, etc. is NOT allowed. 
Must use a random function to generate number to guess
Out of range input must display error message and allow user to reenter number to guess. 
7 pts
Documentation: Clearly documented including descriptions of all variables, purpose of functions and control structure (if applicable). A flow chart or pseudocode (or both) is provided.
2 pts
Total Points: 10

References and Resources

References and Resources
A sample lecture on while loops is included as a reference.Lecture 05-While Loop.ppt (PowerPoint 490kB Nov8 21)
A sample solution MATLAB file is included as a reference.GuessingGameSampleSOL.m (Matlab File 2kB Nov8 21) GuessingGameWORKSHOP.mlx (MATLAB Live Script 6kB Nov8 21)