Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:01:59+00:00 2026-06-14T05:01:59+00:00

I have written a program based on trying to solve the Knight’s Tour problem.

  • 0

I have written a program based on trying to solve the Knight’s Tour problem. I believe I have come up with an appropriate solution and everything looks fine.

The small issue I am curious about is with a small section of code which implements the best move based on looking ahead to future possible squares.

If I implement it like this (implementation1)–

if( moveMade )  // if any move is possible
{
    currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    board[ currentRow ][ currentColumn ] = squareCounter;
    squareCounter++;
    moveMade = false;
}
else
moveMade = true;    // if there are no moves possible this is the end

the software will hang – why?

If I make a small innocuous and seemingly trivial change such as this (implementation2) –

int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );

if( moveMade )  // if any move is possible
{
    currentRow += vertical[ temp1 ];
    currentColumn += horizontal[ temp1 ];
    board[ currentRow ][ currentColumn ] = squareCounter;
    squareCounter++;
    moveMade = false;
}
else
    moveMade = true;    // if there are no moves possible this is the end

then everything is fine and the code will reach its conclusion.

I’m using netbeans 7.1 to write my software and thought it must be something to do with the IDE so I tried to compile it using just ‘javac’ and the command line only to find the same results. I don’t understand why I can’t make this method call within the array parameter like this – vertical[ HERE ] or horizontal[ HERE ] and have the result which returns used in the expression. I’ve managed to code like this before without any problem.

Here is the method being called –

public static int betterMove( int moverMatrix[], int theHorizontal[], int theVertical[], int accessBoard[][], int newCurrentRow, int newCurrentColumn )
{
    int[] equalMatrix = new int [ 8 ];  // records the positions which are equal in value with a (1)
    int[] bmValueMatrix = new int[ 8 ]; // holds the numbers taken from accessibility heuristic
    int[] finalTable = new int[ 8 ];    // the best move discovered
    int best = bestMove( moverMatrix ); // the lowest number in the given array
    int startPos = best + 1;
    int moveNumber = 0;
    int originalCurrentRow = newCurrentRow;
    int originalCurrentColumn = newCurrentColumn;

    equalMatrix[ best ] = 1;    // mark the lowest value position with a 1

    initVMatrix( bmValueMatrix );
    initVMatrix( finalTable );

    for( int i = startPos; i < 8; i++ ) // mark the elements of equal value in equalMatrix with (1)
    {
        if( moverMatrix[ best ] == moverMatrix[ i ] )
            equalMatrix[ i ] = 1;
    }

    for( int j = 0; j < 8; j++ )    // go through each element of equalMatrix and look forward
    {                               // for best accessibility heuristic
        newCurrentRow = originalCurrentRow;
        newCurrentColumn = originalCurrentColumn;
        if( equalMatrix[ j ] == 1 )
        {
            newCurrentRow += theVertical[ j ];
            newCurrentColumn += theHorizontal[ j ];
            while( moveNumber < 8 )
            {
                if( newCurrentRow + theVertical[ moveNumber ] >= 0 &&
                        newCurrentRow + theVertical[ moveNumber ] < 8 )
                {
                    if( newCurrentColumn + theHorizontal[ moveNumber ] >= 0 &&
                            newCurrentColumn + theHorizontal[ moveNumber ] < 8 )
                    {
                        bmValueMatrix[ moveNumber ] = accessBoard[ newCurrentRow + theVertical[ moveNumber ] ]
                                                                    [ newCurrentColumn + theHorizontal[ moveNumber ] ]; 
                    }   // end if
                }   // end if
                moveNumber++;
            }   // end while 
            moveNumber = 0;
            finalTable[ j ] = bestMove( bmValueMatrix );
            initVMatrix( bmValueMatrix );
        }   // end if

    }   // end for
    return bestMove( finalTable );   
}

The method bestmove used in the return statement above –

public static int bestMove( int theMoves[] )
{
    int theLowest = 10,
        idealMove = 0;

    for( int i = 0; i < 8; i++ )
    {
        if( theMoves[ i ] < theLowest )
        {
            theLowest = theMoves[i];
            idealMove = i;
        }
    }
    return idealMove;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:02:01+00:00Added an answer on June 14, 2026 at 5:02 am
    currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    

    In the above case, index value for both the arrays are not same.

    The problem is, by the time you invoke the betterMove method the second time, the currentRow value has changed and as you are passing currentRow in your method as one of the paramters, so, the index that is used for horizontal array, is not the same as the one that would be used when you invoke the method outside your if (moveMade).

    Note, the difference is only for the 2nd statement. First one will be same as in both the ways.

    So, that might be a problem.

    So, you need to first store the return value of the execution of that method, and then use that return value as the index of both vertical and horizontal array.

    int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );
    
    if( moveMade )  // if any move is possible
    {
        currentRow += vertical[ temp1 ];
        currentColumn += horizontal[ temp1 ];
        // Rest of the code
    }
    

    Now in this case, both the arrays have same index value – temp1.

    Also, if you are using the return value of a method call more than one time, and that method is modifying any of your passed parameter, you should always invoke the method one time, and store the return value, and use it rather. This will save you from witnessing weird results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small program just like hello the world , everything was
First to tell you my issue: I have written a program that based on
I have written a small GUI based vb.net program that speaks to embedded devices
I have written a python console based program. It takes few arguments (like file
I have written php program and uploaded on server. I want run this program
I have written a program in VB6. When I compile it and send it
I have written a program that uses qhttp to get a webpage. This works
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written my program in python. It is written across seven files and
Visual Studio C++ 2008 / GCC 4.4.2 I have written a program to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.