【问题标题】:Issues with Knights Tour骑士之旅的问题
【发布时间】:2021-02-22 00:45:13
【问题描述】:

我编写了这段代码来完成骑士之旅,你必须让骑士在棋盘上的每个方格中都没有重复,但我遇到了一些问题。它只会打印一个没有移动的空棋盘,我不知道如何解决它。


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);
    }
}

测试人员:


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

任何建议都将不胜感激,我正在为一堂课这样​​做,所以我需要尽快完成。

【问题讨论】:

  • 我正在为一个班级做这个,所以我需要尽快完成它。 - 与问题无关。时间管理取决于您,而不是我们。

标签: java knights-tour


【解决方案1】:

while(moveMade = false) - 赋值而不是比较(请不要在布尔值上使用比较 - while (!moveMade))

【讨论】:

    【解决方案2】:

    在我看来,好像您的 moveKnight() 方法移动了马但从不更新 ChessBoard。所以在 64 步之后,它似乎仍然没有受到影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多