public class TrafficLightsSolver
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
COLS |
static int |
GREEN |
static int[][] |
lines |
static int |
LOSS |
static int |
OFF |
static int |
RED |
static int |
ROWS |
static int |
UNKNOWN |
static int |
WIN |
static int |
YELLOW |
Constructor and Description |
---|
TrafficLightsSolver(java.lang.String filename)
Create a TwoBitRandomAccessFile large enough to access any Traffic Lights game state (reachable or not).
|
Modifier and Type | Method and Description |
---|---|
int |
eval(int[] state)
Given a game state, recursively evaluate the value of that state and all states reachable from it
given optimal play, assigning it the value WIN or LOSS.
|
boolean |
isGameOver(int[] state)
Return whether or not the game is over (3 GREEN/YELLOW/RED in a row horizontally, vertically, or diagonally).
|
static void |
main(java.lang.String[] args)
Test code that solves the game and then allows users to play the game with solution information.
|
void |
play()
Play a text-based Traffic Lights game, displaying winning/losing state information as the game progresses.
|
void |
solve()
Starting with the initial game state, recursively evaluate the value of each state given optimal play,
assigning each reachable game state to WIN or LOSS.
|
static int |
stateToInt(int[] state)
Convert the state to a unique integer by treating positions a base 4 digits.
|
static java.lang.String |
stateToString(int[] state)
Return a String representation of the given game state.
|
public static final int ROWS
public static final int COLS
public static final int OFF
public static final int GREEN
public static final int YELLOW
public static final int RED
public static final int UNKNOWN
public static final int LOSS
public static final int WIN
public static int[][] lines
public TrafficLightsSolver(java.lang.String filename)
filename
- name of binary solution filepublic void play()
public void solve()
public int eval(int[] state)
state
- current state to be evaluated as WIN or LOSSpublic boolean isGameOver(int[] state)
state
- game statepublic static int stateToInt(int[] state)
state
- game statepublic static java.lang.String stateToString(int[] state)
state
- game statepublic static void main(java.lang.String[] args)
args
- (unused)