1

Right now I'm at the point in the knight's tour where I just want the knight to be able to move until he can't move anymore and then just stop. The code works fine for the most part, but once and a while the code will print out two of the same number.

Code from my move function:

    board[0][0] = 1;

    if (row + 1 > -1 && row + 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row + 1][col + 2] == 0) {
        move++;
        row += 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 1 > -1 && row + 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row + 1][col - 2] == 0) {
        move++;
        row += 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row - 1][col + 2] == 0) {
        move++;
        row -= 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row - 1][col - 2] == 0) {
        move++;
        row -= 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row - 2][col + 1] == 0) {
        move++;
        row -= 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row - 2][col - 1] == 0) {
        move++;
        row -= 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row + 2][col + 1] == 0) {
        move++;
        row += 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row + 2][col - 1] == 0) {
        move++;
        row += 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }
}

The Output:

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  0  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  4  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  0  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  7  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  7  4  0  0
  0  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0  0
  0  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0 10
 10  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4  0 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4 13 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1 16  0  0  0
  0  0  2  0 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1 16  0  0  0
  0  0  2 17 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

printBoard function:

void printBoard() {
    cout << endl;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cout << setw(3) << board[i][j];
        }
            cout << endl;
    }

}

The whole program:

// ConsoleApplication15.cpp : Defines the entry point for the console  application.


#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

void makeBoard();
void moveKnight(int row, int col, int move);
void printBoard();

int board[4][4];

int main()
{
    int row = 0;
int col = 0;
int move = 1;

makeBoard();
moveKnight(0, 0, 1);
printBoard();



system("pause");
return 0;
}

void makeBoard() {
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        board[i][j] = 0;

    }
}

}

void printBoard() {
cout << endl;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        cout << setw(3) << board[i][j];
    }
    cout << endl;
}

}

void moveKnight(int row, int col, int move) {

board[0][0] = 1;

    if (row + 1 > -1 && row + 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row + 1][col + 2] == 0) {
        move++;
        row += 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 1 > -1 && row + 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row + 1][col - 2] == 0) {
        move++;
        row += 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row - 1][col + 2] == 0) {
        move++;
        row -= 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row - 1][col - 2] == 0) {
        move++;
        row -= 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row - 2][col + 1] == 0) {
        move++;
        row -= 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row - 2][col - 1] == 0) {
        move++;
        row -= 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row + 2][col + 1] == 0) {
        move++;
        row += 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row + 2][col - 1] == 0) {
        move++;
        row += 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
Nick
  • 23
  • 4
  • Must be in the `ifs` somewhere. Note that the second moves are not always even valid (look at 3s and 10s). I would suggest refactoring your code so its easier to follow whats going on. – code11 Jan 16 '17 at 20:41
  • @code11 how can I make it easier to follow? and yes it is the 3rd move and the 10th move that are the problem. I don't know why it's printing them out twice – Nick Jan 16 '17 at 20:43
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Jan 16 '17 at 20:58
  • @NathanOliver I've tried using the debugger. I still can't find the problem – Nick Jan 16 '17 at 21:00
  • Can you please show us your printBoard()-function? It seems as if duplicate values are printed always when they are written in the last column of a line (then the first column of the next line repeats this value), right? So it's probably just a printing issue... – Stephan Lechner Jan 16 '17 at 21:05
  • @StephanLechner I just added it – Nick Jan 16 '17 at 21:11
  • Are this all the `if`s, or are there some more (not shown)? I would guess that you have somewhere in a condition a typo of the form `&& board[row + 2][col - 1] = xxx` (note the `=` instead of `==`). Anyway, could you post the complete move-function (or probably the complete program)? – Stephan Lechner Jan 16 '17 at 21:20
  • BTW: the print function seems correct to me. – Stephan Lechner Jan 16 '17 at 21:20
  • @StephanLechner ok, I just added in the complete program – Nick Jan 16 '17 at 21:26
  • @StephanLechner oh my god. I can't believe it was that simple. I've wasted hours of my life for nothing :O – Nick Jan 16 '17 at 21:32
  • @StephanLechner Thanks for all the help though :) – Nick Jan 16 '17 at 21:33
  • @StephanLechner why does changing the array size fix the problem though? wasn't I creating a 5 x 5 array with int board[4][4]? – Nick Jan 16 '17 at 21:34
  • `board[4][4]` actually creates a 4x4-array, and the allowed indices range from `0..3` (what actually means 4 values, i.e. 0, 1, 2, 3). – Stephan Lechner Jan 16 '17 at 21:37
  • @StephanLechner ahh yes that makes sense. – Nick Jan 16 '17 at 21:44

1 Answers1

2

Try to replace int board[4][4]; with int board[5][5]; :-)

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58