Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. package juego;
  2.  
  3. public class CuatroEnLinea {
  4. private String jugadorRojo;
  5. private String jugadorNegro;
  6. private String jugadorGanador;
  7.  
  8. private Casillero[][] tablero;
  9. private int jugadaNumero = 0;
  10. private boolean huboGanador = false;
  11.  
  12. public CuatroEnLinea(int filas, int columnas, String jugadorRojo, String jugadorNegro) {
  13. //*Crea un objeto de la clase Mensajes.
  14. Mensajes mensaje = new Mensajes();
  15.  
  16. if (filas < 4 || columnas < 4){
  17. mensaje.startWhitError();
  18. throw new Error("Tanto las filas como las columnas deben ser mayores o iguales a 4");
  19.  
  20. }
  21. this.jugadorNegro = jugadorNegro;
  22. this.jugadorRojo = jugadorRojo;
  23. this.tablero = new Casillero[columnas][filas];
  24. for (int i = 0; i < columnas; i++){
  25. for(int j =0; j < filas; j++){
  26. tablero[i][j] = Casillero.VACIO;
  27.  
  28.  
  29. }
  30. }
  31. mensaje.start();
  32. }
  33.  
  34. /**
  35. * post: devuelve la cantidad máxima de fichas que se pueden apilar en el
  36. * tablero.
  37. */
  38. public int contarFilas() {
  39. return tablero.length;
  40. }
  41.  
  42. /**
  43. * post: devuelve la cantidad máxima de fichas que se pueden alinear en el
  44. * tablero.
  45. */
  46. public int contarColumnas() {
  47. return tablero[0].length;
  48. }
  49.  
  50. /**
  51. * pre : fila está en el intervalo [1, contarFilas()], columnas está en el
  52. * intervalo [1, contarColumnas()]. post: indica qué ocupa el casillero en
  53. * la posición dada por fila y columna.
  54. *
  55. * @param fila
  56. * @param columna
  57. */
  58. public Casillero obtenerCasillero(int fila, int columna) {
  59. fila--;
  60. columna--;
  61.  
  62. if ((fila < 0) || (fila >= contarFilas()) || (columna < 0)
  63. || (columna >= contarColumnas())) {
  64. throw new Error(
  65. "El índice de fila/columna está fuera del intervalo.");
  66. }
  67.  
  68. return tablero[fila][columna];
  69. }
  70.  
  71. /**
  72. * pre : el juego no terminó, columna está en el intervalo [1,
  73. * contarColumnas()] y aún queda un Casillero.VACIO en la columna indicada.
  74. * post: deja caer una ficha en la columna indicada.
  75. *
  76. * @param columna
  77. */
  78. public void soltarFicha(int columna) {
  79. columna--; //*Se resta uno a la columna dado que el parámetro esperado sera una unidad superior*/
  80.  
  81.  
  82. int ultimoLugarVacio = 0; //*Guarda posicion del ultimo lugar vacio encontrado*/
  83. boolean lugarVacio = false;
  84.  
  85. for (int i = 0; i < tablero.length; i++) { //*Se recorre el arreglo de abajo para arriba buscando el ultimo lugar vacio*//
  86. if (tablero[i][columna] == Casillero.VACIO) {
  87. lugarVacio = true; //Si cumple la condicion lugarVacio es verdadero*/
  88. ultimoLugarVacio = i; //* Se guarda la posicion del lugar vacio/*
  89. i = tablero.length; //* Termina el ciclo for*/
  90. }
  91. }
  92.  
  93. boolean puedeSoltar = (termino() == false) && (columna >= 0) //*Comprueba la condicion del juego y la jugada que se pretende hacer*/
  94. && (columna < contarColumnas()) && lugarVacio;
  95.  
  96. if (puedeSoltar) {
  97. if (ultimoLugarVacio < tablero.length) {
  98. for (int i = ultimoLugarVacio + 1; i < tablero.length; i++) {
  99. if (tablero[i][columna] == Casillero.VACIO) {
  100. ultimoLugarVacio = i;
  101. }
  102. }
  103. }
  104.  
  105. tablero[ultimoLugarVacio][columna] = ((jugadaNumero % 2) == 0 ? Casillero.ROJO
  106. : Casillero.AMARILLO);
  107.  
  108. procesarGanador();
  109. jugadaNumero++;
  110. }
  111. }
  112.  
  113. /**
  114. * post: indica si el juego terminó porque uno de los jugadores ganó o no
  115. * existen casilleros vacíos.
  116. */
  117. public boolean termino() {
  118. if (hayGanador()) {
  119. return true;
  120. }
  121.  
  122. for (int i = 0; i < contarFilas(); i++) {
  123. for (int j = 0; j < contarColumnas(); j++) {
  124. if (tablero[i][j] == Casillero.VACIO) {
  125. return false;
  126. }
  127. }
  128. }
  129.  
  130. return true;
  131. }
  132.  
  133. //Con el casillero podemos saber qué jugador de qué color fue el ganador:
  134. private boolean hayCuatroCasillerosSeguidos(Casillero color, int x, int y) {
  135. if (tablero[x][y] == color) {
  136. int horizontal = 1;
  137. int vertical = 1;
  138. int diagonalDerecha = 1;
  139. int diagonalIzquierda = 1;
  140.  
  141. for (int i = 1; i < 4; i++) {
  142. boolean puedeEnX = ((x + i) < contarFilas());
  143. boolean puedeEnY = ((y - i) >= 0);
  144.  
  145. if (puedeEnX){
  146. if (tablero[x + i][y] == color) {
  147. horizontal++;
  148. if (horizontal == 4)
  149. return true;
  150. }
  151. }
  152.  
  153. if (puedeEnY){
  154. if (tablero[x][y - i] == color) {
  155. vertical++;
  156. if (vertical == 4)
  157. return true;
  158. }
  159. }
  160.  
  161. if (puedeEnX && ((y + i) < contarColumnas())){
  162. if (tablero[x + i][y + i] == color) {
  163. diagonalDerecha++;
  164. if (diagonalDerecha == 4)
  165. return true;
  166. }
  167. }
  168.  
  169. if (((x - i) > 0) && ((y + i) < contarColumnas())){
  170. if (tablero[x - i][y + i] == color) {
  171. diagonalIzquierda++;
  172. if (diagonalIzquierda == 4)
  173. return true;
  174. }
  175. }
  176. }
  177.  
  178. }
  179.  
  180. return false;
  181. }
  182.  
  183. /**
  184. * post: indica si el juego terminó y tiene un ganador.
  185. */
  186. private void procesarGanador() {
  187. for (int x = 0; x < contarFilas(); x++) {
  188. for (int y = 0; y < contarColumnas(); y++) {
  189. if ((jugadaNumero % 2) == 0) {
  190. if (hayCuatroCasillerosSeguidos(Casillero.ROJO, x, y)){
  191. jugadorGanador = jugadorRojo;
  192. huboGanador = true;
  193. }
  194. } else {
  195. if (hayCuatroCasillerosSeguidos(Casillero.AMARILLO, x, y)){
  196. jugadorGanador = jugadorNegro;
  197. huboGanador = true;
  198. }
  199. }
  200. }
  201. }
  202. }
  203.  
  204. /**
  205. * post: indica si el juego terminó y tiene un ganador.
  206. */
  207. public boolean hayGanador() {
  208. return huboGanador;
  209. }
  210.  
  211. /**
  212. * pre : el juego terminó. post: devuelve el nombre del jugador que ganó el
  213. * juego.
  214. */
  215. public String obtenerGanador() {
  216. return jugadorGanador;
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement