Computer Science 523
Advanced Programming

Summer 2014, The College of Saint Rose

Lab 5B: Lights Out
Due: 4:30 PM, Tuesday, July 1, 2014

For this lab, which serves as a kind of capstone for the first half of the course, you will implement a version of the game "Lights Out".

You may work alone or with a partner on this lab. Only one submission per group is needed.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab5B" (no spaces) for the project.

A working solution to this program can be downloaded here.

Programming Assignment

If you are not familiar with the "Lights Out" game, take a look at this Wikipedia article.

The basic idea is that we have a 5 ×5 array of "lights" (which we will represent with colored JButtons) and your goal is to turn off all of the lights. You toggle a light by clicking on it, but the complication is that the 4 adjacent lights also toggle.

As stated above, your version of the program will use JButton components to represent the lights. There should also be a button to reset the game, and a label showing the number of clicks taken so far.

If the player is able to turn off all of the lights, a message should pop up which includes the number of turns it took to win.

To have a JButton change its background color to indicate whether a light is on or off, we need to take a few steps. Suppose we created a JButton called b. The following would make that JButton have a red background:


Not all randomly generated configurations of on/off lights leads to a solvable puzzle. Therefore, we cannot simply choose 25 random boolean values and use those to set the initial states of the lights. Instead, create a board where all of the lights are off, and randomly make some number of moves before game play begins. A good randomization might make somewhere between 12 and 25 moves (choose this randomly) each of which has the effect of clicking on a randomly chosen light.

The sample solution code has an additional feature which is not required for your programs: a cheat button. It shows the list of moves that, when made in any order, would lead to a win. Definitely useful for trying to debug.


A working solution for the program will appear below. Click inside the applet to interact with it.


Before 4:30 PM, Tuesday, July 1, 2014, submit your lab for grading. To complete the submission, upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab5B".


This assignment is worth 50 points, which are distributed as follows:

> FeatureValueScore
Basic GUI setup 4
"Light" buttons in a square grid layout 5
"Light" button "on" and "off" appearance 3
Reset button starts new game 3
Generates random solvable configuration 5
Track number of moves 3
Click on a light toggles 5
Click on a light toggles neighbors 4
Detect and report win 5
Comments 5
Appropriate variable declarations 2
Named constants 1
Naming conventions 2
Code organization 2
Formatting 1
Total 50