Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package juego;
  2.  
  3. import javafx.geometry.Insets;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.layout.BorderPane;
  7. import javafx.scene.layout.GridPane;
  8. import javafx.scene.paint.Color;
  9. import javafx.scene.paint.Paint;
  10. import javafx.scene.shape.Circle;
  11. import javafx.scene.text.Font;
  12. import javafx.scene.text.Text;
  13. import javafx.stage.Modality;
  14. import javafx.stage.Stage;
  15.  
  16. /**
  17. * Representación gráfica del Tablero del Juego Cuatro en Lí­nea.
  18. *
  19. */
  20. public class Tablero {
  21.  
  22. private static final int ALTO_FILA = 80;
  23. private static final int ANCHO_COLUMNA = 80;
  24. private static final int ALTURA_BOTON = 40;
  25. private static final double RADIO = Math.min(ALTO_FILA - 1, ANCHO_COLUMNA - 1) / 2;
  26.  
  27. private CuatroEnLinea juego;
  28. private GridPane grilla;
  29. private Stage escenario;
  30.  
  31. /**
  32. * post: asocia el Tablero a 'nuevoJuego' y lo inicializa a partir de su estado.
  33. *
  34. * @param nuevoJuego
  35. */
  36. public Tablero(CuatroEnLinea nuevoJuego) {
  37.  
  38. juego = nuevoJuego;
  39. escenario = new Stage();
  40. grilla = new GridPane();
  41. }
  42.  
  43. /**
  44. * post: muestra el Tablero en pantalla.
  45. */
  46. public void mostrar() {
  47.  
  48. dibujarBotones();
  49.  
  50. double ancho = juego.contarColumnas() * ANCHO_COLUMNA;
  51. double alto = (juego.contarFilas() * ALTO_FILA) + ALTURA_BOTON;
  52.  
  53. Scene escena = new Scene(grilla, ancho, alto);
  54.  
  55. escenario.setScene(escena);
  56. escenario.setResizable(false);
  57. escenario.setTitle(Aplicacion.TITULO);
  58.  
  59. dibujar();
  60.  
  61. escenario.show();
  62. }
  63.  
  64. /**
  65. * post: agrega los botones para soltar una ficha en cada columna del Tablero.
  66. */
  67. private void dibujarBotones() {
  68.  
  69. for (int columna = 1; columna <= juego.contarColumnas(); columna++) {
  70.  
  71. Button botonSoltarFicha = new Button("soltar");
  72. botonSoltarFicha.setMinHeight(ALTURA_BOTON);
  73.  
  74. botonSoltarFicha.setOnAction(new SoltarFicha(this, juego, columna));
  75. botonSoltarFicha.setMinWidth(ANCHO_COLUMNA);
  76. grilla.add(botonSoltarFicha, columna - 1, 0);
  77. }
  78. }
  79.  
  80. /**
  81. * post: actualiza el Tablero a partir del estado del juego asociado.
  82. */
  83. public void dibujar() {
  84.  
  85. for (int fila = 1; fila <= juego.contarFilas(); fila++) {
  86.  
  87. for (int columna = 1; columna <= juego.contarColumnas(); columna++) {
  88.  
  89. Casillero casillero = juego.obtenerCasillero(fila, columna);
  90.  
  91. Circle dibujoCasillero = dibujarCasillero(casillero);
  92.  
  93. grilla.add(dibujoCasillero, columna - 1, fila);
  94. }
  95. }
  96. }
  97.  
  98. /**
  99. * post: dibuja y devuelve el casillero dado.
  100. *
  101. * @param casillero
  102. * @return representación gráfica del Casillero.
  103. */
  104. private Circle dibujarCasillero(Casillero casillero) {
  105.  
  106. Circle dibujoCasillero = new Circle(RADIO, obtenerPintura(casillero));
  107.  
  108. dibujoCasillero.setStroke(new Color(0.5, 0.5, 0.5, 1.0));
  109. dibujoCasillero.setScaleX(0.95);
  110. dibujoCasillero.setScaleY(0.95);
  111. return dibujoCasillero;
  112. }
  113.  
  114. /**
  115. * post: determina la pintura a utilizar para 'casillero'.
  116.  
  117. * @param casillero
  118. * @return pintura a utilizar para identificar el Casillero.
  119. */
  120. private Paint obtenerPintura(Casillero casillero) {
  121.  
  122. Paint pintura;
  123.  
  124. switch (casillero) {
  125.  
  126. case AMARILLO:
  127. pintura = Color.BLACK;
  128. break;
  129.  
  130. case ROJO:
  131. pintura = Color.RED;
  132. break;
  133.  
  134. default:
  135. pintura = Color.WHITE;
  136. }
  137.  
  138. return pintura;
  139. }
  140.  
  141. /**
  142. * pre : el juego asociado terminó.
  143. * post: muestra un mensaje indicando el resultado del juego.
  144. */
  145. public void mostrarResultado() {
  146.  
  147. Stage dialogo = new Stage();
  148.  
  149. BorderPane panelGanador = new BorderPane();
  150. panelGanador.setPadding(new Insets(10.0));
  151. Text textoResultado;
  152. Font fuente = new Font(40.0);
  153.  
  154. if (juego.hayGanador()) {
  155.  
  156. textoResultado = new Text("Ganó el jugador " + juego.obtenerGanador());
  157.  
  158. } else {
  159.  
  160. textoResultado = new Text("Empataron");
  161. }
  162.  
  163. textoResultado.setFont(fuente);
  164. panelGanador.setCenter(textoResultado);
  165.  
  166. Scene escenaGanador = new Scene(panelGanador);
  167.  
  168. dialogo.setScene(escenaGanador);
  169. dialogo.initOwner(escenario);
  170. dialogo.initModality(Modality.WINDOW_MODAL);
  171. dialogo.setResizable(false);
  172.  
  173. dialogo.showAndWait();
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement