首页 > 解决方案 > Issues with Knights Tour

问题描述

I've made this piece of code to complete the knight's tour, where you have to make it to every square on the chessboard with a knight with no repeats, but I'm having some issues. It only ever prints an empty chessboard with no moves made, and I'm not sure how to fix it.


import javax.lang.model.util.ElementScanner6;
public class KnightsTour
{
    private int[][] ChessBoard = new int[9][9];
    private boolean[] Taken = new boolean[9];
    private int[] Horizontal = {0,1,2,2,1,-1,-2,-2,-1};
    private int[] Vertical = {0,-2,-1,1,2,2,1,-1,-2};
    private int myRow, myCol;
    private int moveCount;

    public KnightsTour()
    {
        doKnightsTour();
    }
    public void doKnightsTour()
    {
        while(moveKnight()==true)
        {

        }
        printChessBoard();
    }

    private boolean moveKnight()
    {
        int randomPos;
        boolean moveMade = false;

        if(moveCount < 64)
        {
            
            if(allTakenNearby() == false)
            {
              
                while(moveMade = false)
                {
                   
                    randomPos = (int)(Math.random()*8) + 1;

                    if(Taken[randomPos] == false)
                    {
                      
                        myRow += Vertical[randomPos];
                        myCol += Horizontal[randomPos];
                        moveCount++;
                        moveMade = true;
                        
                    }
                }
            }

        }
        
        return moveMade;
    }

    private boolean allTakenNearby()
    {
        int tempRow;
        int tempCol;
        boolean allFilled = true;
        boolean notTaken;
        
        for(int i = 1; i<= 8; i++)
        {
            Taken[i] = false;
        }
        for(int i = 1; i <= 8; i++)
        {
            tempRow = myRow + Vertical[i];
            tempCol = myCol + Horizontal[i];

            if(tempRow >= 1 && tempRow <= 8 && tempCol >= 1 && tempCol <=8 )
            {
                if(ChessBoard[tempRow][tempCol] > 0 )
                {
                   Taken[i] = true;
                }
                else
                {
                    allFilled = false;
                }
            }
            else
            {
                Taken[i] = true;
            }
        }
        return allFilled;
    }

    private void printChessBoard()
    {
        int x;
        int y;

        System.out.println();
        System.out.println("ChessBoard is: ");
        System.out.println();

        for(x = 1; x <= 8; x++)
        {
            for(y = 1; y <= 8; y++)
            {
                if(y == 8)
                {
                    if(ChessBoard[x][y] == 0)
                    {
                        System.out.print("xx");
                        System.out.print("  ");
                    }
                    else if(ChessBoard[x][y] > 0 && ChessBoard[x][y] <= 9)
                    {
                        System.out.print("0" + ChessBoard[x][y]);
                        System.out.print("  ");
                    }
                    else
                    {
                        System.out.print(ChessBoard[x][y]);
                        System.out.print("  ");
                    }
                }
                else
                {
                    if(ChessBoard[x][y] == 0)
                    {
                        System.out.print("xx");
                        System.out.print("  ");
                    }
                    else if(ChessBoard[x][y] > 0 && ChessBoard[x][y] <= 9)
                    {
                        System.out.print("0" + ChessBoard[x][y]);
                        System.out.print("  ");
                    }
                    else
                    {
                        System.out.print(ChessBoard[x][y]);
                        System.out.print("  ");
                    }
                }
            }

            System.out.println();

        }
        System.out.println();
        System.out.println("Amount of Moves Made = " + moveCount);
    }
}

Tester:


import javax.lang.model.util.ElementScanner6;
public class KnightsTourTester
{
    public static void main(String args[])
    {
        KnightsTour tour = new KnightsTour();
        
    }
}

Any suggestions would be appreciated, I'm doing this for a class so I need to get it done soon.

标签: javaknights-tour

解决方案


while(moveMade = false) - assignment instead of comparison (and don't use comparisons on booleans, please - while (!moveMade))


推荐阅读