1. Overall function

Hello everyone. I'm having troubles implementing a certain function so I hope you can help me out. Will try to be simple and quick.

Ok, so not that long ago I posted a couple of doubts regarding a "Slidding Puzzle" code. I am now trying to make the code shorter by not having to test every possibility, considering I'm willing to implement a 3x3, 4x4 and 5x5 board.

This is what I got so far:

Code:
#include <iostream>
#include <algorithm>

//Checks end game conditions (Working)
bool Verif(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;
std::cout<<"You win!";
}

}

//Validates a move (Not working)

bool ValidatesMove(int Move, int array[], int ArraySize)
{

int n;

switch (ArraySize)
{
case 9:
if (Move = array[n])
{
if (array[n - 1] == 0)
{
std::swap(array[n], array[n - 1]);
return true;
}
else if (array[n - 3] == 0)
{
std::swap(array[n], array[n - 3]);
return true;
}
else if (array[n + 1] == 0)
{
std::swap(array[n], array[n + 1]);
return true;
}
else if (array[n + 3] == 0)
{
std::swap(array[n], array[n + 3]);
return true;
}
else
{
return false;
}
}
break;
default:
break;
}
}

int main()
{

while (true)
{

int GameBoard;

GameBoard = 1; GameBoard = 2; GameBoard = 3; GameBoard = 4; GameBoard = 5; GameBoard = 0; GameBoard = 7; GameBoard = 8; GameBoard = 6;

std::cout<<std::endl <<std::endl <<std::endl;
std::cout<<"  " <<GameBoard <<"  |  " <<GameBoard <<"  |  " <<GameBoard <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<"  " <<GameBoard <<"  |  " <<GameBoard <<"  |  " <<GameBoard <<std::endl;
std::cout<<"-----+-----+-----" <<std::endl;
std::cout<<"  " <<GameBoard <<"  |  " <<GameBoard <<"  |  " <<GameBoard <<std::endl <<std::endl;

int Play;
std::cin >> Play;

//If the piece selected is next to "0" then it should swap with it. If not, then the move is invalid.
ValidatesMove(Play, GameBoard, 9);

//Checks for end game conditions
if (Verif(GameBoard, 9) == true)
{
break;
}

}

std::cin.ignore();
std::cin.get();

}

A move is supposed to be valid when the piece selected is next to a zero:

1 2 3
4 5 0
7 8 6

6

1 2 3
4 5 6
7 8 0

You win!

^That's what it is supposed to work but the pieces won't swap. 2. I'll give an example if anyone is missing the point of the following portion of code:

Code:
int n;

switch (ArraySize)
{
case 9:
if (Move = array[n])
{
if (array[n - 1] == 0)
{
std::swap(array[n], array[n - 1]);
return true;
}
else if (array[n - 3] == 0)
{
std::swap(array[n], array[n - 3]);
return true;
}
else if (array[n + 1] == 0)
{
std::swap(array[n], array[n + 1]);
return true;
}
else if (array[n + 3] == 0)
{
std::swap(array[n], array[n + 3]);
return true;
}
else
{
return false;
}
}
1 | 2 | 3
4 | 5 | 0
7 | 8 | 6

If you move piece 5 (= GameBoard[n]), it will check if :
GameBoard[n-3] = 0, which obviously isn't considering it is 2.
GameBoard[n-1] = 0, which is not (4).
GameBoard[n+3] = 0, which is not (8).
GameBoard[n+1] = 0, which is in this case, so it should swap GameBoard[n] (5) with GameBoard[n+1] (0). 3. Sorry this piece of code:

> intGameBoard;
> GameBoard = 1; GameBoard = 2; GameBoard = 3; GameBoard = 4; GameBoard = 5; GameBoard = 0; GameBoard = 7; GameBoard = 8; GameBoard = 6;

goes before the while loop, but it's not working 4. Ok, I fixed as it usually happens. 5. First, n was never initialized. so array[n - *] is undefined.
Second, "if (Move = array[n])", you're assigning, not comparing.

you will need something like this:
Code:
for ( int i =0 ; i < arraysize ; ++i)
if (Move == array[i]) then
{ n = i; break;}
Be careful with the boundary. Example, when n=2, a[n-3] is a[-1] which is bad... very very bad. 6. Yeah it was like 3 am when I was working on the algorithm so I kind of derped. It works fine now though.

And yeah, the program will come to a point where it has to check for negative values of the array which is something dangerous. I don't know what would I do to fix it though, considering it works alright at this point and it doesn't crash. 7. I did the following so it won't check for invalid positions of the array:

Code:
//Checks if is a valid move regardless of the size of the board
void ValidMove(int Move, int array[], int position, int BoardConfig, int arraySIZE)
{

if (Move == array[position])
{
if  ((position - 1) >= 0)
{
if (array[position - 1] == 0)
{
std::swap(array[position], array[position -1]);
}
}
else if ((position - BoardConfig) >= 0)
{
if (array[position - BoardConfig] == 0)
{
std::swap(array[position], array[position - BoardConfig]);
}
}
else if ((position + BoardConfig) < arraySIZE)
{
if (array[position + BoardConfig]  == 0)
{
std::swap(array[position], array[position + BoardConfig]);
}
}
else if ((position + 1) < arraySIZE)
{
if (array[position + 1] == 0)
{
std::swap(array[position], array[position + 1]);
}
}
else
{
std::cout<<"Error";
}
}
} 8. Code:
if (array[position - 1] == 0 && (position - 1) >= 0)
If you write the check this way it doesn't help much.

Try this way
Code:
if ((position - 1) >= 0 && array[position - 1] == 0 )
This way the array at position -1 will not be accessed if position -1 is smaller then 0

Kur 9. Yeah, I ninja edited and changed to something similar :P 10. Originally Posted by Khabz it works alright at this point and it doesn't crash.
worst reason I've ever heard for not fixing obvious bugs. 11. Originally Posted by Elkvis worst reason I've ever heard for not fixing obvious bugs.
1st. I was stating a fact, not an excuse.
2nd. 5 minutes later I had it fixed. 12. Originally Posted by Khabz 1st. I was stating a fact, not an excuse.
2nd. 5 minutes later I had it fixed.
I wasn't necessarily referring to this situation. it's just generally a good idea to avoid thinking that way. Popular pages Recent additions 