Every programming problem needs decomposing so that it can be properly understood. From this, an algorithm can be designed and tested.
# for the purpose of this example the array starts at 1
Options ← [“paper”, “rock”, “scissors”]
OUTPUT “Please enter your choice: 1, 2, or 3:”
player_choice ← USERINPUT
comp_choice ← RANDOM_INT (1, 3)
IF player_choice = comp_choice THEN
OUTPUT “draw”
ELSE
IF player_choice = 1 THEN
IF comp_choice = 2 THEN
winner ← “player”
ELSE
winner ← “comp”
END IF
ELSE IF player_choice = 2 THEN
IF comp_choice = 3 THEN
winner ← “player”
ELSE
winner ← “comp”
END IF
ELSE IF player_choice = 3 THEN
IF comp_choice = 1 THEN
winner ← “player”
ELSE
winner ← “comp”
END IF
IF winner = “player” THEN
OUTPUT choices[player_choice] + “ beats” + choices[comp_choice]
ELSE
OUTPUT choices[comp_choice] + “ beats “ + choices[player_choice]
END IF
END IF
Tests will be required to check that the final program runs correctly, although this will be more difficult than example one as one value is a random number. Because of the complexity of the program, validation is not included in the exam question. However, it would be expected if this was a programming project scenario.
Test no | Description | Test data | Test type | Expected outcome |
---|---|---|---|---|
1 | Both computer and player select 2 | 2 | Normal | “draw” |
2 | Computer selects winning choice of rock | 2 | Normal | “rock beats scissors” |
3 | Player selects winning choice of paper | 1 | Normal | “paper beats rock” |
1 | Both computer and player select 1 | 1 | Normal | “draw” |
2 | Computer selects winning choice of scissors | 3 | Normal | “scissors beats paper” |
3 | Player selects winning choice of rock | 2 | Normal | “rock beats scissors” |
The problem has now been fully decomposed and an algorithm has been designed.