Objectives

  • Inheritance
  • Static methods (i.e. main()) and variables
  • More practice on 2-D arrays

Provided Files: SnLSquareTest.java, SorLSquareTest.java, SnakeSquareTest.java, LadderTest.java, SnakesAndLadderTest.java

Re-used Files: Dice.java

Submit Files: SnakesAndLadders.java, SnLSquare.java, SorLSquare.java, Ladder.java, Snake.java

Background

We will build the Snakes-and-Ladders game. If you have never played it, do a Web search of the game and familiarize yourself with the games rules.

The board that you will have to ultimately create is shown below. see image.

UML Class Diagram

The entire class diagram for the assignment is below. Except where noted below, you must follow the UML exactly including upper and lower- case letters. You are learning programming conventions by doing so. Explanations for the diagram are given in the remainder of the document. Keep this page as a reference.

  • Diamonds are for the has-a composition relationship between classes
  • Triangles are for the is-a inheritance relationship between classes.

See diagram: see image.

Part 1: The Square Hierarchy

Prepare the implementation of the entire inheritance hierarchy, but work incrementally.

1. Write the SnLSquare class. Test it until all unit tests pass.

  • The format for the toString() method is: < number>
    • < > means that it is a variable and you must substitute an actual value; dont include the angle brackets in your string .
    • Example: 1
  • The landOn() method should simply return the squares number.

2. Write the SorLSquare class. Test it until all tests pass. Hopefully, the tests for equals() fail in order to teach you about the proper implementation of the equals() method within an inheritance chain. If you pass on the first time, youre a star!

  • The constructor must throw an IllegalArgumentException if the endSquare is the same as the square itself.
    • Note: We have not covered this topic in class yet; however, there are plenty of examples in Assignment 1s provided code. Learn by doing. Teach yourself.
  • The format for the toString() method is: < number>:< endSquare>
  • Example: 12:45 Means that square 12 is connected to square 45

3. Write one-by-one the Ladder and Snake classes. Test one. Test the other.

  • The constructor of LadderSquare must throw an IllegalArgumentException if the endSquare is lower than itself; the SnakeSquare if it is higher than itself.
  • The format for the Snake::toString() method is: < number>-< endSquare>
    • Example: 12+34 Means that square 12 has a ladder going up to square 34
  • The format for the Ladder:: toString() method is: < number>+< endSquare>
    • Example: 34-12 Means that square 32 has a snake going down to square 12
  • Notice that these two subclasses do not have any local instance variables. For that reason, I have left a question for you in the UML: Do you have to override the equals() method in these classes? You have to make the decision: To override or not to override? You will be marked on your choice. You should also be prepared to explain your choice on an exam.

Part 2: The Game Engine

Implement and test the SnakesAndLadder class.

  • The constructors shall construct a board identical to that shown above, at the top of the document.
    • Squares are to be numbered from 1 to 100 (Max).
    • All players should start on Square 1.
    • The default constructor should build a default game with two players.
  • The takeTurn() method should mimic the actions of a player taking their turn:
    • Roll the dice and calculate to which square the player should move.
    • To win, a player must land EXACTLY on the final square. If the roll is too large, the player must move backwards, in excess amount
    • Example: If the player is on square 98, and rolls a 5, the player moves 2 forward to square 100, but then moves 3 backwards, finally landing on square 97.
    • Move and land on the new square.
    • Return true if a double was rolled (so that another turn can be taken); otherwise return false.
  • The getWinner() method should return the first-found player that is located on the final square; return -1 if no player is found.
  • The format for the toString() method is a text representation of the board, with each square separated by the vertical slash | as well as padded spaces; ten squares per line.
    • (Partial) Example:
| 1 | 2 | 3 | 4+14 | 5 | 6 | 7 | 8 | 9+31 | 10 <- Notice the tw.o ladders in this row
| 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 and so on
  • The format for the toStringCurrentPosition() method is a text representation of the current position of all players (without the board), separated by a space, all on one line.
    • Example: 1:15 2:34 (Player 1 on Square 15 and Player 2 on Square 34).

A full transcript of a sample game is provided at the end of Part 3 to help you understand the expected format of the toString() methods.

Part 3: The Game Client

By this time you have implemented all the classes and tested them using the provided test clients. You must complete this assignment by writing a game client that automatically plays an entire game. You are replacing the test clients with your own client. This client will be written as part of the SnakesAndLadders class by adding a main() method to it.

1. In SnakesAndLadders class, add the call signature for the main() method

public static void main(String args[]) { }

2. In the main() method:

  • Instantiate an object of the SnakesAndLadders class.
  • Print out the board
  • Add code to repeatedly have each player take their turn until one of them reaches the final square. After each turn, print out the locations of all players. Dont forget the players take another turn if they rolled a double.
  • Print out a final message saying who won.

3. Run the program by right-clicking on the SnakesAndLadders class (in the left navigation pane) and selecting Run.

4. Play your game with various numbers of players, simply by changing the value of the NUM_PLAYERS constant.

Sample Transcript to show you expected outputs. Please remove extra debugging statements.

  • Notice how the printed board below matches the image on the first page.
Snakes and Ladders
| 1 | 2 | 3 | 4+14 | 5 | 6 | 7 | 8 | 9+31 | 10
| 11 | 12 | 13 | 14 | 15 | 16 | 17-7 | 18 | 19 | 20+38
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28+84 | 29 | 30
| 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40+59
| 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50
| 51 | 52 | 53 | 54-34 | 55 | 56 | 57 | 58 | 59 | 60
| 61 | 62-18 | 63+81 | 64-60 | 65 | 66 | 67 | 68 | 69 | 70
| 71+91 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80
| 81 | 82 | 83 | 84 | 85 | 86 | 87-24 | 88 | 89 | 90
| 91 | 92 | 93-73 | 94 | 95-75 | 96 | 97 | 98 | 99-78 | 100
Player 0 rolled 12
0:13 1:1
Player 0 rolled 12
0:25 1:1
Player 0 rolled 8
0:33 1:1
Player 0 rolled 9
0:42 1:1
Player 0 rolled 10
0:52 1:1
Player 0 rolled 10
Oh no!
0:18 1:1
…..
.
0:91 1:1
Player 0 rolled 2
Oh no!
0:73 1:1
Player 0 rolled 7
0:80 1:1
Player 0 rolled 11
0:91 1:1
Player 0 rolled 9
Player 0 wins.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.