Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tic Tac Toe如何告诉用户没有赢家?_Java_Android_Tic Tac Toe - Fatal编程技术网

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

Java Tic Tac Toe如何告诉用户没有赢家?,java,android,tic-tac-toe,Java,Android,Tic Tac Toe,我在这个项目中工作了3天,如果你能帮助我,我不知道我哪里做错了。我真的很感谢你的帮助。我正在尝试创建一个井字游戏。当我运行游戏时,如果玩家X或O赢了,会弹出一条消息说X或O赢了。但是如果非用户a赢了,会弹出一条消息说没有赢家。“赢家”的弹出窗口工作得很好,但“无人赢家”的弹出窗口不工作。如果你能帮我修复它,你真的让我开心 public class MainActivity extends AppCompatActivity { MediaPlayer mediaPlayer; int acti

我在这个项目中工作了3天,如果你能帮助我,我不知道我哪里做错了。我真的很感谢你的帮助。我正在尝试创建一个井字游戏。当我运行游戏时,如果玩家X或O赢了,会弹出一条消息说X或O赢了。但是如果非用户a赢了,会弹出一条消息说没有赢家。“赢家”的弹出窗口工作得很好,但“无人赢家”的弹出窗口不工作。如果你能帮我修复它,你真的让我开心

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

}
public类MainActivity扩展了AppCompatActivity{
媒体播放器;
int activePlayer=0;//对于x播放器
int[]gameState={2,2,2,2,2,2,2};//2表示未播放。
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};
布尔gameover=false;
公共void游戏逻辑(视图){
ImageView tappedView=(ImageView)视图;
int-tappedLocation=Integer.parseInt(view.getTag().toString());
如果(游戏状态[tappedLocation]==2&&!游戏结束){
游戏状态[tappedLocation]=活动玩家;
tappedView.setTranslationY(-3000f);
如果(activePlayer==0){
tappedView.setImageResource(R.drawable.x);
activePlayer=1;
}else if(activePlayer==1){
tappedView.setImageResource(R.drawable.o);
activePlayer=0;
}
tappedView.animate().translationBy(3000f).setDuration(500);
}
字符串mesg=“”;
对于(int[]WinningPosition:winningLocation){
如果(游戏状态[winningPostion[0]]==游戏状态[winningPostion[1]]
&&游戏状态[winningPostion[1]]==游戏状态[winningPostion[2]]
&&游戏状态[winningPostion[0]!=2){
如果(activePlayer==0)
mesg=“O是赢家!”;
如果(activePlayer==1)
mesg=“X是赢家!”;
其他的
gameover=true;
mesg=“没有赢家”;
LinearLayout winnerLayout=(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
TextView Winnermasg=(TextView)findViewById(R.id.editText);
winnermesg.setText(mesg);
gameover=true;
}
}
}
//一种让玩家再次玩的方法
再次播放公共void(视图){
LinearLayout winnerLayout=(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(视图.不可见);
gameover=false;
activePlayer=0;
对于(inti=0;i
创建一个用于检查阵列的函数:

int[] gameState ={2,2,2,2,2,2,2,2,2};
如果所有的结果都与2不同,并且没有一个玩家赢了,这意味着是平局

编辑:

使用此功能,它将告诉您的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()
会检查播放的相同符号行

没有赢家意味着没有一行相同的符号,但事实上,董事会是满的。您可以在每次播放符号时不计算格子(数组中的计数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
}
将forloop更改为以下一个。。。。

对于(int i=0;iactivePlayer永远不能与0或1不同,你必须换一种方式来思考。说
这是平局!
谢谢你花时间回答我的问题。还有什么其他方式,兄弟。如果你能再给我解释一下的话plz@xenteros几乎是红旗你,因为我笑哈哈-我说几乎,你的答案是完美的问题…苛刻-现在,真正的问题是,您使用**activePlayer**来确定赢家,所以,您应该像代码中的“如果所有框都选中了,但没有线,那么它就是一个平局”——无法告诉您具体的结果,但我希望这个解释能给您一个答案
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);
            }

        }
    }