-2

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;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
Jason
  • 25
  • 1
  • 8
  • That is because you assign a character to an integer inside a loop. – Raindrop7 Feb 05 '17 at 22:10
  • sorry I didn't word it correctly. It should loop, but it's only looping the first line of the prompt infinitely instead of letting the user enter something else like it would with an integer out of range. – Jason Feb 05 '17 at 22:16

2 Answers2

0

You aren't handling or even checking for read failures. The code

cin >> movet;

tries to read an integer. If something other than an integer is entered, the read fails and the fail state is set on cin. Once cin's failbit has been set, all subsequent read operations will fail. You can test cin to see if the read was successful, and you can use cin.clear() to reset the stream state. For example:

while (!found) {
    std::cout << "This tile is not on the board or is not adjacent to "
              << "the blank tile.\nPlease enter another numerical value "
              << "between 1 and 16: ";
    if (std::cin >> movet)
        found = moveTile(board, movet, blanki, blankj);
    else { //invalid input, reset stream and try again
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        found = false;
    }
}

Also you should probably handle end-of-file as a separate case. Just in case your program is called with input redirected from a file.

Greg Kikola
  • 573
  • 3
  • 6
0

It seems that there is a mistake in your code. For example this statement:

while(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1)

bahves like this:

if(i==blanki-1||i==blanki+1||j==blankj+1||j==blankj-1)

Becuase you return in first iteration. So, first of all take a look on your algorithm or explain that in your question. When moveTile returns False, you do not change any of int gameBoard,blanki or blankj. Therefore, when this function returns False, it will return false is you call it again and you will be in an infinite loop. So, again I think there is a problem in your algorithm.

Sam Mokari
  • 461
  • 3
  • 11