GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Exercise 4: LightOut Game


The game consists of 25 lamps, arranged in a 5 x 5 grid. At the beginning of the game all the lights are turned on. The goal of the game is to switch off all the lights. If you switch a lamp on or off, the lamp itself and the four adjacent ones are inverted.
 

As a template you can use the file LightsOut_0.java .

Edit LightsOut_0.java in the Online-Editor

Download (LightsOut_0.zip)

Step 1: Create lamps in all cells of the grid

Step 2: Not only the lamp that was clicked on, but also the four adjacent lamps are supposed to change their sprites

The affected cells can be recorded in an array:
Location loc = toLocationInGrid(mouse.getX(), mouse.getY());
Location[] locs = new Location[5];
locs[0] = new Location(loc.x, loc.y);
locs[1] = new Location(loc.x, loc.y -1);
....

 

You don't have to start this game with "all lights on". You can start with an arbitrary pattern, with the task staying the same. The solution, if there exists one, can be calculated mathematically (see LightsOutGame).