Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

미션 제출합니다~~ #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/maze/Application.java
Original file line number Diff line number Diff line change
@@ -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<String> 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("이동할 수 없습니다. 다시 입력하세요");
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/maze/Cell.java
Original file line number Diff line number Diff line change
@@ -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("파싱 할 수 없는 값입니다.");
};
}
}
15 changes: 15 additions & 0 deletions src/main/java/maze/Direction.java
Original file line number Diff line number Diff line change
@@ -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("잘못된 입력입니다.");
};
}
}
48 changes: 48 additions & 0 deletions src/main/java/maze/Maze.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
44 changes: 44 additions & 0 deletions src/main/java/maze/Player.java
Original file line number Diff line number Diff line change
@@ -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;
}
}