Code:
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <Windows.h>
#include <ctime>
//Function which converts a char into an int
int digit_to_int(char d)
{
char str[2];
str[0] = d;
str[1] = '\0';
return (int) strtol(str, NULL, 10);
}
//Checks for end game conditions regardless of the board size
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: ";
}
}
}
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: ";
}
}
}
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: ";
}
}
}
int main()
{
std::cout<<"[1] Training Mode" <<std::endl;
std::cout<<"[2] Competition Mode" <<std::endl <<std::endl;
bool ValidOption2 = false;
while (ValidOption2 != true)
{
char Option = ' ';
Option = _getch();
if (Option == '2')
{
ValidOption2 = false;
std::cout<<"\nNot yet developed, choose another option: ";
}
else if (Option == '1')
{
ValidOption2 = true;
}
else
{
ValidOption2 = false;
std::cout<<"\nInvalid Option, try again: ";
}
}
Restart:
system("CLS");
std::cout<<"Choose your width in the format NxN: ";
//Only allows a string 3 characters long (NxN).
bool ValidOption = false;
while (ValidOption != true)
{
char format1, x, format2;
format1 = _getch();
std::cout<<format1;
x = _getch();
std::cout<<x;
format2 = _getch();
std::cout<<format2;
if (format1 == format2 && format1 == '3')
{
if (x == 'x' || x == 'X')
{
ValidOption = true;
goto Boardx3;
}
}
else if (format1 == format2 && format1 == '4')
{
ValidOption = true;
goto Boardx4;
}
else if (format1 == format2 && format1 == '5')
{
ValidOption = true;
goto Boardx5;
}
else
{
ValidOption = false;
std::cout<<"\n\nInvalid/Unavailable width. Try again: ";
}
}
Boardx3:
system("CLS");
//3x3 Slide Puzzle
int GameBoard[9];
GameBoard[0] = 2; GameBoard[1] = 1; GameBoard[2] = 4; GameBoard[3] = 3; GameBoard[4] = 0; GameBoard[5] = 5; GameBoard[6] = 7; GameBoard[7] = 8; GameBoard[8] = 6;
//Starts timing
clock_t begin2 = clock();
while (true)
{
bool Val = false;
std::cout<<std::endl <<std::endl <<std::endl;
std::cout<<" " <<GameBoard[0] <<" | " <<GameBoard[1] <<" | " <<GameBoard[2] <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<" " <<GameBoard[3] <<" | " <<GameBoard[4] <<" | " <<GameBoard[5] <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<" " <<GameBoard[6] <<" | " <<GameBoard[7] <<" | " <<GameBoard[8] <<std::endl <<std::endl;
while (Val != true)
{
char Play = ' ';
Play = _getch();
int P = digit_to_int(Play);
system("CLS");
//Checks for (in)valid moves
//Exits whenever
if (Play == 'q' || Play == 'Q')
{
exit(0);
}
for (int i = 0; i <= 8; i++)
{
if (P == GameBoard[i])
{
if (i == 2 || i == 5)
{
MoveValidation2(P, GameBoard, i, 3, 9, Val);
Val = true;
}
else if (i == 3 || i == 6)
{
MoveValidation3(P, GameBoard, i, 3, 9, Val);
Val = true;
}
else
{
MoveValidation(P, GameBoard, i, 3, 9, Val);
Val = true;
}
}
}
//Calls MoveValidation function for each possiblity of the board
/*
if (P == GameBoard[0]) {
MoveValidation(P, GameBoard, 0, 3, 9, Val);
}
else if (P == GameBoard[1]) {
MoveValidation(P, GameBoard, 1, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[2]) {
MoveValidation2(P, GameBoard, 2, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[3]) {
MoveValidation3(P, GameBoard, 3, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[4]) {
MoveValidation(P, GameBoard, 4, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[5]) {
MoveValidation2(P, GameBoard, 5, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[6]) {
MoveValidation3(P, GameBoard, 6, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[7]) {
MoveValidation(P, GameBoard, 7, 3, 9, Val);
Val = true;
}
else if (P == GameBoard[8]) {
MoveValidation(P, GameBoard, 8, 3, 9, Val);
Val = true;
}*/
}
//End Game Verification
if (EndGameVerif(GameBoard, 9) == true)
{
break;
}
}
system("CLS");
//Obtains the total amount of seconds taken
clock_t end2 = clock();
double elapsed_secs2 = double(end2 - begin2) / CLOCKS_PER_SEC;
std::cout<<std::endl <<std::endl <<std::endl;
std::cout<<" " <<GameBoard[0] <<" | " <<GameBoard[1] <<" | " <<GameBoard[2] <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<" " <<GameBoard[3] <<" | " <<GameBoard[4] <<" | " <<GameBoard[5] <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<" " <<GameBoard[6] <<" | " <<GameBoard[7] <<" | " <<GameBoard[8] <<std::endl <<std::endl;
std::cout<<"You win!";
std::cout<< std::endl <<std::endl;
std::cout<< "You took " <<elapsed_secs2 <<" seconds to solve the puzzle." <<std::endl <<std::endl;
std::cout<<"Play again? (y/n): ";
bool ValidOption3 = false;
while (ValidOption3 != true)
{
char Again = ' ';
Again = _getch();
if (Again == 'y')
{
ValidOption3 = true;
goto Restart;
}
else if (Again == 'Y')
{
ValidOption3 = true;
goto Restart;
}
else if (Again == 'n')
{
ValidOption3 = true;
exit(0);
}
else if (Again == 'N')
{
ValidOption3 = true;
exit(0);
}
else
{
ValidOption3 = false;
std::cout<<"Invalid input, try again: ";
}
}
Boardx4:
Boardx5:
std::cin.get();
exit(0);
}
The for loop is supposed to do the same as the commented part of the program, which is working fine.