Question

Write a Java Program that implements: 1 - The random generation of a maze that will...

Write a Java Program that implements:

1 - The random generation of a maze that will be displayed on screen

       à The open passages are displayed as .(dot) and the walls are represented as xxxx2 - The automatic successful traversal of the maze à 5 points

       à The open passages that have been traversed are displayed

                    a) in color à 5 points

                    b) row by row when the user presses the “Enter” key

use 2D array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Collections;
import java.util.Arrays;
 
public class MazeGenerator {
        private final int x;
        private final int y;
        private final int[][] maze;
 
        public MazeGenerator(int x, int y) {
                this.x = x;
                this.y = y;
                maze = new int[this.x][this.y];
                generateMaze(0, 0);
        }
 
        public void display() {
                for (int i = 0; i < y; i++) {
                        // draw the north edge
                        for (int j = 0; j < x; j++) {
                                System.out.print((maze[j][i] & 1) == 0 ? "+---" : "+   ");
                        }
                        System.out.println("+");
                        // draw the west edge
                        for (int j = 0; j < x; j++) {
                                System.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");
                        }
                        System.out.println("|");
                }
                // draw the bottom line
                for (int j = 0; j < x; j++) {
                        System.out.print("+---");
                }
                System.out.println("+");
        }
 
        private void generateMaze(int cx, int cy) {
                DIR[] dirs = DIR.values();
                Collections.shuffle(Arrays.asList(dirs));
                for (DIR dir : dirs) {
                        int nx = cx + dir.dx;
                        int ny = cy + dir.dy;
                        if (between(nx, x) && between(ny, y)
                                        && (maze[nx][ny] == 0)) {
                                maze[cx][cy] |= dir.bit;
                                maze[nx][ny] |= dir.opposite.bit;
                                generateMaze(nx, ny);
                        }
                }
        }
 
        private static boolean between(int v, int upper) {
                return (v >= 0) && (v < upper);
        }
 
        private enum DIR {
                N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
                private final int bit;
                private final int dx;
                private final int dy;
                private DIR opposite;
 
                // use the static initializer to resolve forward references
                static {
                        N.opposite = S;
                        S.opposite = N;
                        E.opposite = W;
                        W.opposite = E;
                }
 
                private DIR(int bit, int dx, int dy) {
                        this.bit = bit;
                        this.dx = dx;
                        this.dy = dy;
                }
        };
 
        public static void main(String[] args) {
                int x = args.length >= 1 ? (Integer.parseInt(args[0])) : 8;
                int y = args.length == 2 ? (Integer.parseInt(args[1])) : 8;
                MazeGenerator maze = new MazeGenerator(x, y);
                maze.display();
        }
 
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java Program that implements: 1 - The random generation of a maze that will...
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
  • Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5...

    Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5 digits. Of those numbers give me the following information: - Mean , Mode, Median, Range. - Five times, ask the user for a number within the range (from above) and record the time to find the number in the range. (Loop count) 2. Using a any sort algorithm, build sorted 2D array of random numbers (1 TO 5 digits ) with the x direction...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • my subject :Introduction to Computer Graphics Problem #1 You need to write a program which implements...

    my subject :Introduction to Computer Graphics Problem #1 You need to write a program which implements both DDA and Bresenham’s Line Generation Algorithms. The program should prompt the user to enter tow points coordinate and then compute and display the estimated coordinate of the line segment on the screen. Example Please enter your line segment starting point? 10 10 Please enter the coordinate of the line segment end point? 50 30 What algorithm to use DDA Bresenham’s Your program should...

  • in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an...

    in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an interesting two-dimensional color visualization of a sound file while it is playing. Be creative! Additional Notes: double[] data = StdAudio.read(<filename>); -> Takes a sound file and store it as a 2D array of real numbers. Picture pic = new Picture(300,200); -> Creates a picture 300 pixels wide and 200 pixels high Color c= new Color(<red>,<green>,<blue>); -> Each value of <red>, <green>, <blue> is between...

  • Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you......

    Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you... PROGRAM 1 –Linear Data Structure Implementation (Due date: March 5th, 2019, 5% Grade Points) Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically...

  • Instructions Write a program in Java that implements the A* algorithm to find a path from...

    Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...

  • You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton...

    You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton discussed in class. See for example: http://www.bitstorm.org/gameoflife/ Our version has a 10 x 10 grid, numbered like this: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 The grid is represented by a 10 x 10 2­dimensional integer array. If the grid point (i, j) is "populated", the array element [i][j] contains 1;...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Using Java, write a program that teachers can use to enter and calculate grades of individual...

    Using Java, write a program that teachers can use to enter and calculate grades of individual students by utilizing an array, Scanner object, casting, and the print method. First, let the user choose the size for the integer array by using a Scanner object. The integer array will hold individual assignment grades of a fictional student. Next, use a loop to populate the integer array with individual grades. Make sure each individual grade entered by the user fills just one...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

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