Guest User

Untitled

a guest
Aug 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. MINY MAIN
  2. package javaapplication16;
  3.  
  4. import java.util.Scanner;
  5.  
  6. /* hledani min */
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11.  
  12. HraciPole a = new HraciPole(10,10,10);
  13.  
  14. a.Start();
  15.  
  16. while(a.GameOver == false && a.Finish == false) {
  17. a.VypisPrubeznehoPole();
  18. SOPLN("Zadejte souradnice: radek sloupec");
  19. int m = sc.nextInt();
  20. int n = sc.nextInt();
  21. a.Hadam(m, n);
  22. }
  23.  
  24. if (a.GameOver == true) {
  25. SOPLN(" ");
  26. a.VypisKonecnehoPole();
  27. SOPLN(" ");
  28. SOPLN("Slapli jste na minu!");
  29. SOPLN("!!! Game Over !!!");
  30. SOPLN(" ");
  31. }
  32. else {
  33. SOPLN(" ");
  34. a.VypisKonecnehoPole();
  35. SOPLN(" ");
  36. SOPLN("Blahopřeji !! Našli jste všechny miny !!");
  37. }
  38.  
  39. }
  40.  
  41. public static void SOPLN(String s) {
  42. System.out.println(s);
  43. }
  44. public static void SOP(String s) {
  45. System.out.print(s);
  46. }
  47.  
  48.  
  49.  
  50. }
  51. ---------------------------------------------------------------------------------
  52. CLASS HRACIPOLE
  53.  
  54. /*
  55. * To change this template, choose Tools | Templates
  56. * and open the template in the editor.
  57. */
  58.  
  59. package javaapplication16;
  60.  
  61. /**
  62. *
  63. * @author Roman Belda
  64. */
  65. public class HraciPole {
  66. int m;
  67. int n;
  68. int PocetMin;
  69. int[][] HraciPole;
  70. int[][] HadaciPole;
  71. boolean GameOver = false;
  72. boolean Finish = false;
  73.  
  74. HraciPole(int m, int n, int PocetMin) {
  75. this.m = m;
  76. this.n = n;
  77. this.PocetMin = PocetMin;
  78. this.HraciPole = new int[m][n];
  79. this.HadaciPole = new int[m][n];
  80. }
  81.  
  82. public void Start() {
  83. GenerovaniMin();
  84. SpocitejPoleMin();
  85. }
  86.  
  87. public void Hadam(int m, int n) {
  88. for (int i=0; i<this.m; i++){
  89. for (int j=0; j<this.n; j++) {
  90. if (this.HraciPole[i][j] == -1 && i == (m-1) && j == (n-1)) this.GameOver = true;
  91. else {
  92. if (this.HraciPole[i][j] == 0) this.HadaciPole[i][j] = 1;
  93. else {
  94. this.HadaciPole[m-1][n-1] = 1;
  95. }
  96. }
  97.  
  98. }
  99. }
  100. }
  101.  
  102. private void GenerovaniMin() {
  103. int minaM = (int) (Math.random()*(this.m-1))+1; // osetreni aby mina nemela adresu mimo hraci pole
  104. int minaN = (int) (Math.random()*(this.n-1))+1;
  105.  
  106. for (int i=1; i<this.m; i++){
  107. for (int j=1; j<this.n; j++) {
  108. if (i == minaM && j == minaN) {
  109. if (this.HraciPole[i][j] == -1){ // osetreni aby nebyli 2 mini na sobe
  110. GenerovaniMin();
  111. }
  112. this.HraciPole[i][j] = -1;
  113. }
  114. }
  115. }
  116.  
  117. this.PocetMin--;
  118. if (this.PocetMin>0) {
  119. GenerovaniMin();
  120. }
  121. }
  122.  
  123. private void SpocitejPoleMin() {
  124. for (int i=0; i<this.m; i++){
  125. for (int j=0; j<this.n; j++) {
  126. if (this.HraciPole[i][j] == -1){
  127. if((i-1)>=0 && (j-1)>=0) if(this.HraciPole[i-1][j-1] != -1) this.HraciPole[i-1][j-1]++;
  128. if((i-1)>=0) if(this.HraciPole[i-1][j] != -1) this.HraciPole[i-1][j]++;
  129. if((i-1)>=0 && (j+1)<this.n) if(this.HraciPole[i-1][j+1] != -1) this.HraciPole[i-1][j+1]++;
  130. if((j-1)>=0) if(this.HraciPole[i][j-1] != -1) this.HraciPole[i][j-1]++;
  131. if((j+1)<this.n) if(this.HraciPole[i][j+1] != -1) this.HraciPole[i][j+1]++;
  132. if((i+1)<this.m && (j-1)>=0) if(this.HraciPole[i+1][j-1] != -1) this.HraciPole[i+1][j-1]++;
  133. if((i+1)<this.m) if(this.HraciPole[i+1][j] != -1) this.HraciPole[i+1][j]++;
  134. if((i+1)<this.m && (j+1)<this.n) if(this.HraciPole[i+1][j+1] != -1) this.HraciPole[i+1][j+1]++;
  135. }
  136. }
  137. }
  138. }
  139.  
  140. public void VypisKonecnehoPole() {
  141. for (int i=0; i<this.m; i++){
  142. for (int j=0; j<this.n; j++) {
  143. if (this.HraciPole[i][j] == -1) System.out.print("• ");
  144. else if (this.HraciPole[i][j] == 0) System.out.print("* ");
  145. else System.out.print(this.HraciPole[i][j]+" ");
  146. }
  147. System.out.println(" ");
  148. }
  149. }
  150.  
  151. public void VypisPrubeznehoPole() {
  152. for (int i=0; i<this.m; i++){
  153. for (int j=0; j<this.n; j++) {
  154. if (this.HadaciPole[i][j] == 1) System.out.print(this.HraciPole[i][j]+" ");
  155. else System.out.print("* ");
  156. /*
  157. if (this.HraciPole[i][j] == -1) System.out.print("• ");
  158. else if (this.HraciPole[i][j] == 0) System.out.print("* ");
  159. else System.out.print(this.HraciPole[i][j]+" ");*/
  160. }
  161. System.out.println(" ");
  162. }
  163. }
  164.  
  165. }
Add Comment
Please, Sign In to add comment