Tic Tac Toe Full complete Game create in Android Studio.
Firstly we are create tic tac toe for layout using the Relative layout first and inside Grid Layout then
use one ImageView and create a 9 Box 3 row and column .
Ont textView and one button (try again)name of for show the message who is winner .showing in
center area.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="3"
android:rowCount="3"
android:background="@drawable/board"
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView2"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView3"
android:layout_row="0"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView4"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView5"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="40dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView6"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="6" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView8"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="7" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView9"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="8" />
</GridLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffaff0b"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/playAgainLayout"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/winnerMessage"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Again"
android:id="@+id/playAgainButton"
android:layout_gravity="center_horizontal"
android:onClick="playAgain" />
</LinearLayout>
MainActivity.java
package com.dregan.apnatictoegame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//0=yellow 1=red
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 tappidCounter=Integer.parseInt( counter.getTag().toString() );
if(gameState[tappidCounter]==2&& gameIsActive) {
gameState[tappidCounter]=activePlayer;
counter.setTranslationY( -1000f );
if (activePlayer == 0) {
counter.setImageResource( R.drawable.yellow );
activePlayer=1;
} else {
counter.setImageResource( R.drawable.red );
activePlayer=0;
}
counter.animate().translationYBy( 1000f ).rotation( 300 ).setDuration( 300 );
for(int[] winningPositions:winningPositions)
{
if(gameState[winningPositions[0]]==gameState[winningPositions[1]]&&
gameState[winningPositions[1]]==gameState[winningPositions[2]] &&
gameState[winningPositions[0]] !=2)
{
gameIsActive=false;
String winner="Red";
if(gameState[winningPositions[0]]==0){
winner="yellow";
}
TextView winnerMeassage=(TextView)findViewById( R.id.winnerMessage );
winnerMeassage.setText( winner+ "****you winner**** !!!" );
System.out.println(gameState[winningPositions[0]]);
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.VISIBLE );
}
else {
boolean gameIsOver=true;
for(int counterState: gameState)
{
if(counterState==2) gameIsOver=false;
}
if(gameIsOver)
{
TextView winnerMeassage=(TextView)findViewById( R.id.winnerMessage );
winnerMeassage.setText("It's draw game " );
// System.out.println(gameState[winningPositions[0]]);
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.VISIBLE );
}
}
}
}
}
public void playAgain(View view) {
gameIsActive=true;
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.INVISIBLE );
activePlayer=0;
for(int i =0; i<gameState.length;i++)
{
gameState[i]=2;
}
GridLayout gridLayout=(GridLayout)findViewById( R.id.gridLayout );
for(int i =0;i<gridLayout.getChildCount(); i++)
{
( (ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
}
}
Firstly we are create tic tac toe for layout using the Relative layout first and inside Grid Layout then
use one ImageView and create a 9 Box 3 row and column .
Ont textView and one button (try again)name of for show the message who is winner .showing in
center area.
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="3"
android:rowCount="3"
android:background="@drawable/board"
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView2"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView3"
android:layout_row="0"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView4"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView5"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="40dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView6"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="6" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView8"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="7" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView9"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="8" />
</GridLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffaff0b"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/playAgainLayout"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/winnerMessage"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Again"
android:id="@+id/playAgainButton"
android:layout_gravity="center_horizontal"
android:onClick="playAgain" />
</LinearLayout>
MainActivity.java
package com.dregan.apnatictoegame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//0=yellow 1=red
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 tappidCounter=Integer.parseInt( counter.getTag().toString() );
if(gameState[tappidCounter]==2&& gameIsActive) {
gameState[tappidCounter]=activePlayer;
counter.setTranslationY( -1000f );
if (activePlayer == 0) {
counter.setImageResource( R.drawable.yellow );
activePlayer=1;
} else {
counter.setImageResource( R.drawable.red );
activePlayer=0;
}
counter.animate().translationYBy( 1000f ).rotation( 300 ).setDuration( 300 );
for(int[] winningPositions:winningPositions)
{
if(gameState[winningPositions[0]]==gameState[winningPositions[1]]&&
gameState[winningPositions[1]]==gameState[winningPositions[2]] &&
gameState[winningPositions[0]] !=2)
{
gameIsActive=false;
String winner="Red";
if(gameState[winningPositions[0]]==0){
winner="yellow";
}
TextView winnerMeassage=(TextView)findViewById( R.id.winnerMessage );
winnerMeassage.setText( winner+ "****you winner**** !!!" );
System.out.println(gameState[winningPositions[0]]);
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.VISIBLE );
}
else {
boolean gameIsOver=true;
for(int counterState: gameState)
{
if(counterState==2) gameIsOver=false;
}
if(gameIsOver)
{
TextView winnerMeassage=(TextView)findViewById( R.id.winnerMessage );
winnerMeassage.setText("It's draw game " );
// System.out.println(gameState[winningPositions[0]]);
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.VISIBLE );
}
}
}
}
}
public void playAgain(View view) {
gameIsActive=true;
LinearLayout layout=(LinearLayout)findViewById( R.id.playAgainLayout );
layout.setVisibility( View.INVISIBLE );
activePlayer=0;
for(int i =0; i<gameState.length;i++)
{
gameState[i]=2;
}
GridLayout gridLayout=(GridLayout)findViewById( R.id.gridLayout );
for(int i =0;i<gridLayout.getChildCount(); i++)
{
( (ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
}
}
0 comments:
Post a Comment