CS 112 - Introduction to Computer Science II
Homework #10

Due: At the beginning of class 30.

NOTE: This work is to be done in pairs.  I strongly recommend the practice of Pair Programming described simply here.  Although team members are permitted to divide work, each team member should be able to informally present all work of his/her teammate.

Lights Out Puzzle Application

Overview of Game

In this exercise, you will implement a "Lights Out" puzzle game graphical application, with selectable grid sizes, initial and button-triggered randomization, and "light chasing" button.

Lights Out is a puzzle where the player is presented with an n-by-n grid of lights where some lights are on and some lights are off.  The object of the puzzle is to turn all lights off (i.e. "lights out").  We say that a light is toggled when it changes state, that is, off to on or on to off.  When a light is selected (i.e. pressed), that light and all lights immediately adjacent horizontally and vertically are toggled.  This will be illustrated in the screenshots below.

Not every configuration of on/off lights for all grid sizes is solvable, but all of our puzzles will be created in such a way as to be solvable.

You can watch this video demonstration of the application you will create.

Game Implementation Specifications

You will implement class LightsOut, extending Application.  When LightsOut is executed, the first and only stage shown, titled "Lights Out Size" (not visible in window, but visible when minimized), requests the game size from the user as shown in this image:

Note that the user is allowed to select sizes 3-9 from radio buttons.  The "Please select a size:" prompt label, the radio buttons labeled 3-9, and the  "Create Puzzle" button confirming the user's selection should be centered in vertical alignment, and separated by at least 5 pixels between components (e.g. new VBox(5)).  The radio button "5" should be selected by default:

Let us suppose the user selects radio button "3" to create a 3-by-3 puzzle.  Correctly grouped radio buttons will cause radio button "5" to then be deselected:

When the user then clicks the "Create Puzzle" button, a new puzzle stage is created and then the initial size-selection stage is closed:

This new puzzle stage is initialized as follows:

After randomly pressing or not pressing light buttons in a lights out grid, this is one example initial 3-by-3 puzzle grid stage:

One strategy is called "light chasing": Pick an uppermost light that is on and press/select the light immediately beneath it.  Continue this process until the only lights on are in the bottom row.  As an example, we will successively click the light below the uppermost, leftmost light that is on.  In this case, we first click below the upper-middle light, i.e. we click the center light with the following result:>

Note that not only the selected/pressed middle light has been toggled, but also the four lights adjacent above, below, leftward, and rightward have been toggled as well.  If we continue to manually "chase lights", we pass through the following states until the only lights on (if any) are in the bottom row:

Suppose that I click the upper-right button in this state.  Note how adjacency toggling does not wrap around, e.g. from right side to left side, or top to bottom: 

If I click the "Chase Lights" button, the same process will take place, leading (in this case) to a solved grid with all lights out:

If I click the "Randomize" button, then the same randomization process performed initially is performed on the current state (solved or not).  In this instance, it creates a new puzzle for solving:

The application is terminated by clicking the red "X" button in the upper-right of the stage window.

Specific Implementation Recommendations

There are particular private fields and methods that are not required, yet may be helpful to your design.

My implementation is less than 150 lines of Java.  If yours is significantly more, feel free to stop by office hours to see how your code may be streamlined.

Rubric: (20 points total)