Advertisement
tolem

vízzel elárasztott terep

Mar 18th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.77 KB | None | 0 0
  1. package montains_and_rain;
  2.  
  3. public class Main {
  4.  
  5. /**
  6. * Egy egyszerű program egy számokból létrehozott "térképet" irat ki a képernyőre.
  7. * A számok elhelyezkedése a térkép koordinátá is, értékei pedig terep magasságát jelentik.
  8. * A felhasználótól bekért adatok alapján létrjött terepet aztán fokozatosan eláraszt a
  9. * térkép egyik legalacsonyabb ponján feltörő víz.
  10. * A vízzel elárasztott koordinátákat két mínusz jel jelképezi. A programban ugyan el van mentve a víz mélysége,
  11. * de ezt a felhasználó nem látja.
  12. */
  13.  
  14. public static void main(String...ˇ) throws InterruptedException {
  15.  
  16. UserInputHandler userInputHandler = new UserInputHandler();
  17.  
  18. userInputHandler.startingText();
  19.  
  20. //adatok bekérése, validálása és változókba mentése
  21. int percentageOfHills = userInputHandler.getPercentageOfHills();
  22. int size = userInputHandler.getSize();
  23. int smoothness = userInputHandler.getSmoothness();
  24. int amountOfWater = userInputHandler.getAmountOfWater();
  25. long speed = userInputHandler.getSpeed();
  26.  
  27. // játék kezdete, felhasználótól bekért adatok átadása, pálya kirajzolása
  28. PlayGround playGround = new PlayGround(size, (int) (size * 0.7));
  29.  
  30. playGround.setMAX_HEIGHT(15);
  31. playGround.makeMountainsNValleys(percentageOfHills, 30);
  32. playGround.terrainSmoother(smoothness);
  33. playGround.floodWithWater(amountOfWater, speed);
  34. }
  35. }
  36.  
  37.  
  38. package montains_and_rain;
  39.  
  40. import java.util.Random;
  41.  
  42. public class PlayGround {
  43.  
  44. private final int WIDTH;
  45. private int DEPTH;
  46. private int[][] playGround;
  47. private Random random;
  48. private int BASE_HEIGHT = 1;
  49. private int MAX_HEIGHT = 5;
  50.  
  51. public PlayGround(int width, int depth) {
  52. this.WIDTH = width + 1;
  53. this.DEPTH = depth + 1;
  54. initPlayGround();
  55. }
  56.  
  57. public int getBASE_HEIGHT() {
  58. return BASE_HEIGHT;
  59. }
  60.  
  61. public int getMAX_HEIGHT() {
  62. return MAX_HEIGHT;
  63. }
  64.  
  65. public void setBASE_HEIGHT(int BASE_HEIGHT) {
  66. this.BASE_HEIGHT = BASE_HEIGHT;
  67. }
  68.  
  69. public void setMAX_HEIGHT(int MAX_HEIGHT) {
  70. this.MAX_HEIGHT = MAX_HEIGHT;
  71. }
  72.  
  73. private void initPlayGround() {
  74.  
  75. playGround = new int[DEPTH][WIDTH];
  76.  
  77. for (int row = 0; row < DEPTH; row++) {
  78. for (int column = 0; column < WIDTH; column++) {
  79. playGround[row][column] = (BASE_HEIGHT);
  80. }
  81. }
  82.  
  83. }
  84.  
  85. // Gondoltam, rövidebb vagy átláthatóbb lesz a getValue() függvénytől a kód, de nem lett,
  86. // így nem használtam fel, azért benne hagytam érdekességként.
  87. private int getValue(int x, int y, int z) {
  88. switch (z) {
  89. case 1:
  90. return this.playGround[x + 1][y - 1];
  91. case 2:
  92. return this.playGround[x + 1][y];
  93. case 3:
  94. return this.playGround[x + 1][y + 1];
  95. case 4:
  96. return this.playGround[x][y - 1];
  97. case 6:
  98. return this.playGround[x][y + 1];
  99. case 7:
  100. return this.playGround[x - 1][y - 1];
  101. case 8:
  102. return this.playGround[x - 1][y];
  103. case 9:
  104. return this.playGround[x - 1][y + 1];
  105. default:
  106. return getValue(x, y);
  107. }
  108.  
  109. }
  110.  
  111. private int getValue(int x, int y) {
  112. return this.playGround[x][y];
  113. }
  114.  
  115. // Az végig iterál a pályán. Az első körben az esetek percentageOfHills százalékában növeli a koordináta magasságát kettővel.
  116. // A többi körben ezen megnövel magasságok -1 percentageOfHills százalékának a magasságát tovább emeli.
  117. public void makeMountainsNValleys(int percentageOfHills, int roughness) {
  118.  
  119. random = new Random(654465);
  120. int highestPoint = 1;
  121.  
  122. for (int iteration = 1; iteration <= roughness; iteration++) {
  123. if(iteration == 2)percentageOfHills =80;
  124. boolean isRaised = false;
  125. for (int row = 0; row < DEPTH; row++) {
  126. for (int column = 0; column < WIDTH; column++) {
  127. if (((random.nextInt(100) + 1) <= percentageOfHills)
  128. && ((getValue(row, column) > highestPoint - 4) && (playGround[row][column] <= highestPoint))
  129. && (playGround[row][column] < MAX_HEIGHT)) {
  130. playGround[row][column] += random.nextInt(4)+1;
  131. isRaised = true;
  132. }
  133. }
  134. }
  135. if (isRaised) highestPoint += 4;
  136. }
  137.  
  138.  
  139. }
  140.  
  141. // Végig iterál a pályán és megvizgálja a tőle smoothness távolságra lévő koordinátákat.
  142. // Ha két szomszédos koordináta között túl nagy a magasságbeli különbség, a nagyobból levon egyet
  143. // a kisebbhez meg hozzáad egyet.
  144. // A végén ellenőrzi, hogy van-e negatív érték. Ha igen, nullára állítja.
  145. public void terrainSmoother(int smoothness) {
  146.  
  147. for (int iteration = 1; iteration <= smoothness; iteration++) {
  148. for (int row = 0; row < DEPTH; row++) {
  149. for (int column = 0; column < WIDTH; column++) {
  150. if (coordinateChecker(row, column, smoothness) && playGround[row][column] < 15) {
  151.  
  152. if (Math.abs(playGround[row][column] - playGround[row - iteration][column - iteration]) > 2) {
  153. if (playGround[row][column] > playGround[row - iteration][column - iteration]) {
  154. playGround[row][column]--;
  155. playGround[row - iteration][column - iteration]++;
  156. } else {
  157. playGround[row][column]++;
  158. playGround[row - iteration][column - iteration]--;
  159. }
  160. }
  161. if (Math.abs(playGround[row][column] - playGround[row - iteration][column]) > 3) {
  162. if (playGround[row][column] > playGround[row - iteration][column]) {
  163. playGround[row][column]--;
  164. playGround[row - iteration][column]++;
  165. } else {
  166. playGround[row][column]++;
  167. playGround[row - iteration][column]--;
  168. }
  169. }
  170. if (Math.abs(playGround[row][column] - playGround[row - iteration][column + iteration]) > 2) {
  171. if (playGround[row][column] > playGround[row - iteration][column + iteration]) {
  172. playGround[row][column]--;
  173. playGround[row - iteration][column + iteration]++;
  174. } else {
  175. playGround[row][column]++;
  176. playGround[row - iteration][column + iteration]--;
  177. }
  178. }
  179. if (Math.abs(playGround[row][column] - playGround[row][column - iteration]) > 2) {
  180. if (playGround[row][column] > playGround[row][column - iteration]) {
  181. playGround[row][column]--;
  182. playGround[row - iteration][column - iteration]++;
  183. } else {
  184. playGround[row][column]++;
  185. playGround[row - iteration][column - iteration]--;
  186. }
  187. }
  188. if (Math.abs(playGround[row][column] - playGround[row][column + iteration]) > 2) {
  189. if (playGround[row][column] > playGround[row][column + iteration]) {
  190. playGround[row][column]--;
  191. playGround[row][column + iteration]++;
  192. } else {
  193. playGround[row][column]--;
  194. playGround[row][column + iteration]++;
  195. }
  196. }
  197. if (Math.abs(playGround[row][column] - playGround[row + iteration][column - iteration]) > 2) {
  198. if (playGround[row][column] > playGround[row + iteration][column - iteration]) {
  199. playGround[row][column]--;
  200. playGround[row + iteration][column - iteration]++;
  201. } else {
  202. playGround[row][column]++;
  203. playGround[row + iteration][column - iteration]--;
  204. }
  205. }
  206. if (Math.abs(playGround[row][column] - playGround[row + iteration][column]) > 2) {
  207. if (playGround[row][column] > playGround[row + iteration][column]) {
  208. playGround[row][column]--;
  209. playGround[row + iteration][column]++;
  210. } else {
  211. playGround[row][column]++;
  212. playGround[row + iteration][column]--;
  213. }
  214. }
  215. if (Math.abs(playGround[row][column] - playGround[row + iteration][column + iteration]) > 2) {
  216. if (playGround[row][column] > playGround[row + iteration][column + iteration]) {
  217. playGround[row][column]--;
  218. playGround[row + iteration][column + iteration]++;
  219. } else {
  220. playGround[row][column]++;
  221. playGround[row + iteration][column + iteration]--;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. for (int row = 0; row < DEPTH; row++) {
  229. for (int column = 0; column < WIDTH; column++) {
  230. if (playGround[row][column] < 0) playGround[row][column] = 0;
  231. }
  232. }
  233. }
  234.  
  235. public void floodWithWater(int amountOfWater, long waterFlowSpeed) throws InterruptedException {
  236.  
  237. int startingCoordinates[] = lowestPointCoordinates();
  238. int startingRow = startingCoordinates[0];
  239. int startingColumn = startingCoordinates[1];
  240. playGround[startingRow][startingColumn] = -1;
  241. for (int iterations = 1; iterations <= amountOfWater; iterations++) {
  242. boolean needToRaiseLevel = true;
  243. printPlayGround(iterations, waterFlowSpeed);
  244. Thread.sleep(waterFlowSpeed);
  245. for (int row = 0; row < DEPTH; row++) {
  246. for (int column = 0; column < WIDTH; column++) {
  247. if (coordinateChecker(row, column, 1) && playGround[row][column] < 0) {
  248. if (Math.abs(playGround[row - 1][column]) < Math.abs(playGround[row][column])) {
  249. playGround[row - 1][column] = playGround[row][column];
  250. needToRaiseLevel = false;
  251. }
  252. if (Math.abs(playGround[row][column + 1]) < Math.abs(playGround[row][column])) {
  253. playGround[row][column + 1] = playGround[row][column];
  254. needToRaiseLevel = false;
  255. }
  256. if (Math.abs(playGround[row + 1][column]) < Math.abs(playGround[row][column])) {
  257. playGround[row + 1][column] = playGround[row][column];
  258. needToRaiseLevel = false;
  259. }
  260. if (Math.abs(playGround[row][column - 1]) < Math.abs(playGround[row][column])) {
  261. playGround[row][column - 1] = playGround[row][column];
  262. needToRaiseLevel = false;
  263. }
  264. if (needToRaiseLevel) {
  265. playGround[row][column]--;
  266. }
  267. }
  268.  
  269. }
  270. }
  271. }
  272. }
  273.  
  274. //Megtalálja az első olyan pontot a térképen, ami a legalacsonyabbabn helyezkedik el, és nincs a térkép szélén
  275. private int[] lowestPointCoordinates() {
  276. int lowestDepthCoordinate = DEPTH;
  277. int lowestWidthCoordinate = WIDTH;
  278. int[] Coordinates = new int[2];
  279.  
  280. int counter = Integer.MAX_VALUE;
  281.  
  282. for (int row = 1; row < DEPTH - 1; row++) {
  283. for (int column = 1; column < WIDTH - 1; column++) {
  284. if (playGround[row][column] < counter) {
  285. counter = playGround[row][column];
  286. }
  287. }
  288. }
  289.  
  290. for (int row = 1; row < DEPTH - 1; row++) {
  291. for (int column = 1; column < WIDTH - 1; column++) {
  292. if (counter == playGround[row][column]) {
  293. lowestDepthCoordinate = row;
  294. lowestWidthCoordinate = column;
  295. break;
  296. }
  297. }
  298. }
  299. Coordinates[0] = lowestDepthCoordinate;
  300. Coordinates[1] = lowestWidthCoordinate;
  301.  
  302. return Coordinates;
  303. }
  304.  
  305. private boolean coordinateChecker(int row, int column, int spaceToCheck) {
  306.  
  307.  
  308. if (row - spaceToCheck < 0 || row + spaceToCheck >= DEPTH || column - spaceToCheck < 0 || column + spaceToCheck >= WIDTH)
  309. return false;
  310.  
  311. return true;
  312.  
  313. }
  314.  
  315. // A pálya széle nem kerül kirajzolásra, mivel ott (az egyszerűség kedvéért, hogy ne kelljen mind a négy égtájat lekezelni),
  316. // nem működik jól a coordinateChecker algoritmus
  317. private void printPlayGround(int iterations, long waterFlowSpeed) {
  318. String formattedVolume = String.format("%.02f", iterations * waterFlowSpeed * 1.15);
  319.  
  320. String repeat = "---".repeat(WIDTH * 3 < 64 ? 0 : WIDTH / 2 - 12);
  321. System.out.println(repeat
  322. + " " + iterations + " ÓRA ALATT "
  323. + formattedVolume + " KÖBKILOMÉTER VÍZ ÁRASZTOTTA EL A FÖLDRÉSZT! "
  324. + repeat);
  325. System.out.println("---".repeat(WIDTH - 2));
  326. for (int i = 1; i < DEPTH - 1; i++) {
  327. for (int j = 1; j < WIDTH - 1; j++) {
  328. if (playGround[i][j] >= 0) {
  329. System.out.print("+" + playGround[i][j]);
  330. } else {
  331. System.out.print("--");
  332. //System.out.print(playGround[i][j]);
  333. }
  334. System.out.print(" ");
  335. }
  336. System.out.println();
  337. }
  338.  
  339. System.out.println("---".repeat(WIDTH - 2));
  340. System.out.println();
  341.  
  342. }
  343. }
  344. package montains_and_rain;
  345.  
  346. import java.util.Scanner;
  347.  
  348. public class UserInputHandler extends Main {
  349.  
  350. Scanner scanner = new Scanner(System.in);
  351. boolean valid = false;
  352. void startingText() throws InterruptedException {
  353. //bemutatkozó szöveg
  354. System.out.println();
  355. System.out.println("Üdv a Dimbek-Dombok & Végtelen Forrásvíz című, nagy sikerű játékban!");
  356. Thread.sleep(3000);
  357. System.out.println("A feladatod elárasztani a terepet vízzel.");
  358. Thread.sleep(3000);
  359. System.out.println("Mivel a víz elvégzi a munka nagy részét, neked csupán néhány ártalmatlan adatot kell meghatároznod.");
  360. Thread.sleep(3000);
  361. System.out.println("Például hogy mennyire legyen dimbes-dombos a terep 1-től 100-ig: ");
  362. }
  363.  
  364. int getPercentageOfHills() {
  365. int percentageOfHills = 10;
  366.  
  367. do {
  368. try {
  369. percentageOfHills = Integer.parseInt(scanner.nextLine());
  370. valid = true;
  371. } catch (NumberFormatException e) {
  372. System.out.println("Írj be egy számot 1-től 100-ig");
  373. scanner.nextLine();
  374. }
  375. } while (!valid);
  376. return percentageOfHills;
  377. }
  378.  
  379. int getSize() {
  380. boolean valid;
  381. System.out.println("Mekkora legyen a pálya 20-tól 70-ig: ");
  382. valid = false;
  383. int size = 20;
  384.  
  385. do {
  386. try {
  387. size = Integer.parseInt(scanner.nextLine());
  388. valid = true;
  389. } catch (NumberFormatException e) {
  390. System.out.println("Írj be egy számot 10-től 70-ig");
  391. scanner.nextLine();
  392. }
  393. } while (!valid);
  394. return size;
  395. }
  396.  
  397. int getSmoothness() {
  398. boolean valid;
  399. System.out.println("Mennyire legyen elsimítva a terep?");
  400. System.out.println("\t0 --> Durván szeretem!");
  401. System.out.println("\t1 --> Nagy csúcsok");
  402. System.out.println("\t2 --> Magas dombok");
  403. System.out.println("\t3 --> Ausztrália");
  404. System.out.println("\t4 --> Dimbes-dombos");
  405. System.out.println("\t5 --> Sivatag");
  406. System.out.println("\t6 --> Hold felszín!");
  407.  
  408.  
  409. valid = false;
  410. int smoothness = 0;
  411.  
  412. do {
  413. try {
  414. smoothness = Integer.parseInt(scanner.nextLine());
  415. valid = true;
  416. } catch (NumberFormatException e) {
  417. System.out.println("Írj be egy számot 0-tól 3-ig!");
  418. scanner.nextLine();
  419. }
  420. } while (!valid);
  421. return smoothness;
  422. }
  423.  
  424. int getAmountOfWater() {
  425. boolean valid;
  426. System.out.println("Mennyi víz legyen?");
  427. System.out.println("\t20 alatt kevés.");
  428. System.out.println("\t40 felett sok.");
  429. valid = false;
  430. int amountOfWater = 10;
  431.  
  432. do {
  433. try {
  434. amountOfWater = Integer.parseInt(scanner.nextLine());
  435. valid = true;
  436. } catch (NumberFormatException e) {
  437. System.out.println("Írj be egy számot!");
  438. scanner.nextLine();
  439. }
  440. } while (!valid);
  441. return amountOfWater;
  442. }
  443.  
  444. long getSpeed() {
  445. boolean valid;
  446. System.out.println("És végül: Milyen gyorsan törjön elő a víz? (1000 = 1 mp)");
  447. valid = false;
  448. long speed = 10;
  449.  
  450. do {
  451. try {
  452. speed = Integer.parseInt(scanner.nextLine());
  453. valid = true;
  454. } catch (NumberFormatException e) {
  455. System.out.println("Írj be egy számot!");
  456. scanner.nextLine();
  457. }
  458. } while (!valid);
  459. return speed;
  460. }
  461.  
  462. }
  463.  
  464.  
  465.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement