【问题标题】:Sudoku generator loop数独生成器循环
【发布时间】:2014-05-29 02:20:22
【问题描述】:

我正在尝试制作数独生成器,以便制作数独游戏,但遇到了问题... 我已经成功地制作了一种方法来检查某个单元格以及其中的数字是否在它所属的同一行、列或 3x3 正方形中重复,但是我在随机生成数字并填写它们时遇到了问题。 基本上,首先我用 1-9 的随机数填充第一行,这些随机数只在该行中出现一次。 我的问题是,是否可以用适合迄今为止生成的数字的随机数填充单元格,还是应该逐行填充?或者也许是方方正正的?因为我的循环似乎变成了一个无限循环。代码如下:

  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

【问题讨论】:

  • 请在此处发布代码。如果您的问题得到回答并且粘贴箱消失了,其他人将很难从您的问题中受益。
  • 究竟哪个循环无限运行?你们到底是什么句子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?
  • 这个:for(int i=1;i&lt;9;i++){ for(int j=0;j&lt;9;j++){ while(!razlicnostT(j,i,test)){ test[i][j]=(int)(Math.random()*9+1); } } } 我的意思是,是否可以用一个随机数来填充每个单元格,该随机数由基于布尔值中给出的条件完成的循环所概括?还是应该以不同的方式完成?对不起我的英语不好

标签: java generator sudoku


【解决方案1】:

据我所知,导致问题的语句是这样的

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

由于 razlicnost 最初设置为 false,因此该语句显然永远不会正确,这会导致 while(!razlicnostT(j,i,test) 无限运行。

这绝对是应用程序逻辑中的错误。不幸的是,由于您的代码,我无法为您提供帮助

  1. 格式不正确
  2. 使用不同的语言
  3. 使用糟糕的(或不使用)命名约定(yyyxxx 之类的名称是任何人将来审查您的代码的噩梦)

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

【讨论】:

    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多