Ok, I have the following game board:
1 | 2 | 3
4 | 5 | 0
7 | 8 | 6
You're supposed to win the game when your board looks like this:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 0
Which means you'd have to swap the 0 for the 6.
So for this example I defined an array of integers:
Ok so the code works fine for the example above (Note that it will only work if you want to move the "6").Code:int GameBoard[9];
GameBoard[0] = 1;
GameBoard[1] = 2;
(...)
GameBoard[5] = 0;
(...)
GameBoard[8] = 6;
//Checks for end game conditions
if (Play == GameBoard[8])
{
if (GameBoard[7] == 0)
{
swap(GameBoard[8], GameBoard[7]);
goto UpdateBoard;
}
if (GameBoard[5] == 0)
{
swap(GameBoard[8], GameBoard[5]);
goto UpdateBoard;
}
else {
cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
goto InitialBoard;
}
UpdateBoard:
cout<<"You win".;
My issue is I can't make it work with a char type array, because the "0" is supposed to be an empty space and not a number.
I tried with the following code:
Ok so I used the same method for an array of characters but it doesn't work as integers do.Code:char GameBoard[9];
GameBoard[0] = '1';
GameBoard[1] = '2';
(...)
GameBoard[5] = ' ';
(...)
GameBoard[8] = '6';
//Checks for end game conditions
if (Play == GameBoard[8])
{
if (GameBoard[7] == ' ')
{
swap(GameBoard[8], GameBoard[7]);
goto UpdateBoard;
}
if (GameBoard[5] == ' ') //Note that I changed it from 0 to ' ' (Single space).
{
swap(GameBoard[8], GameBoard[5]);
goto UpdateBoard;
}
else {
cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
goto InitialBoard;
}
UpdateBoard:
cout<<"You win".;
If I try to move the "6" piece, I will just get the error message "You can not move that piece. Please, try again!", but if you look closely GameBoard[7] is, in this case, an empty space and it should swap successfully, should it not?
Thanks in advance.