/*Advance AI for Game & Sim Dsgn w/L
Professor: Marty Geier
By: Cody Bennett
References: 1) http://xoax.net/cpp/crs/console/lessons/Lesson29/
2) http://www.cplusplus.com/forum/beginner/11636/
*/
#include <iostream>
#include <string>
#include <conio.h>
#include <ctime>
#include <calculations.h>
#include <Windows.h>
using namespace std;
// To use the arrow keys instead of letters
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
void print(int board[][10], int);
void initialize(int board[][10], int);
void slide(int board[][10],int move,int);
bool isBoardSolved(int board[][10],int);
void scramble(int board[][10],int);
void WaitKey();
int main()
{
int boardAmount = 0;
int moves = 0;
int shuffleCount = 0;
do
{
system("CLS");
cout << endl << endl << endl << "\t\tEnter designated size for sliding puzzle. (3 to 5)";
cout << endl << endl << "\t\t\t\t\t";
cin >> boardAmount;
if(boardAmount <3 || boardAmount > 5)
cout << "\t\t\tSorry, this program is unable to allow playing with that size specified. Please, enter another size." << endl << endl;
Sleep(1300);
}while (boardAmount <3 || boardAmount > 5);
int gameBoard[10][10];
char input;
string direction;
bool invalid = false;
boardAmount--; // Subtract 1, so checking of array would be from 0 to amount. No need to add -1 to checks // to keep it in bounds of board
initialize(gameBoard,boardAmount);
print(gameBoard,boardAmount);
cout << boolalpha;
cout<<"isBoardSolved(): "< > shuffleCount;
cout<<"Press enter to begin"<<endl;
WaitKey();
cout<<"Scrambling board..."<<endl;
scramble(gameBoard, boardAmount);
cout<<"Scrambling complete, press enter to continue"<<endl;
cin.get();
system("CLS");
print(gameBoard, boardAmount);
cout<<endl<<endl;
cout<<"Use arrow keys to move the blank in the desired direction in order to move the tiles!"<<endl;
cout<<"Input: ";
while(!isBoardSolved(gameBoard, boardAmount))
{
input=_getch();
system("CLS");
switch(input)
{
case DOWN:
slide(gameBoard, 2, boardAmount);
direction ="Up";
moves++;
break;
case RIGHT;
slide(gameBoard, 0, boardAmount);
direction="Left";
moves++;
break;
case UP;
slide(gameBoard, 3, boardAmount);
direction="Down";
moves++;
break;
case LEFT;
slide(gameBoard, 1, boardAmount);
direction = "Right";
moves++;
break;
default:
invalid = true;
}
print(gameBoard, boardAmount);
cout<<endl<<endl;
cout<<"use arrow keys to move the tiles in the desired direction!"<<endl;
if(invalid)
invalid = false;
else
cout<<"Last Input: "<<direction;
}
cout <<endl;
cout << "BOARD SOLVED" << endl;
cout << "Moves made: " << moves << endl;
system ("PAUSE");
}
/* Print board and replace zero with a character 177 */
void print(int board[][10], int amt)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(int row = 0; row <= amt; row++)
{
for(int column = 0; column <= amt; column++)
{
if(board[row][column] == 0)
{
SetConsoleTextAttribute(hConsole, 7); //Default color
cout << "\xB1\xB1 ";
}
else
{
if(board[row][column] == solved[row][column]) //If board location is equal to solvedBoard location then set to green SetConsoleTextAttribute(hConsole, 10);
else
SetConsoleTextAttribute(hConsole, 12); //else, set to red
if (board[row][column]<10) // Print a 0 first if # < 10
cout<<"0";
cout<<board[row][column]<<"";
}
}
cout<<endl;
SetConsoleTextAttribute(hConsole, 7);
}
void scramble(int board[][10], int shuffleCount)
{
time_tt;
srand((unsigned)time(&t));
int move;
while (isBoardSolved(board, shuffleCount)) //If the board ends up being solved at the end of the scramble, scramble the board again
{
for(int i=0; i < 100000;i++) //Series of random moves
{
move = rand() % 4;
slide(board,move,shuffleCount);
}
}
}
void WaitKey()
{
while (_kbhit())_getch(); //Empty the input buffer
_getch(); //Wait for a key
while (_kbhit())_getch(); //Empty the input buffer (some keys sends two messages)
}