⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sudokugame.java

📁 数独的一种实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            int l = c - 65;
            int j1 = stdin.nextInt() - 1;
            stdin.nextLine();
            placeAndValidateValue(l, j1, i);
            board.refresh();
            return;
        }
        do
        {
            System.out.println("Enter value followed by position (no commas) or, ");
            System.out.print("enter 'M' to go to the main menu: ");
            int j;
            do
            {
                String s = stdin.next().trim();
                if(s.length() == 0)
                {
                    System.out.print("Invalid input. Please re-enter: ");
                    stdin.nextLine();
                    continue;
                }
                char c1 = s.toUpperCase().charAt(0);
                if(c1 == 'M')
                {
                    System.out.println("Returning to main menu.");
                    stdin.nextLine();
                    return;
                }
                if(!isPositiveInteger(s))
                {
                    System.out.printf("\"%s\" is not a valid value.", new Object[] {
                        s
                    });
                    System.out.print("  Please re-enter value and position: ");
                    stdin.nextLine();
                    continue;
                }
                j = Integer.parseInt(s);
                if(j <= getSize())
                    break;
                System.out.printf("%d is not a valid value.  Please re-enter value and position: ", new Object[] {
                    Integer.valueOf(j)
                });
                stdin.nextLine();
            } while(true);
            int k;
            int i1;
            do
            {
                String s1 = stdin.next();
                k = s1.toUpperCase().charAt(0) - 65;
                i1 = -1;
                String s2 = stdin.next();
                if(isPositiveInteger(s2))
                    i1 = Integer.parseInt(s2) - 1;
                if(validateLocation(k, i1))
                    break;
                System.out.print("You entered an invalid row/column combination.  Please re-enter row and column only: ");
                stdin.nextLine();
            } while(true);
            stdin.nextLine();
            for(; !placeAndValidateValue(k, i1, j); stdin.nextLine())
            {
                board.refresh();
                System.out.print("Enter a new value: ");
                j = stdin.nextInt();
            }

            board.refresh();
        } while(true);
    }

    public boolean placeAndValidateValue(int i, int j, int k)
    {
        matrix.set(i, j, k);
        if(!validateBoard())
        {
            matrix.set(i, j, -k);
            return false;
        } else
        {
            return true;
        }
    }

    public void run()
    {
        if(board == null)
        {
            System.err.println("ERROR! Board not set");
            return;
        }
        board.refresh();
        for(char c = menu.getChoice(); c != 'Q'; c = menu.getChoice())
            switch(c)
            {
            case 80: // 'P'
                move();
                break;

            case 76: // 'L'
                loadGameFromMenu();
                break;

            case 83: // 'S'
                saveGame(getFileName("save"));
                break;

            case 72: // 'H'
                if(!getHint())
                    System.out.println("No hint available");
                break;

            case 85: // 'U'
                System.out.println("Not implemented yet");
                // fall through

            case 79: // 'O'
                solve();
                break;

            case 67: // 'C'
                changeInterface();
                break;

            case 68: // 'D'
            case 69: // 'E'
            case 70: // 'F'
            case 71: // 'G'
            case 73: // 'I'
            case 74: // 'J'
            case 75: // 'K'
            case 77: // 'M'
            case 78: // 'N'
            case 81: // 'Q'
            case 82: // 'R'
            case 84: // 'T'
            default:
                System.err.println("ERROR! Code should never get here!");
                System.exit(0);
                break;
            }

        System.out.println("Good Bye.");
        board.close();
    }

    public static void main(String args[])
    {
        SudokuGame sudokugame = new SudokuGame();
        System.out.println((new StringBuilder()).append("Sudoku version: ").append(sudokugame.getVersion()).toString());
        SudokuBoard_Swing sudokuboard_swing = new SudokuBoard_Swing(sudokugame);
        sudokugame.setBoard(sudokuboard_swing);
        if(args.length >= 1)
            sudokugame.loadGameFromFile(args[0]);
        sudokugame.run();
    }

    protected static boolean isPositiveInteger(String s)
    {
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if(c < '0' || c > '9')
                return false;
        }

        return true;
    }
    
    private int[] getValidValues(int i, int j)
    {
        int ai[] = new int[getSize()];
        int k = 0;
        int l = matrix.get(i, j);
        for(int i1 = 1; i1 <= getSize(); i1++)
        {
            matrix.set(i, j, i1);
            if(validateBoard())
                ai[k++] = i1;
        }

        matrix.set(i, j, l);
        int ai1[] = new int[k];
        for(int j1 = 0; j1 < k; j1++)
            ai1[j1] = ai[j1];

        return ai1;
    }


    private boolean findOneValidValue()
    {
        for(int i = 0; i < getSize(); i++)
        {
            for(int j = 0; j < getSize(); j++)
            {
                if(matrix.get(i, j) > 0)
                    continue;
                int ai[] = getValidValues(i, j);
                if(ai.length == 1)
                {
                    System.out.printf("Placed %d at [%c, %d] because that is the only number that can be placed there.\n", new Object[] {
                        Integer.valueOf(ai[0]), Integer.valueOf(i + 65), Integer.valueOf(j + 1)
                    });
                    matrix.set(i, j, ai[0]);
                    board.refresh();
                    return true;
                }
            }

        }

        return false;
    }

    private Matrix getValidLocations(int i)
    {
        Matrix matrix1 = new Matrix(getSize(), getSize());
        for(int j = 0; j < getSize(); j++)
        {
            for(int k = 0; k < getSize(); k++)
            {
                if(matrix.get(j, k) > 0)
                    continue;
                matrix.set(j, k, i);
                if(validateBoard())
                    matrix1.set(j, k, 1);
                matrix.set(j, k, 0);
            }

        }

        return matrix1;
    }

    private boolean findOneLocation(int i)
    {
        Matrix matrix1 = getValidLocations(i);
        for(int j = 0; j < getSize(); j++)
        {
            int i1 = 0;
            int l1 = -1;
            for(int k2 = 0; k2 < getSize(); k2++)
                if(matrix1.get(j, k2) == 1)
                {
                    i1++;
                    l1 = k2;
                }

            if(i1 == 1)
            {
                System.out.printf("Placed %d at [%c, %d] because that is the only place we can place the %d in this row.\n", new Object[] {
                    Integer.valueOf(i), Integer.valueOf(j + 65), Integer.valueOf(l1 + 1), Integer.valueOf(i)
                });
                matrix.set(j, l1, i);
                return true;
            }
        }

        for(int k = 0; k < getSize(); k++)
        {
            int j1 = 0;
            int i2 = -1;
            for(int l2 = 0; l2 < getSize(); l2++)
                if(matrix1.get(l2, k) == 1)
                {
                    j1++;
                    i2 = l2;
                }

            if(j1 == 1)
            {
                System.out.printf("Placed %d at [%c, %d] because that is the only place we can place the %d in this column.\n", new Object[] {
                    Integer.valueOf(i), Integer.valueOf(i2 + 65), Integer.valueOf(k + 1), Integer.valueOf(i)
                });
                matrix.set(i2, k, i);
                return true;
            }
        }

        int l = getSectionSize();
        for(int k1 = 0; k1 < l; k1++)
        {
            for(int j2 = 0; j2 < l; j2++)
            {
                int i3 = 0;
                Point point = null;
                for(int j3 = k1 * l; j3 < (k1 + 1) * l; j3++)
                {
                    for(int k3 = j2 * l; k3 < (j2 + 1) * l; k3++)
                        if(matrix1.get(j3, k3) == 1)
                        {
                            i3++;
                            point = new Point(j3, k3);
                        }

                }

                if(i3 == 1)
                {
                    matrix.set(point.x, point.y, i);
                    System.out.printf("Placed %d at [%d, %d] because that is the only place we can place the %d in this section.\n", new Object[] {
                        Integer.valueOf(i), Integer.valueOf(point.x + 65), Integer.valueOf(point.y + 1), Integer.valueOf(i)
                    });
                    return true;
                }
            }

        }

        return false;
    }

    boolean getHint()
    {
        boolean flag = findOneValidValue();
        if(!validateBoard())
            System.out.println("***** ERROR in findOneValidValue\n");
        for(int i = 1; !flag && i <= getSize(); i++)
        {
            if(!findOneLocation(i))
                continue;
            flag = true;
            if(!validateBoard())
                System.out.println("***** ERROR in findOneLocation\n");
        }

        if(flag)
            board.refresh();
        return flag;
    }

    private void solve()
    {
        while(getHint()) ;
        boolean flag = true;
label0:
        for(int i = 0; i < getSize(); i++)
        {
            int j = 0;
            do
            {
                if(j >= getSize())
                    continue label0;
                if(matrix.get(i, j) == 0)
                {
                    flag = false;
                    continue label0;
                }
                j++;
            } while(true);
        }

        if(flag)
            System.out.println("\nSOLVED!!!");
        else
            System.out.println("I was unable to completely solve this Sudoku.\n");
    }

    private void changeInterface()
    {
        Menu menu1 = new Menu(4);
        menu1.addItem('S', "Simple GUI");
        menu1.addItem('C', "Complex GUI");
        menu1.addItem('T', "Text");
        menu1.addItem('N', "No chnage");
        char c = menu1.getChoice();
        board.close();
        switch(c)
        {
        case 83: // 'S'
            board = new SudokuBoard_SimpleGUI(this);
            break;

        case 67: // 'C'
            board = new SudokuBoard_Swing(this);
            break;

        case 84: // 'T'
            board = new SudokuBoard_Text(this);
            break;
        }
        board.refresh();
    }

    public static final String VERSION = "2.0.1";
    protected static final int DEFAULT_GAME_SIZE = 9;
    protected Matrix matrix;
    protected SudokuBoard board;
    protected Scanner stdin;
    protected Menu menu;
    protected static final int NOT_PERFECT_SQUARE = -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -