com.brandao.tictactoe.board.BoardFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.brandao.tictactoe.board.BoardFragment.java

Source

/*
 * Copyright (c) 2016 Skytrait
 *
 * Licensed under the Apache License, Version 2.0 (the License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * www.skytrait.com
 *
 */

package com.brandao.tictactoe.board;

import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.brandao.tictactoe.R;
import com.brandao.tictactoe.endgame.StatisticsDatabase;
import com.brandao.tictactoe.extras.AI;
import com.brandao.tictactoe.extras.C;
import com.brandao.tictactoe.extras.Feedback;
import com.brandao.tictactoe.settings.Settings;

import java.util.Random;

public class BoardFragment extends Fragment implements OnClickListener {
    public final String TAG = "BoardFragment";

    private final String SAVED_STATE_PLAYER_ONE_WINS = "player_one_wins";
    private final String SAVED_STATE_PLAYER_TWO_WINS = "player_two_wins";
    private final String SAVED_STATE_TOTAL_TIES = "player_total_ties";
    private final String SAVED_STATE_START_TIME = "start_time";
    private final String SAVED_STATE_LAST_GAME_WINNER = "last_game_winner";
    private final String SAVED_STATE_LAST_MOVE_INDEX = "last_move_index";
    private final String SAVED_STATE_BOARD = "board";
    private final String SAVED_STATE_GAME_STATE = "game_state";

    private MenuItem mMenuItemReplay;
    private MenuItem mMenuItemHint;

    private int mDifficulty;
    private int mGameType;
    private int mWhoGoesFirst;
    private int mGameState;

    private ImageView[] mScreens;
    private Board mBoard;

    private String mPlayerOneName;

    private String mPlayerTwoName;

    private int mPlayerOneWins;
    private int mPlayerTwoWins;
    private int mTotalTies;

    private ImageView mPlayerOneImage;
    private ImageView mPlayerTwoImage;
    private TextView mPlayerOneNameDisplay;
    private TextView mPlayerTwoNameDisplay;

    private boolean mAnimating;
    private long mStartTime;
    private long mGameTime;

    private int mLastGameWinner;
    private int mLastMoveIndex;

    private SharedPreferences mPrefs;

    public static BoardFragment newInstance() {
        BoardFragment f = new BoardFragment();

        return f;
    }

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

        setHasOptionsMenu(true);

        mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

        if (savedInstanceState == null) {
            mPlayerOneWins = 0;
            mPlayerTwoWins = 0;
            mTotalTies = 0;
        } else {
            mPlayerOneWins = savedInstanceState.getInt(SAVED_STATE_PLAYER_ONE_WINS);
            mPlayerTwoWins = savedInstanceState.getInt(SAVED_STATE_PLAYER_TWO_WINS);
            mTotalTies = savedInstanceState.getInt(SAVED_STATE_TOTAL_TIES);
        }

