Using MSR as a Gaming Engine: LIGHTS OUT
-
CONCEPT
Inspired by my love for MSR as a logic engine, as well as my undying nostalgia for two early console games -- Merlin (1978) by Parker Brothers and Lights Out (1995) by Tiger -- I decided to try my hand at recreating "LIGHTS OUT" just using MSR.DESIGN
After an evening of playing around with Expressions inside a single Rule, my gambit succeeded. The result is a 3x3 version of "Lights Out" that can be played on any suitably configured Dashboard (Home Remote, Imperihome, MSR's built-in, etc.), where multiple buttons can be arranged in a grid. To play without a Dashboard, one can advance play manually and monitor game status under Rule Sets.IMPLEMENTATION
Each button press must send an HTTP GET Request (examples to follow) to MSR. (Alternatively, game play could involve inputs from another Rule or even physical device.) In turn, MSR will toggle the virtual switch corresponding to that position on the grid along with those adjacent to it in the up/down/left/right directions. The objective is to turn all of the devices (e.g. lights) OFF.NOTE: This on/off behavior (of devices) is not yet implemented, as it would require nine virtual or actual switches that I have not installed. (Suitable candidate: Multi-switch VS in Switchboard on Vera.) Instead, as a proof of concept, the 3x3 playing grid is -- for now at least -- represented internally by a 9-element array of 0's (Off) and 1's (On). A player 'wins' once the array contains all zeroes.
In the posts below, I present how I crafted "Lights Out" in MSR.
-
Readers may first want to familiarize themselves with the game of "Lights Out" by visiting an online solver. This lets you mock up your own starting grid of on/off lights, then either try solving it yourself, or compute a solution. Many sites (references to follow) exist online which teach playing strategies, logical concepts, and even programming approaches to "Lights Out". Most of them also generalize from the 3x3 playing grid to larger grids of arbitrary dimension.
My MSR version of the game could likewise be expanded to larger grids, but does not attempt to solve the board, nor show you a potential solution. It only offers the following modes:
- 'reset' = return the playing grid to all zeros / Off
- 'start' = generate a random grid of On / Off values
- 0 .. 8 = press button N to advance play one cycle
- 3, 1, ... , 5 = advance play multiple cycles at once
The values to the left of the
=
get sent via HTTP GET Request to MSR, using the following syntax:http://<msr-ip>:8111/api/v1/variable/LightsOutGame/set?value=
String values
reset
orstart
must not contain quotes. Single digits0
through8
correspond to grid positions, as shown:_____________ | 0 | 1 | 2 | | 3 | 4 | 5 | | 6 | 7 | 8 | –––––––––––––
A typical randomized starting grid might look like this, where
X
represents an 'On', and empty positions are 'Off':_____________ | X | | | | X | | X | | | X | | –––––––––––––
the (only) solution for which is the series of keypresses -- in no particular order -- of
0
,1
,2
,3
,4
,7
and8
. (The game allows all seven moves to be made in a single cycle usingvalue
=0,1,2,3,4,7,8
.) -
As is often the case, the line between genius and madness can be a bit blurry.
Kudos on this tour de force!
-
T toggledbits locked this topic on