views:

762

answers:

5

Suggest an algorithm and data structure for solving the game Globs (http://www.deadwhale.com/play.php?game=131). It's pretty fun in a geeky kind of way.

(MatrixFrog correctly points out this game is also known as FloodIt, and Smashery gave a solution 3 months ago in the link he cites below. All you dudes suggesting pruning/greedy with only 1 lookahead, that gives suboptimal solutions.)

The game generates a random square grid of nxn nodes, where each node is colored one of six colors (Grn=1, Ylw=2, Red=3, Blu=4, Pur=5, Orn=6). Level 1 has 9x9 grid, then n increases each level, up to 14. Each level you can take up to 25 turns or else you lose. On each turn you choose which color to change the top left node to e.g. Grn->Red, such that any connected adjacent (horiz/vert) nodes of the new color get assimilated into a shape, and 1 pt per node assimilated is ADDED to your score. The scoring objective is to complete each grid in as few turns as possible, e.g. if you do it in 16 turns, then your 9 unused moves => 2*9 MULTIPLIER times your total accumulated score.

Obviously there are a ton of ways to decompose this, and the default choice of recursive backtracking with a 14x14 grid is a viable contender; What other types of data structures does this lend itself to? A* ? Don't get hung up on optimality, I'm wondering if there is a "good-enough" algorithm.

(I thought it might be a fun project to code up a robot and get silly-high scores. Although I scored 3.5E+12 all by my fleshware self.)

A: 
  1. if you can, eliminate a color.
  2. chose the color that generate more new neighbors for you !
  3. goto step 1.
amir beygi
Step 1 is almost correct, but does not necessarily generate the highest score in the case when you know you wont finish the alloted number of moves. Step 2 can be easily shown to be non optimal when you take into account the score for accepting smaller steps now in preparation for taking larger steps later.
NickLarsen
Could scan with depth of 1 or 2 generates a better result?
amir beygi
@AmirStep 1 is fine, Step 2 is pruning too early and with a suboptimal heuristic.Step 1: let's not worry too much about insoluble grids, and certainly not sacrifice performance in the common-case where the grid is soluble.Eliminating a color is in general good because it reduces the branching factor for all future regression.Step 2: Depth-1 or -2 is pruning too early. Depth-26 is full recursion (on a 14x14 grid) A side-question would be to investigate the effect of the pruning horizon on optimality. (Maybe say depth-12 is "good-enough").
smci
+1  A: 

A brute-force recursive search will find the maximum score. You have at most 5^25 ending states to consider. Many intermediate states will be equivalent; it may be faster to recognize these and prune the search space of duplicates. Keep track of the highest score found so far as you search, along with the path (sequence of moves) that it took to get there.

William Graham
While it is true that a brute force search will find the answer, I dont know of any system which can evaluate 298 million billion positions in any reason amount of time. Most of the pruning for this specific scenario will be in the very first move (at most 2 possible next useful states) and last 5 moves, which will keep you from finding a good answer in any reasonable amount of time. That being said, it is technically a valid solution.
NickLarsen
+1  A: 

I don't know the solution, but this sounds like a job for dynamic programming.

asmeurer
+1  A: 

Given the fixed starting state and limited number of moves I think you can fully explore a decision tree. For each round, there are only 5 possible moves and wasted moves (choosing a color that will not 'glob' any neighbors what-so-ever) can be eliminated as the tree is built. Once the decision tree is built I think you could explore the point value of each path but if you needed more optimization a A* would definitely get you close.

For each round, I would have the basic state as a matrix of bit arrays for the state of the unglobbed locations (since the color no longer matters in the globbed locations you could save memory on your state data structure by leaving off the color bits) and a point value for each decision possible. Then your A*, or breadth first algorithm can just maximize the path values as normal. Save the path, and once your analysis is complete, make all of the determined moves.

Ichorus
+1  A: 

This game really grabbed my interest, so I spent a couple of days working on it.

The first thing I noticed, is that it is easy to show that after the first board (maybe 2 in some cases), the fastest way to raise the score is by using the multiplier. Because of this, I built a system with the goal of solving each board in the fewest number of steps. I started out wanting to use A* because it is generally built for just these types of search problems... however, this problem still turned out to be a doozie.

When talking about A*, the effectiveness of it really boils down your choice of heuristic estimation. The closer you get to guessing the actual distance, the fewer nodes that will have to be expanded in order to reach the goal. For this problem, I went through a number of ideas for estimation, but most of them broke the A* rule, which is that you can NOT over estimate the actual distance, or else you break the optimality of A*.

There are a few that work however. Others in this thread have posted about just taking the number of remaining colors as the estimation, which is admissible because it cannot over estimate (you have to change colors at least once for each remaining color not part of the main "flood" area. The problem with this heuristic is that it very poorly estimates the actual distance. Take for instance the first move, which generally has an estimation of the number of colors, 6. It often expands into 2 moves, each of which generally has an estimation of 7, and so on and so on. Take this 5 levels deep and for a board size of 10x10, most leafs have an estimation of 11. This heuristic is basically an implementation of a breadth first search until you reach within 4 or 5 moves from your goal. This is not very efficient and in my own tests, the exponents run a much around board size 9, which often requires about 14 moves in the solution. It should be noted my solution was very high level however and not much care was taken to speed things up.

The problem is that A* is really only good when each step makes a significant refinement to the actual distance of the overall solution. Looking at the problem directly, you probably wont find a good heuristic that can do much better than this without over estimating the cost. However, if you transform the problem into another problem, better heuristics jump out at you. The heuristic "number of colors remaining" is answering the question, what is the smallest number of possible moves remaining. To the answer that question, I asked myself "which spot on the board requires the maximum number of steps to get to"? I ended up settling on the answer to "how many steps is it to the bottom right corner" for my heuristic. This is fairly easy to implement by running another A* search that works more like finding map directions and then counting the number of steps in the solution. I realize this is an arbitrary point on the board to select, however it worked quite well in testing and running A* on every remaining point took a fair amount of time on my single processor test machine.

This heuristic alone had a tendency to collapse after the bottom right corner became part of the flooded area however, so the final result was MAX(bottom right corner min steps, number of colors remaining not part of main flood). This was finally able to achieve some very large board sizes in under a second with my high level implementation.

I'll leave the record setting to you.

NickLarsen
@Nick: "The problem is that A* is really only good when each step makes a significant refinement to the actual distance of the overall solution" <=> needs a good heuristic.Why not use "how many steps is it to the most distant square (in the color-connectivity sense, not Euclidean sense"?
smci
I'm thinking by color-connectivity sense that you mean the number of color changes you have to go through in order to get to the answer, which brings you right back to the original problem, having no heuristic better than the number of colors not connected to the main flood. With Euclidean distance, you have an easily admissible (and good) heuristic and then you count the number of steps in the solution to the sub problem.
NickLarsen
No, I had proposed "distance of the most distant point in terms of the the number of successive color changes needed for the glob to reach it".But using Manhattan (not Euclidean) distance to most-distant-node, similar to what you suggest, would be way faster and require less computation. Strictly all we would need to do is check the corners/outer perimeter of the glob, and compute Manhattan distance to the most distant non-globbed nodes (in BR, BL, TR, TL). That should evaluate very fast.
smci
It turns out to be very fast to use Dijkstra's algorithm for shortest path to all nodes, find the highest number and take the greater of that or number of non connected colors remaining. Its also way less complicated than my nested A* original approach.
NickLarsen
If you use color connectivity as your distance than there is a clear number of minimum steps to each node and you could use that distance in your heuristic. I think there is two more important factors in each step. The first is you should also try to maximize the number of squares you fill. Whenever I play the game I try to keep myself close to the diagonal and get to the south-east corner as fast as possible. This tends maximize my later moves since I flood in all directions. The second is that if you can flood all remaining squares of a color this is a free move.
Juan Besa
@Jaun Besa Point 2 is correct, once you can flood all remaining squares of a color, there is no point in waiting. Point 1 however is not necessarily correct when you incorporate the value of the round. Since score is increased by selecting the most of one color at the same time, it can often be a good idea to flood a smaller color now in hopes of building a larger value from a subsequent move.
NickLarsen