Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. * Created by rickardkruusberg on 10/02/16.
  6. */
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. int[] params;
  11.  
  12. while (true) {
  13. try {
  14. params = getGameParams();
  15. break;
  16. } catch (Exception e) {
  17. System.out.println("Sisestasite midagi valesti, proovige uuesti!");
  18. }
  19. }
  20.  
  21.  
  22. int[][] map = makeMap(params[0], params[1], params[2]);
  23. startGame(map, params[2]);
  24. }
  25.  
  26.  
  27. public static void startGame(int[][] map, int treasuresCount) {
  28. System.out.println("Head kaevamist!");
  29. Scanner sc = new Scanner(System.in);
  30. int digCount = 0;
  31. int treasuresLeft = treasuresCount;
  32. int[] coordinates;
  33.  
  34. while (true) {
  35. System.out.println("Tehtud on " + digCount + " kaevamist ja aardeid on jäänud " + treasuresLeft + " tükki!");
  36. System.out.println("Mida kaevame? ");
  37.  
  38. while (true) {
  39. try {
  40. coordinates = makeMove(sc, map.length, map[0].length);
  41. break;
  42. } catch (Exception e) {
  43. System.out.println("Sisestasite valed koordinaadid, proovige uuesti!");
  44. }
  45. }
  46.  
  47. if (map[coordinates[0]][coordinates[1]] == 1) {
  48. System.out.println("AARE!");
  49. map[coordinates[0]][coordinates[1]] = 3;
  50. treasuresLeft--;
  51. } else if (map[coordinates[0]][coordinates[1]] == 2) {
  52. System.out.println("Siit oled juba kaevanud!");
  53. } else {
  54. map[coordinates[0]][coordinates[1]] = 2;
  55. }
  56.  
  57. digCount++;
  58.  
  59. System.out.println(getMapForPrint(map));
  60.  
  61. if (treasuresLeft <= 0) {
  62. System.out.println("Mäng on läbi! Aardeid leitud - " + treasuresCount +
  63. ", ja kaevamisi tehtud- " + digCount);
  64. return;
  65. }
  66. }
  67.  
  68. }
  69.  
  70. public static String getMapForPrint(int[][] map) {
  71. String mapString = "";
  72.  
  73. for (int i = 0; i < map.length; i++) {
  74. for (int j = 0; j < map[0].length; j++) {
  75. mapString += map[i][j] + " ";
  76. }
  77. mapString += "\n";
  78. }
  79. return mapString;
  80. }
  81.  
  82. public static int[][] makeMap(int m, int n, int t) {
  83. Random random = new Random();
  84. int[][] map = new int[m][n];
  85.  
  86. for (int i = t; i > 0; i--) {
  87. int x = random.nextInt(m);
  88. int y = random.nextInt(n);
  89. if (map[x][y] == 1) {
  90. i++;
  91. continue;
  92. }
  93. map[x][y] = 1;
  94. }
  95. return map;
  96. }
  97.  
  98. public static int[] getGameParams() {
  99. Scanner sc = new Scanner(System.in);
  100. int m;
  101. int n;
  102. int t;
  103.  
  104. while (true) {
  105. System.out.println("Sisesta mängulaua ridade, veergude ja aarete arv: ");
  106.  
  107. m = sc.nextInt();
  108. n = sc.nextInt();
  109. t = sc.nextInt();
  110. if (m*n > 0 && m != 0 && (t > 0 && t <= m*n)) {
  111. break;
  112. }
  113. }
  114.  
  115. return new int[] {m, n, t};
  116. }
  117.  
  118. public static int[] makeMove(Scanner sc, int boardSizeY, int boardSizeX) {
  119. int x;
  120. int y;
  121.  
  122. while (true) {
  123. x = sc.nextInt();
  124. y = sc.nextInt();
  125. if ((y >= 0 && y <= boardSizeY) && (x >= 0 && x <= boardSizeX)) {
  126. break;
  127. }
  128. }
  129.  
  130. return new int[] {x, y};
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement