java - 为什么我的程序找不到 Peg Solitaire 的解决方案并一直运行?

标签 java

我想编写一个程序来解决 Peg Solitaire 并制作了一个 几乎 可用的程序(“几乎”,因为我已将步骤存储在 3D 中 要打印的数组,但由于我的程序永远不会结束,所以我还没有编写该部分)。我 *已确保递归在 32 次后结束,但仍然继续。 我使用了调试器,但前几次递归的工作似乎很好( 类(class) 检查一切都太多了)。我的程序尝试每种组合 的 移动弹珠来解决游戏。请哪位大佬帮忙找出错误! (不要介意用于调试的代码)

 import java.util.*;

 class Brainvita2

{

 static char a[][][];

 final static int n=7;

 static int max=0;

 static boolean found;

 public static void Do(int c,char temp[][],char b[][][])

 {

if(found) 

 return;

 if(oneMarble(temp))

 {
    a=b;

    found=true;

    System.out.println("Found answer.");

    return;

  }

if(c==33)

return;

    int d[][]=coordinates_Of_Empty(temp);

    for(int i=0 ; i < d.length ; i++)

   {

     if(d[i][0]>1)

   {

   if((temp[(d[i][0])-1][(d[i][1])]=='O')&&(temp[(d[i][0])-2][(d[i][1])]=='O'))

        {

            char temp3[][]=new char[n][n];

            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    temp3[j][k]=temp[j][k];
                }
            }
            temp3[(d[i][0])-2][(d[i][1])]=' ';
            temp3[(d[i][0])-1][(d[i][1])]=' ';
            temp3[(d[i][0])][(d[i][1])]='O';
            char temp2[][][]=new char[b.length+1][n][n];
            for(int j=0;j<b.length;j++)
            {
                for(int k=0;k<n;k++)
            {
                for(int l=0;l<n;l++)
                {
                    temp2[j][k][l]=b[j][k][l];
                }
            }
            }
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
            {
                temp2[b.length][j][k]=temp3[j][k];
            }
            }
            Do(c+1,temp3,temp2);
        }
        }
        if(d[i][0]<n-2)
        {
        if((temp[(d[i][0])+1][(d[i][1])]=='O')&&(temp[(d[i][0])+2][(d[i][1])]=='O'))
        {
            char temp3[][]=new char[n][n];
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    temp3[j][k]=temp[j][k];
                }
            }
            temp3[(d[i][0])+2][(d[i][1])]=' ';
            temp3[(d[i][0])+1][(d[i][1])]=' ';
            temp3[(d[i][0])][(d[i][1])]='O';
            char temp2[][][]=new char[b.length+1][n][n];
            for(int j=0;j<b.length;j++)
            {
                for(int k=0;k<n;k++)
            {
                for(int l=0;l<n;l++)
                {
                    temp2[j][k][l]=b[j][k][l];
                }
            }
            }
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
            {
                temp2[b.length][j][k]=temp3[j][k];
            }
            }
            Do(c+1,temp3,temp2);
        }
        }
        if(d[i][1]>1)
        {
        if((temp[(d[i][0])][(d[i][1])-1]=='O')&&(temp[(d[i][0])][(d[i][1])-2]=='O'))
        {
            char temp3[][]=new char[n][n];
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    temp3[j][k]=temp[j][k];
                }
            }
            temp3[(d[i][0])][(d[i][1])-2]=' ';
            temp3[(d[i][0])][(d[i][1])-1]=' ';
            temp3[(d[i][0])][(d[i][1])]='O';
            char temp2[][][]=new char[b.length+1][n][n];
            for(int j=0;j<b.length;j++)
            {
                for(int k=0;k<n;k++)
            {
                for(int l=0;l<n;l++)
                {
                    temp2[j][k][l]=b[j][k][l];
                }
            }
            }
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
            {
                temp2[b.length][j][k]=temp3[j][k];
            }
            }
            Do(c+1,temp3,temp2);
        }
        }
        if(d[i][1]<n-2)
        {

        if((temp[(d[i][0])][(d[i][1])+2]=='O')&&(temp[(d[i][0])][(d[i][1])+1]=='O'))
        {
            char temp3[][]=new char[n][n];

            for(int j=0;j<n;j++)

            {

                for(int k=0;k<n;k++)

                {

                    temp3[j][k]=temp[j][k];

                }

            }

            temp3[(d[i][0])][(d[i][1])+2]=' ';

            temp3[(d[i][0])][(d[i][1])+1]=' ';

            temp3[(d[i][0])][(d[i][1])]='O';

            char temp2[][][]=new char[b.length+1][n][n];

            for(int j=0;j<b.length;j++)
            {
                for(int k=0;k<n;k++)
            {
                for(int l=0;l<n;l++)
                {
                    temp2[j][k][l]=b[j][k][l];
                }
            }
            }

            for(int j=0;j<n;j++)

            {

                for(int k=0;k<n;k++)

            {

                temp2[b.length][j][k]=temp3[j][k];

            }

            }

            Do(c+1,temp3,temp2);

        }


        }


   //The below few codes are just to find errors(to debug)


        if(max<c)

        {

            for(int j=0;j<n;j++)

            {

                for(int k=0;k<n;k++)

            {

                System.out.print(temp[j][k]+"|");

            }

              System.out.println();

              max=c;

          }

          System.out.println("Highest c value = "+c);

          System.out.println("-----------------------------------");

          System.out.println();

        }

   }

}
public static void main()
{
  char p[][]={{'*','*','O','O','O','*','*'},{'*','*','O','O','O','*','*'},{'O','O','O','O','O','O','O'},{'O','O','O',' ','O','O','O'},{'O','O','O','O','O','O','O'},{'*','*','O','O','O','*','*'},{'*','*','O','O','O','*','*'}};
  char q[][][]={{{'*','*','O','O','O','*','*'},{'*','*','O','O','O','*','*'},{'O','O','O','O','O','O','O'},{'O','O','O',' ','O','O','O'},{'O','O','O','O','O','O','O'},{'*','*','O','O','O','*','*'},{'*','*','O','O','O','*','*'}}};
  found=false;
  Do(1,p,q);
}
  public static int[][] coordinates_Of_Empty(char x[][])
{
    int c=0,c2=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(x[i][j]==' ')
            c++;
        }
    }
    int t[][]=new int[c][2];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(x[i][j]==' ')
            {
                t[c2][0]=i;
                t[c2][1]=j;
                c2++;
            }
        }
    }
    return t;
}
public static boolean oneMarble(char temp[][])
{
    int c=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(temp.length>n)
            c+=0;
            if(temp[i][j]=='O')
            c++;
        }
    }
    if(c==1)
    return true;
    else
    return false;
}

}

     //Expected to print board with one marble(during debug).
     //Instead printed all boards till 3 marbles.

