Abcol Logo

You have now reached Chrissie's web pages for

Java Programming

Javopoly - Tutorial 1. Basic Abstraction.

As part of this series of tutorials, we are going to build a command-line based virtual board game. This game will simulate the board shown below, and is designed for four players.

Playing Board with 12 squares

When the program opens, a dialog will be displayed to obtain the names of the four players. A menu will then be displayed as follows:-

Option 1 will accept a number from 1 – 6 and increment the position of the player on the board. This will happen for each of the 4 players in turn. Note that if the roll of the die takes the player past the “Craiginches” square, they continue on past “Go” again, collecting £200 on the way.

If the player lands on a property square that he/she owns, no action is taken. If the property is not owned by any player, the person who landed on it has the option to buy it. In this case, their money is decreased accordingly but they then become the owner of that property. The property cannot then subsequently be re-sold.

If the player lands on a square that is owned by another player, a rent is calculated depending on the rent attribute of that property. The players’ money is decreased and the owners’ money is increased, by the appropriate amount.

If a player lands in “Craiginches”, their money is decreased by £50 to pay their bail.

Option 2 will display a sub-menu as follows :-

The first choice displays the name of the player, their position on the board and how much money they hold. The second choice shows the colour, position and name of the property, plus the name of the player who owns it. At the end of the display the program will return to the sub-menu. The third choice returns the program to the main menu.

Option 3 displays both reports in turn then ends the program.

Each player starts the game on the Go! square with £200.

At first this may seem a very complex exercise, but over the course of the tutorials I will show you how to design the program structure using UML and implement it using any Java compiler.

Back to TOP

When doing a first draft of your design, try and get a "feel" of the entities, or objects, you might use. Do not try to split the project up into detailed, complex elements at this stage! Two or three classes, plus the top level, will be fine for the moment.

You can use the "highlighting pen" method if you wish, to start you off. Read the brief and use a pen to highlight ...

Then apply the four following tests to the words you have highlighted.

Finally you should have a list of possible objects. It is probably better at this stage to have too few than too many. You can always take it to a more detailed level of abstraction later on.

When writing this application I started with only two classes, plus my top-level "application" class and a Terminal Input. The classes I chose were "Player", which will be used to create four player objects, and "Board" which creates one playing board. The UML diagram for this design will look like this -

UML diagram for first draft

The code for the player, board and top level follow. These are all included in a zip file along with the Terminal Input. You can FTP this to your UNIX directory and run it, but do not expect it to do a whole lot yet! We will, of course, be adding to it as the tutorials progress.

Back to TOP

public class Board
{
 public Board()
 {
 }

 //special method to display the whole playing board
 public void printBoard()
 {
  System.out.println ( "Welcome to Javopoly!");
  System.out.println();
 }
}

//define the class Player and make it public
public class Player
{
 //declare variables first

 private String theName = ("Not Specified");
 private int thePosition = 0;
 private double theMoneyHeld = 200; //defaults to start

 //constructors first
 public Player ()
 //we will always start the game with default values.
 {
 }

 public String getName()
 {
   return new String (theName);
 }

 public int getPosition()
 {
  return thePosition;
 }

 public double getMoneyHeld()
 {
   return theMoneyHeld;
 }

 //we are now going to allocate various default values
 //to the variables. For this we will use a SET method.
 //It is a VOID method because it never returns values
 //outside of this class.

 public void setName (String name)
 {
  theName = name;
 }

 public void setPosition (int position)
 {
   thePosition = position;
 }

 public void setMoneyHeld (double money)
 {
  theMoneyHeld = money;
 }

 //special methods

 public String toString()
 {
  String thePlayer = new String (this.getName() + " is on Square " +this.getPosition() + " and holds " + this.getMoneyHeld());
  return thePlayer;
 }

}

//this is the whole class Player declared.

public class TestDriver
//tests the Board and Player classes.
//all it does so far is print out the board and the names of the players!
{
  public static void main (String [] args)
  {

    TerminalInput dialogue = new TerminalInput(System.in);
    Board theGame = new Board();
    Player player0 = new Player();
    Player player1 = new Player();
    Player player2 = new Player();
    Player player3 = new Player();

    theGame.printBoard();

    System.out.println ("Enter a name for first player =>");
    player0.setName (dialogue.readString());
    System.out.println ("Enter a name for second player =>");
    player1.setName (dialogue.readString());
    System.out.println ("Enter a name for third player =>");
    player2.setName (dialogue.readString());
    System.out.println ("Enter a name for fourth player =>");
    player3.setName (dialogue.readString());

    System.out.println("Now we are ready to begin the game....");
    System.out.println (player0.toString());
    System.out.println (player1.toString());
    System.out.println (player2.toString());
    System.out.println (player3.toString());

  }
}

Continue on to Tutorial 2... adding some squares, and setting up the players in an array.

Architecture Home Pascal Home Web Design Home Other Stuff Home Other Stuff Home

Comments? c.nyssen@abcol.ac.uk
Last Updated - 13/11/03

Best viewed at 800x600 in 16-bit high color with Trebuchet MS installed. If you do not have this font, click here