Tic Tac Toe: My First Running App (Part 2)

Tic Tac Toe: My First Running App (Part 2)

Continuing from where we left off in my last article Tic Tac Toe: My First Running App (Part 1) you can check it out here Tic Tac Toe: My First Running App (Part 1) we stopped at the creation of a boolean Method called checkForWin this method detects a win by scanning through all possible wins in a tic tac toe game, so today we kick off from there.

After a win is detected, it should be credited a player innit? so the player1Wins, player2Wins and draw methods were created to credit a win to the right player, record their points and also detect a draw.

    private void player1Wins(){
        player1Points++;
        Toast.makeText(this, "Player One Na You Win O!!!",
                Toast.LENGTH_LONG).show();
        UpdatePointsText();
        resetBoard();
    }
    private void player2Wins(){
        player2Points++;
        Toast.makeText(this, "Player Two Na You Win !!!",
                Toast.LENGTH_LONG).show();
        UpdatePointsText();
        resetBoard();
    }
 private void draw(){
        Toast.makeText(this, "Una no sabi joor na draw una play!",
                Toast.LENGTH_SHORT).show();
        resetBoard();
    }

Gave the toast message a little Nigerian feel, you saw it right? it's called pidgin English(Just joking around don't mind me), so the updatePointsText method was created to update the player's points in their respective text views.

 private void UpdatePointsText(){
        textViewPlayer1.setText("Player 1 :" + player1Points);
        textViewPlayer2.setText("Player 2 :" + player2Points);
    }

The reset board Method resets the buttons to an empty string after a win or draw has been detected, round count is reset to zero as it has to be equal to 9 to be able to detect a win or draw, and the boolean playerOneTurn becomes true to enable the game start afresh, the reset game method resets both players points and their respective text views to zero as well as the board.

 private void resetBoard(){
        for(int i =0; i <3; i++){
            for(int j=0; j<3; j++){
                buttons[i][j].setText("");
            }
        }
        roundCount = 0;
        playerOneTurn = true;
    }
    private void resetGame(){
        player1Points = 0;
        player2Points = 0;
        UpdatePointsText();
        resetBoard();
    }

In case of a change in orientation of the device when the app is in use the app could be restarting a game in play if we don't properly save data, to avert this we implemented the onSaveInstanceState and onRestoreInstanceState method to save round count, player points and player turn respectively.

 @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("roundCount", roundCount);
        outState.putInt("player1Points", player1Points);
        outState.putInt("player2Points", player2Points);
        outState.putBoolean("player1Turn", playerOneTurn);
    }
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        roundCount = savedInstanceState.getInt("roundCount");
        player1Points = savedInstanceState.getInt("player1Points");
        player2Points = savedInstanceState.getInt("player2Points");
        playerOneTurn =savedInstanceState.getBoolean("player1Turn");
    }

Also in the XML file of the app, the freeze text property was set to true for each button on the screen( where the X and O are being displayed) so in case of the orientation of the screen, it retains the text already on it.

 <TextView
            android:id="@+id/text_p1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_1_0"
            android:textSize="30sp"
            android:freezesText="true"/>
        <TextView
            android:id="@+id/text_p2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_p1"
            android:text="@string/player_2_0"
            android:textSize="30sp"
            android:freezesText="true" />

And it's a wrap people, thank you for walking this journey of my first app with me I hope you learnt something?.