Question
**JAVA PLEASE!!**
CSE205 Assignment 2 ASCII Terrain Generator 5Opts Topics: Arrays Multidimensional Arrays Methods Loops and Conditionals Descr
Programming Assignment: Instructions: You are creating software to procedurally generate terrain types. There will be two par
Part Probability Methods Probability Methods: The idea here is to basically ask an existing piece of ter rain (represented by
Desert Should be represented wi th the character D Should generate a neighbor with these probabilities: 30% Grass 0 % Hil1 C
Part 3 2D Array Making an island The island is a little more complex because were going to use more rules. The goal here is
CSE205 Assignment 2 ASCII Terrain Generator 5Opts Topics: Arrays Multidimensional Arrays Methods Loops and Conditionals Description The goal of this assignment is for you to produce a simple procedurally generated terrain map out of ASCII character symbols. This will be done through simple probability distributions and arrays Use the following Guideline s: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. User upper case for constants. Use title case (first 1etter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects) Use tabs or spaces to indent code within blocks (code surrounded by braces) . This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent Use white space to make your program more readable. Important Note A11 submitted assignments must begin with the descriptive comment block. To avoid osing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assi gnme nt to assignment. June 2019
Programming Assignment: Instructions: You are creating software to procedurally generate terrain types. There will be two parts. The first part will have you generate a single dimensional array of characters that uses the previous character in the array as a the seed value for the next. Allowing you to create a single strip of "land." The second part will have you use slightly more advanced rules to create a 2D array of terrain symbols. The terrain will be represented by the characters: G Grasslands H Hills M Mountains D Desert W Water Example output for a 5x5 land: GGDMM ннимм HWNWM MGNHH мнннн Specifications Part 0 Main Menu Write a menu to prompt the user to make a single strip of land (1d array) or an island (2d array) Welcome to the terrain generator. Please select an option 1 Single Strip of Land 2 Island 1 Exit You can optionally have the program 1oop back to the main menu after successfully generating the terrain.
Part Probability Methods Probability Methods: The idea here is to basically ask an existing piece of ter rain (represented by a character in the array) what its neighboring terrain piece should be. We wil1 accomplish this with a simple die rol1 Start this section by writing several method, each of them will return character: getNextFromGrass getNextFromHi11 getNextFromMountain getNextFromDesert getNextFromwater Each of these methods wi1 use a probability distribution (this and next determine what kind of terrain is returned page) to These methods shoul dn 't take a pa rameter, but should return a character : Grass Should be represented with the character G Should generate a neighbor with these probabilities: o 45% Grass 30% Hil1 o o 15% Water o 10 % Desert o 0% Mountain Hill Should be represented wi th the character H Should generate a neighbor with these probabilities: 25% Grass o 45% Hil1 o 10 % Water o 0% Desert o 20% Mountain Water Should be represented wi th the character W Should generate a neighbor with these probabilities: 30% Grass o 15% Hi11 50% Water o 0 % Desert o 5% Mountain
Desert Should be represented wi th the character 'D Should generate a neighbor with these probabilities: 30% Grass 0 % Hil1 C 0% Water o 65% Desert 5% Mountain Mountain Should be represented with the character M Should generate a neighbor with these probabilities: 0% Grass 25% Hills 15% Water 5 % Deser 55% Mountain C C Driver Method Write another method cal1ed GenerateNeighbor. This method should take a character as a parameter and return a character. This method is very simple it should use an if-else-if ladder or switch statement to call on the correct probability method to generate a neighboring terrain. Example: If I call CenerateNei ghbor passing in G (for Grass) it should call on getNext FromGrass and return the character that getNextFromGrass generates Part 2 - Creating the Strip of Land (menu item 1) Write a prompt to ask the user how 1arge the strip of land should be (that is the size of the single dimension character array). Create the array from that size. Assign the 0 index randomly as G, H, W, D or M. Then write a loop to fil1 the array using the GenerateNeighbor method current item as the seed character for GenerateNeighbor Use the value to the left of the G D G G ?? Sample output: Generate a single strip of land How big do you want the land to be? Generating... Your land: GHHHGGGHHHGWGHHMMMWGGGHMMWGWGGGGGW GHWWGGWWGGW Note: 50 was used for this example.
Part 3 2D Array Making an island The island is a little more complex because we're going to use more rules. The goal here is to make sure you're comfort able accessing and using neighbori ng values in a 2D array Algorithm Breakdown: Setup: 1. Get size of array from user (width & heigh t) 2. Create Terrain 2D character array 3. Create first piece of terrain in upper left corner a. Pick randomly First row 1. The first row is essentially part 1 of the assignment, you are creating a single row from a single val ue in the 0 index. 2. Continue down the row by asking the object "to the left" to generate the new object Subsequent rows: 1. The first item in the row wil1 use the previous row's 0 index character to seed the GenerateNeighbor method 2. Each other item in the row will "flip a coin" and either use the same index from the previous row, or the index in the same row to the left 3. Continue G G G G M ?? UH WHAT It's not actually a hard algorithm. You'l1 Take your time and think about it. be building a nested for loop to generate your terrain. You'11 iterate the outer oop on the Y and the inner loop on the X Then you build an if else ifelse chain to make sure you generate the terrain correctly Sample: Width of island? Height of island? Generating Your island: GGреGниммм НMDDGHHMHН НHGDGGGGHN GMHHGGHNGW GHGHDDGHGG Note: 10 Width and 5 height was used for this example
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in Java:

import java.util.Random;
import java.util.Scanner;

public class Solution {

    // random generator
    private static Random rand = new Random();

    public static void main(String[] args){
        Scanner r = new Scanner(System.in);
        while (true) {
            System.out.println("Welcome to the terrain generator.\n" +
                    "Please select an option\n" +
                    "1 - Single Strip of Land\n" +
                    "2 - Island\n" +
                    "-1 - Exit");
            int num = r.nextInt();
            if (num == -1){
                break;
            } else if (num == 1){
                singleStrip(r);
            } else if (num  == 2){
                island(r);
            } else {
                break;
            }
        }
        r.close();
    }

    /*
        generating next for grass
    * */
    private static char getNextFromGrass(){
        int rNum = Math.abs(rand.nextInt()) % 100;
        if (rNum < 10){
            return 'D';
        } else if (rNum < 25){
            return 'W';
        } else if (rNum < 55){
            return 'H';
        } else {
            return 'G';
        }
    }

    /*
        generating next for hill
    * */
    private static char getNextFromHill(){
        int rNum = Math.abs(rand.nextInt()) % 100;
        if (rNum < 20){
            return 'M';
        } else if (rNum < 30){
            return 'W';
        } else if (rNum < 75){
            return 'H';
        } else {
            return 'G';
        }
    }

    /*
        generating next for mountain
    * */
    private static char getNextFromMountain(){
        int rNum = Math.abs(rand.nextInt()) % 100;
        if (rNum < 55){
            return 'M';
        } else if (rNum < 60){
            return 'D';
        } else if (rNum < 75){
            return 'W';
        } else {
            return 'H';
        }
    }

    /*
        generating next for desert
    * */
    private static char getNextFromDesert(){
        int rNum = Math.abs(rand.nextInt()) % 100;
        if (rNum < 5){
            return 'M';
        } else if (rNum < 70){
            return 'D';
        } else {
            return 'G';
        }
    }

    /*
        generating next for water
    * */
    private static char getNextFromWater(){
        int rNum = Math.abs(rand.nextInt()) % 100;
        if (rNum < 5){
            return 'M';
        } else if (rNum < 55){
            return 'W';
        } else if (rNum < 70){
            return 'H';
        } else {
            return 'G';
        }
    }

    /*
        generating neighboring character
    * */
    private static char generateNeighbor(char curCh){
        switch (curCh){
            case 'G':
                return getNextFromGrass();
            case 'M':
                return getNextFromMountain();
            case 'H':
                return getNextFromHill();
            case 'W':
                return getNextFromWater();
            case 'D':
                return getNextFromDesert();
            default:
                break;
        }
        return '\0';
    }

    /*
        generating island
    * */
    private static void island(Scanner r) {
        int n, m;
        System.out.print("Get width: ");
        m = r.nextInt();
        System.out.print("Get height: ");
        n = r.nextInt();
        System.out.print("Generating ... ... ...\n");
        char[][] island = new char[n][m];
        String s = "HWDMG";
        int pos = Math.abs(rand.nextInt()) % 5;
        char ch = s.charAt(pos);
        System.out.print(ch);
        island[0][0] = ch;
        for (int i=1; i<m; ++i){
            ch = generateNeighbor(ch);
            island[0][i] = ch;
        }
        for (int i=1; i<n; ++i){
            island[i][0] = generateNeighbor(island[i - 1][0]);
            for (int j=1; j<m; ++j){
                int id = Math.abs(rand.nextInt()) & 1;
                if (id == 1){
                    island[i][j] = generateNeighbor(island[i][j - 1]);
                } else {
                    island[i][j] = generateNeighbor(island[i - 1][j]);
                }
            }
        }
        System.out.print("Your Island:\n");
        for (int i=0; i<n; ++i){
            System.out.println(island[i]);
        }
        System.out.println();
    }

    /*
        generating strip
    * */
    private static void singleStrip(Scanner r) {
        System.out.print("Get length of the strip: ");
        int size = r.nextInt();
        System.out.print("Generating ... ... ...\n");
        String s = "HWDMG";
        System.out.print("Your Strip:\n");
        int pos = Math.abs(rand.nextInt()) % 5;
        char ch = s.charAt(pos);
        System.out.print(ch);
        for (int i=1; i<size; ++i){
            ch = generateNeighbor(ch);
            System.out.print(ch);
        }
        System.out.print("\n\n");
    }
}

Comment down for any queries Please give a thumb up

Add a comment
Know the answer?
Add Answer to:
**JAVA PLEASE!!** CSE205 Assignment 2 ASCII Terrain Generator 5Opts Topics: Arrays Multidimensional Arrays Metho...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

  • This is java, please follow my request and use netbeans. Thank you. A3 15. 2D Array...

    This is java, please follow my request and use netbeans. Thank you. A3 15. 2D Array Operations Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: • getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. .getAverage. This method should accept a two-dimensional array as its argument and return...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and...

    [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and methods must be used for this program. Process: •Read the number of students in a class. Make sure that the user enters 1 or more students. •Create an array with the number of students entered by the user. •Then, read the name and the test score for each student. •Calculate the best or highest score. •Then, Calculate letter grades based on the following criteria:...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • Programming Assignment 6 Write a Java program that will implement a simple appointment book. The ...

    Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...

  • This assignment will continue our hardware store system. You will turn in a java file named...

    This assignment will continue our hardware store system. You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be exactly as specified (including method names): getAnInt will return a value of type int, and take as an argument a string to prompt the user. The actual prompt presented to the user should include not only the string argument, but also the information that the user...

  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

  • Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a...

    Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a healthy, balanced diet, a grownup needs 2,250 calories a day You will write a program to track calorie intake of a person. Assignment: Calorie intake data from a person is provided in a text file named input.txt. There are arbitrary number of double values on each line, separated by spaces. The numbers represent the number of calories consumed for meals and/or snacks on a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT