-
Notifications
You must be signed in to change notification settings - Fork 432
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
Step3 볼링점수판 #992
base: parkyunhwan
Are you sure you want to change the base?
Step3 볼링점수판 #992
Changes from all commits
c12bbc0
5adeec4
2a9955d
891c0f9
d2f3a0a
e881841
89511f0
233f0ff
aa12420
2c386db
682acf0
c2958f9
0e4cf29
c8b36eb
8f3d3be
fb52c6a
be3e100
0370f04
7c0be80
4cffcd9
1aa45f6
3149326
5cf08a5
aee060a
02b2620
6240488
0fa9b91
1b7adc3
93a9a5d
2ef1e8f
793292b
94be0c5
5e8d2e3
6812ccb
1e39ba1
6f4a592
011e1a0
0f996a8
7ef92d9
443d2f3
832c52f
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,22 @@ | ||
package bowling; | ||
|
||
public class FinalFrame extends Frame { | ||
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. Frame을 상속하는 FinalFrame을 만들어주셨네요! |
||
|
||
@Override | ||
public boolean strikeCondition() { | ||
return (!hitRecords.hitTimes(HIT_TWICE) || hitRecords.isRecordAllStrike()) && bowlingPin.isZero(); | ||
} | ||
|
||
@Override | ||
public boolean finishFrame() { | ||
boolean result = hitRecords.hitTimes(HIT_TRIPLE) || failedBounsFrame(); | ||
if (clearAllFrame()) { | ||
chargeBowlingPin(); | ||
} | ||
return result; | ||
} | ||
|
||
private boolean failedBounsFrame() { | ||
return hitRecords.hitTimes(HIT_TWICE) && !clearAllFrame(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,64 @@ | ||
package bowling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Frame { | ||
private static final int HIT_ONCE = 1; | ||
private static final int HIT_TWICE = 2; | ||
private static final int HIT_TRIPLE = 3; | ||
protected static final int HIT_TWICE = 2; | ||
protected static final int HIT_TRIPLE = 3; | ||
|
||
private BowlingPin bowlingPin; | ||
private final List<HitRecord> hitRecords; | ||
protected BowlingPin bowlingPin; | ||
protected final HitRecords hitRecords; | ||
|
||
public Frame() { | ||
this.bowlingPin = new BowlingPin(BowlingPin.MAX_PIN_NUMBER); | ||
hitRecords = new ArrayList<>(); | ||
this.hitRecords = new HitRecords(); | ||
} | ||
|
||
public boolean finishLastFrame() { | ||
return hitThreeTimes() || hitDouble() || failedBounsFrame(); | ||
} | ||
|
||
private boolean failedBounsFrame() { | ||
return hitTwice() && !clearAllFrame(); | ||
} | ||
|
||
public void hitBowlingPin(int count) { | ||
public BowilingTerm hitBowlingPin(int count) { | ||
bowlingPin = bowlingPin.hitPins(new BowlingPin(count)); | ||
BowilingTerm bowilingTerm = BowilingTerm.MISS; | ||
if ((hitRecords.isEmpty() || hitRecords.size() == HIT_TRIPLE || hitStrike()) && bowlingPin.isZero()) { | ||
bowilingTerm = BowilingTerm.STRIKE; | ||
hitRecords.add(new HitRecord(count, bowilingTerm)); | ||
return ; | ||
if (hitRecords.hitOnce() && bowlingPin.isZero()) { | ||
hitRecords.addSpare(); | ||
return BowilingTerm.SPARE; | ||
} | ||
|
||
if (hitRecords.size() == HIT_ONCE && bowlingPin.isZero()) { | ||
bowilingTerm = BowilingTerm.SPARE; | ||
hitRecords.add(new HitRecord(count, bowilingTerm)); | ||
return ; | ||
if (strikeCondition()) { | ||
hitRecords.addStrike(); | ||
return BowilingTerm.STRIKE; | ||
} | ||
|
||
if (count == 0) { | ||
bowilingTerm = BowilingTerm.GUTTER; | ||
hitRecords.add(new HitRecord(count, bowilingTerm)); | ||
return ; | ||
if (count == BowlingPin.ZERO) { | ||
hitRecords.addGutter(); | ||
return BowilingTerm.GUTTER; | ||
} | ||
hitRecords.add(new HitRecord(count, bowilingTerm)); | ||
hitRecords.addMiss(count); | ||
return BowilingTerm.MISS; | ||
} | ||
|
||
private boolean hitStrike() { | ||
for (HitRecord hitRecord : hitRecords) { | ||
if (!hitRecord.hitAll()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean finishFrame() { | ||
return clearAllFrame() || hitTwice(); | ||
protected boolean strikeCondition() { | ||
return bowlingPin.isZero(); | ||
} | ||
|
||
public boolean hitDouble() { | ||
if (hitRecords.size() != HIT_TWICE) { | ||
return false; | ||
} | ||
return hitStrike(); | ||
protected boolean finishFrame() { | ||
return clearAllFrame() || hitRecords.hitTimes(HIT_TWICE); | ||
Comment on lines
+41
to
+42
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. 위와 같은 내용이지만 FinalFrame이 이 메서드를 override를 하는것보다 각기 구현하는게 구조가 더 나아보여요~ |
||
} | ||
|
||
public void chargeBowlingPin() { | ||
bowlingPin = new BowlingPin(BowlingPin.MAX_PIN_NUMBER); | ||
} | ||
|
||
public List<HitRecord> getHitRecords() { | ||
return hitRecords; | ||
return hitRecords.getHitRecords(); | ||
} | ||
|
||
public boolean clearAllFrame() { | ||
return bowlingPin.isZero(); | ||
} | ||
|
||
public boolean hitThreeTimes() { | ||
return hitRecords.size() == HIT_TRIPLE; | ||
public void calculateBonus(int hitCount) { | ||
hitRecords.calculateBonus(hitCount); | ||
} | ||
|
||
public boolean hitTwice() { | ||
return hitRecords.size() == HIT_TWICE; | ||
public HitRecords hitRecords() { | ||
return hitRecords; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,36 @@ | ||
package bowling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
public class Frames { | ||
private final List<Frame> frames; | ||
private final LinkedList<Frame> frames; | ||
|
||
public Frames(int size) { | ||
this.frames = new ArrayList<>(); | ||
for (int i = 0; i < size; i++) { | ||
this.frames = new LinkedList<>(); | ||
for (int i = 0; i < size - 1; i++) { | ||
this.frames.add(new Frame()); | ||
} | ||
this.frames.add(new FinalFrame()); | ||
} | ||
|
||
public FrameNumber pitching(FrameNumber frameNumber, int hitCount) { | ||
Frame frame = frames.get(frameNumber.retrieveIndexNumber()); | ||
frame.hitBowlingPin(hitCount); | ||
if (frame.finishFrame()) { | ||
return frameNumber.next(); | ||
BowilingTerm bowilingTerm = frame.hitBowlingPin(hitCount); | ||
|
||
if (!bowilingTerm.equals(BowilingTerm.STRIKE)) { | ||
ListIterator<Frame> frameListIterator = frames.listIterator(frameNumber.retrieveIndexNumber()); | ||
while (frameListIterator.hasPrevious()) { | ||
Frame previous = frameListIterator.previous(); | ||
previous.calculateBonus(hitCount); | ||
} | ||
Comment on lines
+23
to
+28
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. depth를 줄이는 방향으로 고민해보면 어떨까요? |
||
} | ||
return frameNumber; | ||
} | ||
|
||
|
||
public FrameNumber pitchingLast(FrameNumber frameNumber, int hitCount) { | ||
Frame frame = frames.get(frameNumber.retrieveIndexNumber()); | ||
frame.hitBowlingPin(hitCount); | ||
if (frame.finishLastFrame()) { | ||
if (frame.finishFrame()) { | ||
return frameNumber.next(); | ||
} | ||
|
||
if (frame.clearAllFrame()) { | ||
frame.chargeBowlingPin(); | ||
return frameNumber; | ||
} | ||
return frameNumber; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,72 @@ | ||
package bowling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HitRecords { | ||
private List<HitRecord> hitRecords; | ||
private static final int HIT_ONCE = 1; | ||
|
||
public HitRecords(List<HitRecord> hitRecords) { | ||
this.hitRecords = hitRecords; | ||
private final List<HitRecord> hitRecords; | ||
private Score score; | ||
Comment on lines
+9
to
+10
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. HitRecord안에 Score를 관리할 수 있는 구조로 개선해보면 어떨까요? 구조가 좀 많이 바뀔거같긴한데 한번 참고만 해보셔요! |
||
|
||
public HitRecords() { | ||
this.hitRecords = new ArrayList<>(); | ||
this.score = Score.ofZero(); | ||
} | ||
|
||
public boolean hitOnce() { | ||
return hitRecords.size() == HIT_ONCE; | ||
} | ||
|
||
|
||
public void addRecord(HitRecord hitRecord) { | ||
this.hitRecords.add(hitRecord); | ||
} | ||
|
||
public void addStrike() { | ||
this.hitRecords.add(HitRecord.of(10, BowilingTerm.STRIKE)); | ||
score = Score.ofStrike(); | ||
} | ||
|
||
public void addSpare() { | ||
this.hitRecords.add(HitRecord.of(10, BowilingTerm.SPARE)); | ||
score = Score.ofSpare(); | ||
} | ||
|
||
public void addGutter() { | ||
this.hitRecords.add(HitRecord.of(0, BowilingTerm.GUTTER)); | ||
score = Score.ofMiss(hitSum()); | ||
} | ||
|
||
public void addMiss(int count) { | ||
this.hitRecords.add(HitRecord.of(count, BowilingTerm.MISS)); | ||
score = Score.ofMiss(hitSum()); | ||
} | ||
|
||
public boolean isRecordAllStrike() { | ||
return hitRecords.stream() | ||
.allMatch(HitRecord::hitAll); | ||
} | ||
|
||
public boolean hitTimes(int hitTwice) { | ||
return hitRecords.size() == hitTwice; | ||
} | ||
|
||
public List<HitRecord> getHitRecords() { | ||
return hitRecords; | ||
} | ||
|
||
private int hitSum() { | ||
return hitRecords.stream().mapToInt(HitRecord::getHitCount).sum(); | ||
} | ||
|
||
public void calculateBonus(int hitCount) { | ||
if (score.remainBonus()) { | ||
score = score.addBonusScore(hitCount); | ||
} | ||
} | ||
|
||
public Score getScore() { | ||
return score; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package bowling; | ||
|
||
public class Score { | ||
private final int hitCount; | ||
private final int bonusCount; | ||
|
||
public static Score ofZero() { | ||
return new Score(0, -1); | ||
} | ||
|
||
public static Score ofStrike() { | ||
return new Score(10, 2); | ||
} | ||
|
||
public static Score ofSpare() { | ||
return new Score(10, 1); | ||
} | ||
|
||
public static Score ofMiss(int hitCount) { | ||
return new Score(hitCount, 0); | ||
} | ||
|
||
private Score(int hitCount, int bonusCount) { | ||
this.hitCount = hitCount; | ||
this.bonusCount = bonusCount; | ||
} | ||
|
||
public boolean remainBonus() { | ||
return bonusCount != 0; | ||
} | ||
|
||
public Score addBonusScore(int hitCount) { | ||
return new Score(this.hitCount + hitCount, bonusCount - 1); | ||
} | ||
|
||
public int getHitCount() { | ||
return hitCount; | ||
} | ||
|
||
public int getBonusCount() { | ||
return bonusCount; | ||
} | ||
|
||
public boolean isMiss() { | ||
return hitCount != 10; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package bowling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Scores { | ||
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 final List<Score> scores; | ||
|
||
public Scores() { | ||
this.scores = new ArrayList<>(); | ||
} | ||
} |
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.
pitchingBall메서드에서 반환값이 필요할까요? 🤔