diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000..57c738c --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,8 @@ +import Controller.Controller; + +public class Application { + public static void main(String[] args) { + Controller controller = new Controller(); + controller.run(); + } +} diff --git a/src/main/java/Controller/Controller.java b/src/main/java/Controller/Controller.java new file mode 100644 index 0000000..d9b85d4 --- /dev/null +++ b/src/main/java/Controller/Controller.java @@ -0,0 +1,28 @@ +package Controller; + +import java.util.List; + +import Domain.Ladder; +import Domain.Player; +import View.InputHandler; +import View.OutputHandler; +import View.ResultHandler; + +public class Controller { + public void run() { + InputHandler inputHandler = new InputHandler(); + + List players = inputHandler.askPlayers(); + List results = inputHandler.askResults(players.size()); + int width = players.size(); + int height = inputHandler.askHeight(); + + Ladder ladder = new Ladder(width, height); + + OutputHandler outputHandler = new OutputHandler(); + outputHandler.printLadder(players, ladder, results); + + ResultHandler resultHandler = new ResultHandler(players, results, ladder); + resultHandler.handleResults(inputHandler); + } +} diff --git a/src/main/java/Domain/Ladder.java b/src/main/java/Domain/Ladder.java new file mode 100644 index 0000000..8bb01f3 --- /dev/null +++ b/src/main/java/Domain/Ladder.java @@ -0,0 +1,35 @@ +package Domain; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder { + private final List line = new ArrayList<>(); + + public Ladder(int width, int height) { + for (int i = 0; i < height; i++) { + line.add(new Line(width)); + } + } + + public List getLines() { + return line; + } + + public int getResult(int startPosition) { + int currentPosition = startPosition; + + for (Line line : line) { + if (currentPosition > 0 && line.getPoints().get(currentPosition - 1)) { + currentPosition--; + continue; + } + if (currentPosition < line.getPoints().size() && line.getPoints().get(currentPosition)) { + currentPosition++; + continue; + } + } + + return currentPosition; + } +} diff --git a/src/main/java/Domain/Line.java b/src/main/java/Domain/Line.java new file mode 100644 index 0000000..2a177d6 --- /dev/null +++ b/src/main/java/Domain/Line.java @@ -0,0 +1,37 @@ +package Domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Line { + private final List points = new ArrayList<>(); + private final Random random = new Random(); + boolean beforeHasLine = false; + + public List getPoints() { + return points; + } + + public Line(int width) { + for (int i = 1; i < width; i++) { + boolean hasLine = random.nextBoolean(); + if (beforeHasLine) { + hasLine = false; + } + points.add(hasLine); + beforeHasLine = hasLine; + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("|"); + for (Boolean point : points) { + sb.append(point ? "-----" : " "); + sb.append("|"); + } + return sb.toString(); + } +} diff --git a/src/main/java/Domain/Player.java b/src/main/java/Domain/Player.java new file mode 100644 index 0000000..03a4228 --- /dev/null +++ b/src/main/java/Domain/Player.java @@ -0,0 +1,27 @@ +package Domain; + +public class Player { + private final String name; + private final int startPosition; + + public Player(String name, int startPosition) { + if (name.length() > 5) { + throw new IllegalArgumentException("이름은 5글자 이하여야 합니다."); + } + this.name = name; + this.startPosition = startPosition; + } + + public String getName() { + return name; + } + + public int getStartPosition() { + return startPosition; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/main/java/View/InputHandler.java b/src/main/java/View/InputHandler.java new file mode 100644 index 0000000..0bdeafa --- /dev/null +++ b/src/main/java/View/InputHandler.java @@ -0,0 +1,55 @@ +package View; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import Domain.Player; + +public class InputHandler { + private final Scanner scanner; + + public InputHandler() { + this.scanner = new Scanner(System.in); + }; + + public List askPlayers() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String input = scanner.nextLine(); + String[] names = input.split(","); + List players = new ArrayList<>(); + for (int i = 0; i < names.length; i++) { + players.add(new Player(names[i].trim(), i)); + } + return players; + } + + public List askResults(int playerCount) { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + String input = scanner.nextLine(); + String[] results = input.split(","); + if (results.length != playerCount) { + throw new IllegalArgumentException("참여자 수와 결과 수가 일치하지 않습니다."); + } + List resultList = new ArrayList<>(); + for (String result : results) { + resultList.add(result.trim()); + } + return resultList; + } + + public int askWidth() { + System.out.println("사다리의 넓이는 몇 개인가요?"); + return scanner.nextInt(); + } + + public int askHeight() { + System.out.println("사다리의 높이는 몇 개인가요?"); + return scanner.nextInt(); + } + + public String askPlayerName() { + System.out.println("결과를 보고 싶은 사람은?"); + return scanner.next(); + } +} diff --git a/src/main/java/View/OutputHandler.java b/src/main/java/View/OutputHandler.java new file mode 100644 index 0000000..25dd567 --- /dev/null +++ b/src/main/java/View/OutputHandler.java @@ -0,0 +1,46 @@ +package View; + +import java.util.List; + +import Domain.Ladder; +import Domain.Line; +import Domain.Player; + +public class OutputHandler { + public void printLadder(List players, Ladder ladder, List results) { + System.out.println("사다리 결과"); + for (Player player : players) { + System.out.print(String.format("%-6s", player.getName())); + } + System.out.println(); + + for (Line line : ladder.getLines()) { + System.out.println(line); + } + + for (String result : results) { + System.out.print(String.format("%-6s", result)); + } + System.out.println(); + } + + public void printIndividualResult(String playerName, List players, List results, Ladder ladder) { + for (Player player : players) { + if (player.getName().equals(playerName)) { + int startPosition = player.getStartPosition(); + int endPosition = ladder.getResult(startPosition); + System.out.println(results.get(endPosition)); + return; + } + } + System.out.println("해당 이름의 플레이어가 없습니다."); + } + + public void printAllResults(List players, List results, Ladder ladder) { + for (Player player : players) { + int startPosition = player.getStartPosition(); + int endPosition = ladder.getResult(startPosition); + System.out.println(player.getName() + " : " + results.get(endPosition)); + } + } +} diff --git a/src/main/java/View/ResultHandler.java b/src/main/java/View/ResultHandler.java new file mode 100644 index 0000000..651790d --- /dev/null +++ b/src/main/java/View/ResultHandler.java @@ -0,0 +1,50 @@ +package View; + +import java.util.List; + +import Domain.Ladder; +import Domain.Player; + + +public class ResultHandler { + private final List players; + private final List results; + private final Ladder ladder; + + public ResultHandler(List players, List results, Ladder ladder) { + this.players = players; + this.results = results; + this.ladder = ladder; + } + + public String getIndividualResult(String playerName) { + for (Player player : players) { + if (player.getName().equals(playerName)) { + int startPosition = player.getStartPosition(); + int endPosition = ladder.getResult(startPosition); + return results.get(endPosition); + } + } + return "해당 이름의 플레이어가 없습니다."; + } + + public void printAllResults() { + for (Player player : players) { + int startPosition = player.getStartPosition(); + int endPosition = ladder.getResult(startPosition); + System.out.println(player.getName() + " : " + results.get(endPosition)); + } + } + + public void handleResults(InputHandler inputHandler) { + while (true) { + String playerName = inputHandler.askPlayerName(); + if (playerName.equals("all")) { + printAllResults(); + } else { + String result = getIndividualResult(playerName); + System.out.println(result); + } + } + } +}