-
Notifications
You must be signed in to change notification settings - Fork 709
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
3단계 - 사다리(게임 실행) #1986
base: pawoo0211
Are you sure you want to change the base?
3단계 - 사다리(게임 실행) #1986
Changes from all commits
d869af9
160a7c0
3823578
46a3da7
5065389
e861b20
59239b5
fdfd1aa
f0368e1
0262c6a
ed9a5c0
73d70b1
cc08a71
70a7d0f
bdeef4f
f746244
90ab5e4
c346c8f
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,16 +1,23 @@ | ||
package nextstep.ladder.application.dto; | ||
|
||
import nextstep.ladder.domain.line.Lines; | ||
import nextstep.ladder.domain.participant.Participants; | ||
|
||
|
||
public class LadderResponse { | ||
private final Lines lines; | ||
private Lines lines; | ||
private Participants participants; | ||
|
||
public LadderResponse(Lines lines) { | ||
public LadderResponse(Lines lines, Participants participants) { | ||
this.lines = lines; | ||
this.participants = participants; | ||
} | ||
|
||
public Lines getLines() { | ||
public final Lines getLines() { | ||
return lines; | ||
} | ||
|
||
public final Participants getParticipants() { | ||
return participants; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,25 @@ | |
import nextstep.ladder.application.dto.LadderResponse; | ||
import nextstep.ladder.domain.line.Line; | ||
import nextstep.ladder.domain.line.Lines; | ||
import nextstep.ladder.domain.participant.Participants; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public class LadderService { | ||
|
||
public LadderResponse createLadder(LadderRequest request) { | ||
Lines lines = new Lines(request.getHighCount()); | ||
IntStream.range(0, request.getHighCount()) | ||
.forEach(index -> lines.addLine(new Line(request.getParticipants()))); | ||
public LadderResponse findWinner(LadderRequest request) { | ||
Lines lines = createLines(request); | ||
Participants participants = Participants.of(request.getParticipants()); | ||
lines.moveParticipants(participants); | ||
participants.insertMyResult(request.getResults()); | ||
return new LadderResponse(lines, participants); | ||
} | ||
|
||
return new LadderResponse(lines); | ||
private Lines createLines(LadderRequest request) { | ||
Lines lines = new Lines(); | ||
int rowLineNumber = request.getParticipants().length - 1; | ||
IntStream.range(0, request.getHighCount()) | ||
.forEach(index -> lines.addLine(new Line(rowLineNumber))); | ||
return lines; | ||
Comment on lines
+21
to
+26
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. request 에서 도메인 객체를 만들어줄수도 있지 않을까요? 🤔 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,60 @@ | ||
package nextstep.ladder.domain.line; | ||
|
||
import nextstep.ladder.domain.participant.Participant; | ||
import nextstep.ladder.domain.participant.Participants; | ||
|
||
public class Line { | ||
private final int rowLineNumber; | ||
private RowLinePositions rowLinePosition; | ||
|
||
public Line(String[] participants) { | ||
int participantNumber = participants.length; | ||
int rowLineNumber = participantNumber - 1; | ||
rowLinePosition = new RowLinePositions(rowLineNumber); | ||
rowLineNumber = participantNumber - 1; | ||
rowLinePosition = RowLinePositions.create(rowLineNumber); | ||
} | ||
|
||
public Line(int rowLineNumber) { | ||
this.rowLineNumber = rowLineNumber; | ||
rowLinePosition = RowLinePositions.create(rowLineNumber); | ||
} | ||
|
||
public Line(int rowLineNumber, RowLinePositions rowLinePositions) { | ||
this.rowLineNumber = rowLineNumber; | ||
this.rowLinePosition = rowLinePositions; | ||
Comment on lines
+12
to
+23
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 boolean isTruePosition(int index) { | ||
return rowLinePosition.isTrue(index); | ||
} | ||
|
||
public void movableParticipants(Participants participants) { | ||
participants.getParticipantList() | ||
.stream() | ||
.forEachOrdered(participant -> movableParticipant(participant)); | ||
} | ||
Comment on lines
+30
to
+34
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 void movableParticipant(Participant participant) { | ||
if (movableLeft(participant)) { | ||
return; | ||
} | ||
movableRight(participant); | ||
} | ||
|
||
private boolean movableLeft(Participant participant) { | ||
int index = participant.getCurrentIndex(); | ||
if (index != 0 && isTruePosition(index -1)) { | ||
participant.moveLeft(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean movableRight(Participant participant) { | ||
int index = participant.getCurrentIndex(); | ||
if (index != rowLineNumber && isTruePosition(index)) { | ||
participant.moveRight(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
package nextstep.ladder.domain.line; | ||
|
||
import nextstep.ladder.domain.participant.Participants; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
public class Lines { | ||
private List<Line> lineList = new ArrayList<>(); | ||
private int highCount; | ||
|
||
public Lines() {} | ||
|
||
public Lines(int highCount) { | ||
this.highCount = highCount; | ||
} | ||
|
||
public void addLine(Line line) { | ||
lineList.add(line); | ||
} | ||
|
||
public void moveParticipants(Participants participants) { | ||
lineList.stream() | ||
.forEachOrdered(line -> line.movableParticipants(participants)); | ||
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 final List<Line> getLineList() { | ||
return Collections.unmodifiableList(lineList); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,35 +7,41 @@ | |
import java.util.stream.IntStream; | ||
|
||
public class RowLinePositions { | ||
private List<Boolean> positionList = new ArrayList<>(); | ||
private int rowLineCount; | ||
private Random random = new Random(); | ||
private List<Boolean> positionList; | ||
private int rowLineNumber; | ||
private static final Random random = new Random(); | ||
|
||
public RowLinePositions(int rowLineCount) { | ||
this.rowLineCount = rowLineCount; | ||
initializePositionList(); | ||
public RowLinePositions(List<Boolean> positionList, int rowLineNumber) { | ||
this.positionList = positionList; | ||
this.rowLineNumber = rowLineNumber; | ||
Comment on lines
+14
to
+16
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. 생성자에선 필수값체크정도 하는게 좋아보여요! |
||
} | ||
|
||
private void initializePositionList() { | ||
public static RowLinePositions create(int rowLineNumber) { | ||
List<Boolean> positionList = new ArrayList<>(); | ||
initializePositionList(positionList, rowLineNumber); | ||
return new RowLinePositions(positionList, rowLineNumber); | ||
} | ||
|
||
private static void initializePositionList(List<Boolean> positionList, int rowLineNumber) { | ||
positionList.add(random.nextBoolean()); | ||
IntStream.range(1, rowLineCount) | ||
.forEach(index -> addRandomBoolean(index)); | ||
addTrueIfAllFalse(); | ||
IntStream.range(1, rowLineNumber) | ||
.forEach(index -> addRandomBoolean(positionList, index)); | ||
addTrueIfAllFalse(positionList, rowLineNumber); | ||
} | ||
|
||
private void addRandomBoolean(int index) { | ||
private static void addRandomBoolean(List<Boolean> positionList, int index) { | ||
if (positionList.get(index - 1)) { | ||
positionList.add(false); | ||
return; | ||
} | ||
positionList.add(random.nextBoolean()); | ||
} | ||
|
||
private void addTrueIfAllFalse() { | ||
private static void addTrueIfAllFalse(List<Boolean> positionList, int rowLineNumber) { | ||
boolean isAllFalse = positionList.stream() | ||
.allMatch(e -> e.equals(Boolean.FALSE)); | ||
if (isAllFalse) { | ||
positionList.set(random.nextInt(rowLineCount), Boolean.TRUE); | ||
positionList.set(random.nextInt(rowLineNumber), Boolean.TRUE); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package nextstep.ladder.domain.participant; | ||
|
||
|
||
public class Participant { | ||
private String name; | ||
private int currentIndex; | ||
private String result; | ||
|
||
public Participant(String name, int currentIndex) { | ||
this.name = name; | ||
this.currentIndex = currentIndex; | ||
} | ||
|
||
public void moveLeft() { | ||
currentIndex -= 1; | ||
} | ||
|
||
public void moveRight() { | ||
currentIndex += 1; | ||
} | ||
|
||
public void insertResult(String[] results) { | ||
result = results[currentIndex]; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public final int getCurrentIndex() { | ||
return currentIndex; | ||
} | ||
|
||
public String getResult() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{name:" + name + "}" + "{currentPosition:" + currentIndex + "}" + "|"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nextstep.ladder.domain.participant; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Participants { | ||
private final List<Participant> participantList; | ||
|
||
public Participants(List<Participant> participantList) { | ||
this.participantList = participantList; | ||
} | ||
|
||
public static Participants of(String[] participants) { | ||
List<Participant> participantList = new ArrayList<>(); | ||
Arrays.stream(participants) | ||
.forEach(participant -> participantList.add(new Participant(participant, participantList.size()))); | ||
return new Participants(participantList); | ||
} | ||
|
||
public void insertMyResult(String[] results) { | ||
participantList.stream() | ||
.forEach(participant -> participant.insertResult(results)); | ||
} | ||
|
||
public Participant findByName(String name) { | ||
return participantList.stream() | ||
.filter(participant -> participant.getName().equals(name)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("Can not find name")); | ||
} | ||
|
||
public int getParticipantsNumber() { | ||
return participantList.size(); | ||
} | ||
|
||
public final List<Participant> getParticipantList() { | ||
return Collections.unmodifiableList(participantList); | ||
} | ||
} |
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.
- [ ] 체크전
- [X] 체크후
문법은 이렇습니다 😄