Assignment 7: Magic Square Game

Due April 5, 12 noon, worth 5% of your final mark. Late assignments can't be accepted.

Game Description

Suppose you have 8 small squares with the numerals 1 through 8 printed on them, and a 9th square with some special symbol (of your choice) printed. The squares are arranged in 3 rows, 3 squares per row. For example, if I decided that my special symbol should be * my arrangement might look like:

1*3
426
758

You are allowed to swap the special symbol with the numeral printed on any of its neighbours (the squares immediately to its left or right, above, or below it). For example, starting with the arrangement above, I can restore the "natural" arrangement (below) by swapping the * with the 2, then with the 5, and then with the 8:

123
456
78*

The object of the game is to begin with the squares in one (possibly random) order, and to get them into another order (for example, the "natural" order shown above), using only swaps between the special symbol and neighbouring numerals. There is a plastic version of this game (3x3 or 4x4) where the squares slide, and the special symbol is represented by an empty square.

Your job

You will build this game in Java. Your version will be a little more flexible: the user can choose the number of rows/columns, and the user can choose to begin with the arrangement either in a random order or in the "natural" order where the numerals are ordered in consecutive rows from left to right, and the special symbol is in the bottom right square.

Details

You are required to design a main class called MagicSquareGame, which is what the user runs in order to play the game. MagicSquareGame, in turn, uses (at least) one other class, with name(s) chosen by you. Each session of MagicSquareGame must do the following:
  1. Ask the user for an integer from 2 to 9 (inclusive), which indicates the number of rows and columns the game will have.
  2. Ask the user to indicate whether she or he wants the numerals arranged in random or "natural" order.
  3. Start the game with the number of rows and columns, and random/natural arrangement, as indicated by the user. If the user selects r rows, you will use the numerals from 1 through r*r-1 plus some special symbol. Display the rows of numerals, and indicate to the user how to quit the game (this should be possible at any move).
  4. Tell the user how to indicate which numeral to swap with the special symbol. Carry out the swap if the numeral chosen is a neighbour of the special symbol. Indicate that the swap is impossible otherwise.
  5. Repeatedly display the rows of numerals and special symbol after each swap, and allow the user to indicate another swap, until it's time to quit.

Marking

This assignment cannot be automarked --- your game will be played and evaluated by a flesh-and-blood Teaching Assistant. They will assign up to 15 points as follows:
Danny Heap
Last modified: Sat Mar 16 08:38:29 EST 2002