-1

I am new in Android development and I want to make a tic tac toe game.

How do I improve this code to make a Random option for computer player? I want to add a single player feature in this tic tac toe game.

I want that if yellow player clicks then red player also automatically plays without clicking.

code here:-

package com.osdeveloper.game;

import android.media.MediaActionSound;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    int Activeplayer = 0;
    boolean gameisActive = true;

    int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
    int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};

    public void Dropin(View view) {
        ImageView counter = (ImageView) view;
        System.out.println(counter.getTag().toString());
        int tappedCounter = Integer.parseInt(counter.getTag().toString());
        if (gameState[tappedCounter] == 2 && gameisActive) {

            gameState[tappedCounter] = Activeplayer;

            counter.setTranslationY(-1000f);
           counter.setImageResource(R.drawable.yellow);

            if (Activeplayer == 0) {
                counter.setImageResource(R.drawable.yellow);
                Activeplayer = 1;

            } else {


                counter.setImageResource(R.drawable.red);
                Activeplayer = 0;

            }
            counter.animate().translationYBy(1000f).rotation(100).setDuration(300);
            for (int[] winningposition : winningPositions) {
                if (gameState[winningposition[0]] == gameState[winningposition[1]] &&
                        gameState[winningposition[1]] == gameState[winningposition[2]]
                        && gameState[winningposition[0]] != 2) {
                    gameisActive = false;
                    String Winner = "red";
                    if (gameState[winningposition[0]] == 0) {

                        Winner = "yellow";
                    }


                    TextView TX = (TextView) findViewById(R.id.winmessage);
                    TX.setText(Winner + " has win ");
                    Toast.makeText(this,Winner +"Congratulation .",
                            Toast.LENGTH_SHORT).show();
                    LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
                    layout.setVisibility(View.VISIBLE);

                } else {
                    boolean gameIsOver = true;

                    for (int CounterState : gameState) {
                        if (CounterState == 2) {
                            gameIsOver = false;
                        }
                    }
                    if (gameIsOver) {
                        TextView TX = (TextView) findViewById(R.id.winmessage);
                        TX.setText("its a draw :(");
                        LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
                        layout.setVisibility(View.VISIBLE);
                    }

                }
            }

        }
    }

    public void playAgain(View view) {
        gameisActive = true;
        LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
        layout.setVisibility(View.INVISIBLE);
        Activeplayer = 0;


        for (int i = 0; i < gameState.length; i++) {
            gameState[i] = 2;
        }
        GridLayout grid = (GridLayout) findViewById(R.id.grid);
        for (int i = 0; i < grid.getChildCount(); i++) {
            ((ImageView) grid.getChildAt(i)).setImageResource(0);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);

        final MediaPlayer Mplayer = MediaPlayer.create(this, R.raw.music2);
        Mplayer.start();

        Button btn = (Button) findViewById(R.id.mutebtn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Mplayer.stop();
                Mplayer.start();
            }

        });
    }
    private Boolean exit = false;
    @Override
    public void onBackPressed() {
        if (exit) {
            finish(); // finish activity
        } else {
            Toast.makeText(this, "Press Back again to Exit.",
                    Toast.LENGTH_SHORT).show();

            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                 }
             }, 3 * 1000);

         }

     }
  }
Community
  • 1
  • 1
Osama Raja
  • 11
  • 2

1 Answers1

0

As far as I understand your question, yellow player is the player who is playing the game. And Red player is the auto player. If that is right, then you can achieve this via call back approach. Once yellow player play his turn( you must be calling any method to play turn) on the end of this method give call back to call method for Red player turn to play automatically.(in call back method you will be calling the method that lays red player's turn). Hope that helps you. As you said, you are new Android developer, for call back implementation help you may refer below help full links ,

https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58