用Java生成数独
我在java编程方面有一些问题。这是我第一次遇到java,所以请耐心等待,因为我可能会错过一些基本的东西。总之,长话短说,我有数独游戏要做,我遇到了五个问题。我需要随机数来制作数独板,或者更确切地说是它的值 如果你不知道数独是关于什么的,那么只能有1-9的数字,而且它们不能在行、列和3x3正方形中重复。电路板本身是9x9,因此可以分成9个3x3的正方形 主要问题是,随机生成的值有时会使模式无法解决。因此,我试图重复随机抽取那些不可能解决的线,以避免这些情况,但正如我所经历的,我不能这样做。这是代码,谢谢你的帮助用Java生成数独,java,sudoku,Java,Sudoku,我在java编程方面有一些问题。这是我第一次遇到java,所以请耐心等待,因为我可能会错过一些基本的东西。总之,长话短说,我有数独游戏要做,我遇到了五个问题。我需要随机数来制作数独板,或者更确切地说是它的值 如果你不知道数独是关于什么的,那么只能有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 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() { ... }
}
这样做将为您提供一个好的、干净的对象来处理,并将您的问题分解为更容易管理的部分。我想你会发现即使是随机板也很难。毕竟,考虑数独的规则:
每行必须精确地包含每个数字一次
每列必须精确地包含每个数字一次
9个小正方形中的每个正方形必须包含每个数字一次
所以随机化卡片不是一件小事。我已经为计算机编程45年了,但我不清楚该如何编写它
但你可以进入turnIntoAPuzzle。这实际上可能是一个更容易的问题。您可以随机选择一个单元格,并根据编程规则决定是否可以安全地隐藏该单元格。例如,在简易模式下,如果此单元格被隐藏:
是否可以根据9个单元格中的所有其他单元格来确定?
可以根据这一行来确定吗?
是否可以根据此列确定?
如果是这样,则可以安全地隐藏该单元。您可以使用随机函数查找尚未隐藏的单元格,并查看规则
对于中等硬度,添加如下规则:
我可以根据这个9的平方、这一行和这一列的组合来确定这个单元格吗?
如果在某个时刻,所有测试都返回否,我不能,那么您将从以后的隐藏考虑中删除该单元格,并从剩余的尚未隐藏的单元格中随机选择
循环,直到可以隐藏单元格的列表为零
-
这篇文章提供了一个如何处理类似问题的模式。把它拆开。想一想在没有计算机的情况下如何手动操作,然后将其转化为计算机的指令
我认为随机化电路板实际上比隐藏一些单元更难,因为这是一个非常简单的算法。我不确定随机化的董事会是简单的,但也许它实际上并不坏。但我认为有可能走上不好的道路。你有没有尝试在互联网上搜索java数独代码?是的,我有,但我只看到了数独解算器/