From 5c1bf84af2b9747baf2aaa15a4db63828dbcf687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=88=98=EB=B9=88?= Date: Thu, 10 Oct 2024 21:35:17 +0900 Subject: [PATCH] =?UTF-8?q?=EB=AF=B8=EC=85=98=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/maze/Application.java | 52 +++++++++++++++++++++++++++++ src/main/java/maze/Cell.java | 25 ++++++++++++++ src/main/java/maze/Direction.java | 15 +++++++++ src/main/java/maze/Maze.java | 48 ++++++++++++++++++++++++++ src/main/java/maze/Player.java | 44 ++++++++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 src/main/java/maze/Cell.java create mode 100644 src/main/java/maze/Direction.java create mode 100644 src/main/java/maze/Maze.java create mode 100644 src/main/java/maze/Player.java diff --git a/src/main/java/maze/Application.java b/src/main/java/maze/Application.java index c21bd99..c32ba74 100644 --- a/src/main/java/maze/Application.java +++ b/src/main/java/maze/Application.java @@ -1,7 +1,59 @@ package maze; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.Buffer; +import java.util.List; +import java.util.Scanner; +import java.util.stream.IntStream; + public class Application { public static void main(String[] args) { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + System.out.println("미로의 가로와 세로 크기를 입력하세요."); + String string = readLine(br); + String[] split = string.split(","); + int y = Integer.parseInt(split[0]); + int x = Integer.parseInt(split[1]); + + System.out.println("미로를 입력합니다."); + System.out.println("1은 벽 0은 통로 x는 현재 위치 e는 도착점입니다."); + + List inputs = IntStream.range(0, y) + .mapToObj(value -> readLine(br)) + .toList(); + + Maze maze = Maze.from(inputs); + while (!maze.isOut()) { + System.out.println("어느 방향으로 이동하겠습니까?"); + Direction direction = getDirectionFromInput(br); + move(maze, direction); + System.out.println(maze); + System.out.println(); + } + System.out.println("미로 탈출에 성공했습니다! 프로그램을 종료합니다"); + } + + private static String readLine(BufferedReader br) { + try { + return br.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static Direction getDirectionFromInput(BufferedReader br) { + String rawDirection = readLine(br); + return Direction.from(rawDirection); + } + private static void move(Maze maze, Direction direction) { + try { + maze.move(direction); + }catch (IllegalArgumentException e) { + System.out.println("이동할 수 없습니다. 다시 입력하세요"); + } } } diff --git a/src/main/java/maze/Cell.java b/src/main/java/maze/Cell.java new file mode 100644 index 0000000..f271167 --- /dev/null +++ b/src/main/java/maze/Cell.java @@ -0,0 +1,25 @@ +package maze; + +public enum Cell { + WALL, TRACK, GOAL, PLAYER; + + @Override + public String toString() { + return switch (this) { + case WALL -> "1"; + case TRACK -> "0"; + case PLAYER -> "x"; + case GOAL -> "e"; + }; + } + + public static Cell from(String cellString) { + return switch (cellString) { + case "1" -> WALL; + case "0" -> TRACK; + case "x" -> PLAYER; + case "e" -> GOAL; + case null, default -> throw new IllegalArgumentException("파싱 할 수 없는 값입니다."); + }; + } +} diff --git a/src/main/java/maze/Direction.java b/src/main/java/maze/Direction.java new file mode 100644 index 0000000..cf3ede1 --- /dev/null +++ b/src/main/java/maze/Direction.java @@ -0,0 +1,15 @@ +package maze; + +public enum Direction { + UP, DOWN, LEFT, RIGHT; + + public static Direction from(String rawValue) { + return switch (rawValue) { + case "u" -> UP; + case "d" -> DOWN; + case "l" -> LEFT; + case "r" -> RIGHT; + default -> throw new IllegalArgumentException("잘못된 입력입니다."); + }; + } +} diff --git a/src/main/java/maze/Maze.java b/src/main/java/maze/Maze.java new file mode 100644 index 0000000..ee08b7c --- /dev/null +++ b/src/main/java/maze/Maze.java @@ -0,0 +1,48 @@ +package maze; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Maze { + + private final Player player; + private final Cell[][] maze; + + public static Maze from(List mazeString) { + int x = mazeString.get(0).split(" ").length; + int y = mazeString.size(); + Cell[][] cells = new Cell[x][y]; + for (int i = 0; i < x; i++) { + String string = mazeString.get(i); + String[] rawCells = string.split(" "); + for (int j = 0; j < y; j++) { + cells[i][j] = Cell.from(rawCells[j]); + } + } + return new Maze(cells); + } + + public Maze(Cell[][] maze) { + this.player = new Player(0, 1); + this.maze = maze; + maze[0][1] = Cell.PLAYER; + } + + public void move(Direction direction) { + player.move(direction, maze); + } + + @Override + public String toString() { + return Arrays.stream(maze). + map(cells -> Arrays.stream(cells) + .map(Cell::toString).collect(Collectors.joining(" ")) + ) + .collect(Collectors.joining("\n")); + } + + public boolean isOut() { + return maze[maze[0].length - 1][maze.length - 2] == Cell.PLAYER; + } +} diff --git a/src/main/java/maze/Player.java b/src/main/java/maze/Player.java new file mode 100644 index 0000000..5188c66 --- /dev/null +++ b/src/main/java/maze/Player.java @@ -0,0 +1,44 @@ +package maze; + +public class Player { + + private int x; + private int y; + + public Player(int x, int y) { + this.x = x; + this.y = y; + } + + public void move(Direction direction, Cell[][] maze) { + int nx = getNx(direction); + int ny = getNy(direction); + if (nx < 0 || nx >= maze[0].length || ny < 0 || ny >= maze.length || maze[nx][ny] == Cell.WALL) { + throw new IllegalArgumentException("움직일 수 없는 방향입니다."); + } + maze[x][y] = Cell.TRACK; + x = nx; + y = ny; + maze[nx][ny] = Cell.PLAYER; + } + + private int getNx(Direction direction) { + if (direction == Direction.DOWN) { + return x + 1; + } + if (direction == Direction.UP) { + return x - 1; + } + return x; + } + + private int getNy(Direction direction) { + if (direction == Direction.LEFT) { + return y - 1; + } + if (direction == Direction.RIGHT) { + return y + 1; + } + return y; + } +}