在 Java 中生成数独

Generating Sudoku in Java

我在 java 编程方面遇到了一些问题。这是我第一次遇到 java 所以请耐心等待,因为我可能会错过绝对的基础知识。不管怎样,长话短说,我有数独要做,我遇到了 problems.I 需要随机数来制作数独板或者它的值。

如果您不知道什么是数独,则必须只有 1-9 的数字,并且它们不能在行列和 3x3 正方形中重复。棋盘本身是 9x9,所以它可以分成 9 个 3x3 的正方形。

主要问题是随机生成的值有时会使模式无法解决。因此,我试图重复随机抽取那些无法解决的问题,以避免出现这些情况,但由于我的经验,我无法做到这一点。这是代码,感谢您的帮助;)

import java.util.Random;

public class tabela {
  public static void main(String[] args) {
      boolean bylo[] = new boolean[10];//this array tells me if the number is avalible to pick

      boolean wysw[][] = new boolean[9][9];//not used yet it is ment to be used while displaying array in GUI
      int tabela[][] = new int[9][9];//here will be generated values of sudoku

      for(int i=0; i<9;i++)
          for(int j=0;j<9;j++)
               tabela[i][j]=0; //filling array with 0s

      for (int i = 0; i < 9; i++) {


             // System.out.print("a"); <- debugging tools
          out:for (int j = 0; j < 9; j++) {
                  //System.out.print("b");
                  for (int h = 0; h < 10; h++)
                      bylo[h] = false;
                  //System.out.print("c");
                  {
                      int zaokr1 = i + 1, zaokr2 = j + 1; //setting values other than j and i +1 
                      int resz;                           //bcouse i want to set values divided by 3 
                      if (zaokr1 % 3 != 0) { //
                          resz = i % 3;
                          zaokr1 = zaokr1 + (3 - resz);//rounding up to 3 to determine in which 3x3 square we are
                      }
                      if (zaokr2 % 3 != 0) {            // 1 2 3
                          resz = j % 3;                 // 4 5 6
                          zaokr2 = zaokr2 + (3 - resz); // 7 8 9
                      }

                      int c = i, d = j;               //Here i take agan values from i and j 
                      while (c > 0) {                 //
                          c--;                        //and i set values of numbers in column true to 
                          bylo[tabela[c][d]] = true;  //reroll them later
                      }
                      c = i;d = j;                    //same here
                      while (d > 0) {                 //
                          d--;                        //this time for rows
                          bylo[tabela[c][d]] = true;  //
                      }

                      if (zaokr1 / 3 == 1 && zaokr2 / 3 == 1) //those are 3x3 squares from 1 - 9(
                                                              //this is first one
                      {

                          bylo[tabela[0][0]] = true;
                          bylo[tabela[0][1]] = true;
                          bylo[tabela[0][2]] = true;
                          bylo[tabela[1][0]] = true;
                          bylo[tabela[1][1]] = true;
                          bylo[tabela[1][2]] = true;
                          bylo[tabela[2][0]] = true;
                          bylo[tabela[2][1]] = true;
                          bylo[tabela[2][2]] = true;


                      }
                      if (zaokr1 / 3 == 1 && zaokr2 / 3 == 2)//second
                      {
                          bylo[tabela[0][3]] = true;
                          bylo[tabela[0][4]] = true;
                          bylo[tabela[0][5]] = true;
                          bylo[tabela[1][3]] = true;
                          bylo[tabela[1][4]] = true;
                          bylo[tabela[1][5]] = true;
                          bylo[tabela[2][3]] = true;
                          bylo[tabela[2][4]] = true;
                          bylo[tabela[2][5]] = true;

                      }
                      if (zaokr1 / 3 == 1 && zaokr2 / 3 == 3)//third
                      {
                          bylo[tabela[0][6]] = true;
                          bylo[tabela[0][7]] = true;
                          bylo[tabela[0][8]] = true;
                          bylo[tabela[1][6]] = true;
                          bylo[tabela[1][7]] = true;
                          bylo[tabela[1][8]] = true;
                          bylo[tabela[2][6]] = true;
                          bylo[tabela[2][7]] = true;
                          bylo[tabela[2][8]] = true;

                      }
                      if (zaokr1 / 3 == 2 && zaokr2 / 3 == 1)//fourth
                      {
                          bylo[tabela[3][0]] = true;
                          bylo[tabela[3][1]] = true;
                          bylo[tabela[3][2]] = true;
                          bylo[tabela[4][0]] = true;
                          bylo[tabela[4][1]] = true;
                          bylo[tabela[4][2]] = true;
                          bylo[tabela[5][0]] = true;
                          bylo[tabela[5][1]] = true;
                          bylo[tabela[5][2]] = true;

                      }
                      if (zaokr1 / 3 == 2 && zaokr2 / 3 == 2)//fifth
                      {
                          bylo[tabela[3][3]] = true;
                          bylo[tabela[3][4]] = true;
                          bylo[tabela[3][5]] = true;
                          bylo[tabela[4][3]] = true;
                          bylo[tabela[4][4]] = true;
                          bylo[tabela[4][5]] = true;
                          bylo[tabela[5][3]] = true;
                          bylo[tabela[5][4]] = true;
                          bylo[tabela[5][5]] = true;
                      }
                      if (zaokr1 / 3 == 2 && zaokr2 / 3 == 3)//sixth
                      {
                          bylo[tabela[3][6]] = true;
                          bylo[tabela[3][7]] = true;
                          bylo[tabela[3][8]] = true;
                          bylo[tabela[4][6]] = true;
                          bylo[tabela[4][7]] = true;
                          bylo[tabela[4][8]] = true;
                          bylo[tabela[5][6]] = true;
                          bylo[tabela[5][7]] = true;
                          bylo[tabela[5][8]] = true;
                      }
                      if (zaokr1 / 3 == 3 && zaokr2 / 3 == 1)//seventh
                      {
                          bylo[tabela[6][0]] = true;
                          bylo[tabela[6][1]] = true;
                          bylo[tabela[6][2]] = true;
                          bylo[tabela[7][0]] = true;
                          bylo[tabela[7][1]] = true;
                          bylo[tabela[7][2]] = true;
                          bylo[tabela[8][0]] = true;
                          bylo[tabela[8][1]] = true;
                          bylo[tabela[8][2]] = true;
                      }
                      if (zaokr1 / 3 == 3 && zaokr2 / 3 == 2)//eighth
                      {
                          bylo[tabela[6][3]] = true;
                          bylo[tabela[6][4]] = true;
                          bylo[tabela[6][5]] = true;
                          bylo[tabela[7][3]] = true;
                          bylo[tabela[7][4]] = true;
                          bylo[tabela[7][5]] = true;
                          bylo[tabela[8][3]] = true;
                          bylo[tabela[8][4]] = true;
                          bylo[tabela[8][5]] = true;
                      }
                      if (zaokr1 / 3 == 3 && zaokr2 / 3 == 3)//ninth
                      {
                          bylo[tabela[6][6]] = true;
                          bylo[tabela[6][7]] = true;
                          bylo[tabela[6][8]] = true;
                          bylo[tabela[7][6]] = true;
                          bylo[tabela[7][7]] = true;
                          bylo[tabela[7][8]] = true;
                          bylo[tabela[8][6]] = true;
                          bylo[tabela[8][7]] = true;
                          bylo[tabela[8][8]] = true;
                      }
                  }
                  //System.out.print("d");
                  int licznik=0;
                  for (int x = 0; x < 10; x++) {
                       if(bylo[x]==true){licznik++;} //here i count if all values are not blocked already
                  }
                  if(licznik==10)
                  {
                      if(i<=3){i=0;j=0;}
                      if(i>3&&i<=6) {i=3;j=0;}
                      if(i>6) {i=6;j=0;}
                      break out;//i read somwhere that this goes back to the certain point ;)
                  }

                  tabela[i][j] = RandomBeetween(1, 10);
                  if (bylo[tabela[i][j]] == true) {
                      do {

                          tabela[i][j] = RandomBeetween(1, 10);//randomizing numbers

                      } while (bylo[tabela[i][j]] == true);
                      bylo[tabela[i][j]] = true;//and setting their value in bool array to true
                      System.out.print(tabela[i][j]);
                  } else {
                      if (bylo[tabela[i][j]] == false) {
                          bylo[tabela[i][j]] = true;
                          System.out.print(tabela[i][j]);
                      }
                      //for(int x=0;x<10;x++)
                      //  System.out.print(bylo[x]+"   ");
                  }



                  //System.out.println("e  ");
              }
              for(int a=0;a<9;a++)
                  System.out.print(tabela[i][a]+"  ");System.out.print("f");


          }



      }
      static int RandomBeetween ( int min, int max)//just random  in custom range
      {
          Random random = new Random();
          int a1 = random.nextInt(max - min);
          int a2 = a1 + min;
          return a2;

      }


}

