Board.java
import java.util.*;
public class Board {
public static
final int NUMBER_OF_SQUARES = 40;
private Square[] squares;
public Board() {
squares = new
Square[NUMBER_OF_SQUARES];
squares[0] = new Corner("Go", 0);
squares[1] = new Street("Mediterranean
Avenue", "purple", 1);
squares[2] = new CommunityChest(2);
squares[3] = new Street("Baltic Avenue",
"purple", 3);
squares[4] = new Tax("Income Tax", 4);
squares[5] = new RailRoad("Reading
Railroad", 5);
squares[6] = new Street("Oriental Avenue",
"lightBLue", 6);
squares[7] = new Chance(7);
squares[8] = new Street("Oriental Avenue",
"lightBLue", 8);
squares[9] = new Street("Oriental Avenue",
"lightBLue", 9);
squares[10] =
new Corner("Jail", 10);
squares[11] =
new Street("St. Charles Place", "lightPurple",
11);
squares[12] =
new Utility("Electric Company", 12);
// etc. etc.
}
// Returns a Square, given its index in the squares array
public Square getSquare(int index) {
return
squares[index];
}
// Just to check …
public static void
main(String[] args) {
Board b = new Board();
for (Square s
: b.squares) {
System.out.println(s);
}
}
}
//-------------------------------------------------------------------
// Compiling
and running class Board produces the following output..
// (with
liberal use of appropriate toString methods ...)
[1]: STREET
- Mediterranean Avenue
purple $99
[2]: Community Chest
[3]: STREET
- Baltic Avenue
purple $99
[4]: Income Tax
[5]: RAILROAD - Reading Railroad $99
[6]: STREET
- Oriental Avenue
lightBLue $99
[7]: Chance
[8]: STREET
- Oriental Avenue
lightBLue $99
[9]: STREET
- Oriental Avenue
lightBLue $99
...
Monopoly.java
import java.util.*;
public class MonopolyGame
{ // the controller
private final int MAX_NUM_PLAYERS
= 8;
private Board theBoard;
private PairOfDice dice;
private Player[] players;
public MonopolyGame() {
theBoard = new
Board();
dice = new PairOfDice();
players = new Player[MAX_NUM_PLAYERS];
}
public void move(Player p) {
dice.roll();
int total = dice.getTotal();
System.out.println(p.getName() + " moves "
+ total);
Token t = p.getToken();
t.move(total);
}
public void go() {
// Add some players
players[0] = new Player("Adam");
players[1] = new Player("Bob");
players[2] = new Player("Sally");
int nPlayers = 3;
// Each player chooses a piece / token
players[0].setToken(new
Token("Wheelbarrow", theBoard));
players[1].setToken(new
Token("Thimble", theBoard));
players[2].setToken(new
Token("Top hat", theBoard));
System.out.println("Ready to start ...");
for (int i = 0; i < nPlayers;
i++) {
System.out.println(players[i].getName()
+ " is the " +
players[i].getToken());
}
// First player takes a turn ...
move(players[0]);
System.out.println(players[0].getName() + " is
the "
+ players[0].getToken());
// Second player takes a turn ...
move(players[1]);
System.out.println(players[1].getName() + " is
the "
+ players[1].getToken());
// etc. etc.
/*
int nextPlayer = 0;
while (true) {
// Play forever!
move(p1ayers[nextPlayer]);
nextPlayer++;
nextPlayer = nextPlayer % nPlayers;
}
*/
}
public static void main(String[] args)
{
MonopolyGame g = new MonopolyGame();
g.go();
}
}
Token.java
public class Token {
private String name;
private Square restingOn;
private Board myBoard;
public Token(String name, Board b) {
this.name = name;
myBoard = b;
// Start on Go
restingOn = myBoard.getSquare(0);
}
public String toString() {
return name + " on " + restingOn.getName();
}
public void move(int nSquares) {
int curIndex = restingOn.getIndex();
int newIndex = (curIndex + nSquares)
% Board.NUMBER_OF_SQUARES;
restingOn = myBoard.getSquare(newIndex);
}
public static void main(String[] args)
{
Board b = new Board();
Token t = new Token("Thimble", b);
System.out.println(t);
t.move(5);
System.out.println(t);
}
}
Etc. etc.