Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A stuck loop

Status
Not open for further replies.

Tchriss99

Programmer
Dec 31, 2002
2
GB
This is the code for the Knights Tour for the Deitel & deitel exercise.

CAN ANYONE HELP ME AND TELL ME WHY IT DOESN'T GO TO NEXT VALUES?


#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>

int main()
{
// Initialise the board array
int board[8][8]={0};
int accessibility[8][8]={
{2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2}
};
int access[8]={0};
int access_array[1][3]={0};
int horizontal[8]={2,1,-1,-2,-2,-1,1,2};
int vertical[8]={-1,-2,-2,-1,1,2,2,1};

// Integer that holds the number of moves
int moveNumber = 0;

int move = 1;
int fail=1;
int currentRow = 0, currentColumn = 0;
int Column_Temp= 0, Row_Temp = 0;
int x=0;


// User input : Enter the values for the starting coordinates

do
{
cout << &quot;\n Enter the starting position coordinates: &quot;;
cin >> currentColumn >> currentRow;
board[currentRow][currentColumn] = move;
cout << &quot;First move:&quot; << move << endl;
cout << &quot;Starting coordinates: &quot; << &quot;x=&quot; << currentRow << &quot; y=&quot; << currentColumn << endl;
}
while ((currentColumn < 0) || (currentRow < 0) || (currentColumn >= 8)
|| (currentRow >= 8));

if ((currentColumn > 8 || currentRow > 8) ||
(currentColumn < 0 || currentRow < 0))
{
cout << &quot;End of journey. Numbers invalid. Number of moves:&quot; << move << endl;

exit(0);
}



// Move until we have 64 valid moves
while (move<=64 ) {
cout << &quot;Entering loop1 (while<65)&quot; << endl;
//If this is the last generated move then make move.
if (moveNumber==8)
{
fail=0;
moveNumber=0;
cout << &quot;New move No: &quot; << move << endl;
move++;
board[access_array[0][0]][access_array[0][1]]= move;
cout << &quot; MOVE MADE: &quot; << &quot;x= &quot; << currentRow << &quot; y= &quot; << currentColumn << endl;
cout << endl;
access_array[0][2]=0;
} // end if

// The move is not the last of the 8 combinations
else
{

// Calculate moves until all 8 moves are added to current coordinates
cout << &quot;moveNumber :&quot; << moveNumber << endl;
currentRow += vertical[moveNumber];
currentColumn += horizontal[moveNumber];
cout << &quot;New coordinates: &quot; << &quot;x=&quot; << currentRow << &quot; y=&quot; << currentColumn << endl;

// Test to verify that the calculated move is valid
if (currentRow<8 && currentRow>-1 && currentColumn<8 && currentColumn>-1
&& board[currentRow][currentColumn] ==0)
{

//If the access_array is empty enter the new values.
if (access_array[0][2]==0)
{
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << &quot;access_array[0][2]= &quot; << access_array[0][2] << endl;
moveNumber++;
cout << &quot;moveNumber: &quot; << moveNumber << endl;
} //end if

//Test if access_array has a value
else if (access_array[0][2]!=0)
{
//Test to see to the current accessibility value is smaller
//than the one just calculated. If it is replace it.
if(accessibility[currentRow][currentColumn]<access_array[0][2])
{
--accessibility[access_array[0][0]][access_array[0][1]];
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << &quot;access_array[0][2]= &quot; << access_array[0][2] << endl;
moveNumber++;
cout << &quot;moveNumber: &quot; << moveNumber << endl;
} //end if

//If the new availability value is larger than the existing one
//then decrement the new availability[x][y] by one and
//make new move
else if (accessibility[currentRow][currentColumn]>access_array[0][2])
{
--accessibility[currentRow][currentColumn];
moveNumber++;
} //end else
} // end else


//If the current board square is unavailable increment
//the fail move variable
else if (board[currentRow][currentColumn]!=0)
{
cout << &quot;SQUARE IS NOT AVAILABLE&quot; << endl;
cout << endl;
fail++;
moveNumber++;
} // end else
} // end if

//If the move is invalid proceed to generate next move
else
{
cout << &quot;INVALID MOVE: &quot; << currentRow << &quot;, &quot; << currentColumn << endl;;
cout << endl;
moveNumber++;
}//end else
} // end else
} // end while

// Draw the board
cout << &quot;\n&quot;;
cout << &quot;A possible solution for the problem is: &quot; << endl;
cout << &quot;\n&quot;;
for (int i=0; i!= 8; i++)
{
for (int j=0; j!=8; j++)
{

cout << setw(3) << board[j];
}
cout << &quot;\n&quot;;

}
cout << &quot;Number of moves: &quot; << move;
return 0;
}
 
You may want go through the code step by step with the debugging tools to see where the loop termination variable is not getting changed when it should.

The other advice I can offer is that it would help with clarity and debugging if you neatly functionalized the code. I did the same project out of the same book and functionalizing helped me out for sure.

I had an isPossible function to see if a given move was legal, a jump function that moved the knight, a decrement array function to decrement the accessibility matrix after a jump, others to reset all of the variables I used for another pass, and I forget what else.

If you think it would help, I can post my source code.

Let me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top