加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java数独生成器无法正常工作

发布时间:2020-12-15 04:11:14 所属栏目:Java 来源:网络整理
导读:我一直在使用 java中的数独谜题生成器,我写了这个类来生成谜题,但它没有正确生成谜题.这是我得到的一个例子: 如您所见,这不是一个有效的数独解决方案.但是看着我的代码,我不明白为什么它没有产生有效的谜题.有人可以解释为什么这不能正常工作? package sud
我一直在使用 java中的数独谜题生成器,我写了这个类来生成谜题,但它没有正确生成谜题.这是我得到的一个例子:

如您所见,这不是一个有效的数独解决方案.但是看着我的代码,我不明白为什么它没有产生有效的谜题.有人可以解释为什么这不能正常工作?

package sudoku;

import java.util.Random;

public class Puzzle {

    // number generator
    Random gen = new Random();

    // 9x9 puzzle
    int puzzle[][] = new int[9][9];

    public int[][] generate() {

        // add each number to the board
        for (int x = 0; x < 9; x++) {
            for (int y = 0; y < 9; y++) {

                boolean isValid = false;

                // keep generating new numbers until a valid number is found
                while (isValid == false) {

                    // generate random number 1-9
                    int num = gen.nextInt(9) + 1;

                    // check if number is valid
                    if (checkRow(num,x) == true || checkCol(num,y) == true
                            || checkSection(num,x,y) == true) {

                        // add number to the board
                        puzzle[x][y] = num;

                        // exit loop
                        isValid = true;
                    }
                }
            }
        }

        return puzzle;
    }

    // check each element of the row for num,if num is found return false
    private boolean checkRow(int num,int row) {

        boolean valid = true;
        for (int i = 0; i < 9; i++) {
            if (puzzle[row][i] == num) {
                valid = false;
                break;
            }
        }

        return valid;
    }

    // check each element of the column for num,if num is found return false
    private boolean checkCol(int num,int col) {

        boolean valid = true;
        for (int i = 0; i < 9; i++) {
            if (puzzle[i][col] == num) {
                valid = false;
                break;
            }
        }

        return valid;
    }

    // check each element of the section for num,if num is found return false
    private boolean checkSection(int num,int xPos,int yPos) {

        int[][] section = new int[3][3];
        section = getSection(xPos,yPos);

        boolean valid = true;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (section[i][j] == num) {
                    valid = false;
                    break;
                }
            }
        }

        return valid;
    }

    // return the 3x3 section the given coordinates are in
    private int[][] getSection(int xPos,int yPos) {

        int xIndex = 0;
        int yIndex = 0;
        int[][] section = new int[3][3];

        // get x index
        if (xPos == 0 || xPos == 3 || xPos == 6) {
            xIndex = xPos;
        } else if (xPos == 1 || xPos == 4 || xPos == 7) {
            xIndex = xPos - 1;
        } else if (xPos == 2 || xPos == 5 || xPos == 8) {
            xIndex = xPos - 2;
        }

        // get y index
        if (yPos == 0 || yPos == 3 || yPos == 6) {
            yIndex = yPos;
        } else if (yPos == 1 || yPos == 4 || yPos == 7) {
            yIndex = yPos - 1;
        } else if (yPos == 2 || yPos == 5 || yPos == 8) {
            yIndex = yPos - 2;
        }

        int i = 0;
        int j = 0;
        // extract section from puzzle
        for (int x = xIndex; x < 3; x++) {
            for (int y = yIndex; y < 3; y++) {
                section[x][y] = puzzle[i][j];
                i++;
            }
            j++;
        }

        return section;

    }
}

解决方法

行,列或节是有效的是不够的.他们都必须有效.所以,改变这一行:

if (checkRow(num,y) == true || checkSection(num,y) == true)

if (checkRow(num,x) == true && checkCol(num,y) == true && checkSection(num,y) == true)

或者只是简单

if (checkRow(num,x) && checkCol(num,y) && checkSection(num,y)) {

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读