Question: convert JAVA to javascript import java.util.Scanner; class Mazetrav { static final int DOWN = 0; static final int RIGHT = 1; static final int UP
convert JAVA to javascript
import java.util.Scanner; class Mazetrav { static final int DOWN = 0; static final int RIGHT = 1; static final int UP = 2; static final int LEFT = 3; static final int X_START = 2; static final int Y_START = 0; static int move = 0; static int maze [][] ={ {'#','#','#','#','#','#','#','#','#','#','#','#'}, {'#','.','.','.','#','.','.','.','.','.','.','#'}, {'.','.','#','.','#','.','#','#','#','#','#','.'}, {'#','#','#','.','#','.','.','.','.','#','.','#'}, {'#','.','.','.','.','#','#','#','.','#','.','.'}, {'#','#','#','#','.','#','.','#','.','#','.','#'}, {'#','.','.','#','.','#','.','#','.','#','.','#'}, {'#','#','.','#','.','#','.','#','.','#','.','#'}, {'#','.','.','.','.','.','.','.','.','#','.','#'}, {'#','#','#','#','#','#','.','#','#','#','.','#'}, {'#','.','.','.','.','.','.','#','.','.','.','#'}, {'#','#','#','#','#','#','#','#','#','#','#','#'} }; static Scanner scanner = new Scanner(System.in); public void traverse() { boolean result = mazeTraversal(maze, X_START, Y_START); if (!result) system.out.println("Maze has no solution."); } public boolean mazeTraversal(char maze2[][],int x, int y){ maze[x][y] = 'x'; move++; printMaze(); if((x==X_START)&&(y==Y_START)&&(move>1)) { System.out.print("Returned to starting location!"); return false; } else if (mazeExited(x,y)&&(move>1)) { system.out.println("Maze successfully exited!"); return true; } else{ system.out.print("Enter 'y' to continue, 'n' to exit:"); char response = scanner.nextLine().charAt(0); if (response == 'n') system.exit(0);
for(int count = 0; count < 4; count++){ switch (count){ case DOWN: if (validMove(x + 1, y)){ if (mazeTraversal(maze2, x + 1, y)) return true; } break;
case RIGHT: if (validMove(x, y + 1)){ if (mazeTraversal(maze2, x, y + 1)) return true; } break;
case UP: if (validMove(x - 1, y)){ if (mazeTraversal(maze2, x, y - 1)) return true; } } } maze2[x][y] = '0'; return false; } }
public static boolean validMove (int row, int column){ return ((row >= 0)&&(column <= 11) && (column >= 0) && (column <= 11) &&(maze[row][column]=='.')); }
public static boolean mazeExited (int row, int column){ return((row == 0) || (row == 11) || (column == 0) || (column == 11)); }
public void printMaze(){ int x = 5, y = 30;
for (int row = 0; row < maze.length; row++){ for (int column = 0; column < maze[row].length; column++){ if (maze[row][column]=='0') System.out.print("."); else System.out.print(""+ maze[row][column]); } y += 10; x = 5; System.out.println(); } System.out.println(); } } public class MazeRecv{ public static void main(String args[]){ Mazetrav maze = new Mazetrav(); maze.traverse(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
