Thread: Knight's Tour Game Problem!

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Smile Knight's Tour Game Problem!

    Hi all,

    I'm teaching myself C++ from a textbook and I have lost my way with a Knight's Tour program implementation.
    After having chosen the start position subsequent moves don't land where they should or no incremental mark is left on the board.
    Any help from an eagle eyed programmer would be greatly recieved!

    Please see the code below:

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    const int side = 9;
    const int maxMoves = 64;
    const int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
    const int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };
    
    
    
    int moveValidation(); // to ensure that moves are between 0 & 7
    int initialRow(); // to set the starting position ( Row )
    int initialColumn(); // to set the starting position ( Column )
    void initializeBoard( int[][ side ], int ); // to set row & column 0 to increment, everything else to zero!
    void displayBoard( int[][ side ], int ); // to print the board
    
    
    
    int main()
    {
    
        int board[ side ][ side ];
        int currentRow; // initialized to fail validation tests
        int currentColumn; // initialized to fail validation tests
        int move;
        int currentMove = 1;
    
        initializeBoard( board, side );
    
        displayBoard( board, side );
    
        currentRow = initialRow();
    
        currentColumn = initialColumn();
    
        board[ currentRow ][ currentColumn ] = currentMove;
    
        currentMove++;
    
        displayBoard( board, side );
    
        while ( currentMove < maxMoves )
        {
    
            move = moveValidation();
    
            cout << "currentRow += vertical[ move ] = " << ( currentRow += vertical[ move ] ) << endl;
            cout << "currentColumn += horizontal[ move ] = " << ( currentColumn += horizontal[ move ] ) << endl;
            cout << "currentMove = " << currentMove << endl;
    
            if ( ( currentRow += ( vertical[ move ] ) ) < 1 || ( currentRow += ( vertical[ move ] ) ) > 9 )
            {
    
                cout << "That move is forbidden!!!";
    
            }
            else if ( ( currentColumn += ( horizontal[ move ] ) ) < 1 || ( currentColumn += ( horizontal[ move ] ) ) > 9 )
            {
    
                cout << "That move is forbidden!!!";
    
            }
            else if ( board[ ( currentRow += ( vertical[ move ] ) ) ][ ( currentColumn += ( horizontal[ move ] ) ) ]  == 0 )
            {
    
                ( currentColumn += ( horizontal[ move ] ) );
                ( currentRow += ( vertical[ move ] ) );
    
                board[ currentRow ][ currentColumn ] = currentMove;
    
                currentMove++;
    
                displayBoard( board, side );
    
            }
            else
            {
    
                cout << "That move is forbidden!!!";
    
            }
    
        }
    
    } // end main
    
    
    
    // to print the board
    void displayBoard( int board[][ side ], int side )
    {
    
        cout << endl;
    
        for ( int i = 0; i < side; i++ )
        {
    
            for ( int j = 0; j < side; j++ )
            {
    
                if ( board[ i ][ j ] != 0 )
                {
    
                    cout << board[ i ][ j ];
    
                }
                else
                {
    
                    cout << " ";
    
                }
    
            }
    
            cout << endl;
    
        }
    
        cout << endl;
    
    } // end function displayBoard
    
    
    
    // to set row & column 0 to increment, everything else to zero!
    void initializeBoard( int board[][ side ], int side )
    {
    
        for ( int i = 0; i < side; i++ )
        {
    
            board[ 0 ][ i ] = i;
    
        }
    
        for ( int i = 0; i < side; i++ )
        {
    
            board[ i ][ 0 ] = i;
    
        }
    
        for ( int i = 1; i < side; i++ )
        {
    
            for ( int j = 1; j < side; j++ )
            {
    
                board[ i ][ j ] = false;
    
            }
    
        }
    
    } // end function initializeBoard
    
    
    
    // to set the starting position ( Row )
    int initialRow()
    {
    
        int a = 10;
    
        while ( a < 1 || a > 9 )
        {
    
            cout << "Where to begin?\nRow: ";
            cin >> a;
    
        }
    
        return a;
    
    } // end function initialRow
    
    
    
    // to set the starting position ( Column )
    int initialColumn()
    {
    
        int a = 10;
    
        while ( a < 1 || a > 9 )
        {
    
            cout << "Column: ";
            cin >> a;
    
        }
    
        return a;
    
    } // end function initialColumn
    
    
    
    // to ensure that moves are between 0 & 7
    int moveValidation()
    {
    
        int userInput;
    
        cout << "Enter move number ( between 0 & 7 ): ";
        cin >> userInput;
    
        while ( userInput < 0 || userInput > 7 )
        {
    
            cout << "Enter move number ( between 0 & 7 ): ";
            cin >> userInput;
    
        }
    
        return userInput;
    
    } // end function moveValidation

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            if ( ( currentRow += ( vertical[ move ] ) ) < 1 || ( currentRow += ( vertical[ move ] ) ) > 9 )
            else if ( ( currentColumn += ( horizontal[ move ] ) ) < 1 || ( currentColumn += ( horizontal[ move ] ) ) > 9 )
            else if ( board[ ( currentRow += ( vertical[ move ] ) ) ][ ( currentColumn += ( horizontal[ move ] ) ) ]  == 0 )
    How many times are you changing currentRow ?

    Make a single assignment for the next test position, then validate it.
    Trying to compress everything down into a single statement with lots of side effects is not good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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-Help Please!
    By rickster11 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2010, 07:59 PM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Knight's Tour Problem 2
    By Nutshell in forum C Programming
    Replies: 11
    Last Post: 01-09-2002, 09:32 PM
  5. Knight's Tour
    By Nutshell in forum C Programming
    Replies: 31
    Last Post: 01-08-2002, 10:58 PM