Skip to content

Commit 0a4f1d6

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 3a8cf14 commit 0a4f1d6

File tree

7 files changed

+201
-0
lines changed

7 files changed

+201
-0
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencies {
1414
testImplementation platform('org.assertj:assertj-bom:3.25.1')
1515
testImplementation('org.junit.jupiter:junit-jupiter')
1616
testImplementation('org.assertj:assertj-core')
17+
testImplementation('org.mockito:mockito-core:5.7.0')
18+
testImplementation 'org.mockito:mockito-junit-jupiter:5.7.0'
1719
}
1820

1921
test {

src/main/java/GameController.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class GameController {
2+
private final RacingGame game;
3+
private final InputView inputView;
4+
private final OutputView outputView;
5+
6+
public GameController(RacingGame game, InputView inputView, OutputView outputView){
7+
this.game = game;
8+
this.inputView = inputView;
9+
this.outputView = outputView;
10+
}
11+
12+
public void startGame() {
13+
String[] carNames = inputView.inputCarName().split(",");
14+
int runCount = inputView.inputRunCount();
15+
16+
game.initializeGame(carNames, runCount);
17+
outputView.printGameStart();
18+
19+
for (int i = 0; i < game.getRunCount(); ++i) {
20+
game.playRound();
21+
outputView.printRound(game.getRacingCars());
22+
}
23+
24+
outputView.printWinner(game.findWinners());
25+
}
26+
}

src/main/java/InputView.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Scanner;
2+
3+
public class InputView {
4+
static Scanner in = new Scanner(System.in);
5+
6+
public String inputCarName(){
7+
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
8+
String carName = in.nextLine();
9+
if(carName == null || carName.isEmpty()) throw new RuntimeException("자동차 이름을 제대로 입력해주세요.");
10+
return carName;
11+
}
12+
13+
public int inputRunCount(){
14+
System.out.println("시도할 횟수는 몇 회인가요?");
15+
int runCount;
16+
try{
17+
runCount = in.nextInt();
18+
runCountMinusCheck(runCount);
19+
}
20+
catch (NumberFormatException e) {
21+
throw new RuntimeException("정수만 가능합니다.");
22+
}
23+
return runCount;
24+
}
25+
26+
private void runCountMinusCheck(int runCount){
27+
if(runCount <= 0) throw new RuntimeException("실행횟수가 0이하 입니다.");
28+
}
29+
}

src/main/java/OutputView.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.List;
2+
3+
public class OutputView {
4+
5+
public void printGameStart() {
6+
System.out.println("\n실행 결과");
7+
}
8+
9+
public void printRound(List<RacingCar> racingCars) {
10+
for (RacingCar car : racingCars) {
11+
System.out.println(car.getCarName() + " : " + "-".repeat(car.getForwardCount()));
12+
}
13+
System.out.println();
14+
}
15+
16+
public void printWinner(List<String> winnerCars){
17+
System.out.printf("최종 우승자 : %s%n", String.join(", ", winnerCars));
18+
}
19+
}

src/main/java/RacingCar.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class RacingCar {
2+
private final String carName;
3+
private int forwardCount = 0;
4+
RacingCar(String carName){
5+
if(carName == null || carName.isEmpty()) throw new RuntimeException("제대로된 자동차 이름을 입력해주세요.");
6+
if(carName.length() > 5) throw new RuntimeException("자동차 이름은 5자 이하로 설정해주세요.");
7+
this.carName = carName;
8+
}
9+
10+
public String getCarName(){
11+
return carName;
12+
}
13+
14+
public void move(int randomNumber){
15+
if(randomNumber >= 4) ++forwardCount;
16+
}
17+
18+
public int getForwardCount(){
19+
return forwardCount;
20+
}
21+
}

src/main/java/RacingGame.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Random;
4+
5+
public class RacingGame {
6+
private final List<RacingCar> racingCars = new ArrayList<>();
7+
private final Random random;
8+
private int runCount;
9+
10+
public RacingGame(Random random){
11+
this.random = random;
12+
}
13+
14+
public void initializeGame(String[] carNames, int runCount) {
15+
this.runCount = runCount;
16+
for (String name : carNames) {
17+
racingCars.add(new RacingCar(name));
18+
}
19+
}
20+
21+
public void playRound() {
22+
for (RacingCar car : racingCars) {
23+
car.move(random.nextInt(10));
24+
}
25+
}
26+
27+
public int getRunCount() {
28+
return runCount;
29+
}
30+
31+
public List<RacingCar> getRacingCars() {
32+
return racingCars;
33+
}
34+
35+
public List<String> findWinners() {
36+
int maxPosition = racingCars.stream()
37+
.mapToInt(RacingCar::getForwardCount)
38+
.max()
39+
.orElse(0);
40+
List<String> winners = new ArrayList<>();
41+
for (RacingCar car : racingCars) {
42+
if (checkWinner(car, maxPosition)) {
43+
winners.add(car.getCarName());
44+
}
45+
}
46+
return winners;
47+
}
48+
49+
private boolean checkWinner(RacingCar car, int maxPosition){
50+
return car.getForwardCount() == maxPosition;
51+
}
52+
}

src/test/java/GameTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import org.junit.jupiter.api.Test;
2+
import org.junit.jupiter.api.extension.ExtendWith;
3+
import org.mockito.junit.jupiter.MockitoExtension;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
import static org.mockito.Mockito.*;
7+
import java.util.*;
8+
9+
@ExtendWith(MockitoExtension.class)
10+
class GameTest {
11+
@Test
12+
void 자동차_전진_테스트() {
13+
RacingGame game = new RacingGame(new Random());
14+
game.initializeGame(new String[]{"neo", "brie"}, 5);
15+
16+
RacingCar neo = game.getRacingCars().get(0);
17+
neo.move(4);
18+
assertEquals(1, neo.getForwardCount());
19+
20+
neo.move(3);
21+
assertEquals(1, neo.getForwardCount());
22+
}
23+
24+
@Test
25+
void 우승자_선택_테스트() {
26+
RacingGame game = new RacingGame(new Random());
27+
game.initializeGame(new String[]{"neo", "brie"}, 5);
28+
29+
game.getRacingCars().get(0).move(5);
30+
game.getRacingCars().get(1).move(3);
31+
32+
List<String> winners = game.findWinners();
33+
assertEquals(Collections.singletonList("neo"), winners);
34+
}
35+
36+
@Test
37+
void 랜덤_전진_테스트() {
38+
Random mockRandom = mock(Random.class);
39+
when(mockRandom.nextInt(10)).thenReturn(4, 2, 7, 8, 3);
40+
41+
RacingGame game = new RacingGame(mockRandom);
42+
game.initializeGame(new String[]{"pobi", "crong"}, 5);
43+
44+
game.playRound(); // 1라운드 실행
45+
assertEquals(1, game.getRacingCars().get(0).getForwardCount());
46+
assertEquals(0, game.getRacingCars().get(1).getForwardCount());
47+
48+
game.playRound();
49+
assertEquals(2, game.getRacingCars().get(0).getForwardCount());
50+
assertEquals(1, game.getRacingCars().get(1).getForwardCount());
51+
}
52+
}

0 commit comments

Comments
 (0)