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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ repositories {
mavenCentral()
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

dependencies {
compile('com.sparkjava:spark-core:2.6.0')
compile('com.sparkjava:spark-template-handlebars:2.5.5')
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/BowlingGameConsoleApp.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import domain.BowlingGame;
import domain.frame.Frame;
import domain.frame.NormalFrame;
import view.InputView;
import view.OutputView;

import java.util.ArrayList;
import java.util.List;

public class BowlingGameConsoleApp {
private static BowlingGame bowlingGame;

public static void main(String[] args) {
String playerName = InputView.showGetPlayerNameView();
List<Frame> frames = new ArrayList<>();
Frame currentFrame;
bowlingGame = new BowlingGame(InputView.showGetPlayerNameView());
do {
int pinCount = InputView.showGetPinCountView(frames);
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(!currentFrame.isFinalFrame() || !currentFrame.isComplete());
bowlingGame.play(InputView.showGetPinCountView(bowlingGame.getFrames().getNextFrameNumber()));
OutputView.showStatusBoardView(bowlingGame);
} while(bowlingGame.canPlay());
}
}
8 changes: 2 additions & 6 deletions src/main/java/BowlingGameWebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) {
Map<String, Object> model = new HashMap<>();
model.put("bowlingGames", bowlingGames);
model.put("player", bowlingGames.get(playerIndex).getPlayerName());
model.put("currentFrameNumber", getCurrentFrameNumber());
model.put("nowFrameNumber", bowlingGames.get(playerIndex).getFrames().getNextFrameNumber());
return render(model, "/play.html");
});

Expand All @@ -38,15 +38,11 @@ public static void main(String[] args) {
Map<String, Object> model = new HashMap<>();
model.put("bowlingGames", bowlingGames);
model.put("player", getNextPlayer());
model.put("currentFrameNumber", getCurrentFrameNumber());
model.put("nowFrameNumber", bowlingGames.get(playerIndex).getFrames().getNextFrameNumber());
return render(model, "/play.html");
});
}

private static int getCurrentFrameNumber() {
return bowlingGames.get(playerIndex).getFrames().size() + 1;
}

private static String getNextPlayer() {
playerIndex = (playerIndex + 1) % bowlingGames.size();
return bowlingGames.get(playerIndex).getPlayerName();
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/domain/BowlingGame.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package domain;

import domain.frame.Frame;
import domain.frame.Frames;
import domain.frame.NormalFrame;

import java.util.ArrayList;
import java.util.List;

public class BowlingGame {
private List<Frame> frames;
private String playerName;
private Frames frames;

public BowlingGame(String playerName) {
this.playerName = playerName;
this.frames = new ArrayList<>();
this.frames = new Frames();
}

public List<Frame> getFrames() {
public Frames getFrames() {
return frames;
}

public String getPlayerName() {
return playerName;
}

public List<Frame> play(int pinCount) {
Frame currentFrame = frames.isEmpty() ? new NormalFrame(pinCount) : frames.get(frames.size() - 1).bowl(pinCount);
if (!frames.contains(currentFrame)) {
frames.add(currentFrame);
public Frames play(int pin) {
Frame nowFrame = frames.isEmpty() ? new NormalFrame(pin) : frames.getLast().bowl(pin);
if (!frames.contains(nowFrame)) {
frames.add(nowFrame);
}
return frames;
}
Expand All @@ -36,7 +34,7 @@ public boolean canPlay() {
return true;
}

Frame lastFrame = frames.get(frames.size() - 1);
Frame lastFrame = frames.getLast();
return !lastFrame.isFinalFrame() || !lastFrame.isComplete();
}
}
20 changes: 0 additions & 20 deletions src/main/java/domain/PlayStatus.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/main/java/domain/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package domain;

import domain.pin.Pins;
import domain.status.*;

public class Score {
private int score;
private int canScoreCount;

public Score(int score, int canScoreCount) {
this.score = score;
this.canScoreCount = canScoreCount;
}

public static Score ofStatus(Status status, Pins pins) {
if (status.ofInstance(Strike.class)) {
return ofStrike();
}
if (status.ofInstance(Spare.class)) {
return ofSpare();
}
if (status.ofInstance(Gutter.class, None.class)) {
return new Score(pins.sum(), 1);
}
return ofNone(pins.sum());
}

public static Score ofNone(int score) {
return new Score(score, 0);
}

public static Score ofSpare() {
return new Score(10, 1);
}

public static Score ofStrike() {
return new Score(10, 2);
}

public int getScore() {
if(!getScoreFlag()) {
throw new IllegalStateException("점수를 구할 수 없는 상태인 프레임의 점수를 합산시도하였습니다.");
}

return score;
}

public Score next(int pin) {
return new Score(score + pin, canScoreCount - 1);
}

public boolean getScoreFlag() {
return canScoreCount == 0;
}
}
36 changes: 17 additions & 19 deletions src/main/java/domain/frame/FinalFrame.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package domain.frame;

import domain.PlayStatus;
import domain.Score;
import domain.status.Spare;
import domain.status.Strike;

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

@Override
public boolean isComplete() {
if (getPitches().has(3)) {
if (getPins().size() > 2) {
return true;
}

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

return false;
return getStatus().isComplete() && !getStatus().ofInstance(Spare.class, Strike.class);

}

@Override
public boolean getScoreFlag() {
return isComplete();
public Score getScore() {
if(isComplete()) {
return Score.ofNone(getPins().sum());
}

return Score.ofStatus(getStatus(), getPins());
}

@Override
public int getScore() {
if (!getScoreFlag()) {
throw new IllegalStateException(getFrameNumber() + "프레임은 점수를 구할 수 없는 상태입니다.");
}
return getPitches().sum();
protected Score getScoreWith(Score beforeScore) {
return getPins().scoreWith(beforeScore);
}
}
52 changes: 32 additions & 20 deletions src/main/java/domain/frame/Frame.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package domain.frame;

import domain.PlayStatus;
import domain.pitch.Pitches;
import domain.Score;
import domain.pin.Pin;
import domain.pin.Pins;
import domain.status.Spare;
import domain.status.Status;
import domain.status.Strike;

import java.util.stream.Collectors;

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

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

public Frame(int frameNumber, int firstPitch) {
public Frame(int frameNumber, int firstPin) {
this.frameNumber = frameNumber;
this.pitches = new Pitches(firstPitch);
this.pins = new Pins(firstPin);
}

public Frame getNextFrame() {
Expand All @@ -24,12 +30,16 @@ public boolean hasNextFrame() {
return nextFrame != null;
}

public Pins getPins() {
return pins;
}

public int getFrameNumber() {
return frameNumber;
}

public Pitches getPitches() {
return pitches;
public Status getStatus() {
return pins.getLastStatus();
}

public abstract boolean isComplete();
Expand All @@ -38,31 +48,33 @@ public boolean isFinalFrame() {
return frameNumber == MAX_FRAME_NUMBER;
}

public Frame bowl(int pinCount) {
public Frame bowl(int pin) {
if (isComplete()) {
return createNextFrame(pinCount);
return createNextFrame(pin);
}

pitches.add(pinCount);
pins.add(pin);
return this;
}

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

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

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

public String getDisplayStatus() {
return pins.stream()
.map(Pin::getStatus)
.map(Status::display)
.collect(Collectors.joining("|"));
}

public abstract boolean getScoreFlag();

public abstract int getScore();
public abstract Score getScore();
protected abstract Score getScoreWith(Score score);
}
Loading