数独游戏抛出java.lang.ClassNotFoundException

时间:2016-02-02 03:06:26

标签: java sudoku

我用Java写了一个Sudoku,这是董事会,

import java.util.ArrayList;
import java.util.Iterator;

public class Sudoku {

    private int[][] board;

    public Sudoku(){
        board = new int[Constants.size][Constants.size];
    }

    public int getValue(int row, int col){
        return board[row][col];
    }

    public void add(int row, int col, int item){
        if(row < 0 || row >= Constants.size || col < 0 || col >= Constants.size || item < 1 || item > Constants.size) throw new IllegalStateException("One or more of your entries doesn't follow thr rules of Sudoku bummer!!!");
        board[row][col] = item;
    }

    public void initialize(ArrayList<Triplet> initializerList){
        for(Iterator itr = initializerList.iterator(); itr.hasNext();){
            Triplet T = (Triplet) itr.next();
            board[T.row][T.col] = T.item;

        }
    }

    public void trace(){
        for (int i = 0; i <Constants.size ; i++) {
            for (int j = 0; j < Constants.size; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    public void reinitialize(){
        for (int i = 0; i <Constants.size ; i++) {
            for (int j = 0; j <Constants.size ; j++) {
                board[i][j] = 0;
            }
        }
    }

    public boolean complete(){
        for (int i = 0; i <Constants.size ; i++) {
            for (int j = 0; j <Constants.size ; j++) {
                if(board[i][j] == 0) return false;
            }
        }
        return true;
    }


    public void checkRelevantRow(int[] arr, int row){
        for(int i= 0; i < Constants.size; i++){
            if(board[row][i] != 0){
                int num = board[row][i];
                arr[num] = 1;
            }

        }
    }

    public void checkRelevantCol(int[] arr, int col){
        for (int i = 0; i < Constants.size ; i++) {
            if(board[i][col] != 0){
                int num = board[i][col];
                arr[num] = 1;
            }
        }
    }

    public void checkRelevantGrid(int[] arr, int row, int col){
        if(row >= 0 && row < 3 && col >= 0 && col <3){

            gridChecker(0,3,0,3, arr);
        }else if(row >= 3 && row < 6 && col >= 0 && col < 3){

            gridChecker(3,6,0,3, arr);
        }else if(row >= 6 && row < 9 && col >= 0 && col < 3){

            gridChecker(6,9,0,3, arr);
        }else if(row >= 0 && row < 3 && col >= 3 && col < 6){

            gridChecker(0,3,3,6, arr);
        }else if(row >= 0 && row < 3 && col >= 6 && col < 9){

            gridChecker(0,3,6,9, arr);
        }else if(row >= 3 && row < 6 && col >= 3 && col < 6){

            gridChecker(3,6,3,6, arr);
        }else if(row >= 3 && row < 6 && col >= 6 && col < 9){

            gridChecker(3,6,6,9, arr);
        }else if(row >= 6 && row < 9 && col >= 3 && col < 6){

            gridChecker(6,9,3,6, arr);
        }else if(row >= 6 && row < 9 && col >= 6 && col < 9){

            gridChecker(6,9,6,9, arr);
        }else{
            throw new IllegalStateException("Wrong co-ordiantes passed to Grid Relevance Checker!!");
        }
    }

    public void gridChecker(int rowStart, int rowEnd, int colStart, int colEnd, int[] arr){
        for (int i = rowStart; i <rowEnd ; i++) {
            for (int j = colStart; j < colEnd; j++) {
                if(board[i][j] != 0){
                    int num = board[i][j];
                    arr[num] = 1;
                }
            }
        }
    }



}

这是调用数独板的Game类。

import java.util.ArrayList;
import java.util.Random;


public class Game {
    private Sudoku sudoku;
    private ArrayList<Triplet> values;

    public Game(ArrayList<Triplet> values){
        sudoku = new Sudoku();
        this.values = values;
        sudoku.initialize(values);
    }

    public void trace(){
        sudoku.trace();
    }

    public void play(){
        sudoku.trace();
        long startTime = System.nanoTime();
        while(true){
            for (int i = 0; i < Constants.size; i++) {
                for (int j = 0; j <Constants.size ; j++) {
                    if(sudoku.getValue(i, i) != 0){
                        int[] check = new int[10];
                        check[0] = 1;
                        sudoku.checkRelevantRow(check, i);
                        sudoku.checkRelevantCol(check, j);
                        sudoku.checkRelevantGrid(check, i, j);
                        ArrayList<Integer> list = new ArrayList<>();
                        for (int k = 1; i <10 ; k++) {
                            if(check[k] == 0) list.add(i);
                        }
                        if(list.size() > 0){
                            int len = list.size() -1;
                            Random rand = new Random();
                            int index = rand.nextInt(len);
                            int value = list.get(index);
                            sudoku.add(i, j, value);
                        }else break;

                    }
                }
            }
            if(sudoku.complete()){
                sudoku.trace();
                break;
            }else {
                sudoku.reinitialize();
                sudoku.initialize(values);
            }
        }
        long endTime = System.nanoTime();
        System.out.println("Time taken: " +(endTime - startTime)+ "ns" );

    }



}

辅助三重奏在这里定义,

public class Triplet {
    int row;
    int col;
    int item;

    public Triplet(int row, int col, int item){
        this.row = row;
        this.col = col;
        this. item = item;
    }

}

程序使用电路板大小作为全局常量

public class Constants {
    public static int size = 9;
}

这是我的测试客户端,

public class TestClient{

    public ArrayList<Triplet> list;

    public TestClient(){

        list = new ArrayList<>();

        Triplet t1 = new Triplet(0, 1, 9);
        list.add(t1);
        Triplet t2 = new Triplet(0, 6, 4);
        list.add(t2);
        Triplet t3 = new Triplet(0, 8, 1);
        list.add(t3);
        Triplet t4 = new Triplet(1, 1, 3);
        list.add(t4);
        Triplet t5 = new Triplet(1, 4, 9);
        list.add(t5);
        Triplet t6 = new Triplet(1, 5, 2);
        list.add(t6);
        Triplet t7 = new Triplet(1, 7, 8);
        list.add(t7);
        Triplet t8 = new Triplet(2, 0, 7);
        list.add(t8);
        Triplet t9 = new Triplet(2, 1, 5);
        list.add(t9);
        Triplet t10 = new Triplet(2, 5, 4);
        list.add(t10);
        Triplet t11 = new Triplet(3, 0, 8);
        list.add(t11);
        Triplet t12 = new Triplet(3, 2, 9);
        list.add(t12);
        Triplet t13 = new Triplet(3, 3, 1);
        list.add(t13);
        Triplet t14 = new Triplet(3, 5, 5);
        list.add(t14);
        Triplet t15 = new Triplet(3, 6, 6);
        list.add(t15);
        Triplet t16 = new Triplet(4, 1, 2);
        list.add(t16);
        Triplet t17 = new Triplet(4, 2, 5);
        list.add(t17);
        Triplet t18 = new Triplet(4, 4, 7);
        list.add(t18);
        Triplet t19 = new Triplet(4, 6, 8);
        list.add(t19);
        Triplet t20 = new Triplet(4, 7, 9);
        list.add(t20);
        Triplet t21 = new Triplet(5, 2, 6);
        list.add(t21);
        Triplet t22 = new Triplet(5, 3, 9);
        list.add(t22);
        Triplet t23 = new Triplet(5, 5, 8);
        list.add(t23);
        Triplet t24 = new Triplet(5, 6, 1);
        list.add(t24);
        Triplet t25 = new Triplet(5, 8, 7);
        list.add(t25);
        Triplet t26 = new Triplet(6, 3, 5);
        list.add(t26);
        Triplet t27 = new Triplet(6, 7, 6);
        list.add(t27);
        Triplet t28 = new Triplet(6, 8, 4);
        list.add(t28);
        Triplet t29 = new Triplet(7, 1, 8);
        list.add(t29);
        Triplet t30 = new Triplet(7, 3, 3);
        list.add(t30);
        Triplet t31 = new Triplet(7, 4, 6);
        list.add(t31);
        Triplet t32 = new Triplet(7, 7, 1);
        list.add(t32);
        Triplet t33 = new Triplet(8, 0, 9);
        list.add(t33);
        Triplet t34 = new Triplet(8, 2, 3);
        list.add(t34);
        Triplet t35 = new Triplet(8, 7, 7);
        list.add(t35);
    }

    public ArrayList<Triplet> getList(){
        return list;
    }
}

这是我的主要课程

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    // write your code here

    TestClient client = new TestClient();
        ArrayList<Triplet> list = client.getList();

        Game game = new Game(list);
        game.trace();
        game.play();


    }
}

当我运行此代码时,我得到以下内容,

Exception in thread "main" java.lang.ClassNotFoundException: io.username.Main
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

Process finished with exit code 1

我是Java的新手,所以似乎无法指出这里出了什么问题。任何帮助表示赞赏。

0 个答案:

没有答案