Java:如何在棋盘上移动骑士直到没有可能的移动

时间:2017-01-02 20:25:52

标签: java

我有一个任务让骑士在棋盘周围移动,直到它完成一次完整的巡回赛或者没有其他地方去。

我有一个问题,在没有更多动作之后弄清楚如何让它实际停止。我有下移动的算法,以及棋盘上的边界检查。

将循环次数设置为64次,因为如果没有创建完美游览,程序将尝试不断找到不存在的地点。

我必须解决的两个想法是,每次在特定位置周围检查某个位置时递增变量,如果前一次移动都采取了所有可能的移动,则终止程序。问题是我不知道如何实际做到这一点。

我的第二个想法是让程序在2秒后退出for循环(在此期间for循环将多次检查每个位置)但我觉得如果我这样做,我的教授会把我钉在十字架上

这是我的代码:

import apcslib.*; //this is only for Format()
public class ktour
{
  int[][] kboard = new int[9][9];
  int[] vert = new int[9];
  int[] horiz = new int[9];
  ktour()
  {
        vert[1] = -2;vert[2] = -1;vert[3] = 1;vert[4] = 2;vert[5] = 2;vert[6] = 1;vert[7] = -1;vert[8] = -2;
        horiz[1] = 1;horiz[2] = 2;horiz[3] = 2;horiz[4] = 1;horiz[5] = -1;horiz[6] = -2;horiz[7] = -2;horiz[8] = -1;
        path();
  }
  public void path()
  {
        int row = 1;
        int col = 1;
        int loops = 10; //i have this set to 10 for now
        int col2 = 1;
        int row2 = 1;
        int r = (int)(Math.random() * (8) +1); //returns a random from 1 to 9
        //System.out.println(r);
        kboard[col][row] = 1;
        for(int x = 2; x < loops; x++) //this runs the bounds check and places each number for the amount that loops is
        {
        r = (int)(Math.random() * (8) +1);
        col = col2;
        row = row2;
        col = col + vert[r];
        row = row + horiz[r];
        while(col <= 0 || col > 8 || row <= 0 || row > 8) //bounds check, will keep running until both row and columb is in the board
        {
          r = (int)(Math.random() * (8) + 1);
          col = col2;
          row = row2;
          col = col + vert[r];
          row = row + horiz[r]; 
        }
            if(kboard[col][row] == 0)
            {
                kboard[col][row] = x;
                row2 = row; 
                col2 = col;

            }
            else
            {
                x--; //if the above if is false and a number already occupies the generated spot, x is decremented and the program tries again


            }
        }
        printboard();
  }
  public void printboard()
  {
      for(int y = 1; y < 9; y++)
      {
       System.out.println();
        for(int x = 1; x < 9; x++)
        {
            System.out.print(Format.right(kboard[y][x],3));
        }
      }
  }
}

1 个答案:

答案 0 :(得分:2)

我能够使用以下代码修复我的实验室。我创建了一个名为count的变量,我用它来检查是否在任何移动时都没有剩下的移动。由于只有8个移动,当变量达到9时,代码终止,并打印到它所到达的点。

如果count不为0,我必须放置多个if语句,不包括r = math.random,这意味着我正在检查r 1-9,即每个可能的移动。因此,我无法使用随机数,我不得不遍历所有8种可能的动作。

当我到达检查kboard [col] [row] == 0的行时,我也遇到了问题。如果你在计数大于1的循环中运行,那么col或row可能是由于边界检查器中缺少随机化器而超出范围。如果没有中断,边界检查器将永远运行,而不会每次生成随机数。我通过添加一个if语句修复了这个问题,如果col和row在board中,那么该语句允许程序继续。如果不是,则x递减,计数再次增加,表示尝试失败。

通过这种方式,我可以检查所有可能的动作,无论他们是否在董事会内部。

public void path()
  {
        int row = 1;
        int col = 1;
        int loops = 64; //i have this set to 10 for now
        int col2 = 1;
        int row2 = 1;
        int count = 0;
        boolean end = false;
        int r = (int)(Math.random() * (8) +1); //returns a random from 1 to 9
        //System.out.println(r);
        kboard[col][row] = 1;
        for(int x = 2; x < loops; x++) //this runs the bounds check and places each number for the amount that loops is
        {
        if(count == 0)
            r = (int)(Math.random() * (8) +1);
        if(count >= 1 && r != 8)
            r++;
        col = col2;
        row = row2;
        col = col + vert[r];
        row = row + horiz[r];
        while(col <= 0 || col > 8 || row <= 0 || row > 8) //bounds check, will keep running until both row and columb is in the board
        {
          if(count == 0)
            r = (int)(Math.random() * (8) + 1);
          col = col2;
          row = row2;
          col = col + vert[r];
          row = row + horiz[r];
          if(count >= 1)
                break;
        }
        end = false;
        if(r == 8 || r == 9)
            r = 1;
        if(count >= 9)
        {
            System.out.println("Halting... no where else to go");
            loops = 0;
        }
        if(!(col <= 0 || row <= 0 || row > 8 || col > 8))
        {
            if(kboard[col][row] == 0)
            {
                kboard[col][row] = x;
                row2 = row; 
                col2 = col;
                count = 0;
            }
            else
            {
                count++;
                x--; //if the above if is false and a number already occupies the generated spot, x is decremented and the program tries again
            }


        }
        else
        {
            count++;
                x--;
        }

        }
        printboard();
  }