public class Board
extends javax.swing.JPanel
implements java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener
Tile
,
Seed
,
Serialized FormModifier and Type | Class and Description |
---|---|
private class |
Board.StateChange
Event describing a state change of a single tile.
|
javax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
private static java.awt.Color |
BACKDROP
Background color for numbers when they start moving with the view.
|
private Tile |
baseType
Current tile type of the tile an area selection was started from.
|
private int[][] |
colHints
Hint numbers for each column.
|
private java.lang.Boolean[][] |
colJudgement
Current marking judgement for all the columns.
|
private static int |
DELTA
Number of pixels to move by when moving the view.
|
private double |
dx
Grid translation along the x-axis.
|
private double |
dy
Grid translation along the y-axis.
|
private long |
endTime
The time at which this board was solved or
-1L . |
private static java.awt.Composite |
FADE_COMPOSITE
Composite to draw area actions with.
|
private int |
height
Number of rows in the grid.
|
private int |
hx
Additional tiles selected on the x-axis
for the currently selected area.
|
private int |
hy
Additional tiles selected on the y-axis
for the currently selected area.
|
private java.awt.Point |
last
Last drag location.
|
private java.awt.Point |
lastPress
Last click location.
|
private static java.awt.Color |
MISTAKE_COLOR
Color used to indicate hint numbers that aren't marked correctly.
|
private Tile |
nextType
The type of tile to place when the mouse is released.
|
private static java.awt.Font |
NUMBERS
Font to use to draw the hint numbers.
|
private java.util.Random |
random
Random number generator for this board.
|
private java.util.Deque<java.util.List<Board.StateChange>> |
redoStack
Redo stack of moves that can be redone.
|
private boolean |
reveal
Whether or not the solution is shown.
|
private int[][] |
rowHints
Hint numbers for each row.
|
private java.lang.Boolean[][] |
rowJudgement
Current marking judgement for all the rows.
|
private Seed |
seed
The seed for this board.
|
private static long |
serialVersionUID
Serial ID.
|
private static int |
SIZE
Size in pixels of the grid cells.
|
private boolean[][] |
solution
Solution this puzzle is based on.
|
private boolean |
solved
Whether or not this puzzle is currently solved.
|
private static java.awt.Color |
SOLVED_COLOR
Color used to indicate elements of a solved puzzle.
|
private long |
startTime
The time at which this board was created.
|
private Tile[][] |
state
Current state of each grid cell.
|
private static java.awt.Color |
TEST_MODE_COLOR
Color used to indicate test mode related elements.
|
private boolean |
testMode
Whether or not test mode is enabled.
|
private java.util.Deque<java.util.List<Board.StateChange>> |
undoStack
Undo stack of moves that can be undone.
|
private int |
width
Number of columns in the grid.
|
private int |
x
x-coordinate of the currently selected grid cell.
|
private int |
y
y-coordinate of the currently selected grid cell.
|
private double |
zoom
Current zoom level.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
Board(Seed seed)
Constructs a new board from
the given seed.
|
Modifier and Type | Method and Description |
---|---|
private Board.StateChange |
applyStateChange(int x,
int y,
Tile set)
Changes the state of the given title
to the given new value.
|
void |
changeZoom(double newZoom)
Changes the current zoom level to the given level.
|
private void |
checkSolution()
Checks to see if the board is
in a solved state and if so
updates the internal state of
this board to reflect that by
stopping time from passing and
setting the
solved flag
to true . |
void |
clearMistakes()
Clears any titles that have a tile that doesn't
match the original solution for the board.
|
private void |
computeColJudgement(int x)
Computes the judgement for the given column.
|
private void |
computeJudgement(java.lang.Boolean[] result,
int[] hints,
int max,
java.util.function.Function<java.lang.Integer,Tile> state)
Computes the judgement for a row or column by
stating which numbers have to be grayed out
or which numbers have to be rendered in red
because they are marked invalid.
|
private void |
computeJudgement(int x,
int y)
Computes the judgement for the given row and column.
|
private void |
computeRowJudgement(int y)
Computes the judgement for the given row.
|
void |
enterTestMode()
Enters test mode.
|
long |
getPassedTime()
Gets the number of milliseconds that have
passed since this board was created.
|
java.awt.Dimension |
getPreferredSize() |
Seed |
getSeed()
Gets the seed for this board.
|
private Tile |
getState(int x,
int y)
Gets the state of the tile at the given coordinates.
|
int |
getTileCount()
Returns the total number of tiles in the grid.
|
int |
getTileCount(Tile type)
Returns the total number of tiles in the grid
of the given type.
|
double |
getZoom()
Gets the current zoom level.
|
private void |
initialiseGrid()
Initialises the gird by computing the hints from the seed.
|
boolean |
isGridComplete()
Checks to see if the entire grid is filled
with final solution tiles.
|
boolean |
isSolutionValid()
Checks to see if board in its
current states contain no errors
and that all hints have been met.
|
boolean |
isSolved()
Returns whether this board is solved.
|
boolean |
isTestMode()
Check whether or not test mode
is currently enabled.
|
private boolean |
isWithinBounds(java.awt.Point p)
Checks if the given on screen coordinate is within
the game grid.
|
private boolean |
isWithinGridBounds(int x,
int y)
Checks if the cell denoted by the given
coordinates is within the bounds of the grid.
|
private boolean |
isWithinGridBounds(java.awt.Point p)
Checks if the grid cell denoted by the given
point is within the bounds of the grid.
|
void |
keyPressed(java.awt.event.KeyEvent e) |
void |
keyReleased(java.awt.event.KeyEvent e) |
void |
keyTyped(java.awt.event.KeyEvent e) |
void |
leaveTestMode(boolean save)
Leaves test mode.
|
void |
mouseClicked(java.awt.event.MouseEvent e) |
void |
mouseDragged(java.awt.event.MouseEvent e) |
void |
mouseEntered(java.awt.event.MouseEvent e) |
void |
mouseExited(java.awt.event.MouseEvent e) |
void |
mouseMoved(java.awt.event.MouseEvent e) |
void |
mousePressed(java.awt.event.MouseEvent e) |
void |
mouseReleased(java.awt.event.MouseEvent e) |
void |
mouseWheelMoved(java.awt.event.MouseWheelEvent e) |
void |
moveViewDown()
Moves the view down.
|
void |
moveViewLeft()
Moves the view left.
|
void |
moveViewRight()
Moves the view right.
|
void |
moveViewUp()
Moves the view up.
|
private Tile |
nextTileState(int x,
int y,
Tile base)
Gets the next tile stage for the tile at the given
location given a specific base action.
|
void |
paintComponent(java.awt.Graphics g1) |
void |
redo()
Redoes the last move undone.
|
void |
reset()
Resets the board to its initial cleared state.
|
void |
resetTranslation()
Resets all view translations.
|
void |
setNextState(int x,
int y,
Tile newState)
Sets the new state for the tile at the
given coordinates based on the current game state.
|
void |
showSolution(boolean shown)
Sets whether to show the solution or not.
|
private int |
toGridX(int px)
Converts the given x-coordinate
in pixel to a grid tile.
|
private int |
toGridY(int py)
Converts the given y-coordinate
in pixel to a grid tile.
|
void |
undo()
Undoes the last move executed.
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
private static final long serialVersionUID
private static final java.awt.Color TEST_MODE_COLOR
private static final java.awt.Color MISTAKE_COLOR
private static final java.awt.Color SOLVED_COLOR
private static final java.awt.Color BACKDROP
private static final java.awt.Font NUMBERS
private static final java.awt.Composite FADE_COMPOSITE
private static final int SIZE
private static final int DELTA
private long startTime
private long endTime
-1L
.private final java.util.Random random
private final boolean[][] solution
private final int[][] rowHints
private final int[][] colHints
private final int width
private final int height
private double dx
private double dy
private boolean reveal
private boolean testMode
private java.awt.Point last
private java.awt.Point lastPress
private int x
-1
if the users is not playing
with the keyboard.private int y
private java.lang.Boolean[][] rowJudgement
private java.lang.Boolean[][] colJudgement
private boolean solved
private int hx
private int hy
private Tile nextType
private Tile baseType
private double zoom
private java.util.Deque<java.util.List<Board.StateChange>> undoStack
private java.util.Deque<java.util.List<Board.StateChange>> redoStack
public long getPassedTime()
public boolean isTestMode()
public boolean isSolved()
public int getTileCount(Tile type)
type
- The type of tile to count.public Seed getSeed()
private int toGridX(int px)
px
- The x-coordinate to convert.private int toGridY(int py)
py
- The y-coordinate to convert.public void setNextState(int x, int y, Tile newState)
x
- The x-coordinate for the tile.y
- The y-coordinate for the tile.newState
- The new state to change to.private Board.StateChange applyStateChange(int x, int y, Tile set)
x
- The x-coordinate to update.y
- The y-coordinate to update.set
- The new title state.private void checkSolution()
solved
flag
to true
.public boolean isGridComplete()
Tile.FILL
and
Tile.CROSS
tiles.Tile
public boolean isSolutionValid()
isGridComplete()
public void enterTestMode()
public void leaveTestMode(boolean save)
save
- Whether to save the changes
made or to revert them.public void showSolution(boolean shown)
shown
- True to show the solution,
false to hide it.public void changeZoom(double newZoom)
newZoom
- The new zoom level.public double getZoom()
public void moveViewDown()
public void moveViewUp()
public void moveViewRight()
public void moveViewLeft()
public void resetTranslation()
public void reset()
public void clearMistakes()
public int getTileCount()
private final void initialiseGrid()
private void computeJudgement(int x, int y)
x
- The column to compute the judgement for.y
- The row to compute the judgement for.computeJudgement(Boolean[], int[], int, Function)
,
computeColJudgement(int)
,
computeRowJudgement(int)
private void computeRowJudgement(int y)
y
- The row to compute the judgement for.computeJudgement(Boolean[], int[], int, Function)
,
computeColJudgement(int)
,
computeJudgement(int, int)
private void computeColJudgement(int x)
x
- The row to compute the judgement for.computeJudgement(Boolean[], int[], int, Function)
,
computeRowJudgement(int)
,
computeJudgement(int, int)
private void computeJudgement(java.lang.Boolean[] result, int[] hints, int max, java.util.function.Function<java.lang.Integer,Tile> state)
result
- The array to store the resulting
judgement in. The judgement for a row or column
is stored as an array of Boolean
.
Each index of this array corresponds
to the hint at the same index in the
hints
array. If the Boolean
value is null
then there
is an error with the sequence the hint represents,
if the value is true
then the
sequence is correctly marked, if the value
if false
then the sequence is not marked yet.hints
- The hints or numbers for the row
or column to check.max
- The maximum row or column index.state
- A function that maps an integer
index to the row or column state at
that index.computeColJudgement(int)
,
computeRowJudgement(int)
,
computeJudgement(int, int)
private Tile getState(int x, int y)
x
- The x coordinate of the tile to get.y
- The y coordinate of the tile to get.Tile
private Tile nextTileState(int x, int y, Tile base)
x
- The x coordinate of the tile to check.y
- The y coordinate of the tile to check.base
- The base tile action.private boolean isWithinBounds(java.awt.Point p)
p
- The point to check.private boolean isWithinGridBounds(java.awt.Point p)
p
- The point to check.private boolean isWithinGridBounds(int x, int y)
x
- The x coordinate.y
- The y coordinate.public void undo()
public void redo()
public java.awt.Dimension getPreferredSize()
getPreferredSize
in class javax.swing.JComponent
public void paintComponent(java.awt.Graphics g1)
paintComponent
in class javax.swing.JComponent
public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked
in interface java.awt.event.MouseListener
public void mousePressed(java.awt.event.MouseEvent e)
mousePressed
in interface java.awt.event.MouseListener
public void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased
in interface java.awt.event.MouseListener
public void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered
in interface java.awt.event.MouseListener
public void mouseExited(java.awt.event.MouseEvent e)
mouseExited
in interface java.awt.event.MouseListener
public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
public void keyPressed(java.awt.event.KeyEvent e)
keyPressed
in interface java.awt.event.KeyListener
public void keyReleased(java.awt.event.KeyEvent e)
keyReleased
in interface java.awt.event.KeyListener
public void mouseDragged(java.awt.event.MouseEvent e)
mouseDragged
in interface java.awt.event.MouseMotionListener
public void mouseMoved(java.awt.event.MouseEvent e)
mouseMoved
in interface java.awt.event.MouseMotionListener
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e)
mouseWheelMoved
in interface java.awt.event.MouseWheelListener