dd.util
Class FreewaySupport

java.lang.Object
  extended bydd.util.FreewaySupport

public class FreewaySupport
extends java.lang.Object

Support utilities for the Freeway game. This is a high-level interface for interacting with the game board for the game Freeway. It encapsulates most of the game logic for the Freeway game, including scoring, finding road attributes, and other relevant functions.

Freeway is a consensus building game for two to six players. Each player adopts a role and tries to build a road from the southern end to the northern end of the map minimizing that player's score. Depending on the things that must be demolished to make room for the road, the player's score will be impacted in different ways.

This class is made up entire of static methods. No state is cached to make these methods as general-purpose as possible.

Author:
Eric Scharff

Field Summary
static java.util.HashMap playerLookup
          A mapping from player names to core table rows.
static int[][] scoreTable
          The matrix used to compute player scores.
 
Constructor Summary
FreewaySupport()
           
 
Method Summary
static java.util.List allRoads(MapCell cell)
          Returns a list of players who have a road in this cell.
static java.lang.String hasRoad(MapCell cell)
          Returns the first player encountered who has a road in this cell.
static boolean hasRoad(MapCell cell, java.lang.String player)
          Returns whether a player has a road in this cell.
static Map readMap(java.lang.String fileName)
          Creates a new map from a text file representation.
static float scoreForPlayer(java.lang.String player, float residential, float business, float hills, float historic, float dig)
          Computes the score for a player given the makeup of a cell.
static int scoreForPlayer(java.lang.String player, Map map)
          Compute the total score for a player in the current game state.
static float scoreForPlayer(java.lang.String player, MapCell cell, Layer features)
          Compute the score for a player in the given cell.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scoreTable

public static final int[][] scoreTable
The matrix used to compute player scores. Each row in the table represents a different role (representative, archaeologist, taxpayer, resident, merchant, and city engineer), and each column represents a land type (residential, business, hills, historic site, dig). The row, column value indicates the pentalty (score) for that role and that type being demolished.

The score for a player is computed by looking at a cell and multiplying the number of features of a type by the score for that feature type for the player. This table presentation should make it easy to add new land types, new roles, or modify existing roles.


playerLookup

public static java.util.HashMap playerLookup
A mapping from player names to core table rows. This map simplifies the process of lookup up and adding roles. The map is used to look up a score line for a player, where the key is the name of the role, and the value returned is an array of 5 integers as defined by the score table.

Constructor Detail

FreewaySupport

public FreewaySupport()
Method Detail

scoreForPlayer

public static float scoreForPlayer(java.lang.String player,
                                   float residential,
                                   float business,
                                   float hills,
                                   float historic,
                                   float dig)
Computes the score for a player given the makeup of a cell. This method does all the work to compute a score using the score table. The score for a cell is the sum of the scores for each land use in the cell, given by the scoreTable.

Parameters:
player - name of the player's role
residential - count of residences in the cell
business - count of businesses in the cell
hills - count of hills in the cell
historic - count of historic sites in the cell
dig - count of dig sites in the cell
Returns:
the score for the player for this cell, given the cell makeup specified

scoreForPlayer

public static float scoreForPlayer(java.lang.String player,
                                   MapCell cell,
                                   Layer features)
Compute the score for a player in the given cell. The score is computed using the features layer of information. The features layer has properties which correspond to the count of land types in each cell. Note that this is the score without taking into account if the player has a road in the cell or not.

Parameters:
player - name of the player's role
cell - Map cell to test
features - layer upon which features for land uses are located
Returns:
the score for the player for this cell

scoreForPlayer

public static int scoreForPlayer(java.lang.String player,
                                 Map map)
Compute the total score for a player in the current game state. The player's score is the sum of the scores for the cells in which the player has a road, plus 5 for every cell that has a road. This computes the global score for a player from the current Map.

Parameters:
player - name of the player's role
map - current Freeway map
Returns:
the score for the player for the entire map

hasRoad

public static java.lang.String hasRoad(MapCell cell)
Returns the first player encountered who has a road in this cell. This is a combination boolean function that returns null if a cell is empty, and a non-null string of the first role that has a road in the cell. This returns a boolean because knowing who has a road in a cell is often as required as determining if it is empty.

Parameters:
cell - cell to test for a road
Returns:
the name of the role that has a road in the cell, null if the cell contains no roads.

hasRoad

public static boolean hasRoad(MapCell cell,
                              java.lang.String player)
Returns whether a player has a road in this cell. Since a cell can contain multiple roads (one for each player) this searches for the player in the cell.

Parameters:
cell - cell in which to search for the road
player - name of the role to search
Returns:
true if the player has a road in this cell, false otherwise.

allRoads

public static java.util.List allRoads(MapCell cell)
Returns a list of players who have a road in this cell. The list returned is a (possibly empty) list of strings, where each string is the name of a role that has a road in this cell.

Parameters:
cell - cell to search for roads
Returns:
a list of strings, each string being the name of a role that has a road in this cell.

readMap

public static Map readMap(java.lang.String fileName)
                   throws java.io.IOException
Creates a new map from a text file representation. The map format is a tab-delimeted file containing row, column, business, residential, historic, hill, and dig counts for that cell. The first two lines of the file specify the map height and width. Using this ASCII representation, this method creates a new Map and populates it with the proper information.

Parameters:
fileName - name of the file to read
Returns:
a new Map with contents initialized from the file
Throws:
java.io.IOException - if an error occurs when reading the file