The University of Western Australia
CITS2002 Systems Programming
 

Department of Computer Science and Software Engineering

CITS2002 Systems Programming

Workshop 4: Functions and 2-D arrays

Consider a simple game often seen on mobile phones, with names such as Jawbreaker and Bejeweled. The game consists of cells arranged in a square; each cell initially holds an object of one of (say) 4 colours. The goal is to remove objects by clicking on them; removing an object also removes each of its neighbours that have the same colour (in the directions North, South, East, West).

Every time n neighbouring like-coloured objects are removed, you score n2 points.

jawbreaker Consider the game at left - a 6x6 board employing 4 colours. The top-left cell has the coordinates (col=0,row=0), the bottom-right cell has the coordinates (col=5,row=5). You are NOT expected to develop a graphical interface!

You are asked to think about these things:

  • How, and where, would you declare and define a 2-dimensional array to hold each cell's character values of 'R', 'G', 'B', 'Y', or '  ' (for empty)?

  • Consider the function     void reset(void);
    which initializes each cell's colour. Note - the expression   (rand() % 4)  produces one of 4 random integers: 0, 1, 2, or 3.

  • Consider the function     bool valid(int col, int row);
    which determines if the indicated coordinates are 'within' the array (on the board).

  • Consider the function     int click(int col, int row);
    which simulates someone clicking in the indicated cell.

    1. If the cell is non-empty, we remove its object, and all neighbouring objects (N,S,E,W) of the same colour (yes, this part is getting hard!).
    2. When all like-coloured neighbours have been removed, objects "fall down" if there is an empty cell below them.
    3. If n like-coloured objects are removed, the function should return the value of n2.
    4. chilli If ever a complete column becomes empty, adjacent columns should "move toward the centre of the board" to keep all objects together.


BEFORE the workshop session you're strongly encouraged to think how you would do it. You are not expected to have developed and tested a perfect solution before the workshop, but you should at least scratch out your approach on paper.

From the information recently presented in lectures, we know:

  • our programs can have functions, which identify a single task or calculation to be performed,
  • functions may receive parameters, which direct how the function performs its task,
  • functions may return a value, often a calculated result, to the code which called our function,
  • we may combine multiple pieces of data, and refer to each using a single name, into an array,
  • arrays may have 1, 2, 3, or more dimensions, which may be considered similar to a vector, rectangle, cube, ... of data
  • we employ integer indices to access individual array elements.

So, before the workshop session, think how you can combine the (above) information that we already know to develop a solution.

Department of Computer Science and Software Engineering

This Page

Written by: Chris.McDonald@uwa.edu.au