Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.juegotresenraya;
- import android.content.Intent;
- import android.graphics.Color;
- import android.graphics.Typeface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity
- {
- Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnReset, btnSalir;
- TextView tvTexto;
- //Variables
- int i;
- int posJugador = -1;
- int posOrdenador = -1;
- Button[] arrayBtn;
- //Instanciamos la clase juego3R
- juego3R play;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- play = new juego3R();
- btn1 = findViewById(R.id.idBtn1);
- btn2 = findViewById(R.id.idBtn2);
- btn3 = findViewById(R.id.idBtn3);
- btn4 = findViewById(R.id.idBtn4);
- btn5 = findViewById(R.id.idBtn5);
- btn6 = findViewById(R.id.idBtn6);
- btn7 = findViewById(R.id.idBtn7);
- btn8 = findViewById(R.id.idBtn8);
- btn9 = findViewById(R.id.idBtn9);
- btnReset = findViewById(R.id.idBtnReset);
- btnSalir = findViewById(R.id.idBtnSalir);
- tvTexto = findViewById(R.id.idTexto);
- arrayBtn = new Button[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8,btn9};
- }
- public void onClick(View v)
- {
- Button b = (Button) v;
- posJugador = Integer.parseInt(b.getTag().toString());
- // 1 - comprobar qué botón se ha pulsado
- // 2 - comprobar si el movimiento es válido para el jug1
- if(play.movimientoValido(posJugador))
- {
- play.mueveJugador1(posJugador);
- b.setTextColor(Color.RED);
- b.setText("X");
- // 3 - comprobar si se ha acabado la partida ganajugador1 o quedanmovimiento
- if(!play.ganaJugador1() && play.quedanMovimientos())
- {
- // 4 - tocaría mover al ordenador -> mueveordenador2
- // 5 - hay que poner el O en el botón correspendiente
- // 6 - comprobar si ha ganado el ordenador
- posOrdenador = play.mueveOrdenador2();
- arrayBtn[posOrdenador-1].setTextColor(Color.YELLOW);
- arrayBtn[posOrdenador-1].setText("O");
- if(play.ganaJugador2())
- {
- tvTexto.setTextColor(Color.YELLOW);
- tvTexto.setTypeface(null, Typeface.BOLD);
- tvTexto.setText("GANA LA MÁQUINA");
- for(i = 0; i < arrayBtn.length; i++)
- {
- arrayBtn[i].setEnabled(false);
- }
- }
- }
- else
- {
- if(!play.quedanMovimientos() && !play.ganaJugador1() && !play.ganaJugador2())
- {
- //Empate que se lanza al Activity2
- Intent intent = new Intent(this, ActivityCalabera.class);
- String mensajeEmpate = "EMPATE";
- intent.putExtra("MENSAJE",mensajeEmpate);
- startActivity(intent);
- /*tvTexto.setTextColor(Color.BLUE);
- tvTexto.setText("EMPATE");*/
- for(i = 0; i < arrayBtn.length; i++)
- {
- arrayBtn[i].setEnabled(false);
- }
- }
- else
- {
- tvTexto.setTextColor(Color.RED);
- tvTexto.setTypeface(null, Typeface.BOLD);
- tvTexto.setText("GANA JUGADOR");
- for(i = 0; i < arrayBtn.length; i++)
- {
- arrayBtn[i].setEnabled(false);
- }
- }
- }
- }
- else
- {
- mensaje("Movimiento no válido");
- }
- }
- public void onClickReset(View v)
- {
- play.iniciar();
- for(int i = 0; i < arrayBtn.length; i++)
- {
- arrayBtn[i].setText("");
- arrayBtn[i].setEnabled(true);
- }
- tvTexto.setTextColor(Color.BLACK);
- tvTexto.setTypeface(null, Typeface.BOLD);
- tvTexto.setText("JUEGO TRES EN RAYA");
- }
- public void onClickSalir(View v)
- {
- System.exit(0);
- }
- public void mensaje(String msg)
- {
- Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement