Create a new project in your Android Studio and name it as TicTacToe .
Go to your browser and download the images for the tic tac toe game. Mainly you have to download three images for it, O , X and the image for the grid.
Now go to the activity_main.xml file in your project and click on the TextView and change the text inside that text view from “Hello world” to “Welcome to Tic Tac Toe“.
activity_main.xml
step 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
tools:context=".TictocActivity">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3">
<Button
android:id="@+id/card1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp" />
<Button
android:id="@+id/card2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp">
</Button>
<Button
android:id="@+id/card3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp">
</Button>
<Button
android:id="@+id/card4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp">
</Button>
<Button
android:id="@+id/card5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp">
</Button>
<Button
android:id="@+id/card6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp">
</Button>
<Button
android:id="@+id/card7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp"></Button>
<Button
android:id="@+id/card8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp"></Button>
<Button
android:id="@+id/card9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:onClick="click"
android:textSize="40sp"
app:cardBackgroundColor="@color/underWeight"
app:cardCornerRadius="100dp"></Button>
</GridLayout>
</LinearLayout>
MainActivity.java
step 2:
package com.sandhya.androidallproject2023;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TictocActivity extends AppCompatActivity {
Button card1,card2,card3,card4,card5,card6,card7,card8,card9;
String c1,c2,c3,c4,c5,c6,c7,c8,c9;
int flag = 0;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tictoc);
initId();
}
public void click(View v){
Button currentBtn = (Button) v;
if (currentBtn.getText().toString().equals("")){
count++;
if (flag==0){
currentBtn.setText("X");
flag = 1;
}else {
currentBtn.setText("O");
flag = 0;
}
if (count>4){
initStringBtn();
///condition
if (c1.equals(c2)&&c2.equals(c3)&&!c1.equals("")){
Toast.makeText(this, "Winner is : "+c1, Toast.LENGTH_SHORT).show();
newGame();
}else if (c4.equals(c5)&&c5.equals(c6)&&!c4.equals("")){
Toast.makeText(this, "Winner is : "+c4, Toast.LENGTH_SHORT).show();
newGame();
}else if (c7.equals(c8)&&c8.equals(c9)&&!c7.equals("")){
Toast.makeText(this, "Winner is : "+c7, Toast.LENGTH_SHORT).show();
newGame();
}else if (c1.equals(c4)&&c4.equals(c7)&&!c1.equals("")){
Toast.makeText(this, "Winner is : "+c1, Toast.LENGTH_SHORT).show();
newGame();
}else if (c2.equals(c5)&&c5.equals(c8)&&!c2.equals("")){
Toast.makeText(this, "Winner is : "+c2, Toast.LENGTH_SHORT).show();
newGame();
}else if (c3.equals(c6)&&c6.equals(c9)&&!c3.equals("")){
Toast.makeText(this, "Winner is : "+c3, Toast.LENGTH_SHORT).show();
newGame();
}else if (c1.equals(c5)&&c5.equals(c9)&&!c1.equals("")){
Toast.makeText(this, "Winner is : "+c1, Toast.LENGTH_SHORT).show();
newGame();
}else if (c3.equals(c5)&&c5.equals(c7)&&!c3.equals("")){
Toast.makeText(this, "Winner is : "+c3, Toast.LENGTH_SHORT).show();
newGame();
}else if (!c1.equals("")&&!c2.equals("")&&!c3.equals("")&&!c4.equals("")
&&!c5.equals("")&&!c6.equals("")&&!c7.equals("")&&!c8.equals("")
&&!c9.equals("")){
Toast.makeText(this, "Game is Drown", Toast.LENGTH_SHORT).show();
newGame();
}
}
}
}
public void newGame(){
card1.setText("");
card2.setText("");
card3.setText("");
card4.setText("");
card5.setText("");
card6.setText("");
card7.setText("");
card8.setText("");
card9.setText("");
flag = 0;
count = 0;
}
private void initStringBtn() {
c1 = card1.getText().toString();
c2 = card2.getText().toString();
c3 = card3.getText().toString();
c4 = card4.getText().toString();
c5 = card5.getText().toString();
c6 = card6.getText().toString();
c7 = card7.getText().toString();
c8 = card8.getText().toString();
c9 = card9.getText().toString();
}
public void initId(){
card1 = findViewById(R.id.card1);
card2 = findViewById(R.id.card2);
card3 = findViewById(R.id.card3);
card4 = findViewById(R.id.card4);
card5 = findViewById(R.id.card5);
card6 = findViewById(R.id.card6);
card7 = findViewById(R.id.card7);
card8 = findViewById(R.id.card8);
card9 = findViewById(R.id.card9);
}
}
output :-
Tic Tac Toe game android studio
Post a Comment