tags:

views:

57

answers:

1

I'm currently making a rummikub "game" so that when I make a Mistake I am able to get the board back easily to what it was before.

Currently in need to find out for a given number of tiles if a solution exists and what the solution is.

Example

Elements Selected are {Red1, Red1, Blue1, Black1, Orange1, Orange1}

And a solution would be {Red1, Blue1, Orange1} and {Red1, Black1, Orange1}

I can currently determine which groups and runs are possible ({Red1, Blue1, Black1, Orange1} being a valid group that wouldn't appear in the valid solution). I need a solution that can go the next step and tell me which of the groups/runs can exist together and make sure each tile is used once. If no such solution exist, it needs to be able to report that. The solution does not have to be optimal (ie use the smallest number of groups/run), it just has to be valid.

A: 

If you have a method of determining what groups and runs are possible, why don't you modify your algorithm to remove tiles that you have already used, and make the function recursive. Here is some pseudo-code:

array PickTiles(TileArray) {

    GroupsArray = all possible groups/runs;

    foreach Group in GroupsArray {
         newTileArray = TileArray;
         remove Group from newTileArray;

         if(newTileArray.length() == 0) {
               return array(Group);
         }

         result = PickTiles(newTileArray);
         if(result.length() > 0) {
               return result.append(array(Group));
         }
    }

    return array();
}
Jeff B
Thanks for this solution. I was building all possible solutions as they selected tiles, not finding all solutions as a function which you feed tiles into. I'll have to move some code around, but this will work great. Is this algorithm based on a more common one, or is it something you've just come up with?
Joshua
Mostly just something I came up with, although I play with puzzle solving algorithms occasionally as a diversion, usually word-based jumble-type puzzles.
Jeff B