Passing 2 dementional array to a new class
Hello everyone, first post =D
Okay, so I am writing a program (which is an assignment for school) that reads a maze from an input file (or creates a random maze of any size) and then searches that maze and prints out the correct path (if there is one) to traverse the maze (as well as the coordinates that it took along its path).
I have just finished writing my read maze from file, create random maze, and print maze to screen (as well as all the user interaction stuff). All i have left to do is the searching part, which I plan on writing in a different class because it is such a major (and likely large) part of the final program.
My question does not lie with any of this, but rather how to correctly pass a variable from one class to another. My code is as follows, and I will highlight the relevant parts of the code.
"MANAGER" class:
Code:
import java.util.*;
import java.io.*;
public class Manager
{
Scanner reader, input;
String[][] maze;
String command;
int cols, rows;[COLOR="Red"]
searchMaze search;[/COLOR]
[COLOR="Red"] public Manager()
{
search = new searchMaze();
input = new Scanner(System.in);
}[/COLOR]
public String getCommand()
{
System.out.print("enter command: ");
return input.next();
}
public void readMaze(String fileName) //saves maze from file to 2d array "maze"
{
boolean opened;
boolean setSize = false;
boolean wrongSize = false;
boolean blockReached = false;
int rowVerify = 0;
int colVerify = 0;
File infile = new File(fileName);
try
{
reader = new Scanner(infile);
opened = true;
}
catch (FileNotFoundException e)
{
opened = false;
}
if (opened == true)
{
String first = reader.next();
String second = reader.next();
if (isInt(first) == true && isInt(second) == true)
{
rows = Integer.parseInt(first);
cols = Integer.parseInt(second);
maze = new String[rows][cols];
setSize = true;
}
String currentLine = "";
int currentRow = 0;
int currentCol = 0;
while (reader.hasNext() && setSize == true)
{
String currentString = reader.next();
currentLine = currentLine + currentString + " "; // concatinate string untill the next if-statement stops the process
if (currentLine.length() > 2*cols + 2) // if true, then array will be too small to be filled completely. IE: the user gave incorrect
{ // row/col values. Breaks loop here and is dealt with below.
wrongSize = true;
break;
}
else if (currentLine.length() == (2*cols + 2)) // "2*cols + 2" is the number of characters contained in a maze of any size
{ // | |x| | | <<-- 4 cols, 4*2 + 1 = total chars = 9. the reason that it is + 2 and not + 1
// is due to the concatination of one extra white space
for (int i = 1; i < 2*cols + 1; i = i + 2)
{
char item = currentLine.charAt(i);
if (currentRow >= rows || currentCol >= cols) // if either currentRow or currentCol exceeds its respective fixed array value, then
break; // there is a sizing issue, and an error will eventually culminate. This error is SIZE
// based, and is caught pre-emptively and delt with below. This ultimately means that
maze[currentRow][currentCol] = Character.toString(item);
currentCol++;
}
currentRow++; // the current file line means new row
rowVerify = currentRow;
colVerify = currentCol; //necisary for comparison after loop has ended, due to the statement "currentCol = 0;"
currentCol = 0; //reset colums for new row
currentLine = ""; // reset concatination variable for new line in file
}
}
if (colVerify != cols || rowVerify != rows)
wrongSize = true;
}
if (opened == false)
System.out.println("File could not be opened. Ensure maze file exists and is in accessible folder.");
if (setSize == false)
System.out.println("Maze size not found. Ensure maze size is the first item in maze file in format \"<n> <m>\"");
if (wrongSize == true || blockReached == false)
{
System.out.println("Given maze does not fit the specified size. Fix maze size and try again (No maze was saved because of size issues. Fix formatting and try again.)");
cols = 0;
rows = 0;
maze = new String[rows][cols];
}
reader.close();
}
public void show() // prints maze to screen
{
for (int i = 0; i < rows; i++)
{
System.out.println();
for (int j = 0; j < cols; j++)
{
if (j == 0)
System.out.print("|"+maze[i][j]+"|");
else
System.out.print(maze[i][j]+"|");
}
}
System.out.println();
System.out.println();
}
public void makeRandomMaze(int tempRows, int tempCols) // creates random n x m maze saved as 2d array "maze"
{
Random randomGen = new Random();
rows = tempRows;
cols = tempCols;
maze = new String[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int randomInt = randomGen.nextInt(3); // 1/4 probability of X
if (randomInt == 0 && (i + j) != 0 && (i + j) != (rows + cols - 2)) // Prevents X from being placed at enterance position (0,0)
maze[i][j] = "X";
else
maze[i][j] = " ";
}
}
}
[COLOR="Red"] public String[][] passMaze()
{
return maze;
}[/COLOR]
public void run() // contains all user-interaction logic
{
showMenu();
command = getCommand();
while (!command.equals("exit"))
{
[COLOR="Red"] if (command.equals("make"))
{
String n = input.next();
String m = input.next();
if (isInt(n) == true && isInt(m) == true)
{
makeRandomMaze(Integer.parseInt(n), Integer.parseInt(m));
search.getMaze();
}[/COLOR]
else
System.out.println("Invalid row/column combination. Ensure both values given are integers and try again.");
}
else if (command.equals("read"))
{
String mazeFile = input.next();
readMaze(mazeFile);
}
else if (command.equals("search"))
{
System.out.println("method does not exist yet");
}
else if (command.equals("show"))
show();
else if (command.equals("help"))
showMenu();
else
System.out.println("Command not found. Try again.");
command = getCommand();
}
}
public void showMenu() //command options
{
System.out.println();
System.out.println("make <n> <m> // make a new randomly generated maze with n rows and m columns");
System.out.println("read <filename> // read a maze from a text file");
System.out.println("search // find a path through the current maze");
System.out.println("show // display the current maze on the screen");
System.out.println("help // display menu");
System.out.println("exit // close program");
System.out.println();
}
public boolean isInt(String item) //Generic pre-writen method to verify if a String is an Integer
{
int intCount = 0;
char chars[] = item.toCharArray();
for (int i = 0; i < chars.length; i++)
{
char currentChar = chars[i];
if (Character.isDigit(currentChar))
intCount = intCount + 1;
}
if (intCount == chars.length)
return true;
else
return false;
}
public static void main(String args[])
{
Manager driver = new Manager();
driver.run();
}
}
"SEARCH MAZE" class:
Code:
public class searchMaze
{
String[][] mazeToSearch;
[COLOR="Red"] Manager manager;[/COLOR]
public void getMaze()
{
[COLOR="Red"]Manager main = new Manager();
System.out.println(mazeToSearch); //prints null, which is expected.
mazeToSearch = main.passMaze();
System.out.println(mazeToSearch); //still prints null...?[/COLOR]
}
}
The code in the searchMaze class's getMaze method is of course not what I am ultimately going to do, but if you guys can help me get that code to work, then I will be able to continue writing my code. The getMaze method was simply created because I did not think that it would work, so I wanted to test it before moving on.
If anyone knows what is wrong with this "passing" aspect of my code, any help would be very much appreciated.
Also, if you don't know what is specifically wrong with my code, but know how to fix my problem, that's completely fine with me =S just want to get it figured out so i can finish this up.
Thanks in advance,
anonymous
PS: I am only allowed to use the very basic built in methods/class, as this is an introductory course to data structures...they want us to do everything (within reason) by hand.