Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class MineSweeper2 {
  5. static ArrayList<String> ranks = new ArrayList<>();
  6. static String nom;
  7. static Scanner sc = new Scanner(System.in);
  8. static int files;
  9. static int columnes;
  10. static int nmines;
  11. static int[][] mines = new int[files][columnes];
  12. static int[][] camp = new int[files][columnes];
  13. static Taulell t = new Taulell();
  14. static Finestra f = new Finestra(t);
  15.  
  16. public static void main(String[] args) {
  17.  
  18. //////Inicialització GUI
  19.  
  20.  
  21.  
  22. /////
  23. boolean fi=false;
  24. while (fi == false) {
  25. int op = mostramenu();
  26. switch (op) {
  27. case 2:
  28. opcions();
  29. break;
  30. case 3:
  31. jugar();
  32. break;
  33. case 0:
  34. fi=true;
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. private static int mostramenu() {
  41. // TODO Auto-generated method stub
  42. System.out.println("1 instruccions 2 opcions 3 jugar 0 bye");
  43. int n = sc.nextInt();
  44. return n;
  45. }
  46.  
  47. private static void opcions() {
  48. // TODO Auto-generated method stub
  49. nom = sc.nextLine();
  50. files = sc.nextInt();
  51. columnes = sc.nextInt();
  52. nmines = sc.nextInt();
  53.  
  54. }
  55.  
  56.  
  57. private static void jugar() {
  58. // TODO Auto-generated method stub
  59.  
  60. inicialitzarMines();
  61. inicialitzarCamp();
  62. inicialitzarGUI();
  63. System.out.println("init done");
  64. int viu = 0;
  65. //visualitzarMatriu(mines);
  66. while (viu == 0) {
  67. System.out.println("coords?");
  68. int x = sc.nextInt();
  69. int y = sc.nextInt();
  70. int d = descobrir(x, y);
  71. viu = partidaAcabada(d);
  72. visualitzarMatriu(camp);
  73. }
  74. fiPartida(viu);
  75. }
  76.  
  77.  
  78. private static void visualitzarMatriu(int matriu[][]) {
  79. // TODO Auto-generated method stub
  80. for (int f = 0; f < files; f++) {
  81. for (int c = 0; c < columnes; c++) {
  82. System.out.print(matriu[f][c] + " ");
  83. }
  84. System.out.println();
  85. }
  86. t.dibuixa(matriu);
  87. }
  88.  
  89. private static int descobrir(int x, int y) {
  90.  
  91. int valor = click(x, y, mines);
  92. if(valor==0 && camp[x][y]==9) {
  93. camp[x][y]=0;
  94.  
  95. }
  96. if(valor!=-1) {
  97. camp[x][y] = valor;
  98.  
  99. }
  100.  
  101. return valor;
  102.  
  103. }
  104.  
  105.  
  106. private static void inicialitzarCamp() {
  107. // TODO Auto-generated method stub
  108. camp = new int[files][columnes];
  109. for (int f = 0; f < files; f++) {
  110. for (int c = 0; c < columnes; c++) {
  111. camp[f][c] = 9;
  112. }
  113. }
  114.  
  115. }
  116.  
  117. private static void inicialitzarMines() {
  118. // TODO Auto-generated method stub
  119. mines = new int[files][columnes];
  120. int n = nmines;
  121. while (n > 0) {
  122. int f = (int) (Math.random() * files);
  123. int c = (int) (Math.random() * columnes);
  124. if (mines[f][c] != 1) {
  125. mines[f][c] = 1;
  126. n--;
  127. //System.out.println("mina colocada en"+f+" "+c);
  128. }
  129. }
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement