Android“新游戏按钮”TicTacToe游戏

时间:2014-01-08 21:09:24

标签: java android button

我正在尝试使用“开始新游戏”(Joc nou)按钮,但是当我按下此按钮时,它无法正常工作。通常,它会启动一个新游戏,但它也会在表的第一个位置放置一个“X”(mBoardsButtons [0])

      package xsi0.cristi.ro;

      import android.os.Bundle;
      import android.app.Activity;
      import android.graphics.Color;
      //import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.widget.TextView;
      //import android.view.*;
      public class MainActivity extends Activity {

private XSI0 mGame;

private Button[] mBoardsButtons;
private Button  jocnou;

private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;

private int mHumanCounter=0;
private int mTieCounter=0;
private int mAndroidCounter=0;

private boolean mHumanFirst=true;
private boolean mGameOver=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mGame=new XSI0();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //cod nou
    jocnou=new Button(this);
    //cod nou
            jocnou =(Button)findViewById(R.id.JocNew);

    mBoardsButtons = new Button[mGame.getMarimeTabla()];
    mBoardsButtons[0]= (Button)findViewById(R.id.unu);
    mBoardsButtons[1]= (Button)findViewById(R.id.doi);
    mBoardsButtons[2]= (Button)findViewById(R.id.trei);
    mBoardsButtons[3]= (Button)findViewById(R.id.patru);
    mBoardsButtons[4]= (Button)findViewById(R.id.cinci);
    mBoardsButtons[5]= (Button)findViewById(R.id.sase);
    mBoardsButtons[6]= (Button)findViewById(R.id.sapte);
    mBoardsButtons[7]= (Button)findViewById(R.id.opt);
    mBoardsButtons[8]= (Button)findViewById(R.id.noua);

    mInfoTextView=(TextView)findViewById(R.id.Info);
    mHumanCount=(TextView)findViewById(R.id.uwins);
    mTieCount=(TextView)findViewById(R.id.Drawscount);
    mAndroidCount=(TextView)findViewById(R.id.defeatscount);

    mHumanCount.setText(Integer.toString(mHumanCounter));
    mTieCount.setText(Integer.toString(mTieCounter));
    mAndroidCount.setText(Integer.toString(mAndroidCounter));

    //mGame=new XSI0();
    jocnou.setText("Joc nou");
    jocnou.setEnabled(true);
    jocnou.setOnClickListener(new ButtonClickListener());

    incepeJocNou();
}

private void incepeJocNou(){
    //mGameOver=false;
    mGame.curataTabla();

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

    /*//codnou

    jocnou.setText("Joc nou");
    jocnou.setEnabled(true);
    jocnou.setOnClickListener(new ButtonClickListener());
    */

    if(mHumanFirst){
        mInfoTextView.setText(R.string.Utilizatorul_primul);
        mHumanFirst=false;
    }
    else{
        mInfoTextView.setText(R.string.mutare_android);
        int move=mGame.getMiscareComputer();
        setMove(mGame.Android,move);
        mHumanFirst=true;
    }
}

private class ButtonClickListener implements View.OnClickListener{
    int location;

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

    }
    @Override
    public void onClick(View view) {

        //cod nou
        if(jocnou.isPressed()){

            mGameOver=true;

            incepeJocNou();
            //mBoardsButtons[0].setText("");

            mGameOver=false;
            }

        if(!mGameOver){
            if(mBoardsButtons[location].isEnabled()){
                setMove(mGame.User,location);

                int winner=mGame.verificaCastigator();

                if(winner==0)
                {
                        mInfoTextView.setText(R.string.mutare_android);
                    int move=mGame.getMiscareComputer();
                    setMove(mGame.Android,move);
                    winner=mGame.verificaCastigator();
                }

                if(winner==0)
                    mInfoTextView.setText(R.string.mutare_utilizator);
                else if(winner==1){
                    mInfoTextView.setText(R.string.rezultat_egal);
                    mTieCounter++;
                    //mTieCounter.setText(Integer.toString(mTieCounter));

                    mGameOver=true;
                }
                else if(winner==2){
                    mInfoTextView.setText(R.string.utilizator_castiga);
                    mHumanCounter++;
                    //mHumanCounter.setText(Integer.toString(mHumanCounter));
                    mGameOver=true;
                }

                else{
                    mInfoTextView.setText(R.string.android_castiga);
                    mAndroidCounter++;
                    //mAndroidCounter.setText(Integer.toString(mAndroidCounter));
                    mGameOver=true;
            }
        }

    }
}

/*public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}*/

    }
private void setMove(char player,int location){
    mGame.setMiscare(player, location);

    mBoardsButtons[location].setEnabled(false);
    mBoardsButtons[location].setText(String.valueOf(player));
    if(player==mGame.User){
        mBoardsButtons[location].setTextColor(Color.BLUE);
    }
        else
            mBoardsButtons[location].setTextColor(Color.RED);

}
}

1 个答案:

答案 0 :(得分:0)

两种可能性:

1)curataTabla()有什么作用?我没有看到这种方法 - 你可能在这段代码中有一个错误导致该位置保持设置。

2)你确定人类第一被选中了吗?计算机也可能总是先行,而且每次都恰好选择这个位置。

没有更多代码就很难调试。

另外,请检查此代码:

 //cod nou
    if(jocnou.isPressed()){

        mGameOver=true;

        incepeJocNou();
        //mBoardsButtons[0].setText("");

        mGameOver=false;
        }

    if(!mGameOver){

每次选择新游戏时,都会将mGameover设置为true,创建新游戏,然后将其设置为false。这意味着每次你做一个新的游戏,你实际上是在做一个新的游戏和一个移动,而不仅仅是一个新的游戏。我不确定这是bug来自哪里,但它可能会导致意外行为。

我认为您应该尝试将New Game onClickListener和普通的ClickListernes分成两个独立的侦听器,看看是否能解决这个问题。我猜你是因为你是一个双重目的的听众而无意中的行为。