Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package com.example.juegotresenraya;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.graphics.Typeface;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends AppCompatActivity
  14. {
  15. Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnReset, btnSalir;
  16. TextView tvTexto;
  17.  
  18. //Variables
  19. int i;
  20. int posJugador = -1;
  21. int posOrdenador = -1;
  22. Button[] arrayBtn;
  23.  
  24. //Instanciamos la clase juego3R
  25. juego3R play;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState)
  29. {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32.  
  33. play = new juego3R();
  34.  
  35. btn1 = findViewById(R.id.idBtn1);
  36. btn2 = findViewById(R.id.idBtn2);
  37. btn3 = findViewById(R.id.idBtn3);
  38. btn4 = findViewById(R.id.idBtn4);
  39. btn5 = findViewById(R.id.idBtn5);
  40. btn6 = findViewById(R.id.idBtn6);
  41. btn7 = findViewById(R.id.idBtn7);
  42. btn8 = findViewById(R.id.idBtn8);
  43. btn9 = findViewById(R.id.idBtn9);
  44. btnReset = findViewById(R.id.idBtnReset);
  45. btnSalir = findViewById(R.id.idBtnSalir);
  46. tvTexto = findViewById(R.id.idTexto);
  47.  
  48. arrayBtn = new Button[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8,btn9};
  49. }
  50.  
  51. public void onClick(View v)
  52. {
  53. Button b = (Button) v;
  54. posJugador = Integer.parseInt(b.getTag().toString());
  55.  
  56. // 1 - comprobar qué botón se ha pulsado
  57. // 2 - comprobar si el movimiento es válido para el jug1
  58.  
  59. if(play.movimientoValido(posJugador))
  60. {
  61. play.mueveJugador1(posJugador);
  62. b.setTextColor(Color.RED);
  63. b.setText("X");
  64.  
  65. // 3 - comprobar si se ha acabado la partida ganajugador1 o quedanmovimiento
  66. if(!play.ganaJugador1() && play.quedanMovimientos())
  67. {
  68. // 4 - tocaría mover al ordenador -> mueveordenador2
  69. // 5 - hay que poner el O en el botón correspendiente
  70. // 6 - comprobar si ha ganado el ordenador
  71.  
  72. posOrdenador = play.mueveOrdenador2();
  73. arrayBtn[posOrdenador-1].setTextColor(Color.YELLOW);
  74. arrayBtn[posOrdenador-1].setText("O");
  75.  
  76. if(play.ganaJugador2())
  77. {
  78. tvTexto.setTextColor(Color.YELLOW);
  79. tvTexto.setTypeface(null, Typeface.BOLD);
  80. tvTexto.setText("GANA LA MÁQUINA");
  81.  
  82. for(i = 0; i < arrayBtn.length; i++)
  83. {
  84. arrayBtn[i].setEnabled(false);
  85. }
  86. }
  87. }
  88. else
  89. {
  90. if(!play.quedanMovimientos() && !play.ganaJugador1() && !play.ganaJugador2())
  91. {
  92. //Empate que se lanza al Activity2
  93. Intent intent = new Intent(this, ActivityCalabera.class);
  94. String mensajeEmpate = "EMPATE";
  95. intent.putExtra("MENSAJE",mensajeEmpate);
  96. startActivity(intent);
  97.  
  98. /*tvTexto.setTextColor(Color.BLUE);
  99. tvTexto.setText("EMPATE");*/
  100.  
  101. for(i = 0; i < arrayBtn.length; i++)
  102. {
  103. arrayBtn[i].setEnabled(false);
  104. }
  105. }
  106. else
  107. {
  108. tvTexto.setTextColor(Color.RED);
  109. tvTexto.setTypeface(null, Typeface.BOLD);
  110. tvTexto.setText("GANA JUGADOR");
  111. for(i = 0; i < arrayBtn.length; i++)
  112. {
  113. arrayBtn[i].setEnabled(false);
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. mensaje("Movimiento no válido");
  121. }
  122. }
  123.  
  124. public void onClickReset(View v)
  125. {
  126. play.iniciar();
  127. for(int i = 0; i < arrayBtn.length; i++)
  128. {
  129. arrayBtn[i].setText("");
  130. arrayBtn[i].setEnabled(true);
  131. }
  132. tvTexto.setTextColor(Color.BLACK);
  133. tvTexto.setTypeface(null, Typeface.BOLD);
  134. tvTexto.setText("JUEGO TRES EN RAYA");
  135. }
  136.  
  137. public void onClickSalir(View v)
  138. {
  139. System.exit(0);
  140. }
  141.  
  142. public void mensaje(String msg)
  143. {
  144. Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement