Class LightsOut

java.lang.Object
LightsOut

public class LightsOut extends Object

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:
  1. row 3, column 2 switches from off to on.
  2. Above: row 2, column 2 switches from on to off.
  3. Below: row 4, column 2 switches from on to off.
  4. Left: row 3, column 1 switches from off to on.
  5. Right: row 3, column 3 switches from on to off.
On the second step, the user selects to toggle row 3, column 1 and the following occurs: *
  1. row 3, column 1 switches from on to off.
  2. Above: row 2, column 1 switches from on to off.
  3. Below: row 4, column 1 switches from on to off.
  4. Left: this is out of bounds, so nothing happens
  5. Right: row 3, column 2 switches from on to off.
At this point, all of the lights are off, so the game ends. The actual execution is shown below with user input in bold.
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
    Modifier and Type
    Field
    Description
    static final char
    The character used to represent a light which is off.
    static final char
    The character used to represent a light which is on.
    static final int
    The number of rows and columns in the game board.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    initBoard(boolean[][] board)
    Initialize the game board, by calling toggleLights on randomly generated locations.
    static boolean
    isSolution(boolean[][] board)
    Determine if all the lights on the board are off.
    static void
    main(String[] args)
    Play the game.
    static void
    printBoard(boolean[][] board)
    Output the board in an easy to read format.
    static int
    Reads an integer from the given scanner.
    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.
    static void
    toggleOneLight(boolean[][] board, int row, int col)
    If the light is on at the given location, turn it off.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SIZE

      public static final int SIZE
      The number of rows and columns in the game board. The value is 4.
      See Also:
    • LIGHT_ON

      public static final char LIGHT_ON
      The character used to represent a light which is on. The value is '@'.
      See Also:
    • LIGHT_OFF

      public static final char LIGHT_OFF
      The character used to represent a light which is off. The value is '.'.
      See Also:
  • Method Details

    • main

      public static void main(String[] args)
      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

      public static int readInt(Scanner in)
      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.