-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
147 lines (127 loc) · 3.67 KB
/
Main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.Scanner;
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
Stack postack = new Stack();
Stack dir = new Stack();
Stack solution = new Stack();
Scanner file = new Scanner(new File("maze.txt"));
char[][] maze1;
int rows = 0;
int cols = 0;
while(file.hasNext()){
String line = file.next();
rows++;
cols = line.length();
}
maze1 = new char[rows+2][cols+2];
file = new Scanner(new File("maze.txt"));
for(int sjd = 0; sjd < rows+2; sjd++){
for(int hdd = 0; hdd < cols+2; hdd++){
maze1[sjd][hdd] = '#';
}
}
for(int r = 1; r < rows+1; r++){
String thisRow = file.next();
for(int c =1; c < cols; c++){
maze1[r][c] = thisRow.charAt(c);
}
}
int posx = 0;
int posy = 0;
char symbol;
for(int q = 0; q< rows+2; q++){
for(int w = 0; w < cols+2; w++){
if(maze1[q][w] == '@'){
posx = q;
posy = w;
postack.push("("+posx+","+posy+")");
}
}
}
for(int e = 0; e< rows+2; e++){
System.out.println("");
for(int t = 0; t < cols+2; t++){
System.out.print(maze1[e][t]);
}
}
dir.push("start");
while(maze1[posx][posy] != '$'){
if(maze1[posx -1][posy] == '.'){
maze1[posx][posy] = '#';
posx--;
System.out.println(maze1[posx][posy] );
postack.push("("+posx+","+posy+")");
dir.push("left");
} else if(maze1[posx+1][posy] == '.'){
maze1[posx][posy] = '#';
posx++;
postack.push("("+posx+","+posy+")");
dir.push("right");
} else if(maze1[posx][posy-1] == '.'){
maze1[posx][posy] = '#';
posy--;
postack.push("("+posx+","+posy+")");
dir.push("down");
} else if(maze1[posx][posy+1] == '.'){
maze1[posx][posy] = '#';
posy++;
postack.push("("+posx+","+posy+")");
dir.push("up");
} else if(maze1[posx+1][posy] == '$'){
posx++;
postack.push("("+posx+","+posy+")");
break;
} else if(maze1[posx-1][posy] == '$'){
posx--;
postack.push("("+posx+","+posy+")");
break;
} else if(maze1[posx][posy+1] == '$'){
posy++;
postack.push("("+posx+","+posy+")");
break;
} else if(maze1[posx][posy-1] == '$'){
posy--;
postack.push("("+posx+","+posy+")");
break;
} else if(maze1[posx+1][posy] == '#' && maze1[posx-1][posy] == '#' && maze1[posx][posy+1] == '#' && maze1[posx][posy-1] == '#'){
if(dir.peek().equals("left")){
maze1[posx][posy] = '#';
dir.pop();
posx++;
} else if(dir.peek().equals("right")){
maze1[posx][posy] = '#';
dir.pop();
posx--;
} else if(dir.peek().equals("up")){
maze1[posx][posy] = '#';
dir.pop();
posy--;
} else if(dir.peek().equals("down")){
maze1[posx][posy] = '#';
dir.pop();
posy++;
} else if(dir.peek().equals("start")){
System.out.println("Bruhh why");
for(int yt = 0; yt< rows+2; yt++){
System.out.println("");
for(int yr = 0; yr < cols+2; yr++){
System.out.print(maze1[yt][yr]);
}
}
} else{
System.out.println("STOP AA");
}
postack.pop();
} else{
break;
}
}
while(postack.peek() != null){
solution.push(postack.pop());
}
while(solution.peek() != null){
System.out.println(solution.pop());
}
}
}