|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final String WIN = "YOU WIN!"; |
| 9 | + private static final String DEAD = "YOU HAVE BEEN KILLED BY "; |
| 10 | + private static final String SPIKE = "SPIKE TRAP"; |
| 11 | + private static final String FININPUT = "Press any key to continue."; |
| 12 | + private static final int TRAPDAMAGE = 5; |
| 13 | + private static final int LOWWERTRAPDAMAGE = 1; |
| 14 | + private static int[] dx = {0, 0, -1, 1}; |
| 15 | + private static int[] dy = {-1, 1, 0, 0}; |
| 16 | + private static Player player; |
| 17 | + private static int[] startPos, bossPos; |
| 18 | + private static Map<String, Monster> monsters; |
| 19 | + private static Map<String, Item> items; |
| 20 | + private static char[][] map; |
| 21 | + private static char[] command; |
| 22 | + private static String dyingMessage; |
| 23 | + private static int N, M; |
| 24 | + private static int finished; |
| 25 | +
|
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + init(); |
| 28 | + int turns = play(); |
| 29 | + if (finished == 0) { |
| 30 | + dyingMessage = WIN; |
| 31 | + map[player.x][player.y] = '@'; |
| 32 | + } |
| 33 | + else if (finished == 2) { |
| 34 | + dyingMessage = FININPUT; |
| 35 | + map[player.x][player.y] = '@'; |
| 36 | + } |
| 37 | +
|
| 38 | + for (int i = 0; i < N; i++) { |
| 39 | + for (int j = 0; j < M; j++) { |
| 40 | + bw.write(map[i][j]); |
| 41 | + } |
| 42 | + bw.write("\n"); |
| 43 | + } |
| 44 | +
|
| 45 | + bw.write("Passed Turns : " + turns + "\n"); |
| 46 | + bw.write("LV : " + player.lv + "\n"); |
| 47 | + bw.write("HP : " + player.hp + "/" + player.maxHp + "\n"); |
| 48 | + bw.write("ATT : " + player.baseW + "+" + player.weapon + "\n"); |
| 49 | + bw.write("DEF : " + player.baseA + "+" + player.armor + "\n"); |
| 50 | + bw.write("EXP : " + player.exp + "/" + (player.lv*5) + "\n"); |
| 51 | + bw.write(dyingMessage + "\n"); |
| 52 | + bw.flush(); |
| 53 | + bw.close(); |
| 54 | + br.close(); |
| 55 | + } |
| 56 | +
|
| 57 | + private static void init() throws IOException { |
| 58 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 59 | + N = Integer.parseInt(st.nextToken()); |
| 60 | + M = Integer.parseInt(st.nextToken()); |
| 61 | + finished = -1; |
| 62 | + startPos = new int[2]; |
| 63 | + bossPos = new int[2]; |
| 64 | + int K = 0; |
| 65 | + int L = 0; |
| 66 | +
|
| 67 | + map = new char[N][M]; |
| 68 | + monsters = new HashMap<>(); |
| 69 | + items = new HashMap<>(); |
| 70 | +
|
| 71 | + for (int i = 0; i < N; i++) { |
| 72 | + map[i] = br.readLine().toCharArray(); |
| 73 | + for (int j = 0; j < M; j++) { |
| 74 | + if (map[i][j] == '@') { |
| 75 | + map[i][j] = '.'; |
| 76 | + player = new Player(i, j); |
| 77 | + startPos[0] = i; |
| 78 | + startPos[1] = j; |
| 79 | + } else if (map[i][j] == '&' || map[i][j] == 'M') { |
| 80 | + K++; |
| 81 | + if (map[i][j] == 'M') { |
| 82 | + bossPos[0] = i; |
| 83 | + bossPos[1] = j; |
| 84 | + } |
| 85 | + } else if (map[i][j] == 'B') { |
| 86 | + L++; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + command = br.readLine().toCharArray(); |
| 92 | +
|
| 93 | + for (int i = 1; i <= K; i++) { |
| 94 | + st = new StringTokenizer(br.readLine()); |
| 95 | + int R = Integer.parseInt(st.nextToken())-1; |
| 96 | + int C = Integer.parseInt(st.nextToken())-1; |
| 97 | + String S = st.nextToken(); |
| 98 | + int W = Integer.parseInt(st.nextToken()); |
| 99 | + int A = Integer.parseInt(st.nextToken()); |
| 100 | + int H = Integer.parseInt(st.nextToken()); |
| 101 | + int E = Integer.parseInt(st.nextToken()); |
| 102 | +
|
| 103 | + if (map[R][C] == 'M') monsters.put(R+","+C, new Monster(S, 'M', W, A, H, E)); |
| 104 | + else monsters.put(R+","+C, new Monster(S, '&', W, A, H, E)); |
| 105 | + } |
| 106 | +
|
| 107 | + for (int i = 0; i < L; i++) { |
| 108 | + st = new StringTokenizer(br.readLine()); |
| 109 | + int R = Integer.parseInt(st.nextToken())-1; |
| 110 | + int C = Integer.parseInt(st.nextToken())-1; |
| 111 | + char T = st.nextToken().charAt(0); |
| 112 | +
|
| 113 | + if (T == 'O') items.put(R+","+C, new Item(T, st.nextToken())); |
| 114 | + else items.put(R+","+C, new Item(T, Integer.parseInt(st.nextToken()))); |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + private static int play() { |
| 119 | + int turns = 0; |
| 120 | + for (int i = 0; i < command.length; i++) { |
| 121 | + int d = dir(command[i]); |
| 122 | + int nx = player.x + dx[d]; |
| 123 | + int ny = player.y + dy[d]; |
| 124 | +
|
| 125 | + if (OOB(nx, ny) || map[nx][ny] == '#') { |
| 126 | + nx = player.x; |
| 127 | + ny = player.y; |
| 128 | + } |
| 129 | +
|
| 130 | + turn(nx, ny); |
| 131 | + turns++; |
| 132 | + if (checkFin()) break; |
| 133 | + } |
| 134 | +
|
| 135 | + if (finished == -1) finished = 2; |
| 136 | + return turns; |
| 137 | + } |
| 138 | +
|
| 139 | + private static boolean checkFin() { |
| 140 | + if (finished > -1) return true; |
| 141 | + return false; |
| 142 | + } |
| 143 | +
|
| 144 | + private static void turn(int nx, int ny) { |
| 145 | + move(nx, ny); |
| 146 | +
|
| 147 | + if (map[nx][ny] == 'B') { |
| 148 | + getO(nx, ny); |
| 149 | + } else if (map[nx][ny] == '^') { |
| 150 | + onTrap(); |
| 151 | + if (player.hp <= 0) { |
| 152 | + if (player.oSet.contains("RE")) { |
| 153 | + player.oSet.remove("RE"); |
| 154 | + player.revive(); |
| 155 | + } else { |
| 156 | + finished = 1; |
| 157 | + dyingMessage = DEAD + SPIKE+ ".."; |
| 158 | + } |
| 159 | + } |
| 160 | + } else if (map[nx][ny] == '&') { |
| 161 | + battle(nx, ny); |
| 162 | + if (player.hp <= 0) { |
| 163 | + if (player.oSet.contains("RE")) { |
| 164 | + player.oSet.remove("RE"); |
| 165 | + player.revive(); |
| 166 | + monsters.get(nx+","+ny).maxHeal(); |
| 167 | + } else { |
| 168 | + finished = 1; |
| 169 | + player.hp = 0; |
| 170 | + dyingMessage = DEAD + monsters.get(nx+","+ny).name + ".."; |
| 171 | + } |
| 172 | + } |
| 173 | + } else if (map[nx][ny] == 'M'){ |
| 174 | + battle(nx, ny); |
| 175 | + if (player.hp > 0) finished = 0; |
| 176 | + else { |
| 177 | + if (player.oSet.contains("RE")) { |
| 178 | + player.oSet.remove("RE"); |
| 179 | + player.revive(); |
| 180 | + monsters.get(nx+","+ny).maxHeal(); |
| 181 | + } else { |
| 182 | + finished = 1; |
| 183 | + player.hp = 0; |
| 184 | + dyingMessage = DEAD + monsters.get(nx+","+ny).name+ ".."; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +
|
| 190 | + private static void getO(int nx, int ny) { |
| 191 | + map[nx][ny] = '.'; |
| 192 | + Item item = items.get(nx+","+ny); |
| 193 | +
|
| 194 | + if (item.type == 'W') { |
| 195 | + player.weapon = item.value; |
| 196 | + } else if (item.type == 'A') { |
| 197 | + player.armor = item.value; |
| 198 | + } else { |
| 199 | + if (player.oSet.size() >= 4 || player.oSet.contains(item.name)) return; |
| 200 | + player.oSet.add(item.name); |
| 201 | + } |
| 202 | + } |
| 203 | +
|
| 204 | + // 난 무조건 이동시키고 배틀, 문제에선 배틀에서 이기면 이동 |
| 205 | + private static void battle(int nx, int ny) { |
| 206 | + Monster monster = monsters.get(nx+","+ny); |
| 207 | + if (monster.role == 'M' && player.oSet.contains("HU")) { |
| 208 | + player.maxHeal(); |
| 209 | + } |
| 210 | + int playerOff = player.baseW + player.weapon; |
| 211 | + int enhanceOff = player.baseW + player.weapon; |
| 212 | + if (player.oSet.contains("CO")) { |
| 213 | + if (player.oSet.contains("DX")) { |
| 214 | + enhanceOff *= 3; |
| 215 | + } else { |
| 216 | + enhanceOff *= 2; |
| 217 | + } |
| 218 | + } |
| 219 | + int playerDef = player.baseA + player.armor; |
| 220 | + int count = 0; |
| 221 | +
|
| 222 | + while (true) { |
| 223 | + int playerDamage = 0; |
| 224 | + if (count == 0) playerDamage = Math.max(1, enhanceOff-monster.baseA); |
| 225 | + else playerDamage = Math.max(1, playerOff-monster.baseA); |
| 226 | + monster.hp -= playerDamage; |
| 227 | + if (monster.hp <= 0) break; |
| 228 | + int monsterDamage = 0; |
| 229 | + if (!(monster.role == 'M' && player.oSet.contains("HU") && count == 0)) { |
| 230 | + monsterDamage = Math.max(1, monster.baseW-playerDef); |
| 231 | + } |
| 232 | + player.hp -= monsterDamage; |
| 233 | + if (player.hp <= 0) break; |
| 234 | + count++; |
| 235 | + } |
| 236 | +
|
| 237 | + if (player.hp > 0) { |
| 238 | + player.exp += calExp(monster.exp); |
| 239 | + if (player.oSet.contains("HR")) player.hp = Math.min(player.hp+3, player.maxHp); |
| 240 | + if (player.exp >= player.lv*5) { |
| 241 | + player.levelUp(); |
| 242 | + } |
| 243 | + map[nx][ny] = '.'; |
| 244 | + } |
| 245 | + } |
| 246 | +
|
| 247 | + private static int calExp(int exp) { |
| 248 | + if (player.oSet.contains("EX")) return (int)(exp*(1.2)); |
| 249 | + return exp; |
| 250 | + } |
| 251 | + private static void onTrap() { |
| 252 | + if (player.oSet.contains("DX")) player.hp -= LOWWERTRAPDAMAGE; |
| 253 | + else player.hp -= TRAPDAMAGE; |
| 254 | + } |
| 255 | +
|
| 256 | + private static void move(int nx, int ny) { |
| 257 | + player.x = nx; |
| 258 | + player.y = ny; |
| 259 | + } |
| 260 | +
|
| 261 | + private static boolean OOB(int nx, int ny) { |
| 262 | + return nx < 0 || nx > N-1 || ny < 0 || ny > M-1; |
| 263 | + } |
| 264 | +
|
| 265 | + private static int dir(char d) { |
| 266 | + if (d == 'L') return 0; |
| 267 | + if (d == 'R') return 1; |
| 268 | + if (d == 'U') return 2; |
| 269 | + return 3; |
| 270 | + } |
| 271 | +
|
| 272 | + static abstract class Living { |
| 273 | + protected int baseW; |
| 274 | + protected int baseA; |
| 275 | + protected int maxHp; |
| 276 | + protected int hp; |
| 277 | + protected int exp; |
| 278 | +
|
| 279 | + public Living(int w, int a, int h, int e) { |
| 280 | + baseW = w; |
| 281 | + baseA = a; |
| 282 | + maxHp = h; |
| 283 | + hp = h; |
| 284 | + exp = e; |
| 285 | + } |
| 286 | +
|
| 287 | + public Living() { |
| 288 | + } |
| 289 | +
|
| 290 | + public void maxHeal() { |
| 291 | + this.hp = this.maxHp; |
| 292 | + } |
| 293 | + } |
| 294 | +
|
| 295 | + static class Player extends Living { |
| 296 | + int x; |
| 297 | + int y; |
| 298 | + int lv; |
| 299 | + int weapon; |
| 300 | + int armor; |
| 301 | + Set<String> oSet; |
| 302 | +
|
| 303 | + public Player(int x, int y) { |
| 304 | + this.x = x; |
| 305 | + this.y = y; |
| 306 | + this.baseW = 2; |
| 307 | + this.baseA = 2; |
| 308 | + this.maxHp = 20; |
| 309 | + this.hp = 20; |
| 310 | + this.lv = 1; |
| 311 | + this.weapon = 0; |
| 312 | + this.armor = 0; |
| 313 | + this.oSet = new HashSet<>(); |
| 314 | + this.exp = 0; |
| 315 | + } |
| 316 | +
|
| 317 | + public void levelUp() { |
| 318 | + this.lv++; |
| 319 | + this.exp = 0; |
| 320 | + this.maxHp += 5; |
| 321 | + this.hp = this.maxHp; |
| 322 | + this.baseW += 2; |
| 323 | + this.baseA += 2; |
| 324 | + } |
| 325 | +
|
| 326 | + public void revive() { |
| 327 | + this.hp = this.maxHp; |
| 328 | + this.x = startPos[0]; |
| 329 | + this.y = startPos[1]; |
| 330 | + } |
| 331 | + } |
| 332 | +
|
| 333 | + static class Monster extends Living { |
| 334 | + String name; |
| 335 | + char role; |
| 336 | +
|
| 337 | + public Monster(String name, char role, int w, int a, int h, int e) { |
| 338 | + super(w, a, h, e); |
| 339 | + this.name = name; |
| 340 | + this.role = role; |
| 341 | + } |
| 342 | + } |
| 343 | +
|
| 344 | + static class Item { |
| 345 | + char type; |
| 346 | + int value; |
| 347 | + String name; |
| 348 | +
|
| 349 | + public Item(char type, String name) { |
| 350 | + this.type = type; |
| 351 | + this.name = name; |
| 352 | + } |
| 353 | +
|
| 354 | + public Item(char type, int value) { |
| 355 | + this.type = type; |
| 356 | + this.value = value; |
| 357 | + } |
| 358 | + } |
| 359 | +} |
| 360 | +``` |
0 commit comments