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);
}
}
}