+1 (315) 557-6473 

Java Program to Create Basic Procedural Generation Assignment Solution.


Instructions

Objective
You need to write a java homework that implements basic procedural generation in Java. Procedural generation involves creating algorithms to generate content, such as landscapes or levels, on-the-fly rather than designing them manually. In this assignment, you'll explore how to use Java to generate random terrain or patterns, providing a unique experience each time the program runs. Utilize Java's libraries and control structures to define the rules for generating content dynamically. By accomplishing this task, you'll gain a better understanding of Java programming concepts and enhance your skills in algorithmic thinking and problem-solving, which are crucial in software development.

Requirements and Specifications

program to create basic procedural generation in java

Source Code

APP

import java.util.Scanner;

public class App {

    public static void main(String[] args) throws Exception {

        //Create scanner

        Scanner sc = new Scanner(System.in);

        // First, get size of marray from user

        System.out.print("Enter size of array: ");

        int N = Integer.valueOf(sc.nextLine());

        // Now, create a 2D array of size NxN

        Zone zones[][] = new Zone[N][N];

        // Now, generate the top-left zone by using a AverageDenResidential object, since this one

        // can randomly generate all other zones (including itself)

        zones[0][0] = new AverageDenResidential().generateNeighbor();

        System.out.print(zones[0][0].toString());

        // Now, populate all the array.

        for(int i = 0; i < N; i++) {

            for(int j = 0; j < N; j++) {

                if( i == 0 && j == 0)

                    continue; // Ignore indx (0,0) since this one was generated outside the loop

                if(i == 0) // first row

                {

                    zones[i][j] = zones[i][j-1].generateNeighbor(); // ask the object to the left to generate the neighbor

                }

                else // subsequent rows

                {

                    // Check if we are at the first element in the row i

                    if(j == 0) {

                        // Ask the first object in the previous row to generate the neighbor

                        zones[i][j] = zones[i-1][j].generateNeighbor();

                    }

                    else

                    {

                        // Ask both the object "up from" and the one "to the left" to generate neighbors

                        // and flip a coin to pick one

                        // First, flip coin

                        double chance = Math.random();

                        Zone upfrom = zones[i-1][j].generateNeighbor();

                        Zone leftfrom = zones[i][j-1].generateNeighbor();

                        if(chance < 0.5) // pick the one from up

                        {

                            zones[i][j] = upfrom;

                        }

                        else // pick one from the left

                        {

                            zones[i][j] = leftfrom;

                        }

                    }

                }

                // Print

                System.out.print(zones[i][j].toString());

            }

            // We are jumping to a new row, so print a new line

            System.out.print("\n");

        }

        // Close Scanner

        sc.close();

    }

}

AVERAGE DEN RESIDENTIAL

import org.newdawn.slick.Color;

import org.newdawn.slick.Graphics;

public class AverageDenResidential extends Zone {

    // Constructor

    public AverageDenResidential()

    {

        // Set color

        color = Color.gray;

    }

    @Override

    public void drawSelf(Graphics g) {

        // TODO Auto-generated method stub

    }

    @Override

    public Zone generateNeighbor() {

        // TODO Auto-generated method stub

        // Generate random number between 0 and 1

        double chance = Math.random();

        if(chance <= 0.05) // first 5%, generate a Industrial

            return new Industrial();

        else if(chance > 0.05 && chance <= 0.1) // second 5%, generate LowDenResidential

            return new LowDenResidential();

        else if(chance > 0.1 && chance <= 0.3) // 20%, HighDenResidential

            return new HighDenResidential();

        else if(chance > 0.3 && chance <= 0.5) // 20%, Commercial

            return new Commercial();

        else if(chance > 0.5 && chance <= 0.7) // 20%, RecreationSpace

            return new RecreationSpace();

        else // 30%, AverageDenResidential

            return new AverageDenResidential();

    }

    @Override

    public String toString() {

        // TODO Auto-generated method stub

        return "A";

    }

}

RECREATION SPACE

import org.newdawn.slick.Color;

import org.newdawn.slick.Graphics;

public class RecreationSpace extends Zone {

    // Constructor

    public RecreationSpace()

    {

        // Set color

        color = Color.blue;

    }

    @Override

    public void drawSelf(Graphics g) {

        // TODO Auto-generated method stub

    }

    @Override

    public Zone generateNeighbor() {

        // TODO Auto-generated method stub

        // Generate random number between 0 and 1

        double chance = Math.random();

        if(chance <= 0.05) // first 5%, generate a HighDenResidential

            return new HighDenResidential();

        else if(chance > 0.05 && chance <= 0.15) // 10%, generate AverageDenResidential

            return new AverageDenResidential();

        else if(chance > 0.15 && chance <= 0.3) // 15% Commercial

            return new Commercial();

        else if(chance > 0.3 && chance <= 0.55) // 25% LowDenResidential

            return new LowDenResidential();

        else // 45% RecreationSpace

            return new RecreationSpace();

    }

    @Override

    public String toString() {

        // TODO Auto-generated method stub

        return "R";

    }

}