Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.







Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.


Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

Juego del Gato/ Tres en Raya/ Tic Tac Toe en Android Studio con Lenguaje de Programación Java.

MainActivity.java


package com.anzal.gato;


import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }


    //Método para queal oprimir el botón cambie de ventana

    public void Tablero(View view){

        Intent WebView= new Intent(this,Tablero.class);

        startActivity(WebView);

    }

    //Método del botón salir

    public void Salir(View view){

        finish();

    }

}




String.xml


<resources>

    <string name="app_name">Gato</string>

    <string name="txt_titulo">Tres en Raya</string>

    <string name="txt_btn_jugar">¡A jugar!</string>

    <string name="txt_btn_salir">Salir</string>


    <string name="turno">Turno de:</string>

    <string name="jugador">X</string>

    <string name="Defecto">A</string>

    <string name="txt_btn_reiniciar">Reiniciar Juego</string>

</resources>




Tablero.java


package com.anzal.gato;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;


public class Tablero extends AppCompatActivity {


    //Objetos que se ocupan del la interface Grafica

    TextView b1, b2, b3 ,b4, b5, b6, b7, b8, b9, jugador, tv1;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tablero);


        //Conexión con la interface gráfica

        jugador = (TextView) findViewById(R.id.txt_jugador);

        b1 = (TextView) findViewById(R.id.a1);

        b2 = (TextView) findViewById(R.id.a2);

        b3 = (TextView) findViewById(R.id.a3);

        b4 = (TextView) findViewById(R.id.a4);

        b5 = (TextView) findViewById(R.id.a5);

        b6 = (TextView) findViewById(R.id.a6);

        b7 = (TextView) findViewById(R.id.a7);

        b8 = (TextView) findViewById(R.id.a8);

        b9 = (TextView) findViewById(R.id.a9);

        tv1 = (TextView) findViewById(R.id.txt_t1);

    }


    //El juego sigue esta configuración

    //b1 b2 b3

    //b4 b5 b6

    //b7 b8 b9

    //En las casillas

    int turno = 2;


    //O = 1

    //X = 2

    int i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0, i8 = 0, i9 = 0;

    Boolean Ganar = false;



    public void Ganador(){

        //Si O gana

        //Si gana con lineas b1 b2 b3

        String og = "O GANA";

        String xg = "X GANA";

        if((i1 == 1) && (i2 == 1) && (i3 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con lineas b4 b5 b6

        if((i4 == 1) && (i5 == 1) && (i6 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con lineas b7 b8 b9

        if((i7 == 1) && (i8 == 1) && (i9 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con lineas b1 b4 b7

        if((i1 == 1) && (i4 == 1) && (i7 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con lineas b2 b5 b8

        if((i2 == 1) && (i5 == 1) && (i8 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con lineas b3 b6 b9

        if((i3 == 1) && (i6 == 1) && (i9 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con linea b1 b5 b9

        if((i1 == 1) && (i5 == 1) && (i9 == 1)){

            jugador.setText(og);

            Ganar = true;

        }

        //Si gana con linea b7 b5 b3

        if((i7 == 1) && (i5 == 1) && (i3 == 1)){

            jugador.setText(og);

            Ganar = true;

        }


        //Si X gana

        //Si gana con lineas b1 b2 b3

        if((i1 == 2) && (i2 == 2) && (i3 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con lineas b4 b5 b6

        if((i4 == 2) && (i5 == 2) && (i6 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con lineas b7 b8 b9

        if((i7 == 2) && (i8 == 2) && (i9 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con lineas b1 b4 b7

        if((i1 == 2) && (i4 == 2) && (i7 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con lineas b2 b5 b8

        if((i2 == 2) && (i5 == 2) && (i8 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con lineas b3 b6 b9

        if((i3 == 2) && (i6 == 2) && (i9 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con linea b1 b5 b9

        if((i1 == 2) && (i5 == 2) && (i9 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }

        //Si gana con linea b7 b5 b3

        if((i7 == 2) && (i5 == 2) && (i3 == 2)){

            jugador.setText(xg);

            Ganar = true;

        }


        if(Ganar){

            i1 = 3;

            i2 = 3;

            i3 = 3;

            i4 = 3;

            i5 = 3;

            i6 = 3;

            i7 = 3;

            i8 = 3;

            i9 = 3;

            tv1.setText("");

        }

    }

    String turnoO = "O";

    String turnoX = "X";

    //btn_1

    public void btn_1(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i1 == 0) {

                if (turno == 2) {

                    b1.setText("X");

                    i1 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b1.setText("O");

                    i1 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_2

    public void btn_2(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i2 == 0) {

                if (turno == 2) {

                    b2.setText("X");

                    i2 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b2.setText("O");

                    i2 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_3

    public void btn_3(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i3 == 0) {

                if (turno == 2) {

                    b3.setText("X");

                    i3 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b3.setText("O");

                    i3 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_4

    public void btn_4(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i4 == 0) {

                if (turno == 2) {

                    b4.setText("X");

                    i4 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b4.setText("O");

                    i4 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_5

    public void btn_5(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i5 == 0) {

                if (turno == 2) {

                    b5.setText("X");

                    i5 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b5.setText("O");

                    i5 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_6

    public void btn_6(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i6 == 0) {

                if (turno == 2) {

                    b6.setText("X");

                    i6 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b6.setText("O");

                    i6 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_7

    public void btn_7(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i7 == 0) {

                if (turno == 2) {

                    b7.setText("X");

                    i7 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b7.setText("O");

                    i7 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_8

    public void btn_8(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i8 == 0) {

                if (turno == 2) {

                    b8.setText("X");

                    i8 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b8.setText("O");

                    i8 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //btn_9

    public void btn_9(View view){

        if(Ganar){

            Toast.makeText(this,"Ya hay un ganador",Toast.LENGTH_SHORT).show();

            Toast.makeText(this, "Reinicia el Juego", Toast.LENGTH_SHORT).show();

        }else {

            if (i9 == 0) {

                if (turno == 2) {

                    b9.setText("X");

                    i9 = 2;

                    turno = 1;

                    jugador.setText(turnoO);

                } else if (turno == 1) {

                    b9.setText("O");

                    i9 = 1;

                    turno = 2;

                    jugador.setText(turnoX);

                }

            } else {

                Toast.makeText(this, "La casilla ya esta llena.", Toast.LENGTH_SHORT).show();

                Toast.makeText(this, "Intenta en otra", Toast.LENGTH_SHORT).show();

            }

            Ganador();

        }

    }

    //Método para reiniciar juego

    public void ReiniciarJuego(View view){

        //Limpiamos etiquetas

        b1.setText("A");

        b2.setText("A");

        b3.setText("A");

        b4.setText("A");

        b5.setText("A");

        b6.setText("A");

        b7.setText("A");

        b8.setText("A");

        b9.setText("A");

        //Ganador = false

        Ganar = false;


        //reiniciamos jugador

        String RJ = "Turno de X";

        jugador.setText(RJ);


        //Reinciiamos variables numericas

        i1 = 0;

        i2 = 0;

        i3 = 0;

        i4 = 0;

        i5 = 0;

        i6 = 0;

        i7 = 0;

        i8 = 0;

        i9 = 0;

        //Reiniciamos Turno

        turno = 2;

        tv1.setText("Turno de:");

    }

}

 




Enlace de descarga del Proyecto.

Descargar

Comentarios

Entradas populares de este blog

Calculadora Básica con Lenguaje de Programación Prolog en SWI-Prolog con Interface Gráfica. Operaciones suma, resta, multiplicación y división.

Calculadora Básica en SWI-Prolog 8.4.2 con Prolog.