-
Notifications
You must be signed in to change notification settings - Fork 18
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
[사다리 미션] 김성재 미션 제출합니다. #5
base: seongjae6751
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Controller.Controller; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
Controller controller = new Controller(); | ||
controller.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Player> players = inputHandler.askPlayers(); | ||
List<String> 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package Domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private final List<Line> line = new ArrayList<>(); | ||
|
||
public Ladder(int width, int height) { | ||
for (int i = 0; i < height; i++) { | ||
line.add(new Line(width)); | ||
} | ||
} | ||
|
||
public List<Line> 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package Domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Line { | ||
private final List<Boolean> points = new ArrayList<>(); | ||
private final Random random = new Random(); | ||
boolean beforeHasLine = false; | ||
|
||
public List<Boolean> 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(); | ||
} | ||
Comment on lines
+27
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 방법은 생각 못해봤네요 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
Comment on lines
+7
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 검증 좋은것 같아요 |
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getStartPosition() { | ||
return startPosition; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Player> askPlayers() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
String input = scanner.nextLine(); | ||
String[] names = input.split(","); | ||
List<Player> players = new ArrayList<>(); | ||
for (int i = 0; i < names.length; i++) { | ||
players.add(new Player(names[i].trim(), i)); | ||
} | ||
return players; | ||
} | ||
|
||
public List<String> askResults(int playerCount) { | ||
System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); | ||
String input = scanner.nextLine(); | ||
String[] results = input.split(","); | ||
if (results.length != playerCount) { | ||
throw new IllegalArgumentException("참여자 수와 결과 수가 일치하지 않습니다."); | ||
} | ||
List<String> 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 개행을 안해주던데 어떻게 하셨나요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그냥 이렇게 했더니 잘 동작해서 😅 뭐라 말씀드려야 할지 모르겠네요..ㅋㅋ |
||
} | ||
|
||
public String askPlayerName() { | ||
System.out.println("결과를 보고 싶은 사람은?"); | ||
return scanner.next(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Player> players, Ladder ladder, List<String> 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<Player> players, List<String> 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<Player> players, List<String> results, Ladder ladder) { | ||
for (Player player : players) { | ||
int startPosition = player.getStartPosition(); | ||
int endPosition = ladder.getResult(startPosition); | ||
System.out.println(player.getName() + " : " + results.get(endPosition)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package View; | ||
|
||
import java.util.List; | ||
|
||
import Domain.Ladder; | ||
import Domain.Player; | ||
|
||
|
||
public class ResultHandler { | ||
private final List<Player> players; | ||
private final List<String> results; | ||
private final Ladder ladder; | ||
|
||
public ResultHandler(List<Player> players, List<String> 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); | ||
} | ||
} | ||
} | ||
Comment on lines
+40
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 제가 잘못이해했나보네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예시에서 실행결과 보니까 결과를 보고 싶은 사람 여러번 할 수 있게 해놓으셨더라구요! |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 사다리게임에만 신경을 쓰다보니 플레이어를 관리해줄 생각은 못해봤네요 👍