Photo AI

A programmer is writing a game - AQA - GCSE Computer Science - Question 13 - 2023 - Paper 1

Question icon

Question 13

A-programmer-is-writing-a-game-AQA-GCSE Computer Science-Question 13-2023-Paper 1.png

A programmer is writing a game. The game uses a 3 x 3 grid containing nine squares. In the game, a square on the grid is referred to by a letter and a number. For e... show full transcript

Worked Solution & Example Answer:A programmer is writing a game - AQA - GCSE Computer Science - Question 13 - 2023 - Paper 1

Step 1

Use the variable check

96%

114 rated

Answer

Implement a boolean variable check initialized to false, which controls the loop for input validation.

Step 2

Provide the following steps until a valid grid reference is entered

99%

104 rated

Answer

Use a while (check == false) loop to repeatedly prompt the user until a valid reference is received.

Step 3

Output an error if a grid reference

96%

101 rated

Answer

Inside the loop, check the length of the string. If it's not equal to 2, print an error message and prompt for a new entry.

Step 4

Where the first character is not a valid letter

98%

120 rated

Answer

Check if the first character is not among 'A', 'B', or 'C'. If true, display an error message indicating the valid characters.

Step 5

Where the second character is not a valid number

97%

117 rated

Answer

Check if the second character is not among '1', '2', or '3'. If true, display an error message indicating the valid numbers.

Step 6

Show your code in a logically appropriate manner

97%

121 rated

Answer

Here’s a sample implementation:

bool check = false;
while (check == false)
{
    string square = "";
    while (square.Length != 2)
    {
        Console.Write("Enter grid reference (eg C2): ");
        square = Console.ReadLine();
        square = square.ToUpper();

        if (square.Length != 2)
        {
            Console.WriteLine("Error: Grid reference must be 2 characters.");
        }
        else if (square[0] < 'A' || square[0] > 'C')
        {
            Console.WriteLine("Error: First character must be A, B, or C.");
        }
        else if (square[1] < '1' || square[1] > '3')
        {
            Console.WriteLine("Error: Second character must be 1, 2, or 3.");
        }
        else
        {
            check = true;  // Valid input received
        }
    }
}

This code nicely encapsulates the requirements for checking the grid reference.

Join the GCSE students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;