2011-05-21 64 views
2

我無法使用已定義的遊戲初始化無參數構造函數。不知何故,如果我使用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}}, 

public class SudokuPlayer 
{ 
    private int [][] game; 
    public enum CellState { EMPTY, FIXED, PLAYED }; 
    private CellState[][] gamestate; 

    private static final int GRID_SIZE=9; 

    public SudokuPlayer() 
    { 
     int[][] copy= SudokuGames.getGame(4); 
     int size = copy.length; 
     int[][] game = new int[GRID_SIZE][GRID_SIZE]; 
     for (int row = 0; row < size; row++) 
     { 
      for (int col =0; col < size; col++) 
      { 
       game[row][col] = copy[row][col]; 
      } 
     } 
    } 

    public int[][] getGame() 
    { 
     return game; 
    } 
} 

這裏是從diferent類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; 
     } 

    } 
} 
+0

你說'SudokuGame.getGame(4)'返回null,但你不提供源代碼,以'SudokuGame'。我們可以看到它嗎? – 2011-05-21 10:28:29

+0

是的,你可以發送它 – clfc 2011-05-21 10:30:34

回答

5

你是隱藏的實例變量。

更改以下

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

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

是的,似乎工作:)你能告訴我背後的邏輯?爲什麼沒有工作? – clfc 2011-05-21 10:34:07

+1

@clfc - 因爲你用'int [] [] game = new int [GRID_SIZE] [GRID_SIZE];'聲明瞭一個不同的局部變量。它沒有以任何方式與你的類的實例變量連接。這就是爲什麼getGame()返回null。 – 2011-05-21 10:35:11

+0

哦,好吧。但我想我是宣佈一個新的數組,我不知道數組類型的實例變量連接到您創建的數組。無論如何,謝謝 – clfc 2011-05-21 10:37:11