        mAnimating = false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.board_fragment, container, false);

        mPlayerOneImage = (ImageView) v.findViewById(R.id.player_one_image_display);
        mPlayerTwoImage = (ImageView) v.findViewById(R.id.player_two_image_display);

        mScreens = new ImageView[9];
        mScreens[0] = (ImageView) v.findViewById(R.id.screen_one);
        mScreens[1] = (ImageView) v.findViewById(R.id.screen_two);
        mScreens[2] = (ImageView) v.findViewById(R.id.screen_three);
        mScreens[3] = (ImageView) v.findViewById(R.id.screen_four);
        mScreens[4] = (ImageView) v.findViewById(R.id.screen_five);
        mScreens[5] = (ImageView) v.findViewById(R.id.screen_six);
        mScreens[6] = (ImageView) v.findViewById(R.id.screen_seven);
        mScreens[7] = (ImageView) v.findViewById(R.id.screen_eight);
        mScreens[8] = (ImageView) v.findViewById(R.id.screen_nine);

        mPlayerOneNameDisplay = (TextView) v.findViewById(R.id.player_one_name_display);
        mPlayerTwoNameDisplay = (TextView) v.findViewById(R.id.player_two_name_display);

        return v;
    }

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        for (ImageView screen : mScreens) {
            screen.setOnClickListener(this);
        }

        setTheme(v);

        restoreGameState(v);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState == null) {
            restartGame(false);
        } else {
            restartGame(savedInstanceState);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(SAVED_STATE_PLAYER_ONE_WINS, mPlayerOneWins);
        outState.putInt(SAVED_STATE_PLAYER_TWO_WINS, mPlayerTwoWins);
        outState.putInt(SAVED_STATE_TOTAL_TIES, mTotalTies);

        outState.putLong(SAVED_STATE_START_TIME, mStartTime);

        outState.putInt(SAVED_STATE_LAST_GAME_WINNER, mLastGameWinner);
        outState.putInt(SAVED_STATE_LAST_MOVE_INDEX, mLastMoveIndex);

        outState.putParcelable(SAVED_STATE_BOARD, mBoard);
        outState.putInt(SAVED_STATE_GAME_STATE, mGameState);

        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.board_fragment, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        mMenuItemReplay = menu.findItem(R.id.menu_item_replay);
        mMenuItemHint = menu.findItem(R.id.menu_item_hint);

        mMenuItemReplay.setEnabled(mLastMoveIndex != -1);
        mMenuItemHint.setEnabled(mGameState != C.STATE_GAME_OVER);

        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            NavUtils.navigateUpFromSameTask(getActivity());

            return true;
        }
        case R.id.menu_item_new_game: {
            if (!mAnimating && !isBoardClear()) {
                restartGame(mPrefs.getBoolean(Settings.PREF_ANIMATE, Settings.PREF_ANALYTICS_DEFAULT));
            }

            return true;
        }
        case R.id.menu_item_hint: {
            if (mGameState == C.STATE_GAME_OVER || mAnimating) {
                return true;
            }

            animateHintSquare(AI.getHintPosition(mBoard,
                    mGameState == C.STATE_PLAYER_ONE_TURN ? C.SYMBLE_PLAYER_ONE : C.SYMBLE_PLAYER_TWO));

            return true;
        }
        case R.id.menu_item_replay: {
            if (mLastMoveIndex != -1 && !mAnimating) {
                animateSingleSquare(R.anim.grow, R.anim.shrink, mLastMoveIndex);
            }

            return true;
        }
        }

        return super.onOptionsItemSelected(item);
    }

    public void restoreGameState(View v) {
        mGameType = getActivity().getIntent().getIntExtra(C.TAG_INTENT_GAME_TYPE, C.TYPE_ONE_PLAYER);
        mWhoGoesFirst = getActivity().getIntent().getIntExtra(C.TAG_INTENT_WHO_GOES_FIRST, C.X_O_RANDOM);

        if (mGameType == C.TYPE_TWO_PLAYER) {
            mDifficulty = C.DIFFICULTY_TWO_PLAYER;
        } else {
            mDifficulty = getActivity().getIntent().getIntExtra(C.TAG_INTENT_GAME_DIFFICULTY, C.DIFFICULTY_EASY);
        }

        mPlayerOneName = getActivity().getIntent().getStringExtra(C.TAG_INTENT_PLAYER_ONE_NAME);
        mPlayerTwoName = getActivity().getIntent().getStringExtra(C.TAG_INTENT_PLAYER_TWO_NAME);

        if (mPlayerOneName.length() > 7) {
            mPlayerOneName = mPlayerOneName.substring(0, 7);
        }
        if (mPlayerTwoName.length() > 8) {
            mPlayerTwoName = mPlayerTwoName.substring(0, 7);
        }

        String one = mPlayerOneName + ": " + mPlayerOneWins;
        String two = mPlayerTwoName + ": " + mPlayerTwoWins;
        mPlayerOneNameDisplay.setText(one);
        mPlayerTwoNameDisplay.setText(two);
    }

    public int generateAI() {
        switch (mDifficulty) {
        case C.DIFFICULTY_HARD:
            return AI.generateHardAI(mBoard);
        case C.DIFFICULTY_MEDIUM:
            return AI.generateMediumAI(mBoard);
        default:
            return AI.generateEasyAI(mBoard);
        }
    }

    @Override
    public void onClick(View v) {
        if (mAnimating) {
            return;
        }

        if (mGameState == C.STATE_GAME_OVER
                || mGameType == C.TYPE_ONE_PLAYER && mGameState == C.STATE_PLAYER_TWO_TURN) {
            return;
        }

        int index = -1;

        switch (v.getId()) {
        case R.id.screen_one:
            index = 0;
            break;
        case R.id.screen_two:
            index = 1;
            break;
        case R.id.screen_three:
            index = 2;
            break;
        case R.id.screen_four:
            index = 3;
            break;
        case R.id.screen_five:
            index = 4;
            break;
        case R.id.screen_six:
            index = 5;
            break;
        case R.id.screen_seven:
            index = 6;
            break;
        case R.id.screen_eight:
            index = 7;
            break;
        case R.id.screen_nine:
            index = 8;
            break;
        }

        handelNewMove(index);
    }

    public void handelNewMove(int index) {
        if (mBoard.moves[index].player != C.SYMBLE_NULL) {
            return;
        }

        int time = -1;

        for (Move move : mBoard.moves) {
            if (move.player != C.SYMBLE_NULL) {
                time++;
            }
        }

        mLastMoveIndex = index;

        int symble = -1;

        switch (mGameState) {
        case C.STATE_PLAYER_ONE_TURN:
            symble = C.SYMBLE_PLAYER_ONE;
            Feedback.feedback(getActivity(), R.raw.blu_dot_clicked);
            break;
        case C.STATE_PLAYER_TWO_TURN:
            symble = C.SYMBLE_PLAYER_TWO;
            Feedback.feedback(getActivity(), R.raw.button_clicked_sound);
            break;
        case C.STATE_GAME_OVER:
            break;
        }

        mBoard.moves[index].player = symble;
        mBoard.moves[index].time = time;

        if (symble == C.SYMBLE_PLAYER_ONE) {
            mScreens[index].setBackgroundResource(R.drawable.ic_green_x_android);
        } else {
            if (symble == C.SYMBLE_PLAYER_TWO) {
                mScreens[index].setBackgroundResource(R.drawable.ic_red_o_android);
            }
        }

        panelInAnimate(mScreens[index], symble);
    }

    public void finishMove(int symble) {
        boolean animate = mPrefs.getBoolean(Settings.PREF_ANIMATE, true);
        int[] winSpots = checkForWin(symble);
        final int tempSymble = symble;

        if (winSpots != null) {
            Animation animOut = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            animOut.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    mAnimating = true;
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mAnimating = false;

                    setWinner(tempSymble);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

            });

            if (animate) {
                for (int winSpot : winSpots) {
                    mScreens[winSpot].startAnimation(animOut);
                }
            } else {
                setWinner(tempSymble);
            }
        } else {
            if (checkForTie()) {
                setWinner(C.SYMBLE_NULL);
            } else {
                switch (mGameState) {
                case C.STATE_PLAYER_ONE_TURN: {
                    symble = C.SYMBLE_PLAYER_TWO;

                    break;
                }
                case C.STATE_PLAYER_TWO_TURN: {
                    symble = C.SYMBLE_PLAYER_ONE;

                    break;
                }
                }

                mMenuItemReplay.setEnabled(true);
                mMenuItemHint.setEnabled(true);

                nextTurn(symble);
            }
        }
    }

    public int[] checkForWin(int symble) {
        int[] winSpots = null;

        for (int i = 0; i < mBoard.moves.length; i += 3) {
            if (mBoard.moves[i].player == symble && mBoard.moves[i + 1].player == symble
                    && mBoard.moves[i + 2].player == symble) {
                winSpots = new int[] { i, i + 1, i + 2 };
            }
        }

        for (int i = 0; i < 3; i++) {
            if (mBoard.moves[i].player == symble && mBoard.moves[i + 3].player == symble
                    && mBoard.moves[i + 6].player == symble) {
                winSpots = new int[] { i, i + 3, i + 6 };
            }
        }

        if (mBoard.moves[0].player == symble && mBoard.moves[4].player == symble
                && mBoard.moves[8].player == symble) {
            winSpots = new int[] { 0, 4, 8 };
        }

        if (mBoard.moves[2].player == symble && mBoard.moves[4].player == symble
                && mBoard.moves[6].player == symble) {
            winSpots = new int[] { 2, 4, 6 };
        }

        return winSpots;
    }

    public void nextTurn(int symble) {
        switch (symble) {
        case C.SYMBLE_PLAYER_ONE: {
            mPlayerOneNameDisplay.setShadowLayer(4.5f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_green_shadow));
            mPlayerTwoNameDisplay.setShadowLayer(0f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_red_shadow));
            mGameState = C.STATE_PLAYER_ONE_TURN;
            mPlayerOneImage.setBackgroundResource(R.drawable.ic_green_x_android);
            mPlayerTwoImage.setBackgroundResource(R.drawable.ic_grey_o_android);

            break;
        }
        case C.SYMBLE_PLAYER_TWO: {
            mPlayerOneNameDisplay.setShadowLayer(0f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_green_shadow));
            mPlayerTwoNameDisplay.setShadowLayer(4.5f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_red_shadow));
            if (mGameType == C.TYPE_ONE_PLAYER) {
                mGameState = C.STATE_PLAYER_TWO_TURN;
                handelNewMove(generateAI());
            } else {
                if (mGameType == C.TYPE_TWO_PLAYER) {
                    mGameState = C.STATE_PLAYER_TWO_TURN;
                }
            }
            mPlayerOneImage.setBackgroundResource(R.drawable.ic_grey_x_android);
            mPlayerTwoImage.setBackgroundResource(R.drawable.ic_red_o_android);

            break;
        }
        }
    }

    public void setWinner(int winner) {
        Feedback.feedback(getActivity(), R.raw.game_over_sound);

        switch (winner) {
        case C.SYMBLE_PLAYER_ONE: {
            mPlayerOneWins++;
            break;
        }
        case C.SYMBLE_PLAYER_TWO: {
            mPlayerTwoWins++;
            break;
        }
        case C.SYMBLE_NULL: {
            mTotalTies++;
            break;
        }
        }

        mLastGameWinner = winner;
        mGameTime = System.currentTimeMillis() - mStartTime;
        mGameState = C.STATE_GAME_OVER;

        showGameOverDialog();

        String oneWins = mPlayerOneName + ": " + mPlayerOneWins;
        mPlayerOneNameDisplay.setText(oneWins);
        String twoWins = mPlayerTwoName + ": " + mPlayerTwoWins;
        mPlayerTwoNameDisplay.setText(twoWins);

        updateStatistics(mLastGameWinner, mDifficulty);
        mMenuItemReplay.setEnabled(true);
        mMenuItemHint.setEnabled(false);
    }

    public boolean checkForTie() {
        for (int i = 0; i < mBoard.moves.length; i++) {
            if (mBoard.moves[i].player == C.SYMBLE_NULL) {
                return false;
            }
        }

        return true;
    }

    public void animateOut(final ImageView image, final int index) {
        Animation zoomOutOut = AnimationUtils.loadAnimation(getActivity(), R.anim.zoom_out);
        zoomOutOut.setInterpolator(
                AnimationUtils.loadInterpolator(getActivity(), android.R.anim.anticipate_interpolator));
        zoomOutOut.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;
                image.setBackgroundResource(R.drawable.transparent_image);
                if (index == 8) {
                    initFirstTurn();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
            }
        });

        image.startAnimation(zoomOutOut);
    }

    public void showGameOverDialog() {
        Typeface titleFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/DroidSans-Bold.ttf");

        final View gameOverLayout = LayoutInflater.from(getActivity()).inflate(R.layout.game_over_dialog, null);

        ((TextView) gameOverLayout.findViewById(R.id.player_one_name_display)).setText(mPlayerOneName);
        ((TextView) gameOverLayout.findViewById(R.id.player_one_name_display)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.player_two_name_display)).setText(mPlayerTwoName);
        ((TextView) gameOverLayout.findViewById(R.id.player_two_name_display)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.player_tie_name_display)).setText(getString(R.string.ties));
        ((TextView) gameOverLayout.findViewById(R.id.player_tie_name_display)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.winner_name_display)).setTypeface(titleFont);

        if (mLastGameWinner == C.SYMBLE_PLAYER_ONE) {
            String oneWins = mPlayerOneName + " " + getString(R.string.wins) + "!";

            ((TextView) gameOverLayout.findViewById(R.id.winner_name_display)).setText(oneWins);
            ((ImageView) gameOverLayout.findViewById(R.id.winner_image_display))
                    .setBackgroundResource(R.drawable.ic_green_x_android);
        } else {
            if (mLastGameWinner == C.SYMBLE_PLAYER_TWO) {
                String twoWins = mPlayerTwoName + " " + getString(R.string.wins) + "!";

                ((TextView) gameOverLayout.findViewById(R.id.winner_name_display)).setText(twoWins);
                ((ImageView) gameOverLayout.findViewById(R.id.winner_image_display))
                        .setBackgroundResource(R.drawable.ic_red_o_android);
            } else {
                ((TextView) gameOverLayout.findViewById(R.id.winner_name_display))
                        .setText(getString(R.string.its_a_tie));
                ((ImageView) gameOverLayout.findViewById(R.id.winner_image_display))
                        .setBackgroundResource(R.drawable.ic_grey_android);
            }
        }

        ((ImageView) gameOverLayout.findViewById(R.id.player_one_image))
                .setBackgroundResource(R.drawable.ic_green_x_android);
        ((ImageView) gameOverLayout.findViewById(R.id.player_two_image))
                .setBackgroundResource(R.drawable.ic_red_o_android);
        ((ImageView) gameOverLayout.findViewById(R.id.player_tie_image))
                .setBackgroundResource(R.drawable.ic_grey_android);

        ((TextView) gameOverLayout.findViewById(R.id.player_one_score)).setText(": " + mPlayerOneWins);
        ((TextView) gameOverLayout.findViewById(R.id.player_one_score)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.player_two_score)).setText(": " + mPlayerTwoWins);
        ((TextView) gameOverLayout.findViewById(R.id.player_two_score)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.player_tie_score)).setText(": " + mTotalTies);
        ((TextView) gameOverLayout.findViewById(R.id.player_tie_score)).setTypeface(titleFont);

        ((TextView) gameOverLayout.findViewById(R.id.winner_time_display)).setTypeface(titleFont);
        ((TextView) gameOverLayout.findViewById(R.id.winner_time_display)).setText(mGameTime + " ms");

        new Builder(getActivity()).setView(gameOverLayout)
                .setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                }).setPositiveButton(R.string.restart, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        restartGame(mPrefs.getBoolean(Settings.PREF_ANIMATE, true));
                    }
                }).create().show();
    }

    public void restartGame(boolean animateClear) {
        mLastMoveIndex = -1;
        mStartTime = System.currentTimeMillis();

        Feedback.feedback(getActivity(), R.raw.button_missed_sound);

        if (mBoard == null) {
            mBoard = new Board(mStartTime + "");
        }

        for (int i = 0; i < mBoard.moves.length; i++) {
            if (mBoard.moves[i] == null) {
                mBoard.moves[i] = new Move();
            }

            mBoard.moves[i].player = C.SYMBLE_NULL;
            mBoard.moves[i].position = i;
            mBoard.moves[i].time = -1;

            if (animateClear) {
                animateOut(mScreens[i], i);
            } else {
                mScreens[i].setBackgroundResource(R.drawable.transparent_image);
            }
        }

        if (!animateClear) {
            initFirstTurn();
        }
    }

    public void restartGame(Bundle savedInstanceState) {
        mStartTime = savedInstanceState.getInt(SAVED_STATE_START_TIME);

        mLastGameWinner = savedInstanceState.getInt(SAVED_STATE_LAST_GAME_WINNER);
        mLastMoveIndex = savedInstanceState.getInt(SAVED_STATE_LAST_MOVE_INDEX);

        mGameState = savedInstanceState.getInt(SAVED_STATE_GAME_STATE);

        if (mBoard == null) {
            mBoard = savedInstanceState.getParcelable(SAVED_STATE_BOARD);

            if (mBoard == null) {
                restartGame(false);
                return;
            }
        }

        for (int i = 0; i < mBoard.moves.length; i++) {
            if (mBoard.moves[i].player == C.SYMBLE_PLAYER_ONE) {
                mScreens[i].setBackgroundResource(R.drawable.ic_green_x_android);
            } else {
                if (mBoard.moves[i].player == C.SYMBLE_PLAYER_TWO) {
                    mScreens[mBoard.moves[i].position].setBackgroundResource(R.drawable.ic_red_o_android);
                } else {
                    mScreens[mBoard.moves[i].position].setBackgroundResource(R.drawable.transparent_image);
                }
            }
        }

        int state = mGameState;
        if (state == C.STATE_GAME_OVER) {
            if (mBoard.moves[mLastMoveIndex].player == C.SYMBLE_PLAYER_ONE) {
                state = C.STATE_PLAYER_ONE_TURN;
            } else {
                state = C.STATE_PLAYER_TWO_TURN;
            }
        }

        switch (state) {
        case C.STATE_PLAYER_ONE_TURN: {
            mPlayerOneNameDisplay.setShadowLayer(4.5f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_green_shadow));
            mPlayerTwoNameDisplay.setShadowLayer(0f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_red_shadow));
            mPlayerOneImage.setBackgroundResource(R.drawable.ic_green_x_android);
            mPlayerTwoImage.setBackgroundResource(R.drawable.ic_grey_o_android);

            break;
        }
        case C.STATE_PLAYER_TWO_TURN: {
            mPlayerOneNameDisplay.setShadowLayer(0f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_green_shadow));
            mPlayerTwoNameDisplay.setShadowLayer(4.5f, 0, 0,
                    ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_red_shadow));
            mPlayerOneImage.setBackgroundResource(R.drawable.ic_grey_x_android);
            mPlayerTwoImage.setBackgroundResource(R.drawable.ic_red_o_android);

            break;
        }
        }
    }

    public void initFirstTurn() {
        switch (mWhoGoesFirst) {
        case C.X_GOES_FIRST: {
            nextTurn(C.SYMBLE_PLAYER_ONE);

            break;
        }
        case C.O_GOES_FIRST: {
            nextTurn(C.SYMBLE_PLAYER_TWO);

            break;
        }
        case C.X_O_RANDOM: {
            if (new Random().nextBoolean()) {
                nextTurn(C.SYMBLE_PLAYER_ONE);
            } else {
                nextTurn(C.SYMBLE_PLAYER_TWO);
            }

            break;
        }
        }
    }

    public void animateSingleSquare(int inAnim, int outAnim, final int index) {
        final Animation shrink = AnimationUtils.loadAnimation(getActivity(), outAnim);
        shrink.setStartOffset(325);
        shrink.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
            }
        });

        Animation grow = AnimationUtils.loadAnimation(getActivity(), inAnim);
        grow.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;
                mScreens[index].startAnimation(shrink);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
            }
        });

        mScreens[index].startAnimation(grow);
    }

    public void panelInAnimate(ImageView i, final int symble) {
        boolean animate = mPrefs.getBoolean(Settings.PREF_ANIMATE, Settings.PREF_ANIMATE_DEFAULT);

        if (!animate) {
            finishMove(symble);
            return;
        }

        Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.zoom_in);
        anim.setInterpolator(AnimationUtils.loadInterpolator(getActivity(), android.R.anim.overshoot_interpolator));
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;
                finishMove(symble);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
            }
        });

        i.startAnimation(anim);
    }

    public void setTheme(View view) {
        if (view != null) {
            ((LinearLayout) view.findViewById(R.id.board_id)).setBackgroundResource(R.drawable.board);
        }

        Typeface titleFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/DroidSans-Bold.ttf");

        mPlayerOneNameDisplay.setTypeface(titleFont);
        mPlayerTwoNameDisplay.setTypeface(titleFont);
    }

    public void animateHintSquare(final int index) {
        final Animation fadeOut = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);
        fadeOut.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;
                mScreens[index].setBackgroundResource(R.drawable.transparent_image);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
            }
        });

        Animation fadeIn = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
        fadeIn.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimating = false;
                mScreens[index].startAnimation(fadeOut);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {
                mAnimating = true;
                if (mGameState == C.STATE_PLAYER_ONE_TURN) {
                    mScreens[index].setBackgroundResource(R.drawable.ic_green_x_android);
                } else {
                    if (mGameState == C.STATE_PLAYER_TWO_TURN) {
                        mScreens[index].setBackgroundResource(R.drawable.ic_red_o_android);
                    }
                }
            }
        });

        mScreens[index].startAnimation(fadeIn);
    }

    public boolean isBoardClear() {
        for (int i = 0; i < mBoard.moves.length; i++) {
            if (mBoard.moves[i].player != C.SYMBLE_NULL) {
                return false;
            }
        }

        return true;
    }

    public void updateStatistics(int winner, int difficulty) {
        switch (winner) {
        case C.SYMBLE_PLAYER_ONE: {
            StatisticsDatabase.putGamesWonPlusOne(getActivity(), difficulty);
            break;
        }
        case C.SYMBLE_PLAYER_TWO: {
            StatisticsDatabase.putGamesLostPlusOne(getActivity(), difficulty);
            break;
        }
        case C.SYMBLE_NULL: {
            StatisticsDatabase.putGamesTiedPlusOne(getActivity(), difficulty);
            break;
        }
        }
    }
}