Welcome to my blog

Tic tac Toe Game


Introduction 

         This is the Tic tac toe  game making for fun and practice for Android Development in this game we using Android Studio I am only write the logic for making a tic tac toe game in this blog


       Java code of Tic tac toe game

This code is in the main activity code 
package com.example.tictactoegame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Time;

public class MainActivity extends AppCompatActivity {
int activePlayer = 0;
boolean gameActive = true;
int [] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int [][] winpositions = {{0,1,2}, {3,4,5}, {6,7,8},
{0,3,6}, {1,4,7}, {2,5,8},
{0,4,8}, {2,4,6}};
int [][] winpositions2 = {{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 gameReset(View view){
gameActive = true;
activePlayer = 0;
for (int i =0; i<gameState.length; i++){
gameState[i] = 2;
}
((ImageView)findViewById(R.id.imageView0)).setImageResource(0);
((ImageView)findViewById(R.id.imageView1)).setImageResource(0);
((ImageView)findViewById(R.id.imageView2)).setImageResource(0);
((ImageView)findViewById(R.id.imageView3)).setImageResource(0);
((ImageView)findViewById(R.id.imageView4)).setImageResource(0);
((ImageView)findViewById(R.id.imageView5)).setImageResource(0);
((ImageView)findViewById(R.id.imageView6)).setImageResource(0);
((ImageView)findViewById(R.id.imageView7)).setImageResource(0);
((ImageView)findViewById(R.id.imageView8)).setImageResource(0);

TextView status = findViewById(R.id.textView2);
status.setText("X's Turn - Tap to PLAY");
}

public void Draw(View view){
for (int i = 0; i < winpositions.length; i++){
if (winpositions[i] != winpositions2[i]){
gameActive = false;
TextView status = findViewById(R.id.textView2);
status.setText("Game has DRAWN!!");
}
}
}

public void playerTab(View view) {
ImageView img = (ImageView) view;
TextView status = findViewById(R.id.textView2);
int tappedImage = Integer.parseInt(img.getTag().toString());
if (!gameActive) {
gameReset(view);
}
if (gameState[tappedImage] == 2) {
gameState[tappedImage] = activePlayer;
img.setTranslationY(-1000f);
if (activePlayer == 0) {
img.setImageResource(R.drawable.x);
activePlayer = 1;
status.setText("O's Turn - Tap to PLAY");
} else {
img.setImageResource(R.drawable.o);
activePlayer = 0;
status.setText("X's Turn - Tap to PLAY");
}
img.animate().translationYBy(1000f).setDuration(300);
}
for (int[] winposition : winpositions) {
if (gameState[winposition[0]] == gameState[winposition[1]] &&
gameState[winposition[1]] == gameState[winposition[2]] &&
gameState[winposition[0]] != 2) {
String winnerStr = null;
if (gameState[winposition[0]] == 0) {
winnerStr = "X has WON!!";
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
} else {
winnerStr = "O has WON!!";
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
status.setText(winnerStr);
gameActive = false;
}
}
boolean emptySquare = false;
for (int squareState : gameState) {
if (squareState == 2) {
emptySquare = true;
break;
}
}
if (!emptySquare && gameActive) {
// Game is a draw
gameActive = false;
String winnerStr;
winnerStr = "No one won";
status.setText(winnerStr);
}
}

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




}
}

 In second activity code is this

package com.example.tictactoegame;



import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

SurfaceView surfaceView;
MediaPlayer mediaPlayer;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
surfaceView = findViewById(R.id.surfaceView);
mediaPlayer = MediaPlayer.create(this, R.raw.congratulation);
surfaceView.setKeepScreenOn(true);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
mediaPlayer.setDisplay(surfaceHolder);
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
mediaPlayer.stop();
mediaPlayer.release();

}
});

mediaPlayer.start();
Intent intent = new Intent(this, MainActivity.class);

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
startActivity(intent);
}
});
}

}

This is the Activity of this app


The XMl code of this activity is this


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:fontFamily="@font/aladin"
android:text="@string/welcome_to_tic_tac_toe"
android:textColor="#DC2424"
android:textSize="46sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="419dp"
android:layout_height="436dp"
android:contentDescription="@string/todo"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/grid" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="378dp"
android:layout_height="387dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="118dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="10sp"
android:tag="0" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="10sp"
android:tag="1" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="10sp"
android:tag="2" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="134dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="3" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="4" />

<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="5" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="5sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="6" />

<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="5sp"
android:layout_marginTop="12sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="7" />

<ImageView
android:id="@+id/imageView8
"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="5sp"
android:layout_marginTop="12sp"
android:layout_weight="1"
android:contentDescription="@string/todo"
android:onClick="playerTab"
android:padding="15sp"
android:tag="8" />

</LinearLayout>
</LinearLayout>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/aclonica"
android:text="@string/textview"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />




Comments