Provided Code

Download a JBuilder project containing code to get you started: monopoly.zip. The project contains two packages. monopoly.gui contains a graphical user interface (view and controllers) while monopoly.model will contain the model. Your early work will be exclusively in the model.

About monopoly.gui

The code in monopoly.gui is complete and working. You should not modify it in any way until after your program plays a legal game. In particular, most of the classes and methods have package visibility rather than public visibility. You are not permitted to change this.

The user interface, monopoly.gui, requires that the model contain the following public classes with the given public methods:

Class/Method Description
Square A class representing a square on the board such as "Vermont Ave." or "Reading Railroad" or "Go".
getDescription Returns a string describing the property. The string should contain newline characters ("\n") to format it into appropriate lines.
getPlayers Returns an array containing the Players currently on this property.
addObserver A method to add an observer that responds to changes to the property object. This code is provided.
Player A class representing a player in the game.
getName Returns a string containing the name of the player.
getOwnedPropertyNames Returns an array filled with the names of the properties owned by this player.
getBalance Returns an integer with the player's current bank balance.
getNetWorth Returns an integer with the player's current net worth.
canBuyOccupiedSquare Returns true if the player can buy the property they have landed on; false otherwise.
buyHouse Instructs the player to purchase a house for a property.
buyOccupiedSquare Instructs the player to purchase the property they just landed on.
addObserver A method to add an observer that responds to changes to the property object. This code is provided.
Monopoly A class representing the entire game.
playGame A method that plays the game. When this method finishes, the game is over.
getNumPlayers Returns the number of players in the game.
getNumSquares Returns the number of squares on the board.
getPlayer Returns the Player object specified by a parameter.
getSquare Returns the Square object specified by a parameter.

About monopoly.model

The model is where you will spend the vast majority of your time in the first part of the project. The provided code includes the classes required by the interface and stubs for the methods the user interface calls. A stub is just enough for the program to compile -- if the method is a command, it will be empty; if it's a query, it will return a constant such as false, 0 or null.

A class diagram showing the classes and methods required by the user interface is shown below. You will need to add several more classes and methods to the existing classes to make the program work as required.

Communication between the model and the user interface

Every time the model changes, the view (the display portion of the user interface) must update the information it displays. The view is notified of changes in the model when the model calls the view's update method. The view then queries the model for its current state and redisplays itself. That is, it uses queries such as getBalance and getOwnedPropertyNames to find out what information it should display to the user.

The model is not one big thing -- it contains many distinct parts. There are a relatively small number of Player objects and a larger number of Square objects. Similarly, the user interface is not a single, massive, view. Parts of it are dedicated to displaying players while other parts are dedicated to displaying squares. Each part of the model (each Player object and each Square object) keeps a list of observers that are concerned about changes to that Player or Square object. Each observer has a method named update. When a model object changes, it should call the update method in each of the observers on its list. A view adds itself to the list by calling the addObserver method in Player or Square.

The provided code includes a method, updateObservers (note the "s" at the end of the name), that calls the update method in each of the registered listeners. Call this method whenever the model changes. For example, a method to change the bank balance might be:

public void adjustBalance(int amt)
{  this.bankBalance += amt;
   this.updateObservers(this, null);
}

The first parameter to updateObservers is always the model object that changed. The second parameter can included additional information to be used by the view. It is often set to null.

One of the registered observers might be a PlayerView object. It has a method similar to this:

public void update(Object model, Object changeInfo)
{  if (model instanceof Player)
   {  Player p = (Player)model;
      this.balanceLabel.setText("$" + p.getBalance());
      this.netWorthLabel.setText("$" + p.getNetWorth());
      ...
   }
}