Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a681877
기능 정리
pizzaa0930 Sep 17, 2025
ed754d4
도메인 파일 car클래스 정의
pizzaa0930 Sep 22, 2025
980d6fd
메시지 구현
pizzaa0930 Sep 22, 2025
f57bc2e
메시지 구현
pizzaa0930 Sep 22, 2025
9e22f97
경주하는 차의 정보 배열 정리
pizzaa0930 Sep 22, 2025
1ef6c52
게임 내 신호
pizzaa0930 Sep 22, 2025
698aa72
우승자 출력
pizzaa0930 Sep 22, 2025
3e81eff
게임의 전체 기능 조정
pizzaa0930 Sep 22, 2025
0380abd
차 이름 지정 시 예외사항
pizzaa0930 Sep 22, 2025
0f1759c
게임 실행
pizzaa0930 Sep 22, 2025
896b7ff
기능 테스트
pizzaa0930 Sep 22, 2025
3bceec9
기능 테스트
pizzaa0930 Sep 22, 2025
62b44e9
Docs: 기능을 조금 더 보기 편하게 수정했습니다
pizzaa0930 Sep 29, 2025
549f6cd
Refactor: 싱글톤 패턴 방식에서 퍼블릭으로 전환하여 일반 클래스를 사용했습니다
pizzaa0930 Sep 29, 2025
5d703b1
Refactor: 싱글톤 패턴 방식을 삭제하였고 그 과정에서 close 메서드가 비게되었습니다
pizzaa0930 Sep 29, 2025
1fb5f2a
Refactor: 싱글톤 패턴 방식을 삭제하였습니다
pizzaa0930 Sep 29, 2025
ee828d3
Refactor: 싱글톤 패턴 방식을 삭제하고 new로 새로 정의하였습니다
pizzaa0930 Sep 29, 2025
d5bf22f
Refactor: 싱글톤 패턴 방식을 삭제하였습니다
pizzaa0930 Sep 29, 2025
d9f3f71
Refactor: 싱글톤 패턴 방식을 일반 클래스 new로 정의하여 메인으로 동작을 수행하게 했습니다
pizzaa0930 Sep 29, 2025
25e0afe
Refactor: new정의가 하나 빠져있어서 수정했습니다
pizzaa0930 Sep 29, 2025
c4b21b3
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 Sep 29, 2025
6daf792
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 Sep 29, 2025
f560ec2
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 Sep 29, 2025
26248a9
Refactor: 싱글톤 패턴을 수정했습니다
pizzaa0930 Sep 29, 2025
3aa2d9c
Refactor: 싱글톤 패턴을 수정하였습니다
pizzaa0930 Sep 29, 2025
154044c
Refactor: 싱글톤 패턴을 수정하였습니다 이미 커밋을 푸시해버려서 부득이하게 수정이라는 주석을 넣어 다시 푸시했습니다
pizzaa0930 Sep 29, 2025
acbd24c
Refactor: 싱글톤 패턴을 수정하였습니다
pizzaa0930 Sep 29, 2025
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
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
자동차 이름 입력기능
쉼표를 기준으로 구분
5자 이하만 가능
이름이 조건에 맞지 않을 시 에러 메시지
이동할 횟수 입력 기능
랜덤을 사용한 자동차 전진 기능
전진과 자동차 이름 동시 출력 기능
완료 후 우승자 표시 기능
우승자가 여러 명일 경우 쉼표 구분
잘못된 값을 입력할 시 종료하는 기능
47 changes: 47 additions & 0 deletions src/main/java/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package controller;

import service.CarService;

import view.GameView;

public class GameController {



Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각보다 이런 줄넘김이나 네이밍 규칙같은 코드 컨벤션 잘 지켜주시면 좋습니다.

private final GameView gameView;
private final CarService carService;

public GameController() {
gameView = new GameView();
carService = new CarService();
}

/*public static GameController getInstance() {
if(gameController == null) {
gameController = new GameController();
}
return gameController;
}*/

public void raceSet(String[] names) {
gameView.printResult();
carService.fill(names);
}

public void race(int count) {
String result = carService.getResult(count);
gameView.printResult(result);
}

public void raceResult() {
String winners = carService.getWinners();
gameView.printWinners(winners);
}

public void close() {
carService.close();
gameView.close();

}

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

public class Car implements Comparable<Car> {
private static final int STOP = 3;
private final String name;
private int distance;

public Car(final String name, int distance) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매개변수에 final을 붙여주셨네요. 이유도 궁금합니다.

this.name = name;
this.distance = distance;
}

public Car(final String name) {
this.name = name;
}


public void move(final int value) {
if(value > STOP) {
distance++;
}
}

public int getDistance() { return distance; }
public String getName() { return name; }
Comment on lines +24 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메서드들도 줄넘김 규칙 통일해주시고요


@Override
public int compareTo(Car o) {
return o.distance - this.distance;
}
}
38 changes: 38 additions & 0 deletions src/main/java/exception/GameInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package exception;

import message.GameMessage;

public class GameInputException {



public GameInputException() {
}



public void validateNameLength(String[] names) {
for(String name : names) {
if(name.length() > 5) {
throw new IllegalArgumentException(GameMessage.nameError.getMessage());
}
}
}

public void validateNumber(String number) {
if(!number.matches(GameMessage.REGEX.getMessage())) {
throw new IllegalArgumentException(GameMessage.countError.getMessage());
}
}

public void validateNumberZero(String number) {
if(number.length() > 1 && number.charAt(0) == '0') {
throw new IllegalArgumentException(GameMessage.countError.getMessage());
}
}

public void close() {

}
Comment on lines +34 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 이제 필요없지않을까요?


}
58 changes: 58 additions & 0 deletions src/main/java/game/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package game;

