2017-02-17 82 views
0

我想弄清楚我的winorTie方法在我嘗試創建的這個小型tictacttoe遊戲中究竟出了什麼問題。任何人都可以提供幫助嗎?由於似乎無法讓我的方法(在Java中)正確編譯

package tictactoegame; 

/** 
* 
* @author Douglas Boulden 
*/ 
public class tictactoegame { 

    static int [][] gameboard; 
    static final int EMPTY = 0; 
    static final int NOUGHT = -1; 
    static final int CROSS = 1; 

    static void set (int val, int row) throws IllegalArgumentException { 
     int col = 0; 
     if (gameboard[row][col] == EMPTY) 
       gameboard[row][col] = val; 
     else throw new 
      IllegalArgumentException("Player already there!"); 
    } 

    static void displayBoard() { 
     for (int[] gameboard1 : gameboard) { 
      System.out.print("|"); 
      for (int c = 0; c < gameboard1.length; c++) { 
       switch (gameboard1[c]) { 
        case NOUGHT: 
         System.out.print("0"); 
         break; 
        case CROSS: 
         System.out.print("X"); 
         break; 
        default:   //Empty 
         System.out.print(" "); 
       } 
       System.out.print("|"); 
      } 
      System.out.println("\n------\n"); 
     } 
    } 

    static void createBoard(int rows, int cols) { 
     gameboard = new int [rows] [cols]; 
    } 

    static int winOrTie() { 
     if (gameboard [0][0] == NOUGHT && gameboard [0][-1]) 
      return NOUGHT; 
    } else if (gameboard [0][0] == && CROSS) [0][1] { 
      return CROSS; 
    } else if (gameboard [0][0]== && " "()) [0][0] {  
      return 0; 
    } else { 
      return false;     
    } 


    /** 
    * @param args the command line arguments 
    */ /** 
    * @param args the command line arguments 
    */ 

    public static void main(String[] args) { 
     createBoard(3,3); 
     int turn = 0; 
     int playerVal; 
     int outcome; 
     java.util.Scanner scan = new 
      java.util.Scanner(System.in); 
     do { 
      displayBoard(); 
      playerVal = (turn % 2 == 0)? NOUGHT : CROSS; 
      if (playerVal == NOUGHT) { 
       System.out.println ("\n-0's turn-"); 
      } else { 
       System.out.println("\n-X's turn-"); 
       } 
      System.out.print("Enter row and Column:"); 
      try { 
       set(playerVal, scan.nextInt()); 
      } catch (IllegalArgumentException ex) 
      {System.err.println(ex);} 
      turn ++; 
      outcome = winOrTie(); 
     } while (outcome == -2); 
     displayBoard(); 
     switch (outcome) { 
      case NOUGHT: 
       System.out.println("0 wins!"); 
       break; 
      case CROSS: 
       System.out.println("X wins!"); 
       break; 
      case 0: 
       System.out.println("Tie."); 
       break; 
      } 
    } 

} 
+1

'gameboard [0] [ - 1]'是什麼意思? '「」()是什麼意思? –

+0

請使用IDE編寫代碼。在函數'winOrTie()'爲什麼在第一個'else if'的前面有一個'}'?這行的含義是什麼'else if(gameboard [0] [0] == && CROSS)[0] [1]'? –

+0

請看[爲什麼「有人可以幫我嗎?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) – EJoshuaS

回答

1

一些這是在評論中提到的,但這些條件根本沒有任何意義:

if (gameboard [0][0] == NOUGHT && gameboard [0][-1]) 
     return NOUGHT; 
} else if (gameboard [0][0] == && CROSS) [0][1] { 
     return CROSS; 
} else if (gameboard [0][0]== && " "()) [0][0] {  
     return 0; 

例如,你認爲這是什麼if (gameboard [0][0] == && CROSS) [0][1]是應該做的? " "()應該是什麼?你認爲== &&有什麼作用?很難確切地知道你實際上想要在這裏實現什麼。

另外,考慮gameboard [0][-1]。這裏有兩個問題。首先,你知道-1實際上並不是Java中的有效數組索引,對吧? (這在Python中是允許的,但不是Java)。另外,gameboard [0][-1]是一個整數,而不是布爾值,所以&& gameboard [0][-1]沒有意義。如果您有類似A && B的值,AB必須評估爲某種布爾值(即truefalse)。

此外,我鼓勵你不要像你這樣做縮進。我建議把每個「其他如果」放在自己的路線上。

+0

我正在嘗試定義winortie方法。我需要找出X贏了是否有價值,如果是的話,是否贏得了什麼價值,如果是的話,如果是的話,會發生什麼樣的價值,如果有的話,遊戲板上是否有空單元格? – Doug

+0

@Doug你能澄清你認爲那些應該做的事嗎?很坦率地說,上面列出的if語句的語法實際上是不連貫的 - 例如,else if(gameboard [0] [0] == &&「」())[0] [0]'應該是做? – EJoshuaS

+0

相信我,我知道他們是不連貫的。我所擁有的if語句根本不對。這就是我在這裏的原因。如果我可以通過winortie方法獲得幫助,並且正確地使用它,那麼這個項目將會正確編譯 – Doug