我不会试图找出你所有的代码。我将建议解决此问题的基本流程。

首先,您正在使用面向对象的编程语言。我将从制作 class 数独谜题开始。我会给它这些方法(开始):

class SudokuPuzzle {
    // A constructor that initializes to all zeros.
    public SudokuPuzzle() { ... }

    // Create a legal finished board
    public randomizeBoard() { ... }

    // Take the legal board and hide some squares to make it a puzzle
    public turnIntoAPuzzle() { ... }

    // Output
    public display() { ... }
}

这样做会给你一个漂亮、干净的对象来处理,它会把你的问题分解成更容易管理的部分。我认为您甚至会发现 randomizeBoard() 很困难。毕竟,考虑数独的规则:

  • 每行必须恰好包含每个数字一次
  • 每列必须恰好包含每个数字一次
  • 9 个小方块中的每个方块必须恰好包含每个数字一次

所以 randomizeBoard() 不是一项简单的任务。我从事计算机编程已有 45 年了,但我并不清楚我将如何编写它。

但是您可以进入 turnIntoAPuzzle()。这实际上可能是一个更容易的问题。您可以随机选择一个单元格,并根据您编写的规则决定是否可以安全地隐藏该单元格。例如,在简单模式下,如果隐藏此单元格:

  • 是否可以根据这个9格的所有其他单元格来确定?
  • 是否可以根据这一行来判断?
  • 是否可以根据这一栏来判断?

如果是这样,则单元格可以安全隐藏。您可以使用随机函数查找尚未隐藏的单元格并执行您的规则。

对于中等硬度,添加如下规则:

  • 能否根据这个9的方格、这一行、这一列的组合来判断这个单元格?

如果在某个时候,你的所有测试 return "no, I can't",那么你将从未来的隐藏考虑中删除该单元格,并从剩余的尚未隐藏的单元格中随机选择。

循环直到 "might be able to hide" 个单元格的列表为零。

--

此 post 提供了如何处理此类问题的模式。打破它。弄清楚在没有计算机的情况下如何手动完成,然后将其转化为计算机的说明。

我认为随机化棋盘实际上比隐藏一些单元格更难,因为这是一个非常简单的算法。我不确定随机化董事会是否简单,但也许它实际上还不错。但我认为有可能走上错误的道路。