import controller.GameController;

import exception.GameInputException;

import view.InputView;

public class RacingGame {

// private static RacingGame defaultRacingGame;
private final InputView inputView;
private final GameInputException gameInputException;
private final GameController gameController;

public RacingGame() {
inputView = new InputView();
gameInputException = new GameInputException();
gameController = new GameController();
}

/*public static RacingGame getInstance() {
if(defaultRacingGame == null) {
defaultRacingGame = new RacingGame();
}
return defaultRacingGame;
}*/
Comment on lines +21 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석들은 차라리 지워주셔도 좋을 것 같습니다.


public void run() {
String[] names = preHandleNames();
int count = preHandleCount();
gameController.raceSet(names);
gameController.race(count);
gameController.raceResult();
}

private String[] preHandleNames() {
String input = inputView.inputCarNames();
String[] names = input.split(",");
gameInputException.validateNameLength(names);
return names;
}

private int preHandleCount() {
String input = inputView.inputRaceCount();
gameInputException.validateNumber(input);
gameInputException.validateNumberZero(input);
return Integer.parseInt(input);
}

public void close() {
gameController.close();
gameInputException.close();
inputView.close();
//defaultRacingGame = null;
}

}
24 changes: 24 additions & 0 deletions src/main/java/message/GameMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package message;

public enum GameMessage {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 모으면 무슨 장점이 있을까요?


start("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"),
count("시도할 회수는 몇회인가요?"),
result("실행 결과"),
winner("최종 우승자"),
equal(" : "),
bar("-"),
nameError("자동차의 이름은 5자리 이내입니다."),
countError("회수는 음수가 아닌 정수로 입력해주세요."),
REGEX("[0-9]+"),
newLine("\n");

private final String message;

GameMessage(final String message) {
this.message = message;
}

public String getMessage() { return message; }

}
18 changes: 17 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package racingcar;
import camp.nextstep.edu.missionutils.Randoms;
import camp.nextstep.edu.missionutils.Console;
import game.RacingGame;

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


public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
// TODO: 프로그램 구현;
RacingGame game =new RacingGame();

try {
game.run();
}
finally {
game.close();
}
Comment on lines +15 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전역적으로 try-catch하면 어떤 장점과 단점이 있을지 생각해보시면 좋을 것 같습니다.

}
}
//수정
89 changes: 89 additions & 0 deletions src/main/java/service/CarService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package service;

import camp.nextstep.edu.missionutils.Randoms;

import domain.Car;

import message.GameMessage;

import java.util.ArrayList;
import java.util.Collections;

public class CarService {

private final ArrayList<Car> carList;
private final StringBuilder sb;


public CarService() {
carList = new ArrayList<>();
sb = new StringBuilder();
}



public void close() {

}

public void fill(String[] carNameArr,int[] distance) {
int i = 0;
for(String name : carNameArr) {
carList.add(new Car(name,distance[i++]));
}
}

public void fill(String[] carNameArr) {
for(String name : carNameArr) {
carList.add(new Car(name));
}
}

public String getResult(int count) {
while(count-- > 0) {
race();
if(count != 0) {
sb.append(GameMessage.newLine.getMessage());
}
}
return sb.toString();
}

private void race() {
for(Car car : carList) {
int randomNumber = Randoms.pickNumberInRange(0,9);
car.move(randomNumber);
raceRecord(car);
}
}

private void raceRecord(Car car) {
sb.append(car.getName()).append(GameMessage.equal.getMessage());
buildBar(car.getDistance());
}

private void buildBar(int distance) {
while(distance-- > 0) {
sb.append(GameMessage.bar.getMessage());
}
sb.append(GameMessage.newLine.getMessage());
}

public String getWinners() {
Collections.sort(carList);
int winDistance = carList.get(0).getDistance();
ArrayList<String> winnerList = makeWinnerList(winDistance);
return String.join(", ",winnerList);
}

private ArrayList<String> makeWinnerList(int winDistance) {
ArrayList<String> winnerList = new ArrayList<>();
for(Car car : carList) {
int distance = car.getDistance();
if(winDistance > distance) { break; }
winnerList.add(car.getName());
}
return winnerList;
}

}
30 changes: 30 additions & 0 deletions src/main/java/view/GameView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package view;

import message.GameMessage;

public class GameView {

//private static GameView defaultGameView;

public GameView() {
}



public void printResult() {
System.out.println(GameMessage.newLine.getMessage() + GameMessage.result.getMessage());
}

public void printResult(String result) {
System.out.println(result);
}

public void printWinners(String winners) {
System.out.println(GameMessage.winner.getMessage() + GameMessage.equal.getMessage() + winners);
}

public void close() {

}

}
31 changes: 31 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package view;

import camp.nextstep.edu.missionutils.Console;

import message.GameMessage;

public class InputView {



public InputView() {
}



public String inputCarNames() {
System.out.println(GameMessage.start.getMessage());
return Console.readLine();
}

public String inputRaceCount() {
System.out.println(GameMessage.count.getMessage());
return Console.readLine();
}

public void close() {
Console.close();

}

}
Loading