Assignment 4: The Polymorphic Marble Solitaire
1 Purpose
2 Marble Solitaire with other shapes
3 Assignment Requirements and Design Constraints
4 The European  Solitaire  Model  Impl class
5 The Triangle  Solitaire  Model  Impl class
6 The main() method
6.1 To actually run your program with command line arguments in Intelli  J IDEA:
7 Deliverables
8 Grading standards
7.4

Assignment 4: The Polymorphic Marble Solitaire

Due: Wed 10/17 at 9:00pm; self-evaluation due Thurs 10/18 at 10:00pm

Starter files: code.zip

1 Purpose

The benefits of the model-view-controller architecture shine when we need to add new features, by isolating relevant parts of our design and changing them independently. In this assignment we will see those benefits pay off by supporting other forms of Marble Solitaire. The goal of this assignment is to give you a chance to critically examine your earlier design choices, and either leverage or revise them to enable adding variations of this game with minimal change and duplication of code.

All new classes and interfaces for this homework should be in the cs3500.marblesolitaire.model.hw04 package. All classes written in previous assignments, even if improved upon, should remain in their respective packages.

We are giving you a starter file whose sole purpose is to ensure your code is in the correct packages with the correct visibility.

2 Marble Solitaire with other shapes

As was observed in class earlier, there is nothing intrinsic about the board shape or size we’re using, and we can imagine the rules of this game being applied to boards of different sizes and shapes. The board that you implemented in Assignment 2 is known as English Peg Solitaire. In this assignment you will consider two more variations:

A European Peg Solitaire board is similar to an English board, except the corners between the arms of the cross are filled in to produce an octagon shape. The initial hole will again be in the center.

Perhaps a more interesting variant is Triangular Peg Solitaire. As its name suggests, the board for Triangular Peg Solitaire has pegs arranged in an equilateral triangle. A family of such games can be created by modifying the dimension of this board, in the form of the number of pegs in its bottom-most row. The following figure shows a 6-row Triangular Peg Solitaire version.

What sets this variant apart from the English and European versions is that the pegs are not arranged in a rectangular grid. We arbitrarily set a coordinate system to align with the board as shown in the figure above. Specifically, the pegs in row i (starting with row 0) can be indexed as (i,0) to (i,i). You may think of this as a pair of axes (like the rectangular grid) whose vertical axis is aligned with the left edge of the triangle (instead of vertical like that in the rectangular grid).

Like previous versions, this game also starts with a single empty slot whose position can be customized. Marbles can move by jumping over one marble into an empty slot, as before. Because the board is not rectilinear, the jump rules change a bit. Each marble can jump to (a) positions in its own row two columns away left or right; or (b) positions that are two rows above and below, along the four diagonal directions. For example in the above figure the marble from (5,3) can move to (5,1), (5,5), (3,1) and (3,3), if those positions were empty and they could jump over a marble. Similarly, the marble from (3,2) can move to (3,0), (5,2) and (5,4) and (1,0). As before, the score is the number of marbles on the board. The game ends when no more marbles can move, and the game must be played to minimize the score.

3 Assignment Requirements and Design Constraints

  1. Design classes implementing the European and Triangular variants of Marble Solitaire. (These are described in Section 4 and Section 5 below.)

  2. Implement a main method to allow you to choose different board shapes from the command line, when running your program. (This is described in Section 6 below.)

  3. Test everything thoroughly: make sure the new models work properly, and that the controller can control them as well as it could the original model. You do not need to test your main method, though.

You must complete these requirements while respecting the following constraints:

In this assignment it is important not only to have a correctly working model, but also a design that uses interfaces and classes appropriately. Make sure you minimize replication of code. You may refactor your earlier designs to do this. You may also have to change earlier implementations to remove bugs. This is OK, but must be properly documented and justified. Again, you are not allowed to change existing interfaces or add new public methods.

4 The EuropeanSolitaireModelImpl class

Design a class with the above name that implements the MarbleSolitaireModel interface from earlier assignments. Besides implementing all required methods, this class should offer the following constructors:

The getGameState() method should create a string that represents the game board, as before. Here is an example of the default board:

    O O O
  O O O O O
O O O O O O O
O O O _ O O O
O O O O O O O
  O O O O O
    O O O

(As with prior homework, there are no trailing spaces after the last position on each row, and the game state does not end in a newline character.)

Strong hint: this model is very, very similar to the one you build in Assignment 2. Your code should take advantage of that fact. Use an abstract base class to factor out the common code between these two model implementations. Ideally, the only code that should remain in the concrete classes are the constructors themselves.

5 The TriangleSolitaireModelImpl class

Design a class with the above name that implements the MarbleSolitaireModel interface from earlier assignments. Besides implementing all required methods, this class should offer the following constructors:

The getGameState() method should create a string that represents the game board, as before. Here is an illustrative example:

      O
     O O
    O O _
   O O O O
  O O O O O
 O O O O O O
O O O O O O O

(As with prior homework, there are no trailing spaces after the last position on each row, and the game state does not end in a newline character.)

Hint: this board is somewhat different from the other two, because its coordinate system is not standard Cartesian coordinates. Moreover, the set of legal moves from a given coordinate is different (as described earlier). Still, there is much that is similar between these models. You can, and should try to, successfully have this model extend the abstract base class above.

6 The main() method

Add the following class to your project:

package cs3500.marblesolitaire;

public final class MarbleSolitaire {
  public static void main(String[] args) {
    // FILL IN HERE
  }
}

This main() method will be the entry point for your program. Your program needs to take inputs as command-line arguments (available in your program through the argument args above). Review the documentation for command-line arguments in a Java program.

Specifically:

The following are some examples of valid command lines, and their meanings:

This is not an exhaustive list; other command lines are possible.

These arguments will appear in the String[] args parameter to your main method; you can use them however you need to, to configure your models. For this assignment, you do not need to explicitly handle invalid command lines (e.g. by producing an informative error message).

6.1 To actually run your program with command line arguments in IntelliJ IDEA:

You can repeat this process as many times as you want, to make as many run configurations as you need. Then to choose among them, use the dropdown menu next to the run icon in the toolbar:

and press Run.

7 Deliverables

All new classes and interfaces for this homework should be in the cs3500.marblesolitaire.model.hw04 package. All classes written in previous assignments, even if improved upon, should be in their respective packages.

8 Grading standards

For this assignment, you will be graded on

Please submit your homework to https://handins.ccs.neu.edu/ by the above deadline. Then be sure to complete your self evaluation by its deadline.