-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.java
89 lines (80 loc) · 2.12 KB
/
Maze.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.*;
import java.util.*;
public class Maze
{
private static Square[][] maze;
private static Square start;
private static Square end;
public Maze()
{
}
public static boolean loadMaze(String fileName)
{
Scanner input = null;
try {
input = new Scanner(new File(fileName));
int rows = input.nextInt();
int cols = input.nextInt();
maze = new Square[rows][cols];
while(input.hasNextInt())
{
for(int r = 0; r < rows; r++)
{
for(int c = 0; c < cols; c++)
{
maze[r][c] = new Square(r, c, input.nextInt());
if(maze[r][c].getType() == Square.START) start = maze[r][c];
if(maze[r][c].getType() == Square.EXIT) end = maze[r][c];
}
}
}
}
catch(IOException e) {
System.out.print("Error!");
return false;
}
return true;
}
public static List<Square> getNeighbors(Square s)
{
ArrayList<Square> n = new ArrayList<Square>();
if(s.getRow() != 0) n.add(maze[s.getRow()-1][s.getCol()]);
if(s.getCol() < maze[0].length-1) n.add(maze[s.getRow()][s.getCol()+1]);
if(s.getRow() < maze.length-1) n.add(maze[s.getRow()+1][s.getCol()]);
if(s.getCol() != 0) n.add(maze[s.getRow()][s.getCol()-1]);
return n;
}
//wrong use instance variable and return
public static Square getStart()
{
return start;
}
//wrong as well
public static Square getExit()
{
return end;
}
public static void reset()
{
for(int r = 0; r < maze.length; r++)
{
for(int c = 0; c < maze[0].length; c++)
{
maze[r][c].reset();
}
}
}
public String toString()
{
String ret = "";
for(int r = 0; r < maze.length; r++)
{
for(int c = 0; c < maze[0].length; c++)
{
ret += maze[r][c].toString() + " ";
}
ret += "\n";
}
return ret;
}
}