Thread: Knight's Tour

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Knight's Tour - need help

    Hi,

    My program's suppose to move the knight around the chess board touching the each square only once. It's starting off at (0, 0) and there are eight possible moves for the knight depending on its position. The program tests each potential move and if that move is not off the board or it has been visited yet then that move is made. However my program's output only say that the knight make 8 moves. i tried out by hand and it's sure more than that. Pls help , what's wrong ?

    thnx


    Code:
    /* Knight's Tour */
    
    #include <stdio.h>
    
    int main()
    {
       int board[ 8 ][ 8 ] = { 0 };                 /* Chess board */
       int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
       int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };
       int totalMoves = 0;                      /* Total of all moves made */
       int currentRow = 0, currentColumn = 0;   /* Current position of knight */
       int moveNumber;                          /* Move type */
       int i, j;                                /* Loop counters */
       int newPos;                              /* Test next possible move for validity */
       int tryNextMoveType = 0;                        /* Flag to see if try next move number */
       int end = 0;
    
       while ( end != 1 ) {
    
          moveNumber = 0;
    
          for ( i = 0; i <= 7; i++ ) {
    
             tryNextMoveType = 0;
    
             newPos = currentRow + vertical[ moveNumber ];
             if ( newPos < 0 || newPos > 7 )
                tryNextMoveType = 1;
    
             newPos = currentColumn + horizontal[ moveNumber ];
             if ( newPos < 0 || newPos > 7 )
                tryNextMoveType = 1;
    
             if ( board[ currentRow + vertical[ moveNumber ] ][ currentColumn + horizontal[ moveNumber ] ] == 1 )
                tryNextMoveType = 1;
    
             if ( tryNextMoveType == 0 ) {
                currentRow += vertical[ moveNumber ];
                currentColumn += horizontal[ moveNumber ];
                board[ currentRow ][ currentColumn ] = 1;
                ++totalMoves;
                break;
             }
             else
                moveNumber++;
    
             if ( i == 7 && tryNextMoveType == 1 ) /* No more possible moves */
                end = 1;
          }
       }
    
       printf( "Total moves made by the knight: %d", totalMoves );
    
       return 0;
    }
    Last edited by Nutshell; 01-08-2002 at 12:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. knight's tour!
    By Jasin14 in forum C Programming
    Replies: 13
    Last Post: 12-22-2010, 04:30 PM
  2. Knight's Tour: couting problems
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2004, 12:46 PM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Knights Tour Trivia
    By shrestha in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2003, 08:32 AM
  5. Knight's Tour Problem 2
    By Nutshell in forum C Programming
    Replies: 11
    Last Post: 01-09-2002, 09:32 PM