Tic Tac Toe 如何告诉用户没有赢家?

Tic Tac Toe how to tell user there is no winner?

我在这个项目中工作了 3 天,如果你能帮助我,我不知道我哪里做错了我真的很感谢你 help.I 正在尝试创建一个 tic tac脚趾游戏。当我 运行 游戏时,如果玩家 X 或 O 获胜,将弹出一条消息说 X 或 O 获胜。但如果非用户获胜,则应弹出一条消息并说有没有赢家。获胜者的弹出窗口运行良好,但没有人获胜的弹出窗口没有 work.if 你可以帮我解决这个问题,你真的很开心。

public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;

int activePlayer = 0; // for x player

int[] gameState ={2,2,2,2,2,2,2,2,2}; // 2 means unplayed.

int[][] winningLocation ={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
boolean gameover =false;

public void gameLogic(View view){

    ImageView tappedView =(ImageView) view;

    int tappedLocation = Integer.parseInt(view.getTag().toString());

    if(gameState[tappedLocation]==2 && !gameover) {
        gameState[tappedLocation]=activePlayer;

        tappedView.setTranslationY(-3000f);

        if (activePlayer == 0) {

            tappedView.setImageResource(R.drawable.x);

            activePlayer = 1;

        } else if (activePlayer == 1) {
            tappedView.setImageResource(R.drawable.o);
            activePlayer = 0;
        }
        tappedView.animate().translationYBy(3000f).setDuration(500);
    }
  String mesg ="";

    for(int[]winningPostion :winningLocation){

        if(gameState[winningPostion[0]] == gameState [winningPostion[1]]
                && gameState[winningPostion[1]]== gameState [winningPostion[2]]
                && gameState[winningPostion[0]]!=2){

            if (activePlayer ==0)

                mesg = "O is the winner!";

            if(activePlayer==1)

                mesg = "X is the winner!";

            else
                gameover=true;
            mesg="there is no winner ";


            LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
            winnerLayout.setVisibility(View .VISIBLE);

            TextView winnermesg = (TextView) findViewById(R.id.editText);
            winnermesg.setText(mesg);

            gameover=true;
        }
    }




}

// a method that let the players play again

public void playagain(View view){
    LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
    winnerLayout.setVisibility(View.INVISIBLE);
    gameover=false;
    activePlayer=0;

    for (int i =0; i < gameState.length;i++)
        gameState[i]=2;

    GridLayout gridlayout = (GridLayout) findViewById(R.id.gridlayout);
    for(int i =0 ; i < gridlayout.getChildCount(); i++)
        ((ImageView)gridlayout.getChildAt(i)).setImageResource(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 // to play the song
      mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);
      mediaPlayer.start();


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // to show and hide the playing again button

   LinearLayout winnerLayout = ( LinearLayout) findViewById(R.id.winnerLayout);
    winnerLayout.setVisibility(View.INVISIBLE);

   FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

// pause and play the song when the user leaving annd returning the game
@Override
protected void onPause(){
    super.onPause();
    mediaPlayer.stop();
    mediaPlayer.release();
}




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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

创建一个检查数组的函数:

int[] gameState ={2,2,2,2,2,2,2,2,2};

如果全部不等于2,且none的玩家获胜,则平局。

编辑:

使用这个函数,它会判断你的 arr 是否包含项目:

public static boolean my_contains(int[] arr, int item) {
    for (int n : arr) {
        if (item == n) {
            return true;
        }
    }
    return false;
}

然后做:

//Check winning position
for(int[]winningPostion :winningLocation){
    //If there is a winning position
    if(gameState[winningPostion[0]] == gameState [winningPostion[1]]
            && gameState[winningPostion[1]]== gameState[winningPostion[2]]
            && gameState[winningPostion[0]]!=2){
        //Look for the winner
        if (activePlayer ==0)
            mesg = "O is the winner!";

        if(activePlayer==1)
            mesg = "X is the winner!";

        LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
        winnerLayout.setVisibility(View .VISIBLE);

        TextView winnermesg = (TextView) findViewById(R.id.editText);
        winnermesg.setText(mesg);

        gameover=true;
    }
}
//Here, all winning position have been checked, and gameover is still false
//Check if all X and O have been placed
if(!my_contains(gameSate, 2) && !gameover){
    //If so, and gameover is false, then its a tie.
    gameover=true;
    mesg="there is no winner ";

    //README : this may be the wrong layout, its up to you to change it to the good one, but it should pop your message
    LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
    winnerLayout.setVisibility(View .VISIBLE);

    TextView winnermesg = (TextView) findViewById(R.id.editText);
    winnermesg.setText(mesg);
}

用大括号 { } 将块括起来

else {
    gameover=true;
    mesg="there is no winner ";
}

可能是因为这段代码没用

if (activePlayer ==0)

    mesg = "O is the winner!";

if(activePlayer==1)

    mesg = "X is the winner!";

else
    gameover=true;
mesg="there is no winner ";

尝试使用花括号,这是最好的做法。像这样:

if (activePlayer ==0){

    mesg = "O is the winner!";

}else if(activePlayer==1){

    mesg = "X is the winner!";

}else{
    gameover=true;
    mesg="there is no winner ";
}

目前您不会跟踪没有赢家,因为您的 if() 检查一行相同的符号。

没有赢家意味着没有一行相同的符号,而是棋盘已满。您可以计算非格子(在数组中计算 2s)或在每次播放符号时增加一个计数器。一旦你点了9但没有中奖线,那么就是平局。

if(gameState[winningPostion[0]] == gameState[winningPostion[1]]
                && gameState[winningPostion[1]] == gameState[winningPostion[2]]
                && gameState[winningPostion[0]] !=2 ) {
    //somebody has scored a line, but who?
} else if (haveWePlayed9()) {
    //no winner
}
change the forloop with below one....

for(int i=0;i<winningLocation.length;i++){
        Boolean istie = true;
        int []winningPostion = winningLocation[i];
        mesg="there is no winner ";

        if(gameState[winningPostion[0]] == gameState [winningPostion[1]]
                && gameState[winningPostion[1]]== gameState [winningPostion[2]]
                && gameState[winningPostion[0]]!=2){
            mesg="there is no winner ";
            if (activePlayer ==0)
                mesg = "O is the winner!";

            if(activePlayer==1)
                mesg = "X is the winner!";

            LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
            winnerLayout.setVisibility(View .VISIBLE);

            TextView winnermesg = (TextView) findViewById(R.id.editText);
            winnermesg.setText(mesg);
            istie = false;
            gameover=true;
             return;
        }
        if(i==winningLocation.length-1){
            if(istie){
                LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
                winnerLayout.setVisibility(View .VISIBLE);

                TextView winnermesg = (TextView) findViewById(R.id.editText);
                winnermesg.setText(mesg);
            }

        }
    }