A programmer is writing a game. The game uses a row of cells represented as an array. Figure 20 shows an example.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| X | X |
Figure 21 describes how the game is to be played.
| The player starts at position 0 in a row of cells. |
| The aim of the game is for the player to reach the end of the row. |
| At each turn the player must enter either 1 or 2: entering 1 increases the player's position by 1; entering 2 increases the player's position by 2. |
| If the player's position goes beyond the end of the row or contains an X, the message Bad move is displayed and the player goes back to position 0. |
| These steps are repeated until the player reaches the end of the row. |
| If the player reaches the end of the row the game is finished. |
In the example in Figure 20, the player starts at position 0. Inputs 1 then 2 attempt to reach position 3, which contains X, so Bad move is displayed and the player returns to 0. Inputs 2, 2, 1 and 2 then reach positions 2, 4, 5 and 7, finishing the game.
Figure 22 shows part of a C# program that will be used for the game.
| int pos = 0; |
| int lastPos = row.Length - 1; |
| while (pos < lastPos) |
| { |
pos is a variable that contains the player's current position.
Extend the program from Figure 22 so that the game works as described in Figure 21.
- there is an array called row
- the number of X characters in row can vary
- the position of the X characters in row can vary
- the X characters have already been added to the array called row
- the row array can be of any length
You should use meaningful variable name(s) and C# syntax in your answer.
Your answer