Skip to main content

Make your own tic-tac-toe game on Android




Now stay tuned with the new videos.Just click the button mention below of youtube:







App Code for MainActivity.java:


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

int chance=0;

    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,reset;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    btn1=(Button)findViewById(R.id.btn1);

    btn2=(Button)findViewById(R.id.btn2);

    btn3=(Button)findViewById(R.id.btn3);

    btn4=(Button)findViewById(R.id.btn4);

    btn5=(Button)findViewById(R.id.btn5);

    btn6=(Button)findViewById(R.id.btn6);

    btn7=(Button)findViewById(R.id.btn7);

    btn8=(Button)findViewById(R.id.btn8);

    btn9=(Button)findViewById(R.id.btn9);

    reset=(Button)findViewById(R.id.reset);


        btn1.setOnClickListener(this);

        btn2.setOnClickListener(this);

        btn3.setOnClickListener(this);

        btn4.setOnClickListener(this);

        btn5.setOnClickListener(this);

        btn6.setOnClickListener(this);

        btn7.setOnClickListener(this);

        btn8.setOnClickListener(this);

        btn9.setOnClickListener(this);

        reset.setOnClickListener(this);


    }


    @Override

    public void onClick(View view) {

        switch(view.getId()){

            case R.id.btn1:

                if(btn1.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn1.setText("O");


                        result();

                    }

                    else{

                        chance=0;

                        btn1.setText("X");

                        result();

                    }

                }

break;

            case R.id.btn2:

                if(btn2.getText().toString().equals("")){

                    if(chance==0){


                        chance=1;

                        btn2.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn2.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn3:

                if(btn3.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn3.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn3.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn4:

                if(btn4.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn4.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn4.setText("X");

                        result();

                    }

                }

                break;


            case R.id.btn5:

                if(btn5.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn5.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn5.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn6:

                if(btn6.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn6.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn6.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn7:

                if(btn7.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn7.setText("O");

                        result();


                    }

                    else{

                        chance=0;

                        btn7.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn8:

                if(btn8.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn8.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn8.setText("X");

                        result();

                    }

                }


                break;

            case R.id.btn9:

                if(btn9.getText().toString().equals("")){

                    if(chance==0){

                        chance=1;

                        btn9.setText("O");

                        result();

                    }

                    else{

                        chance=0;

                        btn9.setText("X");

                        result();

                    }

                }

            break;

                case R.id.reset:

                btn1.setText("");

                btn2.setText("");

                btn3.setText("");

                btn4.setText("");

                btn5.setText("");

                btn6.setText("");

                btn7.setText("");

                btn8.setText("");

                btn9.setText("");



        }

    }


    public void result(){

        if(btn1.getText().toString().equals("X") && btn2.getText().toString().equals("X") && btn3.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

        reset.callOnClick();

        }

        else if(btn4.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn6.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn7.getText().toString().equals("X") && btn8.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn1.getText().toString().equals("O") && btn2.getText().toString().equals("O") && btn3.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }

        else if(btn4.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn6.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn7.getText().toString().equals("O") && btn8.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }




      else  if(btn1.getText().toString().equals("X") && btn4.getText().toString().equals("X") && btn7.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

      }

        else if(btn2.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn8.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn3.getText().toString().equals("X") && btn6.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn1.getText().toString().equals("O") && btn4.getText().toString().equals("O") && btn7.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }

        else if(btn2.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn8.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn3.getText().toString().equals("O") && btn6.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }



        else if(btn1.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn3.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn7.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }


        else if(btn1.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }

        else if(btn3.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn7.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();

            reset.callOnClick();

        }




    }




}




App code for activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

        <Button

            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:layout_weight="1"
            android:textSize="40sp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />

    </LinearLayout>

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="RESET"
        android:textSize="25sp" />

</LinearLayout>


If you love our work then share it with your friends.


We have made an app with the above code and added some more games on it


Snapshot of our app

link: https://play.google.com/store/apps/details?id=puruworld.fungames1

Keywords:
puruworldofficial
tic tac toe android studio,
tic tac toe in android studio,
tic tac toe,
tictactoe android studio sample,
tic tac toe android,
how to make xo in andriod studio,
tic tac toe game coding in android studio,
how to make xo in android studio,
tic tac toe android app tutorial,
Develop TicTacToe game in Android Studio,
Learn to create a Tic-Tac-Toe Game with Android Studio,
Make your own tictactoe game in android,
Android Development Lecture 22 (Making tic tac toe Game),
How to Make a Tic Tac Toe Game in Android - Part 4 - ORIENTATION CHANGE & RESET FUNCTIONALITY,
Android | TicTacToe Tutorial Part 1,
Android | TicTacToe Tutorial Part 2,
How to Make a Tic Tac Toe Game in Android - Part 1 - THE LAYOUT - Android Studio Tutorial,
how to make simple game in android studio,
Understanding logic of TIC TAC TOE app for android development,
Tic Tac Toe 2D Game in Android Studio Tutorial,
puruworld
puruworldofficial
puru blog
puruworld blog
puruworldofficial
puruworldofficial blog
blog of puru
two player tic tac toe game in android studio
tic tac toe game in android studio source code
tic tac toe game logic android
tic tac toe multiplayer android source code
android tic tac toe layout
tic tac toe android source code download
tic tac toe 2 player android code
tic tac toe android github
make your own tictactoe game in android




Comments

Popular posts from this blog

How to Create a Family Wellness Routine

In today's fast-paced world, it can be easy to neglect our health and wellness in favor of keeping up with work, school, and other obligations. However, it's important to remember that taking care of ourselves and our families is essential for leading happy and fulfilling lives. One great way to ensure that the whole family is staying healthy and happy is by creating a family wellness routine. A family wellness routine is a set of daily habits and activities that promote physical, mental, and emotional well-being for everyone in the household. By establishing a routine that incorporates healthy eating, regular physical activity, and other wellness practices, you can help your family stay strong and resilient in the face of life's challenges. So, how can you create a family wellness routine that works for everyone? Here are some tips to get you started: 1. Set Clear Goals: The first step in creating a family wellness routine is to set clear goals for what you want to achieve...

Part 1: Make your own game in Python

Python Code[Just Copy paste the code and you are done] : # Import a library of functions called 'pygame' import pygame # Initialize the game engine pygame.init() gamedisplay = pygame.display.set_mode((700, 500)) pygame.display.set_caption("Cool Game") clock=pygame.time.Clock() crashed=False while not crashed: for event in pygame.event.get(): if event.type == pygame.QUIT: crashed= True print(event) pygame.display.update() clock.tick(60) pygame.quit() quit() In command prompt: pip install pygame Keywords : puruworldofficial python game engine 2d, python game library, python game development, pygame game, python game creator, python game programming tutorial for beginners, pygame tutorial, python based games, Game development in python, build your own game in python with pygame, build your own game in python, make your own game in python, invent your own game in python, how to make a game in python for beginners, python game programmi...