2017-02-17 66 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