Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. package grafica;
  2. import java.util.Random;
  3.  
  4. public class Gioco2048 {
  5. private int numeroRighe = 10;
  6. private int numeroColonne = 10;
  7. private int[][] matriceGiocatore1 = new int[numeroRighe][numeroColonne];
  8. private int[][] matriceGiocatore2 = new int[numeroRighe][numeroColonne];;
  9. boolean turno;
  10. boolean giocoTerminato1;
  11. boolean giocoTerminato2;
  12. private String istruzione1;
  13. private String istruzione2;
  14.  
  15. public static void main(String[] args) {
  16. Gioco2048 g = new Gioco2048();
  17. int[][] m1 = g.matriceGiocatore1;
  18. int[][] m2 = g.matriceGiocatore2;
  19. /*int[] v = {2,2,0,2,0,2,0,2,0,2,2,0,0};
  20. stampaRiga(g.spostamentoVersoDestra(v));
  21. stampaRiga(g.spostamentoVersoSinistra2(v));*/
  22. g.inizializzazioneMatrice(m1);
  23. stampaMatrice(m1);
  24. System.out.println("");
  25. g.slittamentoCelle(m1,"Destra");
  26. stampaMatrice(m1);
  27. System.out.println("");
  28. g.inizializzazioneMatrice(m2);
  29. stampaMatrice(m2);
  30. System.out.println("");
  31. g.slittamentoCelle(m2,"Sopra");
  32. stampaMatrice(m2);
  33. }
  34.  
  35. public static void stampaRiga (int[] v) {
  36. System.out.print("[ ");
  37. for (int i = 0; i < v.length; i++ ) {
  38. if ( i == v.length - 1) {
  39. System.out.print(v[i]+" ");}
  40. else {
  41. System.out.print(v[i]+", ");} }
  42. System.out.println("]");}
  43.  
  44. public static void stampaMatrice (int [][] m) {
  45. for(int i = 0; i < m.length; i++) {
  46. stampaRiga(m[i]);}}
  47.  
  48.  
  49.  
  50. public void generaRandom2(int[][] m) {
  51. Random generatore = new Random();
  52. int indiceRiga;
  53. int indiceColonna;
  54. indiceRiga = generatore.nextInt(10);
  55. indiceColonna = generatore.nextInt(10);
  56. if(m[indiceRiga][indiceColonna] == 0) {
  57. m[indiceRiga][indiceColonna] = 2;
  58. }
  59. else {
  60. while(m[indiceRiga][indiceColonna] != 0) {
  61. indiceRiga = generatore.nextInt(10);
  62. indiceColonna = generatore.nextInt(10);
  63. }
  64. m[indiceRiga][indiceColonna] = 2;
  65. }
  66. }
  67.  
  68. public void inizializzazioneMatrice(int[][] m) {
  69. for(int i = 0; i < 4; i++) {
  70. generaRandom2(m);
  71. }
  72. }
  73.  
  74. private int[] spostamentoVersoDestra(int[] v) {
  75. int[] result = new int[v.length];
  76. int indiceResult = v.length - 1;
  77. int el_1 = v.length - 1;
  78. while(el_1 >= 0) {
  79. while(el_1 >=0 && v[el_1] == 0) {
  80. el_1 --;
  81. }
  82. if(el_1 == -1) {
  83. return v;
  84. }
  85. int el_2 = el_1 -1;
  86. while(el_2 >=0 && v[el_2] == 0) {
  87. el_2--;
  88. }
  89. if(el_2 == -1) {
  90. result[indiceResult] = v[el_1];
  91. indiceResult--;
  92. return result;
  93. }
  94. if(v[el_1] == v[el_2]) {
  95. result[indiceResult] = v[el_1]+v[el_2];
  96. indiceResult --;
  97. el_1 = el_2-1;
  98. }
  99. else {
  100. result[indiceResult] = v[el_1];
  101. indiceResult --;
  102. el_1 = el_2;
  103. }
  104. }
  105.  
  106. return result;
  107. }
  108.  
  109. private int[] spostamentoVersoSinistra(int[] v) {
  110. int[] result = new int[v.length];
  111. int indiceResult = 0;
  112. int el_1 = 0;
  113. while(el_1 < v.length) {
  114. while(el_1 < v.length && v[el_1] == 0) {
  115. el_1 ++;
  116. }
  117. if(el_1 == v.length) {
  118. return v;
  119. }
  120. int el_2 = el_1 + 1;
  121. while(el_2 < v.length && v[el_2] == 0) {
  122. el_2++;
  123. }
  124. if(el_2 == v.length) {
  125. result[indiceResult] = v[el_1];
  126. indiceResult++;
  127. return result;
  128. }
  129. if(v[el_1] == v[el_2]) {
  130. result[indiceResult] = v[el_1]+v[el_2];
  131. indiceResult ++;
  132. el_1 = el_2+1;
  133. }
  134. else {
  135. result[indiceResult] = v[el_1];
  136. indiceResult ++;
  137. el_1 = el_2;
  138. }
  139. }
  140.  
  141. return result;
  142. }
  143.  
  144. public void slittamentoCelle(int[][] m, String istruzione) {
  145. if(istruzione == "Destra") {
  146. for(int i = 0; i < m.length; i++) {
  147. m[i] = spostamentoVersoDestra(m[i]);
  148. }
  149. }
  150. if(istruzione == "Sinistra") {
  151. for(int i = 0; i < m.length; i++) {
  152. m[i] = spostamentoVersoSinistra(m[i]);
  153. }
  154. }
  155. if(istruzione == "Sopra") {
  156. int indiceColonna = 0;
  157. while(indiceColonna < m.length) {
  158. int[] colonna = new int[m.length];
  159. for(int i = 0; i < m.length; i++) {
  160. colonna[i] = m[i][indiceColonna];
  161. }
  162. colonna = spostamentoVersoSinistra(colonna);
  163. for(int j = 0; j < colonna.length; j++) {
  164. m[j][indiceColonna] = colonna[j];
  165. }
  166. indiceColonna++;
  167. }
  168. }
  169. if(istruzione == "Sotto") {
  170. int indiceColonna = 0;
  171. while(indiceColonna < m.length) {
  172. int[] colonna = new int[m.length];
  173. for(int i = 0; i < m.length; i++) {
  174. colonna[i] = m[i][indiceColonna];
  175. }
  176. colonna = spostamentoVersoDestra(colonna);
  177. for(int j = 0; j < colonna.length; j++) {
  178. m[j][indiceColonna] = colonna[j];
  179. }
  180. indiceColonna++;
  181. }
  182. }
  183. }
  184.  
  185. public boolean mossaConsentita(int[][] m, String istruzione) {
  186. int[][] copiaDiM = copiaMatrice(m);
  187. Gioco2048 g = new Gioco2048();
  188. g.slittamentoCelle(m,istruzione);
  189. if(equivalenzaMatrici(m,copiaDiM)) {
  190. return false;
  191. }
  192. return true;
  193. }
  194.  
  195. private int[][] copiaMatrice(int[][] m) {
  196. int[][] result = new int[m.length][m[0].length];
  197. for(int i = 0; i< m.length; i++) {
  198. for(int j = 0; j < m[0].length; j++) {
  199. result[i][j] = m[i][j];
  200. }
  201. }
  202. return result;
  203. }
  204.  
  205. private boolean equivalenzaMatrici(int[][] m, int[][] n) {
  206. boolean result = true;
  207. for(int i = 0; i< m.length; i++) {
  208. for(int j = 0; j < m[0].length; j++) {
  209. if(m[i][j] != n[i][j]) {
  210. result = false;
  211. break;
  212. }
  213. }
  214. if(!result) {
  215. break;
  216. }
  217. }
  218. return result;
  219. }
  220.  
  221. public boolean controlloSconfitta(int[][] m) {
  222. boolean result = true;
  223. String[] istruzioniPossibili = {"Sopra","Sotto","Destra","Sinistra"};
  224. for(int i = 0; i < istruzioniPossibili.length; i++) {
  225. if(mossaConsentita(m, istruzioniPossibili[i])) {
  226. result = false;
  227. break;
  228. }
  229. }
  230. return result;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement