x无法解析或不是字段

时间:2014-11-15 17:31:41

标签: java android eclipse

所以我刚刚开始编程,我即将发布我的第一个应用程序,我非常高兴。剩下的唯一6个错误是“PLAYER_ONE / PLAYER_TWO无法解析或不是字段”。所有六个错误都是相同的,3个是PLAYER_ONE,3个是PLAYER_TWO。我确信我忘了编码,但我不知道是什么。我已经指出了所有错误,它们在以下几行:108,150,158,191,195和245.

我的MainActivity.java:

package com.wouter.testjk;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.wouter.testjk.R;


public class MainActivity extends Activity implements OnClickListener{

    private TicTacToeGame mGame;

    private Button mBoardButtons[];

    private TextView mInfoTextView;
    private TextView mPlayeroneCount;
    private TextView mTieCount;
    private TextView mPlayertwoCount;
    private TextView mPlayeroneText; 
    private TextView mPlayertwoText;

    private int mPlayeroneCounter = 0;
    private int mTieCounter = 0;
    private int mPlayertwoCounter = 0;

    private boolean mPlayeroneFirst = true;
    private boolean mIsSinglePlayer = false;
    private boolean mIsPlayerOneTurn = true;
    private boolean mGameOver = false;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        boolean mGameType = getIntent().getExtras().getBoolean("gametype");

        mBoardButtons = new Button[mGame.getBOARD_SIZE()];
        mBoardButtons[0] = (Button) findViewById(R.id.one);
        mBoardButtons[1] = (Button) findViewById(R.id.two);
        mBoardButtons[2] = (Button) findViewById(R.id.three);
        mBoardButtons[3] = (Button) findViewById(R.id.four);
        mBoardButtons[4] = (Button) findViewById(R.id.five);
        mBoardButtons[5] = (Button) findViewById(R.id.six);
        mBoardButtons[6] = (Button) findViewById(R.id.seven);
        mBoardButtons[7] = (Button) findViewById(R.id.eight);
        mBoardButtons[8] = (Button) findViewById(R.id.nine);

        Button mTen = (Button) findViewById(R.id.ten);
        mTen.setOnClickListener(this);
        Button mEleven = (Button) findViewById(R.id.eleven);
        mEleven.setOnClickListener(this);



        mInfoTextView = (TextView) findViewById(R.id.information);
        mPlayeroneCount = (TextView) findViewById(R.id.humancount);
        mTieCount = (TextView) findViewById(R.id.tiesCount);
        mPlayertwoCount = (TextView) findViewById(R.id.androidCount);
        mPlayeroneText = (TextView) findViewById(R.id.human);
        mPlayertwoText = (TextView) findViewById(R.id.android);

        mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
        mTieCount.setText(Integer.toString(mTieCounter));
        mPlayeroneCount.setText(Integer.toString(mPlayertwoCounter));

        mGame = new TicTacToeGame();

        startNewGame(mGameType);

    }

    private void startNewGame(boolean isSingle)
    {
        this.mIsSinglePlayer = isSingle;

        mGame.clearBoard();

        for (int i = 0; i < mBoardButtons.length; i++)
        {
            mBoardButtons[i].setText("");
            mBoardButtons[i].setEnabled(true);
            mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));

        }
        if (mIsSinglePlayer)
        {
             mPlayeroneText.setText("speler: ");
             mPlayertwoText.setText("android: ");

             if (mPlayeroneFirst)
             {
                 mInfoTextView.setText(R.string.first_human);
                 mPlayeroneFirst = false;
             }
             else
             {
                 mInfoTextView.setText(R.string.turn_computer);
                 int move = mGame.getComputerMove();
                 setMove(mGame.PLAYER_TWO, move); //error here
                 mPlayeroneFirst = true;
             }
        }
        else
        {
             mPlayeroneText.setText("speler 1: ");
             mPlayertwoText.setText("speler 2: ");

             if (mPlayeroneFirst)
             {
                 mInfoTextView.setText(R.string.turn_player_one);
                 mPlayeroneFirst = false;
             }
             else
             {
                 mInfoTextView.setText(R.string.turn_player_two);
                 mPlayeroneFirst = true;
             }

        }

        mGameOver = false;
    }

    private class ButtonClickListener implements View.OnClickListener
    {
        int location;

        public ButtonClickListener(int location)
        {
            this.location = location;
        }

        @Override
        public void onClick(View view) {
            if (!mGameOver)
            {
                if(mBoardButtons[location].isEnabled())
                {
                    if(mIsSinglePlayer)
                    {
                         setMove(mGame.PLAYER_ONE, location); //error here

                         int winner = mGame.checkForWinner();

                         if (winner == 0)
                         {
                             mInfoTextView.setText(R.string.turn_computer);
                             int move = mGame.getComputerMove();
                             setMove(mGame.PLAYER_TWO, move); //error here
                             winner = mGame.checkForWinner();

                         }
                         if (winner == 0)
                                 mInfoTextView.setText(R.string.turn_human);
                         else if (winner == 1)
                         {
                             mInfoTextView.setText(R.string.result_tie);
                             mTieCounter++;
                             mTieCount.setText(Integer.toString(mTieCounter));
                             mGameOver = true;
                         }       
                         else if (winner ==2)
                         {
                             mInfoTextView.setText(R.string.result_human_wins);
                             mPlayeroneCounter++;
                             mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
                             mGameOver = true;
                         }
                         else if (winner ==3)
                         {
                             mInfoTextView.setText(R.string.result_android_wins);
                             mPlayertwoCounter++;
                             mPlayertwoCount.setText(Integer.toString(mPlayertwoCounter));
                             mGameOver = true;
                         }

                    }
                    else
                    {
                        if(mIsPlayerOneTurn)
                        {
                            setMove(mGame.PLAYER_ONE, location); //error here
                        }
                        else
                        {setMove(mGame.PLAYER_TWO, location); //error here

                        }


                        int winner = mGame.checkForWinner();

                        if (winner == 0)
                        {
                            if(mIsPlayerOneTurn)
                            {
                                mInfoTextView.setText(R.string.turn_player_two);
                                mIsPlayerOneTurn = false;
                            }
                            else
                            {
                                mInfoTextView.setText(R.string.turn_player_one);
                                mIsPlayerOneTurn = true;
                            }
                        }
                        else if (winner == 1)
                        {
                            mInfoTextView.setText(R.string.result_tie);
                            mTieCounter++;
                            mTieCount.setText(Integer.toString(mTieCounter));
                            mGameOver = true;
                        }       
                        else if (winner ==2)
                        {
                            mInfoTextView.setText(R.string.player_one_wins);
                            mPlayeroneCounter++;
                            mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
                            mGameOver = true;
                        }
                        else if (winner ==3)
                        {
                            mInfoTextView.setText(R.string.player_two_wins);
                            mPlayertwoCounter++;
                            mPlayertwoCount.setText(Integer.toString(mPlayertwoCounter));
                            mGameOver = true;
                     }
                    }
                }
            }    
        }
    }

    private void setMove(char player, int location)
    {
        mGame.setMove(player,location);
        mBoardButtons[location].setEnabled(false);
        mBoardButtons[location].setText(String.valueOf(player));
        if (player == mGame.PLAYER_ONE) //error here
            mBoardButtons[location].setTextColor(Color.GREEN);
        else
        {
            mBoardButtons[location].setTextColor(Color.RED);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId())
        {
        case R.id.ten:

            startNewGame(mIsSinglePlayer);
            return;

        case R.id.eleven:

            MainActivity.this.finish();
            return;

        }
    }
}

