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

时间:2017-06-08 12:15:36

标签: java android tic-tac-toe

我在这个项目工作了3天,如果你可以帮助我,我无法弄清楚我在哪里做错了我非常感谢你的帮助。我正在努力创造一个tic tac toe game.When我正在运行游戏,如果玩家X或O赢了一条消息,会弹出说X或O获胜。但是如果非用户赢了一条消息,则会弹出并说没有赢家。赢家的弹出窗口工作得很好,但没有人赢的弹出不起作用。如果你可以帮助我解决它,你真的在​​为我的一天做好准备。

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);
}

}

5 个答案:

答案 0 :(得分:1)

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

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

如果所有人都与2不同,并且没有一名球员获胜,则意味着它的平局。

编辑:

使用此功能,可以判断您的arr是否包含item:

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);
}

答案 1 :(得分:0)

用大括号{}

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

答案 2 :(得分:0)

可能因为这段代码而无法正常工作

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 ";
}

答案 3 :(得分:0)

目前,您不会跟踪任何获胜者,因为您的if()会检查已播放的相同符号行。

没有赢家意味着没有相同符号的行,但董事会已满。您可以计算非格子(在数组中计数2)或每次播放符号时递增计数器。一旦你格子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
}

答案 4 :(得分:0)

$page++