Code:
#include <iostream>
#include <algorithm>
#include <ctime>
#include <conio.h>
using namespace std;
void PuzzleBoard4x4(int array[])
{
std::cout<<" " <<array[0] <<" | " <<array[1] <<" | " <<array[2] <<" | " <<array[3] <<std::endl;
std::cout<<"-----+-----+-----+-----" <<std::endl;
std::cout<<" " <<array[4] <<" | " <<array[5] <<" | " <<array[6] <<" | " <<array[7] <<std::endl;
std::cout<<"-----+-----+-----+-----" <<std::endl;
std::cout<<" " <<array[8] <<" | " <<array[9] <<" | " <<array[10] <<" | " <<array[11] <<std::endl;
std::cout<<"-----+-----+-----+-----" <<std::endl;
std::cout<<" " <<array[12] <<" | " <<array[13] <<" | " <<array[14] <<" | " <<array[15] <<std::endl <<std::endl;
}
bool EndGameVerif(int array[], int ArraySize)
{
int counter = 0;
for (int i = 0; i < (ArraySize - 1); i++)
{
if (array[i] == (i + 1) && array[ArraySize - 1] == 0) {
counter = counter + 1;
}
else {
counter = counter;
}
}
if (counter == (ArraySize - 1)) {
return true;
}
}
void MoveValidation(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
{
if (Move == array[position])
{
if ((position - 1) >= 0 && array[position - 1] == 0) {
Valid = true;
std::swap(array[position], array[position -1]);
}
else if ((position - BoardConfig) > 0 && array[position - BoardConfig] == 0) {
Valid = true;
std::swap(array[position], array[position - BoardConfig]);
}
else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig] == 0) {
Valid = true;
std::swap(array[position], array[position + BoardConfig]);
}
else if ((position + 1) < arraySIZE && array[position + 1] == 0) {
Valid = true;
std::swap(array[position], array[position + 1]);
}
else {
Valid = false;
std::cout<<"Invalid input, try again: ";
}
}
}
//Validates a move for the pieces on the right of the board except for the last one
void MoveValidation2(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
{
if (Move == array[position])
{
if ((position - BoardConfig) >= 0 && array[position - BoardConfig] == 0)
{
Valid = true;
std::swap(array[position], array[position - BoardConfig]);
}
else if (array[position - 1] == 0)
{
Valid = true;
std::swap(array[position], array[position - 1]);
}
else if (array[position + BoardConfig] == 0)
{
Valid = true;
std::swap(array[position], array[position + BoardConfig]);
}
else
{
Valid = false;
std::cout<<"Invalid input, try again: ";
}
}
}
//Validates a move for the pieces on the left of the board except for the first one
void MoveValidation3(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
{
if (Move == array[position])
{
if (array[position - BoardConfig] == 0)
{
Valid = true;
std::swap(array[position], array[position - BoardConfig]);
}
else if (array[position + 1] == 0)
{
Valid = true;
std::swap(array[position], array[position + 1]);
}
else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig] == 0)
{
Valid = true;
std::swap(array[position], array[position + BoardConfig]);
}
else
{
Valid = false;
std::cout<<"Invalid input, try again: ";
}
}
}
//Swaps the piece selected by the player with 0. Different approach on the position of the piece in order to avoid buffer overruns (eg:array[-1])
void MakesMove(int arraySIZE, int Move, int array[], int BoardConfig, int Val2)
{
for (int i = 0; i < arraySIZE; i++)
{
if (Move == array[i])
{
if ((i == (BoardConfig - 1) ) || (i == ((2 * BoardConfig) - 1)) || (i == ((3 * BoardConfig) - 1)) || (i == ((4 * BoardConfig) - 1)))
{
MoveValidation2(Move, array, i, BoardConfig, arraySIZE, Val2);
break;
}
else if ((i == BoardConfig) || (i == (2 * BoardConfig)) || (i == (3 * BoardConfig)) || (i == (4 * BoardConfig)))
{
MoveValidation3(Move, array, i, BoardConfig, arraySIZE, Val2);
break;
}
else
{
MoveValidation(Move, array, i, BoardConfig, arraySIZE, Val2);
break;
}
}
}
}
int main()
{
Restart:
int GameBoard4x4[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 13, 14, 15, 12};
//Starts timing
clock_t begin2 = clock();
//Main loop for the 4x4 slide puzzle
while (true)
{
bool Val2 = false;
//Initial board
std::cout<<std::endl <<std::endl <<std::endl;
PuzzleBoard4x4(GameBoard4x4);
//Prompts the player for a move
int Play4;
std::cin >> Play4;
//Validates a move
MakesMove(16, Play4, GameBoard4x4, 4, Val2);
//End Game Verification
if (EndGameVerif(GameBoard4x4, 16) == true)
{
break;
}
system("CLS");
}
system("CLS");
//Obtains the total amount of seconds taken
clock_t end2 = clock();
double elapsed_secs2 = double(end2 - begin2) / CLOCKS_PER_SEC;
//Displays the final winning board
PuzzleBoard4x4(GameBoard4x4);
std::cout<<"\n\nYou win!";
std::cout<< std::endl <<std::endl;
std::cout<< "You took " <<elapsed_secs2 <<" seconds to solve the puzzle." <<std::endl <<std::endl;
//Play again option
std::cout<<"Play again? (y/n): ";
bool ValidOption4 = false;
while (ValidOption4 != true)
{
char Again2 = ' ';
Again2 = _getch();
if (Again2 == 'y')
{
ValidOption4 = true;
goto Restart;
}
else if (Again2 == 'Y')
{
ValidOption4 = true;
goto Restart;
}
else if (Again2 == 'n')
{
ValidOption4 = true;
exit(0);
}
else if (Again2 == 'N')
{
ValidOption4 = true;
exit(0);
}
else
{
ValidOption4 = false;
std::cout<<"Invalid input, try again: ";
}
}
}