[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Here's a simple puzzle (game?) that's not so easy. 16 tokens, black on one side and white on the other, are arranged on a 4X4 board. The objective is to turn the board all white in as few moves as possible by flipping tokens. The complication is that when a token is turned, the adjacent tokens directly above, below, right, and left of the flipped token are also flipped. Each move may result in one to five tokens being reversed.
Here's a sample that can be solved 3 moves -by clicking the tokens in (Col 3, Row 1), (Col 2, Row 3), (Col 3, Row 4)
Background & Techniques
I ran across this problem in the ACM Programming Contest archive web pages. Implemented here is a version that allows user play as well as program solve. The board is a DrawGrid with an OnDrawCell exit to draw the tokens. An OnClick event exit for the Drawgrid is used to implement user play.
I couldn't find any references to the game, so I'm not sure that it has been analyzed. There is no obvious measure of the path from a starting position to the all white target position. In fact, most randomly generated board configurations seem to have no solution.
I decided to use a breadth first graph search to find solutions. Recall these pertinent aspects of breadth first searching. ("Next" and "adjacent" here refer to board configurations that are one move from the current configuration.)
In order to conserve memory, I created a TBoard class defining board configurations as 16 bit words. "1" bits in the word represent white tokens and "0" bits represent black tokens. I decided to used the TIntList control to create an integer list for the queue of positions being searched. (TIntList is a list similar to TStringlist, but with integer members described here). The integer used for entries in the list are the 16 bit numbers that reflect the board configuration. The TBoard instance is added as an object with each entry to carry the score and path information as well.
I added a Path array to the TBoard definition to contain the list of moves that led to this configuration.
There is a 'Build mode" button which allows individual tokens to be flipped without affecting adjacent tokens.
Addendum: November 24, 2002: Version 2 posted today allows board sizes up to 7X7. The breadth first solution technique described above was replaced by a one that generates trial solutions as combinations of N moves selected from size2 possible move locations. (This technique takes advantage of the fact that the order of moves is not significant in this game.) The new technique can solve boards up to 5X5 in a few minutes. Beyond 5x5 it seems like we'll need to find some intelligent heuristics that will prune the search space dramatically.
Addendum: March 17, 2003: At the request of a viewer, maximum board size has been increased to 10X10. although the exhaustive solution search technique will is still not feasible beyond 5X5.
Addendum: April 16, 2003: A viewer supplied a method, and Pascal code, which finally closes the book on auto-solving large puzzles. He cleverly set up the problem as a system of linear equations and solves it using linear algebra matrix techniques. Check it out at Token Flip - The Final Chapter
Running/Exploring the Program
Suggestions for Further Explorations
Copyright © 2000-2018, Gary Darby All rights reserved.