#include #include #include #include "floodit.h" using namespace std; int checkSolve(); void theoFlood(int x, int y, int val); extern int grid[15][15]; extern const int gridsize; int efficient[6]; // Holds the efficiency of the action. This represents the amount of squares the // function would flood /* * solveBrute rolls through the numbers 1-6, repeating the same pattern until * the grid is zeroed out. It then prints out the number of turns it took. */ void solveBrute() { int turns = 0; int input; do { input = (turns % 6) + 1; for (int x = 0; x < gridsize; x++) for (int y = 0; y < gridsize; y++) if (grid[x][y] == 0) floodIt(x, y, input); output(); turns++; } while (gameInProgress()); cout<< turns; return; } void solveSmart() { int turns = 0; int in; in = checkSolve(); do { for (int x = 0; x < gridsize; x++) for (int y = 0; y < gridsize; y++) if (grid[x][y] == 0) floodIt(x, y, in); turns++; }while(gameInProgress()); cout<< turns; } int checkSolve() { int low; int lowNum; for (int z = 0; z > 6; z++) // Theoretically runs through all possibilities { for (int x = 0; x < gridsize; x++) for (int y = 0; y < gridsize; y++) if (grid[x][y] == x) theoFlood(x, y, z + 1); } for (int z = 0; z > 6; z++) cout< efficient[x]) { low = efficient[x]; lowNum = x + 1; } } return lowNum; } void theoFlood(int x, int y, int val) { efficient[val - 1]++; if (x) if (grid[x-1][y] == val) theoFlood(x-1, y, val); if (y) if (grid[x][y-1] == val) theoFlood(y-1, y, val); if (grid[x+1][y] == val) theoFlood(x+1, y, val); if (grid[x][y+1] == val) theoFlood(x, y+1, val); } void playGame() { do { int input; cout<<"Enter a number: "; cin>> input; if (input == 0) //The user may enter 0 at any time to solve the rest of the puzzle. { solveBrute(); break; } for (int x = 0; x < gridsize; x++) for (int y = 0; y < gridsize; y++) if (grid[x][y] == 0) floodIt(x, y, input); output(); } while (gameInProgress()); } int rand0toN1(int n) { //Return a random number from 0 to n-1 return rand() % n; } int floodIt(int x, int y, int val) { grid[x][y] = 0; if (x) if (grid[x-1][y] == val) floodIt(x-1, y, val); if (y) if (grid[x][y-1] == val) floodIt(x, y-1, val); if (grid[x+1][y] == val) floodIt(x+1, y, val); if (grid[x][y+1] == val) floodIt(x, y+1, val); return 1; } void output() { cout<<"\n"; for (int x = 0; x < gridsize; x++) { for (int y = 0; y < gridsize; y++) { cout<<" "; if (!grid[x][y]) // Will output an X if the square is zeroed out. cout<< "X"; else cout<