Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pe.edu.ulima.tictactoeapp;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- TextView tviMensaje;
- Button[] butarray = new Button[10];
- int turnoX;
- int turnoO;
- int sumaX;
- int sumaO;
- int[] idsarray;
- int[] valsX = new int[5];
- int[] valsO = new int[5];
- int[] valoresarray = {0,
- 8,1,6, // Suma = 15 en cada lado
- 3,5,7,
- 4,9,2};
- boolean esTurnoO;
- boolean gameover;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- turnoX = 0;
- turnoO = 0;
- tviMensaje = (TextView) findViewById(R.id.tviMensaje);
- idsarray = new int[] {R.id.tviMensaje,R.id.But1,R.id.But2,R.id.But3,R.id.But4,
- R.id.But5,R.id.But6,R.id.But7,R.id.But8,R.id.But9};
- esTurnoO = true;
- gameover = false;
- if(esTurnoO) {
- tviMensaje.setText("Le toca al jugador O..");
- }else{
- tviMensaje.setText("Le toca al jugador X..");
- }
- tviMensaje.setOnClickListener(this);
- for(int i = 1; i <= 9; i++) {
- butarray[i] = (Button) findViewById(idsarray[i]);
- butarray[i].setOnClickListener(this);
- }
- }
- @Override
- public void onClick(View view) {
- if(view.getId() == idsarray[0]){
- super.recreate();
- }
- if(!gameover){
- for(int i = 1; i <= 9; i++){
- if(view.getId() == idsarray[i]){
- marcar(butarray[i],i);
- }
- }
- }
- }
- public void marcar(Button boton, int pos){
- if (boton.getText().equals("")){ // Hacer algo si el boton no tiene texto
- if (esTurnoO) {
- valsO[turnoO] = valoresarray[pos]; // Agrega un valor al array cada que se presiona el boton
- turnoO++;
- boton.setText("O");
- if(turnoO >= 3) verificarGanador();
- if(!gameover){ // Si no hay victoria, se continua
- tviMensaje.setText("Le toca al jugador X..");
- esTurnoO = false;
- }
- } else {
- valsX[turnoX] = valoresarray[pos]; // Agrega un valor al array cada que se presiona el boton
- turnoX++;
- boton.setText("X");
- if(turnoX >= 3) verificarGanador();
- if(!gameover){ // Si no hay victoria, se continua
- tviMensaje.setText("Le toca al jugador O..");
- esTurnoO = true;
- }
- }
- }
- }
- public void verificarGanador(){
- int sumaij;
- if(esTurnoO){
- sumaO = 0; // Limpiamos sumaO para cada verificacion
- for(int i = 0; i < turnoO; i++){
- sumaO += valsO[i]; // obtenemos la suma
- }
- if(turnoO == 3 && sumaO == 15){ // Si la suma es 15, O gana en su 3er turno
- tviMensaje.setText("El jugador O ganó!");
- pintar();
- gameover = true;
- }else if(turnoO == 4){
- for(int i = 0; i < turnoO; i++){
- if(sumaO - valsO[i] == 15){ // Si la suma de 3 numeros de los 4 dentro del array es 15, O gana en el 4to turno
- tviMensaje.setText("El jugador O ganó!");
- valsO[i] = 0;
- pintar();
- gameover = true;
- }
- }
- }else if(turnoO == 5){
- for(int i = 0; i < turnoO; i++){
- for(int j = i + 1; j < turnoO; j++){
- sumaij = valsO[i] + valsO[j];
- if(sumaO - sumaij == 15){
- tviMensaje.setText("El jugador O ganó!");
- valsO[i] = 0;
- valsO[j] = 0;
- pintar();
- gameover = true;
- }else{
- tviMensaje.setText("Hubo un empate!");
- gameover = true;
- }
- }
- }
- }
- }else{
- sumaX = 0; // Limpiamos sumaX para cada verificacion
- for(int i = 0; i < turnoX; i++){
- sumaX += valsX[i]; // obtenemos la suma
- }
- if(turnoX == 3 && sumaX == 15){ // Si la suma es 15, O gana en su 3er turno
- tviMensaje.setText("El jugador X ganó!");
- pintar();
- gameover = true;
- }else if(turnoX == 4){
- for(int i = 0; i < turnoX; i++){
- if(sumaX - valsX[i] == 15){ // Si la suma de 3 numeros de los 4 dentro del array es 15, X gana en el 4to turno
- tviMensaje.setText("El jugador X ganó!");
- valsX[i] = 0;
- pintar();
- gameover = true;
- }
- }
- }else if(turnoX == 5){
- for(int i = 0; i < turnoX; i++){
- for(int j = i + 1; j < turnoX; j++){
- sumaij = valsX[i] + valsX[j];
- if(sumaO - sumaij == 15){
- tviMensaje.setText("El jugador O ganó!");
- valsX[i] = 0;
- valsX[j] = 0;
- pintar();
- gameover = true;
- }else{
- tviMensaje.setText("Hubo un empate!");
- gameover = true;
- }
- }
- }
- }
- }
- }
- public void pintar(){
- if(esTurnoO) {
- for(int i = 0; i < turnoO; i++) {
- if(valsO[i] != 0){
- for(int j = 1; j <= 9; j++){
- if(valsO[i] == valoresarray[j]){
- butarray[j].setBackgroundColor(Color.parseColor("#FF3232"));
- }
- }
- }
- }
- }else{
- for(int i = 0; i < turnoX; i++){
- if(valsX[i] != 0){
- for(int j = 1; j <= 9; j++){
- if(valsX[i] == valoresarray[j]){
- butarray[j].setBackgroundColor(Color.parseColor("#3232FF"));
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement