20.12. Introduction to the Monopoly Program Solution
This section presents a sample domain layer of classes in Java for this iteration. Iteration-2 will lead to refinements and improvements in this code and design. Comments excluded on purpose, in the interest of brevity, as the code is simple.
Class Square
// all classes are probably in a package named
// something like:
package com.foo.monopoly.domain;
public class Square
{
private String name;
private Square nextSquare;
private int index;
public Square( String name, int index )
{
this.name = name;
this.index = index;
}
public void setNextSquare( Square s )
{
nextSquare = s;
}
public Square getNextSquare( )
{
return nextSquare;
}
public String getName( )
{
return name;
}
public int getIndex()
{
return index;
}
}
Class Piece
public class Piece
{
private Square location;
public Piece(Square location)
{
this.location = location;
}
public Square getLocation()
{
return location;
}
public void setLocation(Square location)
{
this.location = location;
}
}
Class Die
public class Die
{
public static final int MAX = 6;
private int faceValue;
public Die( )
{
roll( );
}
public void roll( )
{
faceValue = (int) ( ( Math.random( ) * MAX ) + 1 );
}
public int getFaceValue( )
{
return faceValue;
}
}
Class Board
public class Board
{
private static final int SIZE = 40;
private List squares = new ArrayList(SIZE);
public Board()
{
buildSquares();
linkSquares();
}
public Square getSquare(Square start, int distance)
{
int endIndex = (start.getIndex() + distance) % SIZE;
return (Square) squares.get(endIndex);
}
public Square getStartSquare()
{
return (Square) squares.get(0);
}
private void buildSquares()
{
for (int i = 1; i <= SIZE; i++)
{
build(i);
}
}
private void build(int i)
{
Square s = new Square("Square " + i, i - 1);
squares.add(s);
}
private void linkSquares()
{
for (int i = 0; i < (SIZE - 1); i++)
{
link(i);
}
Square first = (Square) squares.get(0);
Square last = (Square) squares.get(SIZE - 1);
last.setNextSquare(first);
}
private void link(int i)
{
Square current = (Square) squares.get(i);
Square next = (Square) squares.get(i + 1);
current.setNextSquare(next);
}
}
Class Player
public class Player
{
private String name;
private Piece piece;
private Board board;
private Die[] dice;
public Player(String name, Die[] dice, Board board)
{
this.name = name;
this.dice = dice;
this.board = board;
piece = new Piece(board.getStartSquare());
}
public void takeTurn()
{
// roll dice
int rollTotal = 0;
for (int i = 0; i < dice.length; i++)
{
dice[i].roll();
rollTotal += dice[i].getFaceValue();
}
Square newLoc = board.getSquare(piece.getLocation(), rollTotal);
piece.setLocation(newLoc);
}
public Square getLocation()
{
return piece.getLocation();
}
public String getName()
{
return name;
}
}
Class MonopolyGame
public class MonopolyGame
{
private static final int ROUNDS_TOTAL = 20;
private static final int PLAYERS_TOTAL = 2;
private List players = new ArrayList( PLAYERS_TOTAL );
private Board board = new Board( );
private Die[] dice = { new Die(), new Die() };
public MonopolyGame( )
{
Player p;
p = new Player( "Horse", dice, board );
players.add( p );
p = new Player( "Car", dice, board );
players.add( p );
}
public void playGame( )
{
for ( int i = 0; i < ROUNDS_TOTAL; i++ )
{
playRound();
}
}
public List getPlayers( )
{
return players;
}
private void playRound( )
{
for ( Iterator iter = players.iterator( ); iter.hasNext( ); )
{
Player player = (Player) iter.next();
player.takeTurn();
}
}
}