Knight's Tour - exit condition

Hi all,

I'm touring a knight on a chessboard in L-shaped moves. The knight needs to avoid already taken squares and not to go off the board.
The knight at some point reaches the final square from which no further moves are possible (other squares either occupied or it would lead off the board).
I did all that (the knight reaches the final square) but have trouble writing up a terminating condition. The knight is stuck on the last square without terminating.

Any tips? (don't say too much :)

Cheers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream> 
using namespace std;

int main()
{
   const int SIZE = 8;
   int board[SIZE][SIZE]={0};

   int currentRow = 3;
   int currentColumn = 4;

   int vertical[SIZE]={-1, -2, -2, -1, 1, 2, 2, 1};
   int horizontal[SIZE]={2, 1, -1, -2, -2, -1, 1, 2};
   
   int moveNumber=-2; // any number outside of SIZE range. Don't leave it uninitialized.
   int counter=1;

   while(counter < 64 && moveNumber != -1)
   {
      cout << "Enter move: ";
      cin >> moveNumber;
      
      if(moveNumber >= 0 && moveNumber < SIZE) // entered numbers 0-7 only
      {
         currentRow += vertical[moveNumber];
         currentColumn += horizontal[moveNumber];
         
         // current square
         if(currentRow >= 0 && currentColumn >= 0 && currentRow < SIZE && currentColumn < SIZE)
         {
            if(board[ currentRow ][ currentColumn ] == 0)
            {
               board[ currentRow ][ currentColumn ] = 1; // assign 1 to the square the knight moves on
               cout << "Counter: " << counter++ << endl;
               cout << "Current row: " << currentRow << endl;  // track current row and column
               cout << "Current column: " << currentColumn << endl;
            }
            else
            {
               currentRow -= vertical[moveNumber];  // retain current position if the knight wants to move on an occupied square
               currentColumn -= horizontal[moveNumber]; // retain current position if the knight wants to move on an occupied square
               cout << "******** Square taken. Try again. ********\n";
            }
         }
         else
         {
            currentRow -= vertical[moveNumber];  // retain current position if off the board
            currentColumn -= horizontal[moveNumber]; // retain current position if off the board
            cout << "******** Off the board. Try again. ********\n";
         }
      }
      else
         if(moveNumber != -1)
            cout << "******** Out of range. Enter within 0-7. ********";
      cout << endl;
   }
     // display two-dimensional array board
     for(int i=0; i<SIZE; i++) 
     {
        for(int j=0; j<SIZE; j++) 
           cout << board[i][j] << ' ';
        cout << endl;
     }
     cout << "The knight made " << counter-1 << " moves." << endl;
return 0;
}
In order to know if the user is "stuck", you'll need to check all possible moves and see if there is at least 1 legal move.

If there's no legal moves, then you know you can terminate.
Topic archived. No new replies allowed.