Class LightsOut
java.lang.Object
LightsOut
An implementation of the puzzle game, Lights Out. The player is presented with a 4x4 grid of light bulbs, some on and some off. The player may toggle a light bulb at a specific row and column, but that will also toggle the four light bulbs adjacent to it. Toggling a light bulb means to turn it on if it is off, or turn it off if it is on.
The goal is to turn off all of the lights.
Example:
The @ symbol indicates a light is on while a . indicates the light is off. The user is given an initial game board with five lights on as shown below. They opt to toggle row 3, column 2 resulting in the following:- row 3, column 2 switches from off to on.
- Above: row 2, column 2 switches from on to off.
- Below: row 4, column 2 switches from on to off.
- Left: row 3, column 1 switches from off to on.
- Right: row 3, column 3 switches from on to off.
- row 3, column 1 switches from on to off.
- Above: row 2, column 1 switches from on to off.
- Below: row 4, column 1 switches from on to off.
- Left: this is out of bounds, so nothing happens
- Right: row 3, column 2 switches from on to off.
Turn off all of the lights. (. = off, @ == on).
1 2 3 4
1 . . . .
2 @ @ . .
3 . . @ .
4 @ @ . .
---------------
Enter row and column: 3 2
1 2 3 4
1 . . . .
2 @ . . .
3 @ @ . .
4 @ . . .
---------------
Enter row and column: 3 1
1 2 3 4
1 . . . .
2 . . . .
3 . . . .
4 . . . .
---------------
You solved the puzzle in 2 moves.
Assignment
Implement the lights out game by building the methods described below. Notice that the user enters row and column values between 1 and 4, but the program uses row and column values from 0 to 3. Do not change the method signatures in any way. You may add additional methods or constants if you like
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidinitBoard(boolean[][] board) Initialize the game board, by calling toggleLights on randomly generated locations.static booleanisSolution(boolean[][] board) Determine if all the lights on the board are off.static voidPlay the game.static voidprintBoard(boolean[][] board) Output the board in an easy to read format.static intReads an integer from the given scanner.static voidtoggleLights(boolean[][] board, int row, int col) Toggle the light at the given location as well as the lights above, below, left and right of the location.static voidtoggleOneLight(boolean[][] board, int row, int col) If the light is on at the given location, turn it off.
-
Field Details
-
SIZE
public static final int SIZEThe number of rows and columns in the game board. The value is 4.- See Also:
-
LIGHT_ON
public static final char LIGHT_ONThe character used to represent a light which is on. The value is '@'.- See Also:
-
LIGHT_OFF
public static final char LIGHT_OFFThe character used to represent a light which is off. The value is '.'.- See Also:
-
-
Method Details
-
main
Play the game.1. Create and randomize the board. 2. Repeat until the board is solved. 2.1 print the board for the user. 2.2 print the instructions for the user. 2.3 read row and col from the user 2.4 toggle the lights around that location 3. Print the final state of the board (solved). 4. Print out the number of moves the user took to get to the solution.
- Parameters:
args- Command line arguments are ignored.
-
initBoard
public static void initBoard(boolean[][] board) Initialize the game board, by calling toggleLights on randomly generated locations.- Parameters:
board- The game board.
-
readInt
Reads an integer from the given scanner. Repeats until an integer is entered. The resulting integer may be out of bounds on the board.- Parameters:
in- Scanner to read from- Returns:
- and integer entered by the user
-
printBoard
public static void printBoard(boolean[][] board) Output the board in an easy to read format.- Parameters:
board- The game board.
-
toggleOneLight
public static void toggleOneLight(boolean[][] board, int row, int col) If the light is on at the given location, turn it off. If the light is off, turn it on. If row or col is out of bounds, do nothing. Row and col are both typical array indices which start at 0.- Parameters:
board- The game board.row- The row of the given light on the board.col- The column of the given light on the board.
-
toggleLights
public static void toggleLights(boolean[][] board, int row, int col) Toggle the light at the given location as well as the lights above, below, left and right of the location. Uses the toggleOneLight method to toggle each light. Row and col are both typical array indices which start at 0.- Parameters:
board- The game board.row- The row of the given light on the board.col- The column of the given light on the board.
-
isSolution
public static boolean isSolution(boolean[][] board) Determine if all the lights on the board are off.- Parameters:
board- The game board.- Returns:
- true if all of the lights are off, false otherwise.
-