Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/BowlingGameConsoleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class BowlingGameConsoleApp {
public static void main(String[] args) {
String playerName = InputView.showGetPlayerNameView();
List<Frame> frames = new ArrayList<>();

Frame currentFrame;
do {
int pinCount = InputView.showGetPinCountView(frames);
Frame currentFrame = frames.isEmpty() ? new NormalFrame(pinCount) : frames.get(frames.size() - 1).bowl(pinCount);
currentFrame = frames.isEmpty() ? new NormalFrame(pinCount) : frames.get(frames.size() - 1).bowl(pinCount);
if (!frames.contains(currentFrame)) {
frames.add(currentFrame);
}
OutputView.showStatusBoardView(playerName, frames);
} while(!frames.get(frames.size() - 1).isFinalFrame() || !frames.get(frames.size() - 1).isComplete());
} while(!currentFrame.isFinalFrame() || !currentFrame.isComplete());
}
}
52 changes: 0 additions & 52 deletions src/main/java/domain/Pitch.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/domain/PlayStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum PlayStatus {
STRIKE("X"),
SPARE("/"),
MISS("-"),
GUTTER("-"),
GUTTER("G"),
OPEN("%d"),
NONE("%d");

Expand Down
50 changes: 14 additions & 36 deletions src/main/java/domain/frame/FinalFrame.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
package domain.frame;

import domain.PlayStatus;
import domain.Pitch;

import java.util.List;

public class FinalFrame extends Frame {
private Pitch thirdPitch;

public FinalFrame(int firstPitch) {
super(Frame.MAX_FRAME_NUMBER, firstPitch);
}

@Override
public List<Pitch> getPitches() {
List<Pitch> pitches = super.getPitches();
if(hasThirdPitch()) {
pitches.add(thirdPitch);
}
return pitches;
}

public boolean hasThirdPitch() {
return thirdPitch != null;
}

@Override
public boolean isComplete() {
if(hasThirdPitch()) {
if (getPitches().has(3)) {
return true;
}

if(hasSecondPitch()) {
PlayStatus secondPitchStatus = getSecondPitch().getStatus();
return secondPitchStatus == PlayStatus.OPEN
|| secondPitchStatus == PlayStatus.STRIKE;
if (getPitches().has(2)) {
PlayStatus secondPitchStatus = getPitches().get(2).getStatus();
return PlayStatus.OPEN.equals(secondPitchStatus)
|| PlayStatus.MISS.equals(secondPitchStatus);
}

return false;
}

@Override
public Frame bowl(int pinCount) {
if(isComplete()) {
return this;
}

if(!hasSecondPitch()) {
return getLastPitch().isClear() ? super.resetAndBowl(pinCount) : super.bowl(pinCount);
}
public boolean canScore() {
return isComplete();
}

if(!hasThirdPitch()) {
thirdPitch = new Pitch(getLastPitch().isClear() ? DEFAULT_START_PIN_COUNT : getLastPitch().getRemainPinCount(), pinCount);
@Override
public int score() {
if (!canScore()) {
throw new IllegalStateException(getFrameNumber() + "프레임은 점수를 구할 수 없는 상태입니다.");
}

return this;
return getPitches().sum();
}
}
77 changes: 28 additions & 49 deletions src/main/java/domain/frame/Frame.java
Original file line number Diff line number Diff line change
@@ -1,89 +1,68 @@
package domain.frame;

import domain.Pitch;
import domain.PlayStatus;

import java.util.*;

import domain.pitch.Pitches;

public abstract class Frame {
public static final int DEFAULT_START_PIN_COUNT = 10;
public static final int MIN_FRAME_NUMBER = 1;
public static final int MAX_FRAME_NUMBER = 10;

private Pitch firstPitch;
private Pitch secondPitch;

private Pitches pitches;
private Frame nextFrame;
private int frameNumber;


public Frame(int frameNumber, int firstPitch) {
this.frameNumber = frameNumber;
if(firstPitch > DEFAULT_START_PIN_COUNT) {
throw new IllegalArgumentException("10개 이상의 pinCount는 집계할 수 없습니다.");
}
this.firstPitch = new Pitch(DEFAULT_START_PIN_COUNT, firstPitch);
this.pitches = new Pitches(firstPitch);
}

public int getFrameNumber() {
return frameNumber;
public Frame getNextFrame() {
return nextFrame;
}

protected Pitch getFirstPitch() {
return firstPitch;
public boolean hasNextFrame() {
return nextFrame != null;
}

protected Pitch getSecondPitch() {
return secondPitch;
public int getFrameNumber() {
return frameNumber;
}

public List<Pitch> getPitches() {
List<Pitch> pitches = new ArrayList<>();
pitches.add(firstPitch);

if(hasSecondPitch()) {
pitches.add(secondPitch);
}

public Pitches getPitches() {
return pitches;
}

public boolean hasSecondPitch() {
return secondPitch != null;
}


public abstract boolean isComplete();

public boolean isFinalFrame() {
return frameNumber == MAX_FRAME_NUMBER;
}

public Frame bowl(int pinCount) {
secondPitch = new Pitch(firstPitch.getRemainPinCount(), pinCount);
return this;
}

public Frame resetAndBowl(int pinCount) {
secondPitch = new Pitch(DEFAULT_START_PIN_COUNT, pinCount);
public Frame bowl(int pinCount) {
if (isComplete()) {
return createNextFrame(pinCount);
}

pitches.add(pinCount);
return this;
}

protected Frame createNextFrame(int firstPitch) {
if(frameNumber == MAX_FRAME_NUMBER) {
if (frameNumber == MAX_FRAME_NUMBER) {
throw new IllegalArgumentException("마지막 프레임 이후에는 프레임을 생성할 수 없습니다.");
}

if(frameNumber < MAX_FRAME_NUMBER - 1) {
if (frameNumber < MAX_FRAME_NUMBER - 1) {
return nextFrame = new NormalFrame(frameNumber + 1, firstPitch);
}
return nextFrame = new FinalFrame(firstPitch);
}

public PlayStatus getStatus() {
return getLastPitch().getStatus();
return pitches.getLast().getStatus();
}

public Pitch getLastPitch() {
List<Pitch> pitches = getPitches();
return pitches.get(pitches.size() - 1);
}
public abstract boolean canScore();

public abstract boolean isComplete();
public abstract int score();
}
60 changes: 55 additions & 5 deletions src/main/java/domain/frame/NormalFrame.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package domain.frame;

import domain.PlayStatus;

public class NormalFrame extends Frame {
public NormalFrame(int frameNumber, int firstPitch) {
super(frameNumber, firstPitch);
Expand All @@ -11,15 +13,63 @@ public NormalFrame(int firstPitch) {

@Override
public boolean isComplete() {
return getFirstPitch().isClear() || getSecondPitch() != null;
return getPitches().get(1).isClear() || getPitches().has(2);
}

@Override
public boolean canScore() {
PlayStatus currentStatus = getStatus();

if (!isComplete()) {
return false;
}

if (!PlayStatus.STRIKE.equals(currentStatus)
&& !PlayStatus.SPARE.equals(currentStatus)) {
return true;
}

if (PlayStatus.SPARE.equals(currentStatus)) {
return hasNextFrame();
}

if (!hasNextFrame()) {
return false;
}

if (!PlayStatus.STRIKE.equals(getNextFrame().getStatus())) {
return getNextFrame().isComplete();
}

return getNextFrame().hasNextFrame();
}

@Override
public Frame bowl(int pinCount) {
if(isComplete()) {
return createNextFrame(pinCount);
public int score() {
if (!canScore()) {
throw new IllegalStateException(getFrameNumber() + "프레임은 점수를 구할 수 없는 상태입니다.");
}

int baseScore = getPitches().sum();
PlayStatus currentStatus = getStatus();

if (!PlayStatus.STRIKE.equals(currentStatus)
&& !PlayStatus.SPARE.equals(currentStatus)) {
return baseScore;
}

if (PlayStatus.SPARE.equals(currentStatus)) {
return baseScore
+ getNextFrame().getPitches().get(1).getPinCount();
}

if (!PlayStatus.STRIKE.equals(getNextFrame().getStatus())) {
return baseScore
+ getNextFrame().getPitches().sum();
}

return super.bowl(pinCount);
return baseScore
+ getNextFrame().getPitches().sum()
+ getNextFrame().getNextFrame().getPitches().get(1).getPinCount();
}
}
Loading