+ qsort (&tile_group->tiles[0], tile_group->num_tiles,
+ sizeof (tile_t), tile_compare);
+
+ if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
+ {
+ return 0;
+ }
+ for (i = 0; i < tile_group->num_tiles - 1; ++i)
+ if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
+ {
+ return 0;
+ }
+ if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
+ {
+ return 0;
+ }
+ return 1;
+}
+
+
+static int tile_group_is_run_two(tile_group_t *tile_group)
+{
+ int i;
+ int lowest = 14, highest = 0;
+ color_t run_color;
+
+ /* By definition, a run must have at least 3 tiles. Also, it's
+ * impossible for any group of tiles with more than 13 tiles to be
+ * a run, (there are only 13 unique numbers so a group with more
+ * than 13 tiles must have some duplicates).
+ */
+ if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
+ {
+ return 0;
+ }
+
+ /* Loop through all tiles in the group, ensuring that they are all
+ * the same color and finding the highest and lowest number in the
+ * group. */
+ run_color = tile_group->tiles[0].color;
+
+ for (i = 0; i < tile_group->num_tiles; i++)
+ {
+ if (tile_group->tiles[i].color != run_color)
+ return 0;
+ if (tile_group->tiles[i].number > highest)
+ {
+ highest = tile_group->tiles[i].number;
+ }
+ if (tile_group->tiles[i].number < lowest)
+ {
+ lowest = tile_group->tiles[i].number;
+ }
+ }
+
+ /* For a run, the difference between the highest and lowest tiles
+ * will always be one less than the number of tiles in the
+ * group. If not then we know it's not a run.
+ */
+ if (highest - lowest != tile_group->num_tiles - 1)
+ {
+ return 0;
+ }
+
+ /* XXX: There's a bug here. We're guessing that at this point
+ * anything we're looking at must be a run. This would be correct
+ * if there were no duplicate tiles, but since there are
+ * duplicates this us quite broken. For example consider two
+ * sequences of entirely red tiles:
+ *
+ * This is a run: 1, 2, 3, 4
+ * But this is not: 1, 3, 4, 4
+ *
+ * As currently written, this function will consider both of these
+ * groups to be a run. One possible fix is to throw away the
+ * highest - lowest heuristic and instead simply sort the tiles up
+ * front and ensure the difference between each adjacent pair is
+ * exactly 1.
+ */
+ return 1;
+}
+
+static int tile_group_is_set(tile_group_t *tile_group)
+{
+ int i;
+ color_t seen_color[tile_group->num_tiles];