So the user is supposed to be able to input a number between 1-15 to move a tile on a 2d game board. If the user inputs any integer that isn't in the range, the program will output a prompt to enter something else. However, if they enter a character, for some reason the program just loops infinitely instead of relaying the same prompt.
Here is the code in main()
while(!found) //if the input is not found on the board, or the user selected an illegal tile, this retry prompt will appear until a legal tile is selected
{
cout << "This tile is not on the board or is not adjacent to the blank tile." << '\n' << "Please enter another numerical value between 1 and 16: ";
cin >> movet;
cout << endl;
found = moveTile(board, movet, blanki, blankj);
}
And here is the function that returns the value for found:
bool moveTile(int gameBoard[][SIZE], int nextMove, int &blanki, int &blankj)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(gameBoard[i][j] == nextMove)
{
while(i == blanki - 1 || i == blanki + 1 || j == blankj + 1 || j == blankj - 1)//ensures the selected tile is within the surrounding 8 tiles
{
if( (i == blanki + 1 && j == blankj + 1)||(i == blanki - 1 && j == blankj - 1) || (i == blanki -1 && j == blankj + 1) || ( i == blanki + 1 && j == blankj - 1) )//removes corner tiles from possible selection to prevent illegal movement of game piece
{
return false; //if the selected value is a corner piece, the program will prompt the user to select something else
}
int temp = gameBoard[i][j];//saves original position into temp
gameBoard[i][j] = gameBoard[blanki][blankj];//stores moved tile into blank tile position
gameBoard[blanki][blankj] = temp;//stores the blank tile into the moved tile's position
blanki = i;//keeps track of the blank tile's position
blankj = j;
return true;
}
}
}
}
return false;
}