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 [“paper”, “rock”, “scissors”]. 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 “rock beats scissors” or “draw”.

Decompose the problem

The first step is to break down - decompose - the overall problem into several smaller, more easily solved problems:

  1. Find out the player’s choice.
  2. Assign a random choice for the computer.
  3. Work out if the outcome was a draw.
  4. If not, work out which choice won.
  5. Output the winning choice over the losing choice.

Variables and constants

From the step above, the solution will require the following variables. These values will change as the program is run.

VariableData type
optionsArray of string

*As user input is needed here, a string is valid as they may answer yes/no.

In this example, no constants are used as the only data that doesn’t change is stored in an array. Not every algorithm uses constants.