-
Notifications
You must be signed in to change notification settings - Fork 39
[송지은] 사다리타기 제출합니다. #38
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: hafnium1923
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,31 @@ | ||
package controller; | ||
|
||
import domain.Ladder; | ||
import domain.LadderGame; | ||
import domain.LadderHeight; | ||
import domain.LadderWidth; | ||
import domain.Players; | ||
import domain.Prizes; | ||
import domain.GameResult; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
public class LadderController { | ||
public static void main(String[] args) { | ||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
Players players = inputView.askPlayers(); | ||
Prizes prizes = inputView.askPrizes(); | ||
LadderHeight height = inputView.askHeight(); | ||
LadderWidth width = new LadderWidth(players.size()); | ||
LadderGame ladderGame = new LadderGame(players, prizes, new Ladder(height, width)); | ||
outputView.printLadderGameBoard(ladderGame); | ||
GameResult gameResult = ladderGame.play(); | ||
String query = inputView.askResultName(); | ||
if (query.equals("all")) { | ||
outputView.printResultAll(gameResult); | ||
return; | ||
} | ||
outputView.printResultSingle(gameResult.getResultByPlayerName(query).getPrize()); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package domain; | ||
|
||
public enum Bridge { | ||
CONNECTED, | ||
NOT_CONNECTED; | ||
|
||
public String display() { | ||
if (this == CONNECTED) { | ||
return "-----"; | ||
} | ||
return " "; | ||
} | ||
|
||
public static Bridge fromBoolean(boolean connected) { | ||
if (connected) { | ||
return CONNECTED; | ||
} | ||
return NOT_CONNECTED; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class GameResult { | ||
private final List<Result> results; | ||
|
||
public GameResult(List<Result> results) { | ||
this.results = results; | ||
} | ||
|
||
public List<Result> getResults() { | ||
return results; | ||
} | ||
|
||
public Result getResultByPlayerName(String name) { | ||
for (Result result : results) { | ||
if (result.getPlayerName().equals(name)) { | ||
return result; | ||
} | ||
} | ||
throw new IllegalArgumentException("해당 이름이 없습니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private final Lines lines; | ||
|
||
public Ladder(LadderHeight height, LadderWidth width) { | ||
this.lines = new Lines(createLines(height, width)); | ||
} | ||
|
||
private List<Line> createLines(LadderHeight height, LadderWidth width) { | ||
List<Line> list = new ArrayList<>(); | ||
Line previousLine = null; | ||
for (int i = 0; i < height.getValue(); i++) { | ||
Line current = new Line(width, previousLine); | ||
list.add(current); | ||
previousLine = current; | ||
} | ||
return list; | ||
} | ||
|
||
public List<String> getLadderBody() { | ||
return lines.drawAll(); | ||
} | ||
|
||
public LadderResult play() { | ||
int columnCount = lines.getColumnCount(); | ||
List<Position> positions = new ArrayList<>(); | ||
for (int i = 0; i < columnCount; i++) { | ||
positions.add(lines.moveAll(new Position(i))); | ||
} | ||
return new LadderResult(positions); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LadderGame { | ||
private final Players players; | ||
private final Prizes prizes; | ||
private final Ladder ladder; | ||
|
||
public LadderGame(Players players, Prizes prizes, Ladder ladder) { | ||
if (players.size() != prizes.getPrizes().size()) { | ||
throw new IllegalArgumentException("참여자 수와 결과 수가 일치해야 합니다."); | ||
} | ||
this.players = players; | ||
this.prizes = prizes; | ||
this.ladder = ladder; | ||
} | ||
|
||
public GameResult play() { | ||
LadderResult ladderResult = ladder.play(); | ||
List<Result> results = new ArrayList<>(); | ||
for (int i = 0; i < players.size(); i++) { | ||
int finalIndex = ladderResult.getPositions().get(i).getValue(); | ||
String playerName = players.getPlayers().get(i).getName().getValue(); | ||
String prize = prizes.getPrizeAt(finalIndex).getValue(); | ||
results.add(new Result(playerName, prize)); | ||
} | ||
return new GameResult(results); | ||
} | ||
|
||
public Ladder getLadder() { | ||
return ladder; | ||
} | ||
|
||
public Players getPlayers() { | ||
return players; | ||
} | ||
|
||
public Prizes getPrizes() { | ||
return prizes; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package domain; | ||
|
||
public class LadderHeight { | ||
private final int value; | ||
|
||
public LadderHeight(int value) { | ||
if (value < 1) { | ||
throw new IllegalArgumentException("사다리의 높이는 1 이상이어야 합니다."); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class LadderResult { | ||
private final List<Position> positions; | ||
|
||
public LadderResult(List<Position> positions) { | ||
this.positions = positions; | ||
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 List<Position> getPositions() { | ||
return positions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package domain; | ||
|
||
public class LadderWidth { | ||
private final int value; | ||
|
||
public LadderWidth(int value) { | ||
if (value < 2) { | ||
throw new IllegalArgumentException("사다리의 넓이는 2 이상이어야 합니다."); | ||
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.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Line { | ||
private final List<Bridge> bridges; | ||
|
||
public Line(LadderWidth ladderWidth, Line previousLine) { | ||
this.bridges = createBridges(ladderWidth, previousLine); | ||
} | ||
|
||
private List<Bridge> createBridges(LadderWidth ladderWidth, Line previousLine) { | ||
List<Bridge> list = new ArrayList<>(); | ||
Random random = new Random(); | ||
boolean previousConnected = false; | ||
List<Bridge> previousBridges = null; | ||
if (previousLine != null) { | ||
previousBridges = previousLine.getBridges(); | ||
} | ||
for (int i = 0; i < ladderWidth.getValue() - 1; i++) { | ||
boolean connected = determineConnection(previousConnected, random, previousBridges, i); | ||
list.add(Bridge.fromBoolean(connected)); | ||
previousConnected = connected; | ||
} | ||
return list; | ||
} | ||
|
||
private boolean determineConnection(boolean previousConnected, Random random, | ||
List<Bridge> previousBridges, int index) { | ||
if (previousConnected) { | ||
return false; | ||
} | ||
if (previousBridges != null && previousBridges.get(index) == Bridge.CONNECTED) { | ||
return false; | ||
} | ||
return random.nextBoolean(); | ||
} | ||
|
||
public List<Bridge> getBridges() { | ||
return bridges; | ||
} | ||
|
||
public String draw() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("|"); | ||
for (Bridge bridge : bridges) { | ||
builder.append(bridge.display()); | ||
builder.append("|"); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
public Position move(Position position) { | ||
int current = position.getValue(); | ||
if (canMoveLeft(current)) { | ||
return new Position(current - 1); | ||
} | ||
if (canMoveRight(current)) { | ||
return new Position(current + 1); | ||
} | ||
return position; | ||
} | ||
|
||
private boolean canMoveLeft(int current) { | ||
return current > 0 && bridges.get(current - 1) == Bridge.CONNECTED; | ||
} | ||
|
||
private boolean canMoveRight(int current) { | ||
int columnCount = bridges.size() + 1; | ||
return current < columnCount - 1 && bridges.get(current) == Bridge.CONNECTED; | ||
} | ||
Comment on lines
+55
to
+73
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,39 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Lines { | ||
private final List<Line> lines; | ||
|
||
public Lines(List<Line> lines) { | ||
this.lines = lines; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
|
||
public List<String> drawAll() { | ||
List<String> drawn = new ArrayList<>(); | ||
for (Line line : lines) { | ||
drawn.add(line.draw()); | ||
} | ||
return drawn; | ||
} | ||
|
||
public Position moveAll(Position position) { | ||
Position result = position; | ||
for (Line line : lines) { | ||
result = line.move(result); | ||
} | ||
return result; | ||
} | ||
|
||
public int getColumnCount() { | ||
if (lines.isEmpty()) { | ||
return 0; | ||
} | ||
return lines.get(0).getBridges().size() + 1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package domain; | ||
|
||
public class Name { | ||
private final String value; | ||
|
||
public Name(String value) { | ||
if (value.length() > 5) { | ||
throw new IllegalArgumentException("이름은 최대 5글자여야 합니다."); | ||
} | ||
this.value = value; | ||
} | ||
Comment on lines
+6
to
+11
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 getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package domain; | ||
|
||
public class Player { | ||
private final Name name; | ||
|
||
public Player(Name name) { | ||
this.name = name; | ||
} | ||
|
||
public Name getName() { | ||
return name; | ||
} | ||
} | ||
Comment on lines
+3
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. 마지막 부분에서 결과 조회할 때 이미 글자 수 확인 로직은 하위 클래스인 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class Players { | ||
private final List<Player> players; | ||
|
||
public Players(List<Player> players) { | ||
this.players = players; | ||
} | ||
|
||
public List<Player> getPlayers() { | ||
return players; | ||
} | ||
|
||
public int size() { | ||
return players.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package domain; | ||
|
||
public class Position { | ||
private final int value; | ||
|
||
public Position(int value) { | ||
if (value < 0) { | ||
throw new IllegalArgumentException("Position은 음수일 수 없습니다."); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public Position moveLeft() { | ||
return new Position(value - 1); | ||
} | ||
|
||
public Position moveRight() { | ||
return new Position(value + 1); | ||
} | ||
} |
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.