标签云

微信群

扫码加入我们

WeChat QR Code

I have a tic tac toe application which is only one little step away from being perfect. Basically the game works perfect during game play, the play makes a move by placing an X, then the computer makes a move a couple of seconds earlier placing their O and then it's back to the players move and then after their move a couple of seconds later the computer moves and etc.

The only issue I have which I cannot seem to figure out is when the game board resets. Basically when the winning move is made, we do not see the winning move, the board is reset, toast message is displayed stating who has won and if it is computers move first then their first move is made and this all happens at the same time. For the user they will be like what is going on how was the game won as it all happened in a flash.

I need to make a slight adjust so that the following happens:

1: Player sees the winning move either if it's their move or computers

2: After seeing the winning move then the game board resets and the toast message appears stating who won or a draw

I don't mind the computer already making the first move already after the game board has reset, but the other two points I believe needs to be addressed.

Below is my code but I cannot seem to figure out how to ensure the above two points are reached when trying to manipulate the code:

public class MainActivityPlayer1 extends AppCompatActivity implements View.OnClickListener {

    private Button[][] buttons = new Button[3][3];

    private boolean playerOneMove = true;

    private int turnsCount;

    private int playerOnePoints;
    private int playerTwoPoints;

    private TextView textViewPlayerOne;
    private TextView textViewPlayerTwo;
    private TextView textViewPlayerOneTurn;
    private TextView textViewPlayerTwoTurn;
    private TextView textViewFooterTitle;

    Button selectButton;

    Random random = new Random();

    boolean firstComputerMove = false;

