

Inverter - dgorrie
http://gorried.github.io/inverter/game.html

======
chaoxu
This problem can be generalized to the following graph light out game: Given a
graph where each vertex is labeled 0 or 1. Each move flips all the bits on a
vertex and all its neighbors. If you are given the end label configuration,
find a set of moves that reaches the end configuration. (why a set of moves?
you can show that you never need to make a move on a vertex twice, and the
order doesn't matter).

So the question is, which vertices do I have to touch once?

It's easy to solve the problem by solving a system of linear equation(in F_2,
not in R). Ax=b, where A is the adjacency matrix, and (b[i]= (current bit on
vertex i != desired bit on vertex i)).

Naively, for the light out game on a nxn board, this implies a O(n^6) using
Gaussian elimination algorithm. Or O((n^2)^ω)) (where ω is the matrix
multiplication constant) by finding a matrix pseudoinverse and then multiply.

The graph light out game can be solved in O(n^(ω/2)) time for planar graphs!
Because most matrix operations can be done much faster on the adjacency matrix
of a planar graph due to nested dissection.
[http://en.wikipedia.org/wiki/Nested_dissection](http://en.wikipedia.org/wiki/Nested_dissection)

In particular, the current game is a planar grid graph. So there exist a
O(n^ω) algorithm.

~~~
sillysaurus3
Ok, so let's implement the algorithm. Where would you start? Which math
libraries would make this easier? It seems like Mathematica might be good
here.

~~~
pantaskios
It's been done:
[http://library.wolfram.com/infocenter/Articles/1231/](http://library.wolfram.com/infocenter/Articles/1231/)

~~~
poulson
Thanks for the link! There is even a nice discussion of the explicit solutions
for Inverter up to 7x7's on the page:
[http://mathworld.wolfram.com/LightsOutPuzzle.html](http://mathworld.wolfram.com/LightsOutPuzzle.html)

Almost nothing about the solver would need to change in order to support
computation over GF(p), which suggests that someone should create a version of
Lights Out / Inverter which cycles through a prime number of colors rather
than just off/on.

~~~
chaoxu
I think it would work for all finite fields. One of the solutions to this IOI
problem was solved that way, and it's not of the form GF(p) but it's GF(p^2).
[http://olympiads.win.tue.nl/ioi/ioi94/contest/day2prb1/probl...](http://olympiads.win.tue.nl/ioi/ioi94/contest/day2prb1/problem.html)
see solution 2:
[http://olympiads.win.tue.nl/ioi/ioi94/contest/day2prb1/solut...](http://olympiads.win.tue.nl/ioi/ioi94/contest/day2prb1/solution.html)

~~~
poulson
Good point. So it would seem to be hard to find solutions for 6, 10, 12, etc.
colors. Have you given any thought to how to solve those cases?

~~~
chaoxu
Wikipedia has an article on this,
[http://en.wikipedia.org/wiki/Linear_equation_over_a_ring](http://en.wikipedia.org/wiki/Linear_equation_over_a_ring)
it would take a long time to read up the background to understand it, and I
don't have the background to distill it to the exact result we want.

Note the wiki article references this book: Ideals, Varieties, and Algorithms
by Cox, Little and O'Shea. This is a standard and approachable reference on
this kind of topics.

Although this topic is interesting, I think it's best to treating it as a
technology. Knowing it's existence and know when to apply them is good enough.
Understanding how it works would be a big time sink and sadly doesn't give
much useful insights.

------
zwegner
Huh, I just made a pretty similar game a couple weeks ago.

Mine is different in that it it's way less polished, starts with a randomly-
initialized grid, and rows/columns that are all the same color disappear (so
the goal is to make them all disappear).

It wasn't quite fun enough for me to clean it up more, but it was mildly
amusing for a while. It's here in case anyone would like to try:
[https://rawgit.com/zwegner/switchy/master/switchy.html](https://rawgit.com/zwegner/switchy/master/switchy.html)
(or the source repo:
[https://github.com/zwegner/switchy](https://github.com/zwegner/switchy))

~~~
rumdz
Doesn't work for me. I'm clicking the blue square and nothing happens. Chrome
37

~~~
zwegner
OK, so it's fixed now. Tested in Firefox, Chrome, and Safari. As soon as
rawgit refreshes its copy, the new version will be live.

~~~
rumdz
It's fun :)

------
wzsddtc
This is the classic lights out right?
[http://en.wikipedia.org/wiki/Lights_Out_(game)](http://en.wikipedia.org/wiki/Lights_Out_\(game\))

------
chmullig
Very interesting game. Unfortunately I found it just devolved into random
clicking - there weren't enough clues to give me much strategy. Gave up when
the grid got to ~9x9, and it was just a waste trying to get it working.

~~~
IanCal
I'm not sure I understand about the clues. When you click a square, it inverts
the colour of that square and the ones around it (up, down, left and right).

~~~
BoppreH
The problem is _strategy_ clues. I know how the game works, but that doesn't
tell me where to play.

Past a certain size I lose all intuition and start clicking everywhere until
something manageable appears.

~~~
IanCal
Oh I see what you mean. I can see a few patterns around but I've not got a
good understanding about how to solve many of them, so I can't logically work
through things after a certain point.

------
bcuccioli
Others have pointed out that this is Lights Out. If anyone's interested, there
is a known algorithm (PDF):

[https://linux.ime.usp.br/~renatolg/lights_out.pdf](https://linux.ime.usp.br/~renatolg/lights_out.pdf)

~~~
tzs
That's the clever solution. Here's a solution that is actually brute force,
but tries to fake being clever:
[http://www.tzs.net/lights.html](http://www.tzs.net/lights.html)

PS: I have not updated that page in years, which is why it contains a link to
a Dutch noodle seller. It was a link to an online game service back when I
wrote the page.

------
octatone2
I find the "on to level x" popover slow and unnecessary, I just want to invert
more colors :)

~~~
lotsofcows
I agree. Can't ever dismiss it with return or escape so I have to track over
there to click and back again. The process is enough of a distraction that at
the first hint of frustration it's a natural point to stop.

------
dickeytk
it's lights out! [http://kbarr.net/static/lo/](http://kbarr.net/static/lo/)

~~~
dkhar
Yup! Est. 1995! Here's a Wikipedia link:
[http://en.wikipedia.org/wiki/Lights_Out_(game)](http://en.wikipedia.org/wiki/Lights_Out_\(game\))

I remember playing a version of this game that was posted on HN a few weeks
ago. Jennifer Dewalt (the person who learned to code by making a website every
day for 180 days) made it!
[http://jenniferdewalt.com/lights_on/game](http://jenniferdewalt.com/lights_on/game)

The mathematics behind this game are apparently pretty interesting. I'm not
big on matrix math, but I bet a lot of you guys are.

~~~
cdr
Anyone who's played Dungeons and Dragons Online (the MMO) for any length of
time is familiar with lights out. It was a favorite puzzle for the designers
of the earlier-vintage quests, and notably the most popular raid in the game
for many years has an entire phase devoted to locking each of the twelve
participants in their own lights out puzzle. Low-effort people find a solver,
really low effort people beg other people to solve theirs for them. Go-getters
learn to solve lights out without a solver.

There's also an infamous quest that ends with a lights out puzzle with a
large, arbitrary board (some squares completely removed) while an overpowered,
invincible, impossible-to-tank scorrow boss chases you around. And both the
boss and the dozen infinitely-respawning trash mobs activate squares when they
step on them.

------
munificent
Wow, that takes me back. I wrote a game very similar to that (but with
randomly generated symmetric boards) a while ago called Cesare. And by "while
ago" I mean around 1998 or so.

[http://robot-frog.stuffwithstuff.com/misc/index.html](http://robot-
frog.stuffwithstuff.com/misc/index.html)

~~~
S4M
Same, I wrote the exact same game - with the exception that the board was
constant, 6x6 maybe - when I was in highschool - 1998-99.

------
dperny
I find this frustrating, because it reminds me just how un-clever I am. Other
commenters are progressing quite far, but I've gotten stuck already at level
5. Puzzle games cause me to call into question my entire sense of being.

------
rubberbandage
Emacs, at least on OS X, has “level 5” in its games as “5x5”, including the
“how many moves has it taken so far” counter. I couldn’t beat that, and once I
got to level 5 here, I couldn’t beat this! Someday I’ll finally figure it out…

Screenshot for comparison:
[http://i.imgur.com/U4ZlgY9.png](http://i.imgur.com/U4ZlgY9.png)

~~~
Sarien
Best screenshot ever.

------
Kiro
Clicking in symetric patterns reminds me of Game of Life. Not sure if it's a
good strategy though.

------
Procrastes
Fun and attractive! I did something similar for android years ago with six
states for each cell (represented by blank, card suites and a gold bar
graphic). It rapidly became so hard I had trouble solving it myself after just
a few levels. If I had stayed with it I would have written a hinting feature,
(best next move flashes).

Huh, looks like it's still up[1] on slideme.org, but I don't recommend buying
it. I'm not supporting it anymore. I'll see about taking it down.

1\.
[http://slideme.org/application/skeedle](http://slideme.org/application/skeedle)

------
borplk
Can someone expand upon Artificial Intelligence strategies for solving this?

Are there "easy" mathematical strategies? Is there a solution for an arbitrary
large grid? Complexity?

From a first glance this reminds me of "constraint satisfaction" classes of
problems. I remember studying the famous one where you are asked to colour a
map such that no two adjacent cities share the same colour.

Does this fall into that group or is there a better way?

Edit: just saw chaoxu's comment. cool.

------
allannienhuis
I kinda like this. Years ago I toyed with building a similar inversion type
puzzle game based on a hexagon field. It also had the problem of devolving
into more or less random clicking once the field got bigger, and so
frustrating the player fairly quickly as the play progressed. The hexagon play
field did lend itself to having differently shaped playfields which added
something to the play on smaller fields. Not sure if that idea has any merit
for this game, though...

------
JadeNB
As others have said, this is a nice casual game; good job! One small comment:
"Lets go" in the start prompt should be "Let's go".

EDIT: On further play, I find the self-dismissing notifications on level
completion distracting; I would like it better if they hung around until I
manually dismissed them.

------
jkrause314
Probably unintended, but you've got a nice Hermann grid illusion
([https://en.wikipedia.org/wiki/Grid_illusion](https://en.wikipedia.org/wiki/Grid_illusion))
there! Haven't seen that done with colors other than black and white.

------
superchink
This reminded me of the first native iPhone game back in 2007:

[http://www.macrumors.com/2007/08/13/lights-off-first-
native-...](http://www.macrumors.com/2007/08/13/lights-off-first-native-
iphone-game/)

------
linkeex
For anyone wondering how to solve this algorithmically:
[http://en.wikipedia.org/wiki/Lights_Out_(game)#Light_chasing](http://en.wikipedia.org/wiki/Lights_Out_\(game\)#Light_chasing)

------
dmitrygr
very old game called lights out

------
LVB
Upon reaching level 3, I couldn't help but think of Merlin.
[http://en.m.wikipedia.org/wiki/Merlin_(game)](http://en.m.wikipedia.org/wiki/Merlin_\(game\))

------
hexleo
It's just like an old game, when I was a middle student in China. The only
different place is game map. Different level has different map to increase the
difficulty.

------
samirmenon
This game is frequently a part of Legend Of Zelda: Phantom Hourglass (in the
Ocean Temple). It's cool that you've made it a standalone game.

------
ojiikun
Minor UX issue: on level up, it scrolls the page back to the top, but on my
laptop I have to scroll down just a hair to see the whole board. Very
annoying.

~~~
dgorrie
Sorry to hear, I totally didnt think about that when I was testing :P

------
jamestomasino
Perfect little casual game. I played around with something similar during a
github game challenge, but never finished. Nice work.

------
elwell
77 points by 4 hours, a valiant effort. But, I don't think we'll be seeing
Doge Flappy Bird Inverter anytime soon.

------
josephschmoe
What libraries/tools did you use to make this game?

~~~
dgorrie
Just bootstrap and jquery! Here's a link to the repo:
[https://github.com/gorried/inverter](https://github.com/gorried/inverter)

------
elwell
Eeek! I ended up with a swastika on level 6.

------
rando289
This is a variation on the game of life.

