Skip to content

Commit 7412b7c

Browse files
Integerousjavajigi
authored andcommitted
Step4 - 볼링 점수판(n명) 리뷰 요청드립니다..! (#46)
* [step4] feat : 게임 참여자 수 입력받기 * [step4] feat : 게임 참여자 리스트 생성 * [step4] feat : BowlingMatch 클래스 생성 * [step4] feat : 현재 진행중인 볼링게임 반환 메서드 구현 * [step4] feat : Application에서 BowlingGame대신 BowlingMatch 실행하도록 수정 * [step4] feat : 플레이어들의 이름 입력받고 빈 점수판 출력 * [step4] feat : 플레이 할 플레이어 이름으로 쓰러진 핀 개수 묻도록 수정 * [step4] refactor : 볼링게임 리스트 반환시 불변리스트 반환, 마지막 프레임 점수표시 수정 * [step4] refactor : 설명 변수 추가
1 parent f6e5940 commit 7412b7c

File tree

7 files changed

+172
-16
lines changed

7 files changed

+172
-16
lines changed

src/main/java/Application.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import domain.BowlingGame;
2+
import domain.BowlingMatch;
23
import domain.Pins;
34
import domain.Player;
45
import domain.frame.FrameIndex;
56
import view.InputView;
67
import view.OutputView;
78

9+
import java.util.ArrayList;
10+
import java.util.List;
11+
812
public class Application {
913

1014
public static void main(String[] args) {
11-
Player player = Player.from(InputView.askPlayerName());
12-
BowlingGame bowlingGame = BowlingGame.from(player);
13-
OutputView.printBoard(bowlingGame);
15+
int numberOfPlayers = InputView.askNumberOfPlayers();
16+
List<Player> players = new ArrayList<>();
17+
18+
for (int index = 1; index <= numberOfPlayers; index++) {
19+
String playerName = InputView.askPlayerName(index);
20+
players.add(Player.from(playerName));
21+
}
22+
23+
BowlingMatch bowlingMatch = BowlingMatch.start(players);
24+
OutputView.printBoard(bowlingMatch);
1425

15-
while(!bowlingGame.isGameOver()) {
16-
FrameIndex currentFrameIndex = bowlingGame.currentFrameIndex();
17-
Pins fallenPins = InputView.askFallenPins(currentFrameIndex);
26+
while(!bowlingMatch.isOver()) {
27+
BowlingGame ongoingGame = bowlingMatch.getOngoingGame();
28+
Pins fallenPins = InputView.askFallenPins(ongoingGame);
1829

19-
bowlingGame.play(fallenPins);
20-
OutputView.printBoard(bowlingGame);
30+
ongoingGame.play(fallenPins);
31+
OutputView.printBoard(bowlingMatch);
2132
}
2233
}
2334
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package domain;
2+
3+
import domain.frame.FrameIndex;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class BowlingMatch {
10+
11+
private List<BowlingGame> bowlingMatch;
12+
13+
private BowlingMatch(List<BowlingGame> bowlingMatch) {
14+
this.bowlingMatch = bowlingMatch;
15+
}
16+
17+
public static BowlingMatch start(List<Player> players) {
18+
return new BowlingMatch(setUpMatch(players));
19+
}
20+
21+
private static List<BowlingGame> setUpMatch(List<Player> players) {
22+
return players.stream()
23+
.map(BowlingGame::from)
24+
.collect(Collectors.toList());
25+
}
26+
27+
public BowlingGame getOngoingGame() {
28+
int index = getOngoingGameIndex();
29+
return bowlingMatch.stream()
30+
.filter(game -> !game.isGameOver())
31+
.filter(game -> game.currentFrameIndex().isOngoingGameIndex(index))
32+
.findFirst()
33+
.orElseThrow(RuntimeException::new);
34+
}
35+
36+
private int getOngoingGameIndex() {
37+
return bowlingMatch.stream()
38+
.map(BowlingGame::currentFrameIndex)
39+
.mapToInt(FrameIndex::getFrameIndex)
40+
.min()
41+
.orElseThrow(RuntimeException::new);
42+
}
43+
44+
public boolean isOver() {
45+
return bowlingMatch.stream()
46+
.allMatch(BowlingGame::isGameOver);
47+
}
48+
49+
public List<BowlingGame> getBowlingMatch() {
50+
return Collections.unmodifiableList(bowlingMatch);
51+
}
52+
}

src/main/java/domain/frame/FrameIndex.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ private void validationFrameNumber(int frameNumber) {
3838
}
3939
}
4040

41+
public boolean isOngoingGameIndex(int index) {
42+
return frameIndex == index;
43+
}
44+
4145
public int getFrameIndex() {
4246
return frameIndex;
4347
}

src/main/java/domain/state/closed/FinalState.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import domain.Score;
44
import domain.state.State;
5+
import domain.state.open.StandBy;
56

67
import java.util.List;
78
import java.util.stream.Collectors;
@@ -42,6 +43,9 @@ public boolean isClosed() {
4243
if (states.get(FIRST_STATE) instanceof Strike) {
4344
return Boolean.FALSE;
4445
}
46+
if (states.get(FIRST_STATE) instanceof StandBy) {
47+
return Boolean.FALSE;
48+
}
4549
return bowlOrder < DEFAULT_CHANCES;
4650
}
4751

src/main/java/view/InputView.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package view;
22

3+
import domain.BowlingGame;
34
import domain.Pins;
45
import domain.frame.FrameIndex;
56

67
import java.util.Scanner;
78

89
public class InputView {
910

10-
private static final String MESSAGE_FOR_ASK_PLAYER_NAME = "플레이어 이름은(3 english letters)?: ";
11+
private static final String MESSAGE_FOR_ASK_NUMBER_OF_PLAYERS = "How many people? ";
1112
private static Scanner scanner = new Scanner(System.in);
1213

13-
public static String askPlayerName() {
14-
System.out.print(MESSAGE_FOR_ASK_PLAYER_NAME);
14+
public static int askNumberOfPlayers() {
15+
System.out.print(MESSAGE_FOR_ASK_NUMBER_OF_PLAYERS);
16+
int numberOfPlayers = scanner.nextInt();
17+
scanner.nextLine();
18+
return numberOfPlayers;
19+
}
20+
21+
public static String askPlayerName(int index) {
22+
System.out.print(String.format("플레이어 %d의 이름은?(3 english letters): ", index));
1523
return scanner.nextLine();
1624
}
1725

18-
public static Pins askFallenPins(FrameIndex index) {
19-
System.out.print(String.format("%s프레임 투구 : ", index.getFrameIndex()));
26+
public static Pins askFallenPins(BowlingGame ongoingGame) {
27+
System.out.print(String.format("%s's turn : ", ongoingGame.getPlayerName()));
2028
int fallenPins = scanner.nextInt();
2129
scanner.nextLine();
2230
return Pins.from(fallenPins);

src/main/java/view/OutputView.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package view;
22

33
import domain.BowlingGame;
4+
import domain.BowlingMatch;
45
import utils.PrintUtils;
56

67
import java.util.List;
@@ -13,10 +14,13 @@ public class OutputView {
1314
public static final String SEPARATOR = "|";
1415
public static final String EMPTY_SYMBOL = " ";
1516

16-
public static void printBoard(BowlingGame bowlingGame) {
17+
public static void printBoard(BowlingMatch bowlingMatch) {
1718
System.out.println(UPPER_SIDE_OF_SCORE_BOARD);
18-
System.out.println(printStates(bowlingGame));
19-
System.out.println(printScores(bowlingGame));
19+
20+
for (BowlingGame bowlingGame : bowlingMatch.getBowlingMatch()) {
21+
System.out.println(printStates(bowlingGame));
22+
System.out.println(printScores(bowlingGame));
23+
}
2024
printEmptyLine();
2125
}
2226

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package domain;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.assertAll;
12+
13+
public class BowlingMatchTest {
14+
15+
private static final int INDEX_OF_FIRST_PLAYER = 0;
16+
private static final int INDEX_OF_SECOND_PLAYER = 1;
17+
private List<Player> players;
18+
private BowlingMatch bowlingMatch;
19+
20+
@BeforeEach
21+
void setUp() {
22+
Player player1 = Player.from("hjs");
23+
Player player2 = Player.from("pje");
24+
Player player3 = Player.from("jek");
25+
players = new ArrayList<>(Arrays.asList(player1, player2, player3));
26+
bowlingMatch = BowlingMatch.start(players);
27+
}
28+
29+
@Test
30+
void 플레이어들을_입력하여_볼링경기를_생성한다() {
31+
assertThat(bowlingMatch).isNotNull();
32+
}
33+
34+
@Test
35+
void 현재_플레이중인_플레이어를_반환한다() {
36+
//when
37+
BowlingGame bowlingGame = bowlingMatch.getOngoingGame();
38+
39+
//then
40+
assertThat(bowlingGame.getPlayer())
41+
.isEqualTo(players.get(INDEX_OF_FIRST_PLAYER));
42+
}
43+
44+
@Test
45+
void 현재_플레이중인_프레임이_종료되면_다음_플레이어가_플레이한다() {
46+
//when
47+
BowlingGame bowlingGame = bowlingMatch.getOngoingGame();
48+
bowlingGame.play(Pins.STRIKE);
49+
50+
BowlingGame ongoingGame = bowlingMatch.getOngoingGame();
51+
52+
//then
53+
assertAll(
54+
() -> assertThat(bowlingGame.getFirstFrame().getState().isClosed()).isTrue(),
55+
() -> assertThat(ongoingGame.getPlayer()).isEqualTo(players.get(INDEX_OF_SECOND_PLAYER))
56+
);
57+
}
58+
59+
@Test
60+
void 모든_볼링게임이_종료되면_볼링경기가_종료된다() {
61+
//given
62+
int numberOfPlaysForPerfectGame = players.size() * 12;
63+
64+
//when
65+
for (int i = 0; i < numberOfPlaysForPerfectGame; i++) {
66+
BowlingGame ongoingGame = bowlingMatch.getOngoingGame();
67+
ongoingGame.play(Pins.STRIKE);
68+
}
69+
70+
//then
71+
assertThat(bowlingMatch.isOver()).isTrue();
72+
}
73+
}

0 commit comments

Comments
 (0)