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:
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:
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:
- Ask the user for an integer from 2 to 9 (inclusive), which
indicates the number of rows and columns the game will have.
- Ask the user to indicate whether she or he wants the
numerals arranged in random or "natural" order.
- 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).
- 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.
- 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:
- 2 points: if
MagicSquareGame compiles, and uses at least
one other class.
- 5 points: if your program displays the
rows of numerals and special character (in some fashion) at the
start, allows the user to make legal swaps, refuses to make
illegal swaps, redisplays the rows of numerals and special
character after each swap, and allows the user to quit. You may
earn these points even if the number of rows/columns is not
user-selectable (but at least 3), or if the user can't select
random/natural order of the numerals at start up.
- 2 points: if your program allows the user
to begin with the rows of numerals and special character in
random order, if they wish, or natural order otherwise.
- 2 points: if your program allows the user
to decide the number of rows and columns (the choice must be
an integer from 2 to 9, inclusive.
- 2 points: for good design: you share the
program's responbilities in a thoughtful way between 2 or more
classes.
- 2 points: for good style, for example see style guide.
Danny Heap
Last modified: Sat Mar 16 08:38:29 EST 2002