public class Lights extends Object
It is meant as a framework for the programming assignment "Light Up" available at the homepage of the lecture.
Please consider checking for updates every now and then since there might still be errors in these classes.
toConsole()
)
...
showWindow()
).
Clause.exactlySetN
)
Modifier and Type | Field and Description |
---|---|
static Object |
BLOCK
this immutable object stands for an unconstrained block
|
static Object |
LIGHT
this immutable object stands for a light
|
Constructor and Description |
---|
Lights(int dimension)
create a new empty Lights-board.
|
Lights(String desc)
convert a board description into a Lights object.
|
Modifier and Type | Method and Description |
---|---|
void |
clear(int row,
int col)
set a field on the board to empty.
|
Lights |
clone()
create a new object of this class which gets a copy the fields array.
|
boolean |
equals(Object obj)
compare this Lights board to another object.
|
int |
getBlockConstraint(int row,
int col)
Get the value of a constraint of a field.
|
int |
getDimension()
get the width/height of this board.
|
Object |
getField(int row,
int col)
return the (immutable) object that is used to describe a field on the
board.
|
int |
getLitNeighbours(int row,
int col)
Get the number of neighbouring fields that contain a light.
|
boolean |
isBlock(int row,
int col)
check if a field on the board has been set to a block.
|
boolean |
isConstrainedBlock(int row,
int col)
check if a field on the board has been set to a constrained block.
|
boolean |
isEmpty(int row,
int col)
check if a field on the board is empty, i.e. neither a light nor a block
is placed on it.
|
boolean |
isLight(int row,
int col)
check if a field on the board has been set to a light.
|
boolean |
isLit(int row,
int col)
check if a field in the board is lit a by a light placed on the board.
|
void |
setBlock(int row,
int col)
set a field on the board to an unconstrained block.
|
void |
setBlockConstraint(int row,
int col,
int constraint)
set a field on the board to a constrained block.
|
void |
setEmpty(int row,
int col)
set a field on the board to "Empty".
|
void |
setLight(int row,
int col)
set a field on the board to "Light".
|
void |
showWindow()
open a modal dialog window that displays the current field.
|
void |
toConsole()
print the board to the console, i.e.
|
String |
toString()
generate a describing String from this board.
|
public static final Object LIGHT
public static final Object BLOCK
public Lights(String desc) throws LightsFormatException
'a'-'z' | 1-26 empty fields in succession. |
'0'-'4' | a constrained block |
'L' | a light |
'B' | a block |
' ', '\t', '\n', '\r' | spaces are ignored |
desc
- a board description as stringLightsFormatException
- if the string is not formated according to the
specifications.toString()
public Lights(int dimension)
dimension
- the height and width of the board, positive.IllegalArgumentException
- if dimension is not positive.public String toString()
Lights(String)
for the format of the string.public Object getField(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.LIGHT
,
BLOCK
public int getDimension()
public boolean isEmpty(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.clear(int, int)
public boolean isLight(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.setLight(int, int)
public boolean isBlock(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.setBlock(int, int)
public boolean isConstrainedBlock(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.public int getBlockConstraint(int row, int col)
IllegalArgumentException
will be thrown.row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.IllegalArgumentException
- if the field is not a constrained blocksetBlockConstraint(int, int, int)
public int getLitNeighbours(int row, int col)
getBlockConstraint(int, int)
(row,col) if this does not yield an
exception.row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.public boolean isLit(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.isLight(int, int)
public void clear(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.setLight(int, int)
,
setBlock(int, int)
,
setBlockConstraint(int, int, int)
public void setLight(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.IllegalArgumentException
- if the indexed field is neither empty nor a light.isLight(int, int)
public void setEmpty(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.IllegalArgumentException
- if the indexed field is neither empty nor a light.isLight(int, int)
public void setBlock(int row, int col)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)IndexOutOfBoundsException
- if row or col are not within the bounds.IllegalArgumentException
- if the indexed field is neither empty nor an unconstrained
block.isBlock(int, int)
public void setBlockConstraint(int row, int col, int constraint)
row
- the index of the row of the field (0 .. dimension-1)col
- the index of the column of the field (0 .. dimension-1)constraint
- the value to set to the field (0..4)IndexOutOfBoundsException
- if row or col are not within the bounds.IllegalArgumentException
- if the indexed field is neither empty nor a constrained block
or if the constraint is not within the valid range.getBlockConstraint(int, int)
,
isConstrainedBlock(int, int)
public void showWindow()
System.exit(int)
if you use this method. This is due to some AWT
thread.JDialog
,
LightsComponent
public void toConsole()
System.out
each field is
represented by a character with the following meanings:
public Lights clone()
public boolean equals(Object obj)