似乎无法正确编译我的方法(在Java中)

时间:2017-02-17 04:57:10

标签: java tic-tac-toe

我正试图弄清楚我正在努力创造的这个小小的tictacttoe游戏中我的winorTie方法究竟出了什么问题。有人能帮忙吗?感谢

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 个答案:

答案 0 :(得分:1)

评论中提到了其中一些内容,但这些条件从根本上说没有用:

<RelativeLayout
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:background="@android:color/transparent"
        android:inputType="textNoSuggestions|textMultiLine"
        android:maxLength="200"
        android:paddingBottom="8dp"
        android:paddingRight="5dp"
        android:paddingTop="8dp"
        android:textColor="#202020"
        android:textColorHint="#979797"
        android:textSize="14sp"/>


    <TextView
        android:id="@+id/charecter_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/add_comment_edit_text"
        android:layout_gravity="bottom"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:gravity="bottom"
        android:text="0/200"
        android:textColor="#979797"
        android:textSize="14sp"/>
</RelativeLayout>

例如,您认为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]应该是什么?您认为" "()有什么作用?很难确切地知道你在这里想要实现的目标。

另外,请考虑== &&。这里有两个问题。首先,您确实意识到-1实际上并不是Java中的有效数组索引,对吧? (这在Python中是允许的,但不是Java)。此外,gameboard [0][-1]是整数,而不是bool,因此gameboard [0][-1]没有意义。如果您有&& gameboard [0][-1]之类的内容,则A && BA 都必须评估某种布尔值(即B或{{1} })。

另外,我鼓励你不要像在这里那样做缩进。我建议把每个&#34;否则如果&#34;在它自己的路线上。