My maze runner code has to be able to pull a already made maze file but it keeps
ID: 3835041 • Letter: M
Question
My maze runner code has to be able to pull a already made maze file but it keeps giving me an error that it cant find it. Even tho the txt file is in the same folder.
package Maze;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main
{
private char [][] board;
private int rows;
private int columns;
private static final char wall = '0';
private static final char space = '1';
private static final char tried = '#';
private static final char path = '.';
private static final char exit = '1';
public Main(String inFileName)
{
Scanner inFile = null;
try
{
inFile = new Scanner(new FileInputStream(inFileName));
}
catch (FileNotFoundException e)
{
System.out.printf("Cannot find '%s'. Exiting... ", inFileName);
System.exit(1);
}
String line = inFile.nextLine();
StringTokenizer tok = new StringTokenizer(line, " ");
rows = Integer.parseInt(tok.nextToken());
columns = Integer.parseInt(tok.nextToken());
board = new char[rows][columns];
for (int i = 0; i < rows; ++i)
{
line = inFile.nextLine();
for (int j = 0; j < columns; ++j)
{
board[i][j] = line.charAt(j);
}
}
}
public boolean solve(int i, int j)
{
System.out.printf("solve(%2d, %2d): start ", i, j);
boolean done = false;
if (blocked(i, j))
{
System.out.printf("solve(%2d, %2d): we're blocked! Returning false. ",
i, j);
return false;
}
else if (board[i][j] == exit)
{
System.out.printf("solve(%2d, %2d): We're done :) Returning true. ",
i, j);
return true;
}
else
{
board[i][j] = tried;
System.out.printf("solve(%2d, %2d): let's go up ", i, j);
done = solve(i - 1, j);
if (! done)
{
System.out.printf("solve(%2d, %2d): let's go right ", i, j);
done = solve(i, j + 1);
}
if (! done)
{
System.out.printf("solve(%2d, %2d): let's go down ", i, j);
done = solve(i + 1, j);
}
if (! done)
{
System.out.printf("solve(%2d, %2d): let's go left ", i, j);
done = solve(i, j - 1);
}
}
if (done)
{
System.out.printf("solve(%2d, %2d): marking path ", i, j);
board[i][j] = path;
}
else
System.out.printf("solve(%2d, %2d): need to backtrack ", i, j);
return done;
}
public boolean blocked(int i, int j)
{
return i < 0 || j < 0 || i >= rows || j >= columns ||
board[i][j] == wall || board[i][j] == tried;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
sb.append(board[i][j]);
}
sb.append(" ");
}
return sb.toString();
}
}
Explanation / Answer
Try this you will get idea about...
Give input file as maze.txt:
20 7
18 0
12 6
xxxxxxxxxxxxxxxxxx x
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx
package com.runMazer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Maze {
private int exit;
private int entrance;
private ArrayList<ArrayList<Character>> maze;
/*Creates a Scanner for the inputed file with a maze. The input must be a file name and path (e.g. file.txt).
* In addition, this file must be formatted such that the length and height of the maze are on the first line,
* the second contains the array coordinates for the exit
* and the third the array coordinates for the entrance.
* The next lines should contain the maze drawn with 'x's and ' 's.
*/
public Maze (File file) throws FileNotFoundException
{
//innitialize maze
maze = new ArrayList<ArrayList<Character>>();
//print path of file
//System.out.println(file.getAbsolutePath());
Scanner readFile = new Scanner (file);
readFile.nextLine();
exit = readFile.nextInt();
System.out.println(exit);
readFile.nextLine();
entrance = readFile.nextInt();
readFile.nextLine();
System.out.println(entrance);
//int lineNumber = 0;
while (readFile.hasNextLine())
{
String line = readFile.nextLine();
ArrayList<Character> tempRow = new ArrayList<Character>();
for (int i = 0; i < line.length(); i++)
{
tempRow.add(line.charAt(i));
}
maze.add(tempRow);
//lineNumber++;
}
}
public void solvePrintMaze()
{
ArrayList<ArrayList<Character>> solvedMaze = solveMaze();
for (int i = 0; i < solvedMaze.size(); i++)
{
for (int j = 0; j < solvedMaze.get(i).size(); j++)
System.out.print(solvedMaze.get(i).get(j));
System.out.print(" ");
}
}
/*solveMaze returns a the solved maze, with 'R's replacing spaces to represent the route taken through the maze.
* If it cannot solve the maze, it returns the original maze. If the program makes 1000 moves and does not solve the maze,
* the maze is considered unsolvable and the original maze is returned. If no move can be made, the maze is also considered unsolvable.
*/
public ArrayList<ArrayList<Character>> solveMaze()
{
ArrayList<ArrayList<Character>> workedMaze = maze;
ArrayList<ArrayList<Character>> solvedMaze = maze;
int row = maze.size() - 1;
int column = entrance;
int moves = 0;
while (!(row == 0 && column == exit))
{
System.out.println(row);
System.out.println(column);
solvedMaze.get(row).set(column, 'R');
ArrayList<Character> tempRowSolved = solvedMaze.get(row);
tempRowSolved.set(column, 'R');
ArrayList<Character> tempRowWorked = workedMaze.get(row);
if (isDeadEnd(row, column, workedMaze))
{
tempRowWorked.set(column, 'x');
}
workedMaze.set(row, tempRowWorked);
System.out.println(workedMaze);
solvedMaze.set(row, tempRowSolved);
/*if (column == nextColumn(row, column, workedMaze) && row == nextRow(row, column, workedMaze)) {
return maze;
} */
column = nextColumn(row, column, workedMaze);
row = nextRow(row, column, workedMaze);
//System.out.println(moves);
/*if (moves == 1000)
return maze;
moves ++; */
}
return solvedMaze;
}
/*Takes in the row and column numbers in the array (if they exist in it) and returns true only if that space is available to move to
* (The space exists and is not an 'x').
*/
public static boolean canMove (int row, int column,ArrayList<ArrayList<Character>> arrayMaze)
{
if (column < arrayMaze.size() && column >=0 && row < arrayMaze.get(row).size() && row >= 0 && arrayMaze.get(row).get(column).equals( ' '))
{
return true;
}
return false;
}
/*The next four methods use canMove to determine if the space below, above, to the left, and to the right respectively can be moved to.
*/
public static boolean canMoveBot (int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(row+1, column, arrayMaze);
}
public static boolean canMoveTop (int row, int column,ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(row-1, column, arrayMaze);
}
public static boolean canMoveLeft (int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(row, column -1, arrayMaze);
}
public static boolean canMoveRight (int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(row, column + 1, arrayMaze);
}
/*Uses the canMove methods to determine if three directions around the current position cannot be moved to.
*/
public static boolean isDeadEnd (int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
int n = 0;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (n >= 3)
return true;
return false;
}
/* This uses the canMove methods to determine where to move next. If there are multiple possibilities, it checks and chooses a direction
* in the order of going up, then down.
*/
public int nextRow(int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
if (canMoveTop(row, column, arrayMaze))
return row - 1;
if (canMoveBot(row, column, arrayMaze))
return row + 1;
return row;
}
/* This works like nextRow, but it returns the column amount, choosing to go right first if there are multiple ways to go
*/
public int nextColumn(int row, int column, ArrayList<ArrayList<Character>> arrayMaze)
{
if (canMoveRight(row, column, arrayMaze))
return column + 1;
if (canMoveLeft(row, column, arrayMaze))
return column - 1;
return column;
}
/**
* @param args
*/
public static void main(String[] args) {
File mazeFile = new File ("maze.txt");
Maze testMaze = null;
try {
testMaze = new Maze(mazeFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (testMaze!= null){
testMaze.solvePrintMaze();
}
else
System.out.println("Maze file cannot be found");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.