bool knightsTour::checkPosition (int r, int c, int size) //validates position { boardSize = size; if((r < 0 || c < 0) || (r >= boardSize || c >= boardSize)) //within bounds return false; else if((board[r][c]) == 0) return true; //not occupied //JEY FIXED THIS LINE else return false;//occupied //JEY FIXED THIS LINE } void knightsTour::movePosition(int r, int c, int size, int count) { board[r][c] = count; int moveRow[8] = {2, 2, -2, -2, 1, 1, -1, -1}; int moveCol[8] = {1, -1, 1, -1, 2, -2, 2, -2}; // JEY ADDED THIS CODE if (count == size * size) { print(size); return; } for(int x = 0; x < 8; x++) { int row = (r + moveRow[x]); int col = (c + moveCol[x]); if(checkPosition(r + row, c + col, size)) { count++; movePosition(r + row, c + col, size, count); if(count < (size * size)) { board[row][col] = 0; count--; } } if(count < (size * size)) { board[r][c] = 0; count--; } } }//end movePosition()
knights tour array in loopproblem convertying if statements into a for loop
Page 1 of 1
2 Replies - 3448 Views - Last Post: 22 October 2009 - 10:16 AM
#1
knights tour array in loop
Posted 22 October 2009 - 07:39 AM
Hello, I've written a knight's tour program and handed in a working version of it already using a bunch of if statements for each possible move. I really wanted to use a couple of arrays for the moves and consolidate my code more, but I ran out of time. I do really want to get it working using the arrays for the moves however. I really thought that I had them set up correctly in the movePosition(), but I'm missing something (probably has to do with my for loop), but I can't seem to get my head around the missing/wrong logic I used with it. Any suggestions or what I should look at? I can post my working program (if statements) if needed, but this is what I've gotten so far with the arrays. I'm coming up with a stack error in the checkPosition(), which works fine with my if statements version.
Replies To: knights tour array in loop
#2
Re: knights tour array in loop
Posted 22 October 2009 - 09:02 AM
else if((board[r][c]) == 0)
i'm not sure if that's an error or not, but try making that else if()
if you have a good compiler, set some breakpoints and check to see exactly what's happening! It's called debugging.
This post has been edited by taylorc8: 22 October 2009 - 09:06 AM
#3
Re: knights tour array in loop
Posted 22 October 2009 - 10:16 AM
taylorc8, on 22 Oct, 2009 - 08:02 AM, said:
else if((board[r][c]) == 0)
i'm not sure if that's an error or not, but try making that else if()
if you have a good compiler, set some breakpoints and check to see exactly what's happening! It's called debugging.
I already did that,
it's throwing a stack overflow error in checkPosition()
Page 1 of 1