Exercise Sheet C


Your work for this exercise sheet will be automatically collected in (if you put it in the right place) at 5 p.m. on Friday 6th December.

Put your work into a subdirectory of your cs2000 directory called sheetC.

In this exercise, you will write a program for playing the Hindu board game of Moksha-Patamu. In this game, players move around a board according to the throw of a dice. Players are occasionally rewarded (for virtue, faith, reliability, generosity, knowledge and asceticism) by being allowed to directly ascend to later squares on the board. Other times, they are punished (for disobedience, vanity, vulgarity, theft, lying, drunkenness, debt, rage, greed, pride, murder and lust) by being required to directly descend to earlier squares on the board. The first to reach the final square wins.

In case you haven't guessed, Moksha-Patamu is the original form of Snakes and Ladders. These days the ladders and snakes are no longer labeled with rights and wrongs. Some might say that this reflects the moral vacuum at the heart of modern society.

Part 1

Write a class definition Dice. For generality, we will allow our dice to be n-sided dice, where n is supplied as a parameter to the constructor.

The interface is as follows:

Part 2

The next class definition we need will represent squares on the board. This has quite a lot of methods, but first here are some hints about its instance variables.

Now here's the interface for your Square class definition:

Part 3

Now it's time to write Player. There are three instance variables: the player's name (use a String for this; don't bother with PersonName on this occasion); the player's dice; and the player's current square.

Part 4

There's just one class definition remaining. It's called SnakesAndLadders. I've made a start on it for you, which you can obtain here.

I've already defined its instance variables, written its constructor, some getters and a main method. The parts I've written create the Snakes & Ladders board, i.e. the parts I've written create an appropriate number of Square objects, and link them together in sequence, using your Square class definition. My code also generates a random number of snakes and ladders and places them into the squares.

So there is only one thing left for you to do, and that is to write the play() method.

Assume that the players (in the array called players) take turns in the same order as they appear in the array.

Place each player onto the starting square of the board. Then each takes a turn to throw their dice and move to a new square. If anyone reaches the final square, the game is immediately over.

Include some print statements in your play method and add some extra print statements to your moveBy method in Player so that when you run the game, the user gets some idea of what is going on.

Here's some example output for three players:

All 3 players are at the start.
Ann rolls 1
After moving 1 place, s/he is now at square 2
But this contains a ladder! S/he clambers to square 16
Bob rolls 4
After moving 4 places, s/he is now at square 5
But this contains a ladder! S/he clambers to square 26
Col rolls 5
After moving 5 places, s/he is now at square 6
Ann rolls 6
After moving 6 places, s/he is now at square 22
Bob rolls 5


Part 5

You've finished. This part is just for fun.

I've written some other class definitions to provide a GUI for your Snakes and Ladders game. My class definitions use your class definitions. So if yours are wrong, my stuff may not compile or may not run correctly.

You need to obtain copies of the following files: MokshaPatamu.java, SLController.java, SLGUI.java and SLSquareGUI.java.

Compile using

javac MokshaPatamu.java

and run using

java MokshaPatamu

The GUI doesn't use lovely pictures of snakes and ladders. Sorry - I've no time to make it look better.

Challenge Exercise

This Challenge Exercise does not involve programming. It only involves thinking!

As mentioned above, my code for randomly generating the snakes and ladders sometimes generates cyclic structures, e.g. a snake head in square i might have its tail in square j; but j might be the foot of a ladder that takes you back up to i!

The challenge is to write down a precise set of rules for the legal placement of snakes and ladders on a m by n board.

If you think this is easy, then you haven't thought about it at all!

Discuss your ideas with me and/or put them into a text file in sheetC/challenge.

If you're mad keen, you could try to implement your rules in Java and add them to your program.