    int playerX = Color.parseColor("#e8e5e5");
    int playerO = Color.parseColor("#737374");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_player1);

        Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/ConcertOne-Regular.ttf");

        textViewPlayerOne = findViewById(R.id.textView_player1);
        textViewPlayerTwo = findViewById(R.id.textView_player2);
        textViewPlayerOneTurn = findViewById(R.id.textView_player1Turn);
        textViewPlayerTwoTurn = findViewById(R.id.textView_player2Turn);
        textViewFooterTitle = findViewById(R.id.footer_title);

        textViewPlayerOne.setTypeface(customFont);
        textViewPlayerTwo.setTypeface(customFont);
        textViewFooterTitle.setTypeface(customFont);


        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                String buttonID = "button_" + i + j;
                int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                buttons[i][j] = findViewById(resID);
                buttons[i][j].setOnClickListener(this);

                if (savedInstanceState != null) {
                    String btnState = savedInstanceState.getCharSequence(buttonID).toString();
                    if (btnState.equals("X")) {
                        buttons[i][j].setTextColor(playerX);
                    } else {
                        buttons[i][j].setTextColor(playerO);
                    }

                }

            }
        }

        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setTypeface(customFont);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetGame();
            }
        });

        Button buttonExit = findViewById(R.id.button_exit);
        buttonExit.setTypeface(customFont);
        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backToMainActivity();
            }
        });

    }

    @Override
    public void onClick(View v) {

        if (!((Button) v).getText().toString().equals("")) {
            return;
        }

        turnsCount++;

        if (playerOneMove) {
            ((Button) v).setText("X");
            ((Button) v).setTextColor(playerX);
            isGameOver();
        }

    }

    public void isGameOver() {

        if (checkGameIsWon()) {
            if (playerOneMove) {
                player1Wins();
            } else {
                player2Wins();
            }
        } else if (turnsCount == 9) {
            draw();
        } else {
            playerOneMove = !playerOneMove;
            if (!playerOneMove) {
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        computerMove();
                    }
                }, 1000);
            }
        }
    }

    private void computerMove() {
        String[][] field = new String[3][3];
        List<Button> emptyButtons = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
                if (field[i][j].equals("")) {
                    emptyButtons.add(buttons[i][j]);
                }
            }
        }

        //IS COMPUTER GOING FIRST

        if (firstComputerMove == true){
            selectButton = emptyButtons.get(random.nextInt(emptyButtons.size()));
        }
        else {

            //TAKE MIDDLE SQUARE IF NOT TAKEN

            if (field[1][1].equals("")) {
                selectButton = buttons[1][1];
            }

            //FIRST ROW ACROSS

            else if (field[0][0] != "" && field[0][0].equals(field[0][1])
                    && field[0][2].equals("")) {
                selectButton = buttons[0][2];
            } else if (field[0][0] != "" && field[0][0].equals(field[0][2])
                    && field[0][1].equals("")) {
                selectButton = buttons[0][1];
            } else if (field[0][1] != "" && field[0][1].equals(field[0][2])
                    && field[0][0].equals("")) {
                selectButton = buttons[0][0];
            }

            //SECOND ROW ACROSS

            else if (field[1][0] != "" && field[1][0].equals(field[1][1])
                    && field[1][2].equals("")) {
                selectButton = buttons[1][2];
            } else if (field[1][0] != "" && field[1][0].equals(field[1][2])
                    && field[1][1].equals("")) {
                selectButton = buttons[1][1];
            } else if (field[1][1] != "" && field[1][1].equals(field[1][2])
                    && field[1][0].equals("")) {
                selectButton = buttons[1][0];
            }

            //THIRD ROW ACROSS

            else if (field[2][0] != "" && field[2][0].equals(field[2][1])
                    && field[2][2].equals("")) {
                selectButton = buttons[2][2];
            } else if (field[2][0] != "" && field[2][0].equals(field[2][2])
                    && field[2][1].equals("")) {
                selectButton = buttons[2][1];
            } else if (field[2][1] != "" && field[2][1].equals(field[2][2])
                    && field[2][0].equals("")) {
                selectButton = buttons[2][0];
            }

            //FIRST COLUMN DOWN

            else if (field[0][2] != "" && field[0][2].equals(field[1][2])
                    && field[2][2].equals("")) {
                selectButton = buttons[2][2];
            } else if (field[0][2] != "" && field[0][2].equals(field[2][2])
                    && field[1][2].equals("")) {
                selectButton = buttons[1][2];
            } else if (field[1][2] != "" && field[1][2].equals(field[2][2])
                    && field[0][2].equals("")) {
                selectButton = buttons[0][2];
            }

            //SECOND COLUMN DOWN

            else if (field[0][1] != "" && field[0][1].equals(field[1][1])
                    && field[2][1].equals("")) {
                selectButton = buttons[2][1];
            } else if (field[0][1] != "" && field[0][1].equals(field[2][1])
                    && field[1][1].equals("")) {
                selectButton = buttons[1][1];
            } else if (field[1][1] != "" && field[1][1].equals(field[2][1])
                    && field[0][1].equals("")) {
                selectButton = buttons[0][1];
            }

            //THIRD COLUMN DOWN

            else if (field[0][0] != "" && field[0][0].equals(field[1][0])
                    && field[2][0].equals("")) {
                selectButton = buttons[2][0];
            } else if (field[0][0] != "" && field[0][0].equals(field[2][0])
                    && field[1][0].equals("")) {
                selectButton = buttons[1][0];
            } else if (field[2][0] != "" && field[2][0].equals(field[1][0])
                    && field[0][0].equals("")) {
                selectButton = buttons[0][0];
            }

            //DIAGAONAL TOP RIGHT BOTTOM LEFT

            else if (field[2][0] != "" && field[2][0].equals(field[0][2])
                    && field[1][1].equals("")) {
                selectButton = buttons[1][1];
            } else if (field[2][0] != "" && field[2][0].equals(field[1][1])
                    && field[0][2].equals("")) {
                selectButton = buttons[0][2];
            } else if (field[1][1] != "" && field[1][1].equals(field[0][2])
                    && field[2][0].equals("")) {
                selectButton = buttons[2][0];
            }

            //DIAGAONAL TOP LEFT BOTTOM RIGHT

            else if (field[0][0] != "" && field[0][0].equals(field[2][2])
                    && field[1][1].equals("")) {
                selectButton = buttons[1][1];
            } else if (field[0][0] != "" && field[0][0].equals(field[1][1])
                    && field[2][2].equals("")) {
                selectButton = buttons[2][2];
            } else if (field[1][1] != "" && field[1][1].equals(field[2][2])
                    && field[0][0].equals("")) {
                selectButton = buttons[0][0];
            } else {
                selectButton = emptyButtons.get(random.nextInt(emptyButtons.size()));
            }
        }
        selectButton.setText("O");
        selectButton.setTextColor(playerO);
        firstComputerMove = false;
        turnsCount++;
        isGameOver();

    }

    private boolean checkGameIsWon() {
        String[][] field = new String[3][3];

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
            }
        }

        for (int i = 0; i < 3; i++) {
            if (field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && !field[i][0].equals("")) {
                return true;
            }
        }

        for (int i = 0; i < 3; i++) {
            if (field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && !field[0][i].equals("")) {
                return true;
            }
        }

        if (field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && !field[0][0].equals("")) {
            return true;
        }

        if (field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && !field[0][2].equals("")) {
            return true;
        }

        return false;
    }

    private void player1Wins() {
        playerOnePoints++;
        Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void player2Wins() {
        playerTwoPoints++;
        Toast.makeText(this, "Computer wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
        firstComputerMove = true;
        computerMove();
    }

    private void draw() {
        Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
        resetBoard();
        playerOneMove = !playerOneMove;
        switchPlayerTurn();
        if (!playerOneMove){
            firstComputerMove = true;
            computerMove();
        }
    }

    @SuppressLint("SetTextI18n")
    private void updatePointsText() {
        textViewPlayerOne.setText("PLAYER 1:  " + playerOnePoints + " ");
        textViewPlayerTwo.setText("COMPUTER: " + playerTwoPoints + " ");
    }

    private void resetBoard() {

                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        buttons[i][j].setText("");
                    }
                }

                turnsCount = 0;
                switchPlayerTurn();
    }


    private void resetGame() {
        playerOnePoints = 0;
        playerTwoPoints = 0;
        turnsCount = 0;
        playerOneMove = true;
        updatePointsText();
        resetBoard();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("turnsCount", turnsCount);
        outState.putInt("playerOnePoints", playerOnePoints);
        outState.putInt("playerTwoPoints", playerTwoPoints);
        outState.putBoolean("playerOneMove", playerOneMove);
        switchPlayerTurn();

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                String buttonID = "button_" + i + j;
                Button btn = buttons[i][j];
                outState.putCharSequence(buttonID, btn.getText());
            }
        }

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) { ;
        super.onRestoreInstanceState(savedInstanceState);

        turnsCount = savedInstanceState.getInt("turnsCount");
        playerOnePoints = savedInstanceState.getInt("playerOnePoints");
        playerTwoPoints = savedInstanceState.getInt("playerTwoPoints");
        playerOneMove = savedInstanceState.getBoolean("playerOneMove");
        switchPlayerTurn();

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                String buttonID = "button_" + i + j;
                Button btn = buttons[i][j];
                savedInstanceState.putCharSequence(buttonID, btn.getText());

            }
        }

    }

    private void switchPlayerTurn(){
        if (playerOneMove){
            textViewPlayerOneTurn.setVisibility(View.VISIBLE);
            textViewPlayerTwoTurn.setVisibility(View.INVISIBLE);
        }
        else{
            textViewPlayerOneTurn.setVisibility(View.INVISIBLE);
            textViewPlayerTwoTurn.setVisibility(View.VISIBLE);
        }

    }

    private void backToMainActivity(){
        Intent intentMainActivity = new Intent(this, MainActivity.class);
        startActivity(intentMainActivity);
    }

}


I like the idea of using thread.sleep, however I noticed when I place it in the location you mentioned, it still doesn't show the last computer move and then reset board. It does the same thing of wiping the board without seeing the last move with the only difference being it takes a second longer until board is clear

2018年06月19日26分10秒

Can you see where to place this because I;m struggling picking out where exactly it should go

2018年06月19日26分10秒

I'm not seeing it either. I would try to close in on the problem with Log statements until you discover where exactly execution stops. I would work towards the final move's Button setText(), checking for both winning scenarios.

1970年01月01日00分03秒