CS1400 Example Assignment 2: High-Low Game

If this were a real assignment, then you should pass each part of this assignment off to a lab assistant before the end of the day on each due date.

If this were a real assignment, then in addition, each part must be submitted electronically before 11:55 pm on the due date. Use the assignment submission system discussed in class to submit your homework.

If necessary, your instructor will give you more detailed instructions for passing off and submitting your work.

Objectives

Assignment

This assignment is to make your computer program guess a number that the user chooses between 1 and 1000 in no more than 10 guesses.

The algorithm used in this assignment is called a binary search. After each guess, the algorithm cuts the number of possible answers to search in half. Pseudocode for the complete program is given below; your task is to turn it into a working python program.

Your program should start by printing instructions to the screen, explaining that the user should pick a number between 1 and 1000 and the computer will guess it in no more than 10 tries. It then starts making guesses, and after each guess it asks the user for feedback. The user should be instructed to enter -1 if the computer needs to guess a lower number, 0 if the computer guessed correctly, and 1 if the computer needs to guess a higher number.

When the program guesses correctly, it should report how many guesses were required. If the user enters an invalid response, the instructions should be repeated and the user allowed to try again. You may assume that the user never gives a false answer.

If you have questions, discuss them with your instructor.

Part 1

We will break the assignment into two parts. Each part will involve the creation of one or two functions. When working together, the functions will create the full behavior of the program. Breaking a problem into smaller pieces to solve a bit at a time is call decomposition.

Giving instructions

Create a function called show_instructions that prints out a description of how to use the program for the users. This function does not receive any parameters. It does return any values.

This is an example of the instructions that may be printed by your function:

Pick a number between 1 and 1000 and I will try to guess it.
I can do this is no more than 10 guesses.

After each guess, enter:
0  - if I got it right
-1 - if I guessed too high
1  - if I guessed too low

Taking a guess

Create a function take_guess to implement a single guess by the program. The function should receive two integers for a formal-parameters that identify the current high and low values in the search.
It should return the response given by the user.

Below is a pseudo-code description of the algorithm you should use to implement the take_guess function.

As you create your take_guess function, you should test it to be sure it behaves correctly.

Below are some example outputs from the take_guess function.

The user responds with a 0. My guess is 500 . Please enter -1, 0, or 1: 0 I got it right.

The user responds with a -1. My guess is 500 . Please enter -1, 0, or 1: -1 I will guess lower.

The user responds with a 1. My guess is 500 . Please enter -1, 0, or 1: 1 I will guess higher.

The user responds with a 2, then a 0. My guess is 500 . Please enter -1, 0, or 1: 2 After each guess, enter: 0 - if I got it right -1 - if I guessed too high 1 - if I guessed too low My guess is 500 . Please enter -1, 0, or 1: 0 I got it right.

Part 2

The main program control

Create a function called main that controls the whole program. This function receives no parameters and returns no values.

Below is a pseudo-code description of the algorithm you should use to implement the main function.

Sample Executions of Part 2

The user picks 317

Pick a number between 1 and 1000 and I will try to guess it.
I can do this is no more than 10 guesses.

After each guess, enter:
0  - if I got it right
-1 - if I guessed too high
1  - if I guessed too low
My guess is 500 .
Please enter -1, 0, or 1: -1
I will guess lower.

My guess is 250 .
Please enter -1, 0, or 1: 1
I will guess higher.

My guess is 375 .
Please enter -1, 0, or 1: -1
I will guess lower.

My guess is 312 .
Please enter -1, 0, or 1: 1
I will guess higher.

My guess is 343 .
Please enter -1, 0, or 1: -1
I will guess lower.

My guess is 327 .
Please enter -1, 0, or 1: -1
I will guess lower.

My guess is 319 .
Please enter -1, 0, or 1: -1
I will guess lower.

My guess is 315 .
Please enter -1, 0, or 1: 1
I will guess higher.

My guess is 317 .
Please enter -1, 0, or 1: 0
I got it right.

That took 9 guesses.

Optional challenge

Instead of always picking a number from 1 to 1000, let the user pick the range when the program starts. Before the game starts, tell the user the maximum number of guesses that your program will require to guess the number. Note: this is not worth any extra points, so do not spend time extending your program unless you are satisfied that you have solved the assigned tasks.

Last Updated 03/12/2013