GAME-SOS棋盘游戏,SOS序列和分数更新无效

时间:2018-03-13 17:40:51

标签: java android

在任何SOS序列之后,我的代码无法正常工作。自动放S和我不能放任何O字符和分数不合理地更新。有什么想法解决我的问题? 你可以看到我的一部分代码。

public void onClick (View view) {

        if (player1turn) {
            ((Button) view).setText("S");
            view.setEnabled( false );

        } else {
            ((Button) view).setText("O");
            view.setEnabled( false );

        }

        roundcount++;
        checkForWin();
    if(checkForWin()) {
        if (player1turn) {
            player1Wins();

        } else {
            player2Wins();

        }
    }else if (roundcount == 49){
        draw();
    }else {
        player1turn=!player1turn;

    }

}
private boolean checkForWin(){         
        String[][] field = new String[7][7];    
        for (int i = 0; i < 7; i++){
            for (int j = 0; j < 7; j++){
                field[i][j]=buttons[i][j].getText().toString();
            }
        }
    //Comparing the Fields(Columns)

    for (int i = 0; i < 7; i++){

            if (field[i][0].contains(("S")) && field[i][1].contains("O")
                    && field[i][2].contains("S")
                    && !field[i][0].equals(""))
            {
                roundcount+=1;
                player1score.setText( ""+roundcount );
                return true;
            }

Start Game

1st SOS Sequence and Score Update

Automatically puts S and automatically score update when click any button

1 个答案:

答案 0 :(得分:0)

if (field[i][0].equals("S") && field[i][1].equals("O") && field[i][2].equals("S")) { roundcount+=1; player1score.setText( ""+roundcount ); return true; }

尝试这样......

for (int i = 0; i < 7; i++){ for (int j = 0; j < 7; j++){ if (field[i][j].equals("S") && field[i][j].equals("O") && field[i][j].equals("S")) { roundcount+=1; player1score.setText( ""+roundcount ); return true; }} }