A Nonogram is a logic puzzle where a player needs to colour in boxes. The puzzle is laid out as a grid and each square needs to be either coloured black or left white.
The numbers at the side of each row and column tells the player how many of the boxes are coloured in consecutively. Where a row has two or more numbers, there must be a white square between the coloured squares.
Juan is creating a program that will store a series of Nonograms for a user to play. The game will randomly select a puzzle and display the blank grid with the numbers for each row and column to the user.
The user plays the game by selecting a box to change its colour. If the box is white it will change to black and if it is black it will change to white. The user can choose to check the answer at any point, and the game will compare the grid to the answers and tell the user if they have got it correct or not.
Juan creates a modular program with a number of subroutines. The program will use two integer 2-dimensional arrays to store the puzzles:
puzzle(5,5)
stores the solution
answerGrid(5,5)
stores the user’s current grid.
A 0 represents a white box and a 1 represents a black box.
Juan creates a function, countRow()
, to count the number of coloured boxes in one row and return the number of consecutive coloured boxes in that row. If there is more than one set of coloured boxes in the row, these are joined together and the string is returned.
For example, in the following grid countRow
for row 0 will return "2" as a string, and countRow
for row 2 will return "1 1" as a string. If there are no 1s in a row, then "0" is returned as a string.
Complete the pseudocode algorithm countRow()
.
01 function countRow(puzzle:byref, rowNum:byval)
02 count = 0
03 output = " "
04 for i = 0 to ……………………………………………
05 if puzzle[rowNum, i] == …………………………………………… then
06 count = count + 1
07 elseif count >= 1 then
08 output = output + str(……………………………………………) + " "
09 count = 0
10 endif
11 next i
12 if count>= 1 then
13 output=output+str(count)
14 elseif output == "" then
15 output = "……………………………………………"
16 endif
17 return ……………………………………………
18 endfunction