最佳答案

据我所知,您的代码可以正常工作。我猜你最大的问题是时间。你的程序“永远不会结束”,因为“33-Hole Peg Solitaire”有 187,636,299 种不同的游戏模式,你可以遇到 (link)(其中许多不止一次),因此它似乎永远不会结束。

您的:

if(c==33)
    return;

我猜你的意思是当你说:“我*已经确保递归在 32 次后结束,但它仍然继续。”因为“c”永远不会变得那么大,所以什么也不做。据我所知,每次递归达到更深层次时“c”都会递增。当一个棋子被移除时就会这样做,因为“c”从 1 开始,有 32 个棋子,当没有棋子剩下时,它会达到 33。

最大“最大”达到 30,因为由于缺乏可能的移动,它经常会在剩下 3 个棋子时停止。要找到解决方案,只需要大量时间。如果您从一个更简单的模式开始,只需要一个 View 移动即可完成,那么程序会在相当短的时间内完成。

要仅打印解决方案,请尝试打印温度:

if (oneMarble(temp))
{
    a = b;
    found = true;
    System.out.println("Found answer.");
    return;
}

我猜你的意思是“期望用一个弹珠打印一 block 板(在调试期间)。”。

关于java - 为什么我的程序找不到 Peg Solitaire 的解决方案并一直运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57868673/

相关文章:

java - 将 YUV 网络摄像头图像转换为 java 字节数组

java - 将两个泛型类型添加到另一个类型中

java - 在 Spring/Hibernate 中加载社交图 - Hibernate session 持续时间

java - 如何从类名字符串中获取单例实例

java - 我的 JavaFX 项目停止在 EventHandler 上运行代码

java - 如何使用 32 位 Visual Studio 生成器使 CMake 的 FindJava 获得 64 位 Java

java - 集合和数组列表

java - 在网络上共享对象(区 block 链)

java - 为什么不能像函数一样调用构造函数?

java - 遍历 String 数组以用用户的输入填充它