-
Notifications
You must be signed in to change notification settings - Fork 56
[BCSD Lab] 임아리 4차시 미션 제출합니다. #75
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
base: dldb-chamchi
Are you sure you want to change the base?
Changes from all commits
7b1a3c7
418e4f6
38f0433
a206dfd
bae2eca
c74c95e
354f100
d67337f
2c31d32
10d8e7a
964b120
f1a4916
5c7da67
a317f47
ec039b5
2d68f6c
6d01fb9
a2a2f88
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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
import java.util.List; | ||
|
||
import view.InputView; | ||
import controller.LadderGameController; | ||
import controller.LadderController; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
List<String> names = InputView.getPlayerNames(); | ||
List<String> results = InputView.getResults(); | ||
int height = InputView.getLadderHeight(); | ||
|
||
LadderGameController game = new LadderGameController(names, results, height); | ||
game.play(); | ||
LadderController ladderGameController = new LadderController(); | ||
ladderGameController.start(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package controller; | ||
|
||
import java.util.*; | ||
|
||
import exception.ErrorMessage; | ||
import model.*; | ||
import view.*; | ||
|
||
public class LadderController { | ||
|
||
private Players players; | ||
private List<String> results; | ||
private Ladder ladder; | ||
private LadderResult ladderResult; | ||
|
||
public void start() { | ||
List<String> names = InputView.getPlayerNames(); | ||
List<String> results = InputView.getResults(); | ||
int height = InputView.getLadderHeight(); | ||
|
||
players = createPlayers(names); | ||
this.results = results; | ||
ladder = new Ladder(players.size(), height); | ||
|
||
play(); | ||
} | ||
|
||
private Players createPlayers(List<String> names) { | ||
List<Player> players = new ArrayList<>(); | ||
for (String name : names) { | ||
players.add(new Player(name)); | ||
} | ||
return new Players(players); | ||
} | ||
|
||
public void play() { | ||
ResultView.printLadder(players, ladder.getLines(), results); | ||
|
||
this.ladderResult = ladder.run(players, results); | ||
|
||
showResults(); | ||
} | ||
|
||
private void showResults() { | ||
while (true) { | ||
String query = InputView.getQuery(); | ||
|
||
if (query.equals("all")) { | ||
ResultView.printAllResults(ladderResult.getAllResults()); | ||
break; | ||
} | ||
|
||
if (ladderResult.getAllResults().containsKey(query)) { | ||
ResultView.printResult(query, ladderResult.getResult(query)); | ||
} | ||
Comment on lines
+48
to
+55
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. 여기처럼 다른 코드도 if문 사이에 줄바꿈이 들어오면 더 가독성이 좋아질 것 같아요.
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. 그것도 좋은 방법입니다! 사람마다 취향이 다른데 저 같은 경우는 이런식으로 if를 걸어서 하는 것을 더 선호합니다! 다른 의견 주셔서 감사합니다! |
||
else { | ||
System.out.println(ErrorMessage.NOT_EXIST_NAME); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package exception; | ||
|
||
public class ErrorMessage { | ||
public static final String LIMIT_NAME_LENGTH = "이름은 최대 5글자까지 가능합니다."; | ||
public static final String NOT_EXIST_NAME = "존재하지 않는 이름입니다. 다시 입력해주세요."; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,23 @@ | |
import java.util.*; | ||
|
||
public class Ladder { | ||
|
||
private final List<Line> lines; | ||
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 Ladder(int playerCount, int height) { | ||
lines = new ArrayList<>(); | ||
|
||
for (int i = 0; i < height; i++) { | ||
lines.add(new Line(playerCount)); | ||
} | ||
} | ||
|
||
public LadderResult run(List<Player> players, List<String> results) { | ||
public LadderResult run(Players players, List<String> results) { | ||
Map<String, String> resultMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < players.size(); i++) { | ||
int finalPosition = move(i); | ||
resultMap.put(players.get(i).getName(), results.get(finalPosition)); | ||
resultMap.put(players.getPlayersList().get(i).getName(), results.get(finalPosition)); | ||
} | ||
return new LadderResult(resultMap); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package model; | ||
|
||
import exception.ErrorMessage; | ||
|
||
public class Player { | ||
|
||
private final String name; | ||
private static final int MAX_NAME_LENGTH = 5; | ||
|
||
public Player(String name) { | ||
if (name.length() > 5) { | ||
throw new IllegalArgumentException("이름은 최대 5글자까지 가능합니다."); | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException(ErrorMessage.LIMIT_NAME_LENGTH); | ||
Comment on lines
+11
to
+12
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. 👍 |
||
} | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Players { | ||
private final List<Player> playersList; | ||
|
||
public Players(List<Player> playersList) { | ||
this.playersList = playersList; | ||
} | ||
|
||
public int size(){ | ||
return playersList.size(); | ||
} | ||
|
||
public List<Player> getPlayersList() { | ||
return playersList; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package view; | ||
|
||
import model.Line; | ||
import model.Player; | ||
import model.Players; | ||
|
||
import java.util.*; | ||
|
||
public class ResultView { | ||
|
||
public static void printLadder(Players players, List<Line> lines, List<String> results) { | ||
for (Player player : players.getPlayersList()) { | ||
System.out.printf("%6s", player.getName()); | ||
} | ||
System.out.println(); | ||
|
||
for (Line line : lines) { | ||
printLine(line); | ||
} | ||
|
||
for (String result : results) { | ||
System.out.printf("%6s", result); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private static void printLine(Line line) { | ||
System.out.print(" "); | ||
for (boolean point : line.getPoints()) { | ||
System.out.print(point ? " |-----" : " | "); | ||
} | ||
System.out.println(" |"); | ||
} | ||
|
||
public static void printResult(String name, String result) { | ||
System.out.println("실행 결과"); | ||
System.out.println(name + " : " + result); | ||
} | ||
|
||
public static void printAllResults(Map<String, String> results) { | ||
System.out.println("실행 결과"); | ||
results.forEach((name, result) -> System.out.println(name + " : " + result)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Map; | ||
|
||
class LadderResultTest { | ||
|
||
@Test | ||
void 특정_플레이어_결과_조회() { | ||
Map<String, String> results = Map.of("Neo", "꽝", "Tommy", "5000", "Brie", "꽝"); | ||
LadderResult ladderResult = new LadderResult(results); | ||
|
||
assertEquals("5000", ladderResult.getResult("Tommy")); | ||
assertEquals("꽝", ladderResult.getResult("Neo")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
class LadderTest { | ||
|
||
@Test | ||
void 사다리_게임_결과_확인() { | ||
Players players = new Players(List.of(new Player("Neo"), new Player("Tommy"), new Player("Brie"))); | ||
List<String> results = List.of("1000", "꽝", "3000"); | ||
|
||
Ladder ladder = new Ladder(players.size(), 5); | ||
LadderResult ladderResult = ladder.run(players, results); | ||
|
||
Map<String, String> resultMap = ladderResult.getAllResults(); | ||
|
||
assertEquals(3, resultMap.size()); | ||
assertTrue(resultMap.containsKey("Neo")); | ||
assertTrue(resultMap.containsKey("Tommy")); | ||
assertTrue(resultMap.containsKey("Brie")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class LineTest { | ||
|
||
@Test | ||
void 사다리_이동() { | ||
Line line = new Line(3); | ||
int newPosition = line.move(1); | ||
|
||
assertTrue(newPosition >= 0 && newPosition < 3); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package model; | ||
|
||
import exception.ErrorMessage; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class PlayerTest { | ||
|
||
@Test | ||
void 이름이_5자를_초과하면_예외() { | ||
IllegalArgumentException exception = assertThrows( | ||
IllegalArgumentException.class, | ||
() -> new Player("abcdef") | ||
); | ||
assertEquals(ErrorMessage.LIMIT_NAME_LENGTH, exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void 이름이_5자_이하면_통과() { | ||
Player player = new Player("Neo"); | ||
assertEquals("Neo", player.getName()); | ||
} | ||
} |
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.
지난번에 비해서 간결해졌네요 👍