数独生成器循环 [英] Sudoku generator loop

查看:181
本文介绍了数独生成器循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作数独生成器,以便可以制作数独游戏,但遇到了问题... 我已经成功地创建了一种方法,该方法可以检查某个单元格,并检查其中的数字是否在其所属的同一行,同一列或3x3正方形中重复,但是我在随机生成数字并将其填充时遇到了问题. 基本上,首先我用从1-9的随机数填充第一行,该数字在该行中仅出现一次. 我的问题是,是否可以在每个单元格中填充与目前为止生成的数字相匹配的随机数,还是应该逐行填充?也许是逐平方?因为我的循环似乎变成了无限循环.这是代码:

I'm trying to make a sudoku generator so I can make into a sudoku game and I've encountered a problem... I have successfully made a method which checks a certain cell and whether the number in it repeats in the same row, column or 3x3 square it belongs to but I have a problem with generating the numbers randomly and filling them in. Basically first I fill the first line with random numbers from 1-9 which only appear once in the line. My question is, is it possible to fill cell after cell with random numbers which suit the numbers generized so far or should I fill line by line? Or maybe square by square? Because my loop seems to turn into an infinite loop. Here's the code:

  package test;

    import java.util.Random;

    public class Test {
        public static void main(String[] args) {
            int[][]test=new int[9][9];
            int[]prva=new int[]{1,2,3,4,5,6,7,8,9};
            zapolniPrvo(test,prva);//fills the first line of the table
            print(test);
            System.out.println();
            int y=1;
            int x=0;
            int rn=0;
            int a=1;
            for(int i=1;i<9;i++){
                for(int j=0;j<9;j++){
                    while(!razlicnostT(j,i,test)){
                        test[i][j]=(int)(Math.random()*9+1);
                    }
                }
            }
            print(test);
        }
        public static boolean razlicnostT(int y,int x,int[][]test){ //checks for same number in the line, row and square
            int vrstica=0;
            int kolona=0;
            int yy=(y/3)*3;
            int xx=(x/3)*3;
            int yyy=(y%3);
            int xxx=(x%3);
            int kvadrat=0;
            boolean razlicnost=false;
            for(int i=yy;i<=yyy;i++){
                for(int j=xx;j<=xxx;j++){
                    if(test[i][j]==test[y][x]){
                        kvadrat++;
                    }
                }
            }
            for(int i=0;i<x;i++){
                if(test[y][i]!=test[y][x]){
                    vrstica++;
                }
            }
            for(int i=0;i<y;i++){
                if(test[i][x]!=test[y][x]){
                    kolona++;
                }
            }
            if((vrstica==x) && (kolona==y)&&(test[y][x]!=0)&&(kvadrat!=1)){
                razlicnost=true;
            } else {
                razlicnost=false;
            }
            return razlicnost;
        }
        public static void zapolniPrvo(int[][]test,int[]prva){
            randomize(prva);
            for(int i=0;i<9;i++){
                test[0][i]=prva[i];
            }
        }
        public static void print(int[][]test){
            for(int i=0;i<test.length;i++){
                for(int j=0;j<test.length;j++){
                    System.out.print(test[i][j]+" ");
                }
                System.out.println();
            }
        }
        public static void randomize (int[]temp){
            Random rnd = new Random();
            for (int i = temp.length - 1; i > 0; i--){
                int index = rnd.nextInt(i + 1);
                int a = temp[index];
                temp[index] = temp[i];
                temp[i] = a;
            }
        }
    }

注意:如果该数字在行/列/3x3正方形中仅出现一次且test是表,则razlicnostT返回true.

Note: razlicnostT returns true if the number appears only once in the row/column/3x3 square and test is the table

推荐答案

从我看到的引起问题的陈述是这个

From what i can see statement causing problem is this

if((vrstica==x) && (kolona==y)&&(test[y][x]!=0)&&(kvadrat!=1)){ razlicnost=true;

if((vrstica==x) && (kolona==y)&&(test[y][x]!=0)&&(kvadrat!=1)){ razlicnost=true;

由于最初将razlicnost设置为false,所以该语句显然永远不会成立,这会导致while(!razlicnostT(j,i,test)无限运行.

Since razlicnost is intitially set to be false this statement is obviously never true and that causes while(!razlicnostT(j,i,test) running infinite.

这绝对是应用程序逻辑中的错误.不幸的是,由于您的代码

It is definitely error in application logic. Unfortunately I am not able to help you with this since your code

  1. 格式不正确
  2. 使用其他语言
  3. 使用awfull(或不使用)命名约定(诸如yyyxxx之类的名称,是将来任何人审查您的代码的噩梦)
  1. Is poorly formated
  2. Uses different language
  3. Uses awfull(or none) naming conventions (names like y,yy,xxx are nightmare of anyone reviewing your code in the future)

我的建议是重写此代码以使其更具可读性,因为修复它可能需要更长的时间

My advice is to rewrite this code to be more readable because fixing it may take even longer time

这篇关于数独生成器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