TicTacToeGame.java:

package com.wouter.testjk;

import java.util.Random;

public class TicTacToeGame {

    private char mBoard[];
    private final static int BOARD_SIZE = 9;

    public static final char PLAYER_ONE = 'X';
    public static final char PLAYER_TWO = '0';
    public static final char EMPTY_SPACE = ' ';

    private Random mRand;

    public static int getBOARD_SIZE() {
        return BOARD_SIZE;
    }

    public TicTacToeGame(){
        mBoard = new char[BOARD_SIZE];

        for (int i = 0; i < BOARD_SIZE; i++)
            mBoard[i] = EMPTY_SPACE;

        mRand = new Random();
    }

    public void clearBoard()
    {
        for (int i = 0;i < BOARD_SIZE; i++)
        {
            mBoard[i] = EMPTY_SPACE;
        }
    }

    public void setMove(char player, int location)
    {
        mBoard[location] = player;
    }

    public int getComputerMove()
    {
        int move;

        for (int i = 0; i < getBOARD_SIZE(); i++)
        {
            if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
            {
                char curr = mBoard[i];
                mBoard[i] = PLAYER_TWO;
                if (checkForWinner() == 3)
                {
                    setMove(PLAYER_TWO, i);
                    return i;
                }
                else 
                    mBoard[i] = curr;
            }
        }
        for (int i = 0; i < getBOARD_SIZE(); i++)
        {
            if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
            {
                char curr = mBoard[i];
                mBoard[i] = PLAYER_ONE;
                if (checkForWinner() == 2)
                {
                    setMove(PLAYER_TWO, i);
                    return i;
                }
                else 
                    mBoard[i] = curr;
            }
        }
        do
        {
            move = mRand.nextInt(getBOARD_SIZE());
        }while (mBoard[move]==PLAYER_ONE || mBoard[move] == PLAYER_TWO);

            setMove(PLAYER_TWO, move);
        return move;
    }
    public int checkForWinner()
    {
        for (int i=0;i<=6;i+=3)
        {
            if (mBoard[i] == PLAYER_ONE && 
                mBoard[i+1] == PLAYER_ONE && 
                mBoard[i+2] == PLAYER_ONE)
                return 2;
            if (mBoard[i] == PLAYER_TWO && 
                mBoard[i+1] == PLAYER_TWO && 
                mBoard[i+2] == PLAYER_TWO)
                return 3;
        }
        for (int i = 0; i <=2; i++)
        {
            if (mBoard[i] == PLAYER_ONE &&
                mBoard[i+3] == PLAYER_ONE &&
                mBoard[i+6] == PLAYER_ONE)
                return 2;
            if (mBoard[i] == PLAYER_TWO &&
            mBoard[i+3] == PLAYER_TWO &&
            mBoard[i+6] == PLAYER_TWO)
                return 3;

        }
        if((mBoard[0] == PLAYER_ONE &&
                mBoard[4] == PLAYER_ONE &&
                mBoard[8] == PLAYER_ONE)|| 
                (mBoard[2] == PLAYER_ONE &&
                mBoard[4] == PLAYER_ONE &&
                mBoard[6] == PLAYER_ONE))
        return 2;
    if((mBoard[0] == PLAYER_TWO &&
            mBoard[4] == PLAYER_TWO &&
            mBoard[8] == PLAYER_TWO)|| 
            (mBoard[2] == PLAYER_TWO &&
            mBoard[4] == PLAYER_TWO &&
            mBoard[6] == PLAYER_TWO))
        return 3;


    for (int i = 0; i < getBOARD_SIZE(); i++)
    {
        if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO)
            return 0;
    }
    return 1;
    }
}

0 个答案:

没有答案