2013-11-02 19 views
0

所以,這是一個任務,它很合得來,我似乎無法得到 過去,這部分在那裏我將它設置爲只添加4 X和5 O的。我一直在嘗試一些選擇,但我想我會傾倒在這裏實際工作的代碼,如果有人願意協助,將不勝感激。我可以定義特定數字在math.random中顯示的次數嗎? (JAVA)

「寫入模擬井字棋和 的遊戲的程序指示玩家是否贏得,並且其 程序應該: •聲明和使用大小的整數的數組[3,3]來表示井字棋格

•填寫陣列中的每個位置與無論是-1或+1如下

•4「表示X的由-1

•5」 O的由+1表示

•顯示網格

•評估並指出使用'X'的玩家是否贏了。

•評估並指出使用'O'的玩家是否贏了。

程序必須實現和至少使用以下方法:

•公共靜態無效fillGridWithRandomValues(INT [] []柵格) ö接收陣列和填充是在-1和1隨機選擇

•公共靜態無效printGrid(INT [] []格) o顯示屏幕

對電網•公共靜態布爾evaluateWinner(INT [] []格,int值) :○當有在返回true在同一個中至少有3個職位行,或包含 參數'值'的相同列或相同對角線。否則,返回false」

而這裏的實際代碼

包123456;

/** * * @author 123456 */ 公共類123456 {

/** 
* @param args the command line arguments 
*/ 
public static void fillGridWithRandomValues(int[][] grid) { 
    for (int row=0; row < grid.length; row++) { 
     for (int column=0; column < grid[row].length; column++) { 
      grid[row][column] = (int) (((Math.random()*2)*2)-2); 
          if (grid[row][column] == 0) grid[row][column]++; 

        } 
    }; 
} 

public static boolean evaluateWinner(int[][] grid, int value) { 

     return false; 

} 

public static void printGrid(int[] row) { 
    for (int i : row) { 
     String str = null; 
     if (i == 1) str = "X"; 
     else if (i == -1) str = "O"; 
     else str = null ; 
     System.out.print(str + " "); 
     System.out.print(" "); 
    } 
    System.out.println(); 
    } 

public static void main(String[] args) { 

    // creating array for the grid 

    int[][] grid ;//= { { 1, -1, 1 }, { -1, 1, -1 }, { 1, -1, } }; 
    grid = new int[3][3]; 

    fillGridWithRandomValues(grid); 

    for(int[] row : grid) { 
     printGrid(row); 
    } 




    } 

}

+2

也許在'fillGridWithRandomValues'中,你應該先用'1'填充網格,然後選擇四個元素設置爲'-1'? – pobrelkey

回答

0

不,Math.random()是專門設計編輯給不可預知的結果。

相關問題