Example programming problem about computer games
This section examines how to take a problem, decompose it and design an algorithm to solve it.
The following example has been taken from an AQA past paper. It reflects the type of question that may appear in an exam paper.
The problem
A programmer has been asked to design a program that enables a player to play the game rock paper scissors. The choices of rock, paper, and scissors are stored in an array [鈥減aper鈥, 鈥渞ock鈥, 鈥渟cissors鈥漖. The player enters the number 1 for paper, number 2 for rock, and number 3 for scissors. If both the user and the computer choose the same number, they draw. At the end of the round, the program outputs which choice beat the other, eg 鈥渞ock beats scissors鈥 or 鈥渄raw鈥.
Decompose the problem
The first step is to break down - decompose - the overall problem into several smaller, more easily solved problems:
- Find out the player鈥檚 choice.
- Assign a random choice for the computer.
- Work out if the outcome was a draw.
- If not, work out which choice won.
- Output the winning choice over the losing choice.
Variables and constants
From the step above, the solution will require the following variableA quantity that can change or that may take on different values. Variable also refers to a letter or symbol representing such a quantity.. These values will change as the program is run.
Variable | Data type |
| Array of string |
| Integer |
| Integer |
| String |
Variable |
|
---|---|
Data type | Array of string |
Variable |
|
---|---|
Data type | Integer |
Variable |
|
---|---|
Data type | Integer |
Variable |
|
---|---|
Data type | String |
*As user input is needed here, a stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. is valid as they may answer yes/no.
In this example, no constantA value in computer programming that does not change when the program is running. are used as the only data that doesn鈥檛 change is stored in an array. Not every algorithm uses constants.