The source code of all applications is included in the JGameGrid distribution. LightsOut The game consists of a 5 by 5 grid of lights; when the game starts, a set of these lights (random, or one of a set of stored puzzle patterns) are switched on. Pressing one of the lights will toggle it and the four lights adjacent to it on and off. (Diagonal neighbours are not affected.) The game provides a puzzle: to switch all the lights off, preferably in as few button presses as possible. Lights Out was invented by a group of people consisting of Avi Olti, Gyora Benedek, Zvi Herman, Revital Bloomberg, Avi Weiner and Michael Ganor. The game involves toggling lights on and off. If a light is on, it must be toggled an odd number of times to be turned off. If a light is off, it must be toggled an even number of times (including not being toggled at all) for it to remain off. A successful solution is therefore a sequence of presses that toggles all the "on" lights an odd number of times and all the "off" lights an even number of times. Two points may be noted:
Despite these results, in practice, given a random board setup, it will not be obvious what sequence will toggle the "on" and "off" lights the required number of times. This example demonstrates that developing a full-fledged game that achieves professional standards with the JGameGrid framework reduces the programming effort an order of magnitude. Only rudimental knowledge of Java and a few lines of code are necessary to implement the game in its basic but already playable version. It is proven to be a problem that can be solved in an entry level programming course. We present here one of many possible solutions where an ArrayList is used (instead of considering the 8 neighbour cells explicitly). The two states of the lamps (off/on) are shown by using an actor with two sprite ids. A GGMouseListener is registered to get the left press mouse event exclusively. Because the neighbour locations may be outside the grid (where getOneActorAt() returns null), isInGrid() is called to test for valid grid locations. The convenient method showNextSprite() runs through all sprite ids. Here it just toggles between id 0 and 1.
Execute the program locally using WebStart. The basic version may be extended step-by-step that gives enough challange for more advanced students. You may even include a solution finder like in the following program (the solution algorithm was presented by David Guichard, Whitman College, USA).
Execute the program locally using WebStart
| |||