Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package montains_and_rain;
- public class Main {
- /**
- * Egy egyszerű program egy számokból létrehozott "térképet" irat ki a képernyőre.
- * A számok elhelyezkedése a térkép koordinátá is, értékei pedig terep magasságát jelentik.
- * A felhasználótól bekért adatok alapján létrjött terepet aztán fokozatosan eláraszt a
- * térkép egyik legalacsonyabb ponján feltörő víz.
- * 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,
- * de ezt a felhasználó nem látja.
- */
- public static void main(String...ˇ) throws InterruptedException {
- UserInputHandler userInputHandler = new UserInputHandler();
- userInputHandler.startingText();
- //adatok bekérése, validálása és változókba mentése
- int percentageOfHills = userInputHandler.getPercentageOfHills();
- int size = userInputHandler.getSize();
- int smoothness = userInputHandler.getSmoothness();
- int amountOfWater = userInputHandler.getAmountOfWater();
- long speed = userInputHandler.getSpeed();
- // játék kezdete, felhasználótól bekért adatok átadása, pálya kirajzolása
- PlayGround playGround = new PlayGround(size, (int) (size * 0.7));
- playGround.setMAX_HEIGHT(15);
- playGround.makeMountainsNValleys(percentageOfHills, 30);
- playGround.terrainSmoother(smoothness);
- playGround.floodWithWater(amountOfWater, speed);
- }
- }
- package montains_and_rain;
- import java.util.Random;
- public class PlayGround {
- private final int WIDTH;
- private int DEPTH;
- private int[][] playGround;
- private Random random;
- private int BASE_HEIGHT = 1;
- private int MAX_HEIGHT = 5;
- public PlayGround(int width, int depth) {
- this.WIDTH = width + 1;
- this.DEPTH = depth + 1;
- initPlayGround();
- }
- public int getBASE_HEIGHT() {
- return BASE_HEIGHT;
- }
- public int getMAX_HEIGHT() {
- return MAX_HEIGHT;
- }
- public void setBASE_HEIGHT(int BASE_HEIGHT) {
- this.BASE_HEIGHT = BASE_HEIGHT;
- }
- public void setMAX_HEIGHT(int MAX_HEIGHT) {
- this.MAX_HEIGHT = MAX_HEIGHT;
- }
- private void initPlayGround() {
- playGround = new int[DEPTH][WIDTH];
- for (int row = 0; row < DEPTH; row++) {
- for (int column = 0; column < WIDTH; column++) {
- playGround[row][column] = (BASE_HEIGHT);
- }
- }
- }
- // Gondoltam, rövidebb vagy átláthatóbb lesz a getValue() függvénytől a kód, de nem lett,
- // így nem használtam fel, azért benne hagytam érdekességként.
- private int getValue(int x, int y, int z) {
- switch (z) {
- case 1:
- return this.playGround[x + 1][y - 1];
- case 2:
- return this.playGround[x + 1][y];
- case 3:
- return this.playGround[x + 1][y + 1];
- case 4:
- return this.playGround[x][y - 1];
- case 6:
- return this.playGround[x][y + 1];
- case 7:
- return this.playGround[x - 1][y - 1];
- case 8:
- return this.playGround[x - 1][y];
- case 9:
- return this.playGround[x - 1][y + 1];
- default:
- return getValue(x, y);
- }
- }
- private int getValue(int x, int y) {
- return this.playGround[x][y];
- }
- // 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.
- // A többi körben ezen megnövel magasságok -1 percentageOfHills százalékának a magasságát tovább emeli.
- public void makeMountainsNValleys(int percentageOfHills, int roughness) {
- random = new Random(654465);
- int highestPoint = 1;
- for (int iteration = 1; iteration <= roughness; iteration++) {
- if(iteration == 2)percentageOfHills =80;
- boolean isRaised = false;
- for (int row = 0; row < DEPTH; row++) {
- for (int column = 0; column < WIDTH; column++) {
- if (((random.nextInt(100) + 1) <= percentageOfHills)
- && ((getValue(row, column) > highestPoint - 4) && (playGround[row][column] <= highestPoint))
- && (playGround[row][column] < MAX_HEIGHT)) {
- playGround[row][column] += random.nextInt(4)+1;
- isRaised = true;
- }
- }
- }
- if (isRaised) highestPoint += 4;
- }
- }
- // Végig iterál a pályán és megvizgálja a tőle smoothness távolságra lévő koordinátákat.
- // 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
- // a kisebbhez meg hozzáad egyet.
- // A végén ellenőrzi, hogy van-e negatív érték. Ha igen, nullára állítja.
- public void terrainSmoother(int smoothness) {
- for (int iteration = 1; iteration <= smoothness; iteration++) {
- for (int row = 0; row < DEPTH; row++) {
- for (int column = 0; column < WIDTH; column++) {
- if (coordinateChecker(row, column, smoothness) && playGround[row][column] < 15) {
- if (Math.abs(playGround[row][column] - playGround[row - iteration][column - iteration]) > 2) {
- if (playGround[row][column] > playGround[row - iteration][column - iteration]) {
- playGround[row][column]--;
- playGround[row - iteration][column - iteration]++;
- } else {
- playGround[row][column]++;
- playGround[row - iteration][column - iteration]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row - iteration][column]) > 3) {
- if (playGround[row][column] > playGround[row - iteration][column]) {
- playGround[row][column]--;
- playGround[row - iteration][column]++;
- } else {
- playGround[row][column]++;
- playGround[row - iteration][column]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row - iteration][column + iteration]) > 2) {
- if (playGround[row][column] > playGround[row - iteration][column + iteration]) {
- playGround[row][column]--;
- playGround[row - iteration][column + iteration]++;
- } else {
- playGround[row][column]++;
- playGround[row - iteration][column + iteration]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row][column - iteration]) > 2) {
- if (playGround[row][column] > playGround[row][column - iteration]) {
- playGround[row][column]--;
- playGround[row - iteration][column - iteration]++;
- } else {
- playGround[row][column]++;
- playGround[row - iteration][column - iteration]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row][column + iteration]) > 2) {
- if (playGround[row][column] > playGround[row][column + iteration]) {
- playGround[row][column]--;
- playGround[row][column + iteration]++;
- } else {
- playGround[row][column]--;
- playGround[row][column + iteration]++;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row + iteration][column - iteration]) > 2) {
- if (playGround[row][column] > playGround[row + iteration][column - iteration]) {
- playGround[row][column]--;
- playGround[row + iteration][column - iteration]++;
- } else {
- playGround[row][column]++;
- playGround[row + iteration][column - iteration]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row + iteration][column]) > 2) {
- if (playGround[row][column] > playGround[row + iteration][column]) {
- playGround[row][column]--;
- playGround[row + iteration][column]++;
- } else {
- playGround[row][column]++;
- playGround[row + iteration][column]--;
- }
- }
- if (Math.abs(playGround[row][column] - playGround[row + iteration][column + iteration]) > 2) {
- if (playGround[row][column] > playGround[row + iteration][column + iteration]) {
- playGround[row][column]--;
- playGround[row + iteration][column + iteration]++;
- } else {
- playGround[row][column]++;
- playGround[row + iteration][column + iteration]--;
- }
- }
- }
- }
- }
- }
- for (int row = 0; row < DEPTH; row++) {
- for (int column = 0; column < WIDTH; column++) {
- if (playGround[row][column] < 0) playGround[row][column] = 0;
- }
- }
- }
- public void floodWithWater(int amountOfWater, long waterFlowSpeed) throws InterruptedException {
- int startingCoordinates[] = lowestPointCoordinates();
- int startingRow = startingCoordinates[0];
- int startingColumn = startingCoordinates[1];
- playGround[startingRow][startingColumn] = -1;
- for (int iterations = 1; iterations <= amountOfWater; iterations++) {
- boolean needToRaiseLevel = true;
- printPlayGround(iterations, waterFlowSpeed);
- Thread.sleep(waterFlowSpeed);
- for (int row = 0; row < DEPTH; row++) {
- for (int column = 0; column < WIDTH; column++) {
- if (coordinateChecker(row, column, 1) && playGround[row][column] < 0) {
- if (Math.abs(playGround[row - 1][column]) < Math.abs(playGround[row][column])) {
- playGround[row - 1][column] = playGround[row][column];
- needToRaiseLevel = false;
- }
- if (Math.abs(playGround[row][column + 1]) < Math.abs(playGround[row][column])) {
- playGround[row][column + 1] = playGround[row][column];
- needToRaiseLevel = false;
- }
- if (Math.abs(playGround[row + 1][column]) < Math.abs(playGround[row][column])) {
- playGround[row + 1][column] = playGround[row][column];
- needToRaiseLevel = false;
- }
- if (Math.abs(playGround[row][column - 1]) < Math.abs(playGround[row][column])) {
- playGround[row][column - 1] = playGround[row][column];
- needToRaiseLevel = false;
- }
- if (needToRaiseLevel) {
- playGround[row][column]--;
- }
- }
- }
- }
- }
- }
- //Megtalálja az első olyan pontot a térképen, ami a legalacsonyabbabn helyezkedik el, és nincs a térkép szélén
- private int[] lowestPointCoordinates() {
- int lowestDepthCoordinate = DEPTH;
- int lowestWidthCoordinate = WIDTH;
- int[] Coordinates = new int[2];
- int counter = Integer.MAX_VALUE;
- for (int row = 1; row < DEPTH - 1; row++) {
- for (int column = 1; column < WIDTH - 1; column++) {
- if (playGround[row][column] < counter) {
- counter = playGround[row][column];
- }
- }
- }
- for (int row = 1; row < DEPTH - 1; row++) {
- for (int column = 1; column < WIDTH - 1; column++) {
- if (counter == playGround[row][column]) {
- lowestDepthCoordinate = row;
- lowestWidthCoordinate = column;
- break;
- }
- }
- }
- Coordinates[0] = lowestDepthCoordinate;
- Coordinates[1] = lowestWidthCoordinate;
- return Coordinates;
- }
- private boolean coordinateChecker(int row, int column, int spaceToCheck) {
- if (row - spaceToCheck < 0 || row + spaceToCheck >= DEPTH || column - spaceToCheck < 0 || column + spaceToCheck >= WIDTH)
- return false;
- return true;
- }
- // 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),
- // nem működik jól a coordinateChecker algoritmus
- private void printPlayGround(int iterations, long waterFlowSpeed) {
- String formattedVolume = String.format("%.02f", iterations * waterFlowSpeed * 1.15);
- String repeat = "---".repeat(WIDTH * 3 < 64 ? 0 : WIDTH / 2 - 12);
- System.out.println(repeat
- + " " + iterations + " ÓRA ALATT "
- + formattedVolume + " KÖBKILOMÉTER VÍZ ÁRASZTOTTA EL A FÖLDRÉSZT! "
- + repeat);
- System.out.println("---".repeat(WIDTH - 2));
- for (int i = 1; i < DEPTH - 1; i++) {
- for (int j = 1; j < WIDTH - 1; j++) {
- if (playGround[i][j] >= 0) {
- System.out.print("+" + playGround[i][j]);
- } else {
- System.out.print("--");
- //System.out.print(playGround[i][j]);
- }
- System.out.print(" ");
- }
- System.out.println();
- }
- System.out.println("---".repeat(WIDTH - 2));
- System.out.println();
- }
- }
- package montains_and_rain;
- import java.util.Scanner;
- public class UserInputHandler extends Main {
- Scanner scanner = new Scanner(System.in);
- boolean valid = false;
- void startingText() throws InterruptedException {
- //bemutatkozó szöveg
- System.out.println();
- System.out.println("Üdv a Dimbek-Dombok & Végtelen Forrásvíz című, nagy sikerű játékban!");
- Thread.sleep(3000);
- System.out.println("A feladatod elárasztani a terepet vízzel.");
- Thread.sleep(3000);
- 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.");
- Thread.sleep(3000);
- System.out.println("Például hogy mennyire legyen dimbes-dombos a terep 1-től 100-ig: ");
- }
- int getPercentageOfHills() {
- int percentageOfHills = 10;
- do {
- try {
- percentageOfHills = Integer.parseInt(scanner.nextLine());
- valid = true;
- } catch (NumberFormatException e) {
- System.out.println("Írj be egy számot 1-től 100-ig");
- scanner.nextLine();
- }
- } while (!valid);
- return percentageOfHills;
- }
- int getSize() {
- boolean valid;
- System.out.println("Mekkora legyen a pálya 20-tól 70-ig: ");
- valid = false;
- int size = 20;
- do {
- try {
- size = Integer.parseInt(scanner.nextLine());
- valid = true;
- } catch (NumberFormatException e) {
- System.out.println("Írj be egy számot 10-től 70-ig");
- scanner.nextLine();
- }
- } while (!valid);
- return size;
- }
- int getSmoothness() {
- boolean valid;
- System.out.println("Mennyire legyen elsimítva a terep?");
- System.out.println("\t0 --> Durván szeretem!");
- System.out.println("\t1 --> Nagy csúcsok");
- System.out.println("\t2 --> Magas dombok");
- System.out.println("\t3 --> Ausztrália");
- System.out.println("\t4 --> Dimbes-dombos");
- System.out.println("\t5 --> Sivatag");
- System.out.println("\t6 --> Hold felszín!");
- valid = false;
- int smoothness = 0;
- do {
- try {
- smoothness = Integer.parseInt(scanner.nextLine());
- valid = true;
- } catch (NumberFormatException e) {
- System.out.println("Írj be egy számot 0-tól 3-ig!");
- scanner.nextLine();
- }
- } while (!valid);
- return smoothness;
- }
- int getAmountOfWater() {
- boolean valid;
- System.out.println("Mennyi víz legyen?");
- System.out.println("\t20 alatt kevés.");
- System.out.println("\t40 felett sok.");
- valid = false;
- int amountOfWater = 10;
- do {
- try {
- amountOfWater = Integer.parseInt(scanner.nextLine());
- valid = true;
- } catch (NumberFormatException e) {
- System.out.println("Írj be egy számot!");
- scanner.nextLine();
- }
- } while (!valid);
- return amountOfWater;
- }
- long getSpeed() {
- boolean valid;
- System.out.println("És végül: Milyen gyorsan törjön elő a víz? (1000 = 1 mp)");
- valid = false;
- long speed = 10;
- do {
- try {
- speed = Integer.parseInt(scanner.nextLine());
- valid = true;
- } catch (NumberFormatException e) {
- System.out.println("Írj be egy számot!");
- scanner.nextLine();
- }
- } while (!valid);
- return speed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement