This are the requirements for the lab that I need tomake:
This lab can be written entirely insidethe Maza.java file. Create a new project calledMaze.java. Then copy all of the text in Maze.java DownloadMaze.java into the file Maze.java that Netbeans created. You need 2methods:
Example text file for a maze:##### ## #####The string that should result from this file is:"####\n# ##\n #\n####"DeMorgan's Law can help you test multiple condition in one ifstatement. Here is someinformation: wikipedia.org/wiki/De_Morgan's_laws
This is the code for Maze.java
package com.mycompany.maze;
import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Random;import java.util.Scanner;
/** * * @author Ulises Boyso */public class Maze { private char[][] squares; private boolean[][] explored; private boolean animated; private int lastIsRow, lastIsCol; // lastlocation on which an 'is' method // was called; used for toString '?' public Maze(String text) { if (text == null || (text =text.trim()).length() == 0) { throw newIllegalArgumentException("empty lines data"); } String[] lines =text.split("[\r]?\n"); squares = newchar[lines.length][lines[0].length()]; explored = newboolean[lines.length][lines[0].length()]; for (int row = 0; row < getHeight();row++) { if (lines[row].length()!= getWidth()) { throw newIllegalArgumentException("line " + (row + 1) + " wrong length (was" + lines[row].length() + " butshould be " + getWidth() + ")"); } for (int col = 0; col< getWidth(); col++) { squares[row][col] = lines[row].charAt(col); } } lastIsRow = -1; lastIsCol = -1; animated = false; display(); } public int getHeight() { return squares.length; } public char getSquare(int row, int col) { checkIndexes(row, col); return squares[row][col]; } public int getWidth() { return squares[0].length; } public boolean isEscaped() { // check left/right edges for (int row = 0; row < getHeight();row++) { if (explored[row][0] ||explored[row][getWidth() - 1]) { returntrue; } }
// check top/bottom edges for (int col = 0; col < getWidth();col++) { if (explored[0][col] ||explored[getHeight() - 1][col]) { returntrue; } } return false; } public boolean isExplored(int row, int col) { checkIndexes(row, col); lastIsRow = row; lastIsCol = col; display(); return explored[row][col]; } public boolean isMarked(int row, int col) { checkIndexes(row, col); lastIsRow = row; lastIsCol = col; display(); return squares[row][col] =='.'; } public boolean isWall(int row, int col) { checkIndexes(row, col); lastIsRow = row; lastIsCol = col; display(); return squares[row][col] =='#'; } public void mark(int row, int col) { checkIndexes(row, col); display(); squares[row][col] = 'x'; } public void setAnimated(boolean value) { animated = value; } public void setExplored(int row, int col, booleanvalue) { checkIndexes(row, col); explored[row][col] = value; display(); } private void display(){ if (animated){ System.out.println(this.toString()); } } public String toString() { StringBuilder result = newStringBuilder(getWidth() * (getHeight() + 1)); for (int row = 0; row <getHeight(); row++) { for (int col =0; col < getWidth(); col++) { if (row == lastIsRow && col == lastIsCol) { result.append('?'); } else if (squares[row][col] == '#') { result.append('#'); } else if (squares[row][col] == 'x') { result.append('x'); } else if (explored[row][col]) { result.append('.'); } else { result.append(' '); } } result.append('\n'); } maybePause(); return result.toString(); } private void checkIndexes(int row, int col) { if (row < 0 || row >= getHeight()|| col < 0 || col >= getWidth()) { throw newIllegalArgumentException("illegal indexes: (" + row + ", " + col +")"); } } private void maybePause() { if (animated) { sleep(100); } } private void sleep(int ms) { try { Thread.sleep(ms); } catch (InterruptedException ie){} } //this is the public method mentioned in theinstructions which is not correctly implemented
//I tried to do it but i can't figure itout. public static void main(String [] args) throwsFileNotFoundException{ Random rand = new Random(); //not sure what file the instructor isgoing to give us,
//so I just copied what the examplehas. // File file; String s = "####" + "###" + " #" +"####";// file = new File(s);// Scanner scan = newScanner(file); Maze m = new Maze(s); m.setAnimated(true); }//this is th esolve methid mention inthe instruction
//but I have not idea hot to solve it public static void solveMaze(int row, int col,Maze m){ m.setExplored(row, col, true); System.out.println(m + " " +m.toString()); }}
This are the requirements for the lab that I need to make: This lab can be written entirely inside the Maza.java file. C
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am