Java 数独游戏构造器

Java 数独游戏构造器,java,arrays,2d,sudoku,Java,Arrays,2d,Sudoku,我在使用已定义的游戏初始化无参数构造函数时遇到问题。如果我使用getter方法返回游戏,它会一直返回null。谁能告诉我初始化游戏的最佳方法是什么 目前,我正在从另一个类中调用一个方法,该类有一个静态方法,但它似乎不起作用,因为如果我得到游戏,它会给出一个null值 这就是我对这个游戏的看法: {{7,8,1,0,0,4,0,0,6}, {2,0,9,3,6,0,1,0,0}, {6,0,0,0,9,0,8,0,0},

我在使用已定义的游戏初始化无参数构造函数时遇到问题。如果我使用getter方法返回游戏,它会一直返回
null
。谁能告诉我初始化游戏的最佳方法是什么

目前,我正在从另一个类中调用一个方法,该类有一个静态方法,但它似乎不起作用,因为如果我得到游戏,它会给出一个
null

这就是我对这个游戏的看法:

           {{7,8,1,0,0,4,0,0,6},
            {2,0,9,3,6,0,1,0,0},
            {6,0,0,0,9,0,8,0,0},
            {0,0,0,0,3,5,0,0,0},
            {3,5,0,0,0,0,0,1,9},
            {0,0,0,4,2,0,0,0,0},
            {0,0,3,0,1,0,0,0,8},
            {0,0,7,0,8,3,4,0,1},
            {9,0,0,6,0,0,5,7,3}},

SudokuPlayer公共类
{
私人int[][]游戏;
公共枚举单元状态{空,已修复,已播放};
私有CellState[][]游戏状态;
专用静态最终整数网格大小=9;
公共数独玩家()
{
int[][]copy=SudokuGames.getGame(4);
int size=copy.length;
int[][]游戏=新int[GRID_SIZE][GRID_SIZE];
对于(int row=0;row
以下是im调用的不同类的方法:

public class SudokuGames {

    public static final int [][][] GAMES = {
            // Game 0 VE - DEFAULT 30 squares filled
            {{5,3,0,0,7,0,0,0,0},
                {6,0,0,1,9,5,0,0,0},
                {0,9,8,0,0,0,0,6,0},
                {8,0,0,0,6,0,0,0,3},
                {4,0,0,8,0,3,0,0,1},
                {7,0,0,0,2,0,0,0,6},
                {0,6,0,0,0,0,2,8,0},
                {0,0,0,4,1,9,0,0,5},
                {0,0,0,0,8,0,0,7,9}},
            // Game 1 VE
            {{8,0,1,0,3,7,0,5,6},
                {0,0,0,9,0,0,0,0,7},
                {6,0,3,0,1,2,0,9,0},
                {0,2,0,0,0,0,7,0,3},
                {3,0,0,0,2,0,0,0,9},
                {1,0,9,0,0,0,0,8,0},
                {0,3,0,2,7,0,4,0,1},
                {7,0,0,0,0,6,0,0,0},
                {5,6,0,1,9,0,3,0,8}},
            // Game 2 VE
            {{0,9,0,0,3,0,1,4,0},
                {7,0,3,0,0,4,0,0,8},
                {5,0,0,6,0,7,0,2,0},
                {0,7,4,5,0,2,9,0,0},
                {1,0,0,0,0,0,0,0,2},
                {0,0,9,1,0,8,4,6,0},
                {0,5,0,7,0,9,0,0,6},
                {4,0,0,2,0,0,5,0,1},
                {0,8,6,0,5,0,0,7,0}},
            // Game 3 VE
            {{0,0,9,7,3,0,5,2,6},
                {0,0,5,0,2,0,8,0,0},
                {6,0,8,0,0,0,0,4,7},
                {0,0,0,0,0,9,0,6,2},
                {0,4,0,6,0,3,0,8,0},
                {8,9,0,5,0,0,0,0,0},
                {2,6,0,0,0,0,1,0,8},
                {0,0,7,0,1,0,6,0,0},
                {9,5,1,0,6,4,2,0,0}},
            // Game 4 VE
            {{7,8,1,0,0,4,0,0,6},
                {2,0,9,3,6,0,1,0,0},
                {6,0,0,0,9,0,8,0,0},
                {0,0,0,0,3,5,0,0,0},
                {3,5,0,0,0,0,0,1,9},
                {0,0,0,4,2,0,0,0,0},
                {0,0,3,0,1,0,0,0,8},
                {0,0,7,0,8,3,4,0,1},
                {9,0,0,6,0,0,5,7,3}},
            // Game 5 E
            {{0,0,0,9,1,0,0,0,2},
                {5,0,0,0,0,0,0,0,0},
                {3,0,0,5,4,0,0,6,8},
                {0,4,2,7,0,0,3,0,5},
                {0,0,3,4,5,6,2,0,0},
                {1,0,9,0,0,8,7,4,0},
                {8,1,0,0,7,5,0,0,4},
                {0,0,0,0,0,0,0,0,1},
                {9,0,0,0,8,4,0,0,0}},
            // Game 6 E
            {{0,0,0,1,0,7,0,9,0},
                {0,0,0,4,9,0,3,0,0},
                {6,0,0,0,3,0,4,1,0},
                {4,0,5,0,0,0,0,3,0},
                {8,2,0,0,0,0,0,5,4},
                {0,3,0,0,0,0,2,0,6},
                {0,1,4,0,7,0,0,0,5},
                {0,0,8,0,2,5,0,0,0},
                {0,6,0,8,0,1,0,0,0}},
            // Game 7 E
            {{0,0,2,8,0,7,5,0,0},
                {6,0,0,0,0,0,0,0,4},
                {0,8,0,0,6,0,0,7,0},
                {1,3,0,4,0,9,0,2,5},
                {0,0,0,0,0,0,0,0,0},
                {4,5,0,7,0,1,0,6,8},
                {0,6,0,0,3,0,0,9,0},
                {5,0,0,0,0,0,0,0,7},
                {0,0,1,6,0,4,2,0,0}}
        };  //End

    /**
     * getGame(int gameID) returns a a Sudoku game as int [][]
     *  as specified by the gameID. If the gameID is outside the
     *  list of possible games, null is returned.
     *  @param gameID number of game to be played
     *  @return int[][] a game to be played
     */
    public static int [][] getGame (int gameID) {

        /** A little dangerous. A copy of the reference to the original array
         * is returned. Not a copy of the array.
         **/
        if (gameID >= 0 && gameID < GAMES.length) {
            return GAMES[gameID];
        }
        else {
            return null;
        }

    }
}
公共级数独游戏{
公共静态最终int[][]游戏={
//游戏0VE-默认填充30个方块
{{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}},
//第一场比赛
{{8,0,1,0,3,7,0,5,6},
{0,0,0,9,0,0,0,0,7},
{6,0,3,0,1,2,0,9,0},
{0,2,0,0,0,0,7,0,3},
{3,0,0,0,2,0,0,0,9},
{1,0,9,0,0,0,0,8,0},
{0,3,0,2,7,0,4,0,1},
{7,0,0,0,0,6,0,0,0},
{5,6,0,1,9,0,3,0,8}},
//第二场比赛
{{0,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0,0,0,0,0,0,0,0},
{4,5,0,7,0,1,0,6,8},
{0,6,0,0,3,0,0,9,0},
{5,0,0,0,0,0,0,0,7},
{0,0,1,6,0,4,2,0,0}}
};//结束
/**
*getGame(int gameID)将数独游戏返回为int[][]
*由gameID指定。如果gameID在
*可能的游戏列表,返回null。
*@param gameID要玩的游戏数
*@return int[][]要玩的游戏
*/
公共静态int[][]getGame(int gameID){
/**有点危险。原始数组引用的副本
*返回。不是数组的副本。
**/
如果(gameID>=0&&gameID
您正在隐藏实例变量

更改以下内容

int[][] game = new int[GRID_SIZE][GRID_SIZE];


你说
SudokuGame.getGame(4)
返回null,但是你没有提供源代码给
SudokuGame
。我们能看到它吗?是的,这似乎有效:)你能告诉我背后的逻辑吗?为什么不起作用?@clfc-因为你用
int[]game=newint[GRID_SIZE][GRID_SIZE]声明了一个不同的局部变量。它与类的实例变量没有任何连接。这就是为什么
getGame()
返回
null
。哦,好的。但是我以为我在声明一个新数组,我不知道数组类型的实例变量连接到您创建的数组。谢谢anyway@clfc-是的,您创建了一个新的数组,但它没有被分配给实例引用,而是指向构造函数中的一个不同的引用,它是本地的。@ CLFC——如果您认为我的答案是正确的,请用左边的绿色标记来检查它。
int[][] game = new int[GRID_SIZE][GRID_SIZE];
game = new int[GRID_SIZE][GRID_SIZE];