how to make tic-tac game ? andriod tutorials with nilesh .

 Tic-tac


Reqirment :

                Andriod Studio  with i7 7 genration minimum genration please noted that and also provide flutter code.

source code of mine 
with images

copy right by NIlesh Vishnu Raut


this is full source code of  10th project an simple tic -tac game with a logic if any one has issue related this to contact at top bar contact me or comment down 

this is very funny game to download it on mi store i wil update link here.




nilesh raut andriod studio




source code




package com.nilesh.try3;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.nilesh.try3.R;

public class MainActivity<winPositions> extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
//0-o
//1-x
boolean gameActive = true;
// Player representation
// 0 - X
// 1 - O
int activePlayer = 0;
int[] gameState = {2, 2 , 2, 2, 2, 2, 2, 2, 2};
// State meanings:
// 0 - X
// 1 - O
// 2 - Null
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}};
private Object adError;
private Object LoadAdError;


public void tap(View view){
ImageView img = (ImageView) view;
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;
TextView status = findViewById(R.id.stat);
status.setText("O's Turn - Tap to play");
} else {
img.setImageResource(R.drawable.o);
activePlayer = 0;
TextView status = findViewById(R.id.stat);
status.setText("X's Turn - Tap to play");
}
img.animate().translationYBy(1000f).setDuration(300);
}
// Check if any player has won
for(int[] winPosition: winPositions){
if(gameState[winPosition[0]] == gameState[winPosition[1]] &&
gameState[winPosition[1]] == gameState[winPosition[2]] &&
gameState[winPosition[0]]!=2){
// Somebody has won! - Find out who!
String winnerStr;
gameActive = false;
if(gameState[winPosition[0]] == 0){
winnerStr = "X has won";
}
else{
winnerStr = "O has won";
}

// Update the status bar for winner announcement
TextView status = findViewById(R.id.stat);
status.setText(winnerStr);

}



}

}

public void gameReset(View view) {
gameActive = true;
activePlayer = 0;
for(int i=0; i<gameState.length; i++){
gameState[i] = 2;
}
((ImageView)findViewById(R.id.imageView9)).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.stat);
status.setText("X's Turn - Tap to play");

}

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


}
public void genads(){
MobileAds.initialize(this,
"ca-app-pub-2889402739074007~2278003733");

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-2889402739074007/3004955569");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener(){
private LoadAdError adError;

@Override
public void onAdClosed() {
// Code to be executed when an ad finishes loading.
super.onAdClosed();
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {

// Code to be executed when an ad finishes loading.
super.onAdFailedToLoad(adError);
}
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}



}

});
}
}


xml code: 

this designed code of my application go cheched it this has not any issue if you 


<?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">

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="399dp"
android:layout_height="717dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/nilesh" />

<TextView
android:id="@+id/textView"
android:layout_width="360dp"
android:layout_height="62dp"
android:layout_marginTop="32dp"
android:fontFamily="serif"
android:text="@string/textview"
android:textColor="#E13D3D"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="414dp"
android:layout_height="501dp"
android:contentDescription="@string/main_grid"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/tac" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="412dp"
android:layout_height="385dp"
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="match_parent"
android:layout_weight="1"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"

android:padding="24sp"
android:tag="0" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="1" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="2" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"

android:orientation="horizontal">

<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="3" />

<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="4" />

<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="5" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="6" />

<ImageView
android:id="@+id/imageView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="7" />

<ImageView
android:id="@+id/imageView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="tap"
android:padding="24sp"
android:tag="8" />
</LinearLayout>

</LinearLayout>

<TextView
android:id="@+id/stat"
android:layout_width="190dp"
android:layout_height="25dp"
android:fontFamily="casual"
android:text="X s tern tap to play"
android:textColor="#DF0D0D"
android:textSize="24sp"
android:textStyle="bold|italic"
app:barrierMargin="12sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:layout_constraintVertical_bias="0.86" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout> 





mainfest 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nilesh.try3">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERACT_ACROSS_PROFILES"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Try3"
>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-2889402739074007~2278003733"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


copyright : this is my code please dont just copy this code and runf this code just refrence for you not                             copy and run


Post a Comment

0 Comments

Ad Code