All pastes #996165 Raw Edit

Unnamed

public cpp v1 · immutable
#996165 ·published 2008-04-24 19:24 UTC
rendered paste body
int FloodGrid::smartSolve()  {    /********************************************     * smartSolve -- Solves the grid using a    *     *  smart algorithm.                        *     *                                          *     * RETURNS                                  *     *  The number of turns taken to solve the  *     *  puzzle.                                 *     *******************************************/    int turns = 0;    int input;    do      {        input = bestMove();        for (int x = 0; x < gridsize; x++)          for (int y = 0; y < gridsize; y++)            if (grid[x][y] == 0)              floodIt(x, y, input);        turns++;      } while (inProgress());    return (turns);  }int FloodGrid::bestMove()  {    /********************************************     * bestMove -- Calculates the amount of     *     *  squares would be turned with all        *     *  possible moves and selects the best one.*     * RETURNS                                  *     *  The best move to make, 1-6.             *     *******************************************/    int highValue;    int highNum;    int efficient[6];    for (int x = 0; x < 6; x++)      efficient[x] = 0;    for (int z = 1; z <= 6; z++) // Theoretically runs through all possibilities      {        FloodGrid theo(*this); //A new grid object to run the tests, identical to current grid        for (int x = 0; x < gridsize; x++)          for (int y = 0; y < gridsize; y++)            if (theo.grid[x][y] == 0)              theo.theoFlood(x, y, z, &efficient[z-1]);        highValue = efficient[0];        highNum = 0;        for (int x = 0; x < 6; x++)          {            if (highValue < efficient[x])              {                highValue = efficient[x];                highNum = x;              }          }      }    return highNum + 1;  }void FloodGrid::theoFlood(int x, int y, int val, int *p)  {    /********************************************     * theoFlood -- floods the grid and gets    *     *  the efficiency of the value passed.     *     * PARAMETERS                               *     *  x       - x coordinate                  *     *  y       - y coordinate                  *     *  val     - The value to flood.           *     *  *p      - Pointer to the efficiency     *     *            table.                        *     *******************************************/    if (grid[x][y] == val)      {        ++*p;        grid[x][y] = 0;      }    if (x)      if (grid[x-1][y] == val)        theoFlood(x-1, y, val, p);    if (y)      if (grid[x][y-1] == val)        theoFlood(x, y-1, val, p);    if (x < gridsize - 1)      if (grid[x+1][y] == val)        theoFlood(x+1, y, val, p);    if (y < gridsize - 1)      if (grid[x][y+1] == val)        theoFlood(x, y+1, val, p);  }FloodGrid::FloodGrid(FloodGrid const &src)  {    /********************************************     * CONSTRUCTOR - Creates new grid that is   *     *  identical to the one passed in the      *     *  argument.                               *     * PARAMETERS                               *     *  src - The grid to be copied from.       *     *******************************************/    for (int x = 0; x < gridsize; x++)      for (int y = 0; y < gridsize; y++)        grid[x][y] = src.grid[x][y];  }