-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem61.java
202 lines (176 loc) · 5.72 KB
/
Problem61.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package dimikOJ;
import java.util.Scanner;
public class Problem61 {
public static char[][] dimension;
public static boolean validateLanding(int x, int y) {
if (dimension[x - 1][y - 1] == ' ') {
return true;
} else if (dimension[x - 1][y - 1] == '*') {
return false;
} else {
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
char facing = 'N';
String line;
int dx = input.nextInt();
while (dx < 1 || dx > 60) {
dx = input.nextInt();
}
int dy = input.nextInt();
while (dy < 1 || dy > 60) {
dy = input.nextInt();
}
input.nextLine();
dimension = new char[dx][dy];
for (int i = 0; i < dimension.length; i++) {
line = input.nextLine();
for (int j = 0; j < dimension[i].length; j++) {
dimension[i][j] = line.charAt(j);
}
}
int x = input.nextInt();
while (x < 1 || x > dy) {
System.out.println("Can't place here! Out of dimension. Enter x again: ");
x = input.nextInt();
}
int y = input.nextInt();
while (y < 1 || y > dx) {
System.out.println("Can't place here! Out of dimension. Enter y again: ");
y = input.nextInt();
}
boolean landing = validateLanding(x, y);
while (landing == false) {
System.out.println("Can't place here! It's a wall (*). Try again: ");
x = input.nextInt();
while (x < 1 || x > dy) {
System.out.println("Can't place here! Out of dimension. Enter x again: ");
x = input.nextInt();
}
y = input.nextInt();
while (y < 1 || y > dx) {
System.out.println("Can't place here! Out of dimension. Enter y again: ");
y = input.nextInt();
}
landing = validateLanding(x, y);
}
input.nextLine();
dimension[x - 1][y - 1] = 'B';
System.out.printf("Initial position and facing: %d %d %c. B indicates position:\n", x, y, facing);
for (int i = 0; i < dimension.length; i++) {
for (int j = 0; j < dimension[i].length; j++) {
System.out.printf("%c", dimension[i][j]);
}
System.out.printf("\n");
}
dimension[x - 1][y - 1] = ' ';
char command;
System.out
.println("Enter just one command at one line. More than one command will not be counted! Now enter: ");
while (true) {
command = input.next().charAt(0);
while (command != 'R' && command != 'L' && command != 'F' && command != 'Q') {
System.out.println("Invalid command " + command + ". Try again: ");
command = input.next().charAt(0);
}
if (command == 'Q') {
System.out.println("Program terminated!");
break;
} else if (command == 'L') {
if (facing == 'N') {
facing = 'W';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'W') {
facing = 'S';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'S') {
facing = 'E';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'E') {
facing = 'N';
System.out.println("Now faing " + facing + ".");
}
} else if (command == 'R') {
if (facing == 'N') {
facing = 'E';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'E') {
facing = 'S';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'S') {
facing = 'W';
System.out.println("Now faing " + facing + ".");
} else if (facing == 'W') {
facing = 'N';
System.out.println("Now faing " + facing + ".");
}
} else if (command == 'F') {
if (facing == 'N') {
x--;
if (x < 1) {
System.out.println("Can't move further! Stuck on the border.");
x++;
} else if (dimension[x - 1][y - 1] == ' ') {
System.out.println("Moved one step up.");
dimension[x][y - 1] = '↑';
} else if (dimension[x - 1][y - 1] == '*') {
System.out.println("Can't move up. Stuck on the wall (*)!");
dimension[x - 1][y - 1] = '*';
x++;
}
} else if (facing == 'E') {
y++;
if (y > dy) {
System.out.println("Can't move further! Stuck on the border.");
y--;
} else if (dimension[x - 1][y - 1] == ' ') {
System.out.println("Moved one step right.");
dimension[x - 1][y - 2] = '→';
} else if (dimension[x - 1][y - 1] == '*') {
System.out.println("Can't move right. Stuck on the wall (*)!");
dimension[x - 1][y - 2] = '*';
y--;
}
} else if (facing == 'S') {
x++;
if (x > dx) {
System.out.println("Can't move further! Stuck on the border.");
x--;
} else if (dimension[x - 1][y - 1] == ' ') {
System.out.println("Moved one step down.");
dimension[x - 2][y - 1] = '↓';
} else if (dimension[x - 1][y - 1] == '*') {
System.out.println("Can't move right. Stuck on the wall (*)!");
dimension[x - 1][y - 1] = '*';
x--;
}
} else if (facing == 'W') {
y--;
if (y < 1) {
System.out.println("Can't move further! Stuck on the border.");
y++;
} else if (dimension[x - 1][y - 1] == ' ') {
System.out.println("Moved one step left.");
dimension[x - 1][y] = '←';
} else if (dimension[x - 1][y - 1] == '*') {
System.out.println("Can't move left. Stuck on the wall (*)!");
dimension[x - 1][y - 1] = '*';
y++;
}
}
}
}
input.close();
System.out.printf("Final position and facing: %d %d %c. B indicates position:\n", x, y, facing);
dimension[x - 1][y - 1] = 'B';
for (int i = 0; i < dimension.length; i++) {
for (int j = 0; j < dimension[i].length; j++) {
System.out.printf("%c", dimension[i][j]);
}
System.out.printf("\n");
}
}
}