2013 Old Spec
Question 4
The algorithm, represented using pseudo-code in Figure 4, and the variable table, Table 3, describe a simple two player game. Player One chooses a whole number between 1 and 10 (inclusive) and then Player Two tries to guess the number chosen by Player One. Player Two gets up to five attempts to guess the number. Player Two wins the game if they correctly guess the number, otherwise Player One wins the game.
Note that in Figure 4, the symbol <> means "is not equal to".
Figure 4
OUTPUT "Player One enter your chosen number: "<br> INPUT NumberToGuess <br> WHILE NumberToGuess < 1 OR NumberToGuess > 10 DO<br> OUTPUT "Not a valid choice, please enter another number: "<br> INPUT NumberToGuess<br> ENDWHILE<br> Guess = 0<br> NumberOfGuesses = 0<br> WHILE Guess <> NumberToGuess AND NumberOfGuesses < 5 DO<br> OUTPUT "Player Two have a guess: "<br> INPUT Guess<br> NumberOfGuesses = NumberOfGuesses + 1<br> ENDWHILE<br> IF Guess = NumberToGuess <br> THEN OUTPUT "Player Two wins"<br> ELSE OUTPUT "Player One wins"<br>
Table 3
Identifier | Data type | Purpose |
---|---|---|
NumberToGuess | Integer | Stores the number entered by Player One |
NumberOfGuesses | Integer | Stores the number of guesses that Player Two has made so far |
Guess | Integer | Stores the most recent guess made by Player Two |
What you need to do
- Write a program for the above algorithm.
- Test the program by conducting the tests Test 1 and Test 2.
Test 1
Test that your program works correctly by conducting the following test:
- Player One enters the number 0
- Player One enters the number 11
- Player One enters the number 5
- Player Two enters a guess of 5
Test 2
Test that your program works correctly by conducting the following test:
- Player One enters the number 6
- Player Two enters guesses of 1, 3, 5, 7, 10