Skip to content
Open
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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# java-racingcar-precourse

## 과제
### 🚀 기능 요구 사항

초간단 자동차 경주 게임을 구현한다.

- 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다.
- 각 자동차에 이름을 부여할 수 있다. 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다.
- 자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다.
- 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다.
- 전진하는 조건은 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상일 경우이다.
- 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다.
- 우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다.
- 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.

## 입출력
### 입력
- 자동차 이름 쉼표 기준으로 입력받기
- 시도할 횟수

### 출력
- 차수별 실행결과
- 단독, 공동 우승자 안내 문구

## Business Logic
- 자동차 이름과 시도할 횟수 입력받기
- 전진하는 로직짜기
- 우승자 도출

## 구현할 기능
- 입출력
- 전진 조건 구현
- 우승자 계산 기능 구현
- 예외케이스 구현
2 changes: 1 addition & 1 deletion gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package racingcar;


import racingcar.controller.*;


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


RacingCarController start = new RacingCarController();
start.RacingGame();

WhoIsWinner w = new WhoIsWinner();
w.JudgeWinner(start);
}
}
52 changes: 52 additions & 0 deletions src/main/java/racingcar/controller/RacingCarController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package racingcar.controller;

import camp.nextstep.edu.missionutils.Randoms;

import racingcar.view.*;
import racingcar.model.Car;
import java.util.ArrayList;

public class RacingCarController{
public int numbersOfCar = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

변수를 private 접근지정자로 하면 더 좋을 것 같아요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다

public Car[] car = new Car[numbersOfCar];

public void setCar(ArrayList<String> carName){
numbersOfCar = carName.size();
for (int i = 0; i < numbersOfCar; i++){
car[i].setName(carName.get(i));
car[i].Distance = 0;
}
}

public int getNumbersOfCar(){
return numbersOfCar;
}

public void RacingGame() {
output.CarNameRequestMessage();
input userInput = new input();
userInput.setCarName();

output.trialRequestMessage();
userInput.setTrial();

setCar(userInput.getCarName());

for (int k = 0; k < userInput.getTrial(); k++){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

여기서 이중 for문으로 인해 가독성이 살짝 떨어지는 것 같은데 다른 API를 쓰지않고 이중 for문을 활용하신 이유가 있을까용?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다. for문을 한번만 써서 시간 복잡도를 줄일 수 있을 것 같습니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

API 공부가 조금 부족했던 것 같네요..ㅜㅜ 혹시 여기서 추천해주실만한 API가 있을까요? @seulnan @BYEONGHWALEE-dev

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

음 어떻게 분리하냐에 따라 달라질 것 같긴하데, 다른 분들 코드처럼 forEach()나 Stream으로 충분히 가능해보여요!

for (int i = 0; i < numbersOfCar; i++) {
int randomDistance = Randoms.pickNumberInRange(0, 9);
car[i].Distance += randomDistance;
}

System.out.print(car[k].getName() + " : ");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

추가적으로 바깥 for루프에서는 k를 활용해서 시도횟수를 반복하는것 같고,
안쪽 for루프에서는 i라는 인덱스를 활용하여서 각 자동차를 이동하는 것 같은데
자동차를 출력할때는 그러면 car[k]가 아닌 안쪽 for루프에서 car[i]를 써야하는거아닌가요...?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

와 여기 코드 단단히 실수했군요 감사합니다 @seulnan

for (int i = 0; i < car[i].Distance; i++){
if (i != car[i].Distance){
System.out.print("-");
}
else {
System.out.println("-");
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"-" 출력을 하나로 합치고 마지막에 '\n'을 출력하는 방법은 어떨까요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다! indent depth를 2까지 허용한다고 했는데 지금은 3인 것 같아요!

}
}
Comment on lines +25 to +51
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

여기에 게임진행하는 로직과 출력로직이 같이 섞여있는 것 같아용 util이나 service을 활용해서 분리하는 것은 어떨까용?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

오 그렇게 하는게 가독성이 더 좋겠네요 감사합니당

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다

}
36 changes: 36 additions & 0 deletions src/main/java/racingcar/controller/WhoIsWinner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racingcar.controller;

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

public class WhoIsWinner{
public List<String> winner= new ArrayList<>();

public void JudgeWinner(RacingCarController cars){
int maxDistance = 0;

for (int i = 0; i < cars.getNumbersOfCar(); i++){
if (cars.car[i].Distance > maxDistance){
maxDistance = cars.car[i].Distance;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stream API를 통해 코드를 더 간결하게 짤 수 있을 것 같아요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다

for (int i = 0; i < cars.getNumbersOfCar(); i++){
if (cars.car[i].Distance == maxDistance){
winner.add(cars.car[i].getName());
}
}

System.out.print("최종 우승자 : ");
for (int i = 0; i < winner.size(); i++){
if (i != winner.size()-1) {
System.out.print(winner.getFirst() + ",");
winner.removeFirst();
}
else {
System.out.print(winner.getFirst());
winner.removeFirst();
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이부분도 겹치는 출력 부분을 하나로 합치는게 나을 것 같아요!

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

import camp.nextstep.edu.missionutils.Randoms;

public class Car{
public int Distance;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

distance필드를 public으로 선언한 이유가 있을까요?? 캡슐화에 신경쓰셨다고해서용! @Siul49

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

field 변수명의 첫 글자는 소문자가 맞는 것 같습니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

헛 private선언을 못한 부분이 있군요..!! 제 실수입니다.. :) @seulnan

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

field 변수명의 첫 글자가 소문자여야하는 이유가 따로 있는건가요? @BYEONGHWALEE-dev

private String Name;

public Car(String carName){
Name = carName;
Distance = 0;
}



public String getName(){
return Name;
}
public void setName(String name){Name = name;}
}
40 changes: 40 additions & 0 deletions src/main/java/racingcar/view/input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package racingcar.view;

import java.util.StringTokenizer;
import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;

public class input{
private int trial;
public ArrayList<String> Cars = new ArrayList<>();

public input(){
trial = 0;
}
public ArrayList<String> getCarName(){
return Cars;
}

public void setCarName() {
String CarNames = Console.readLine();
StringTokenizer separatedCarNames = new StringTokenizer(CarNames, ",");

while(separatedCarNames.hasMoreTokens()){
if (separatedCarNames.nextToken().trim().length() < 5) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

중복코드가 사알짝 보이네용 하필 nextToken이라 두번 호출하면 건너뛰어질 가능성도 있어보여용
nextToken을 한번만 호출한 값을 변수에 저장하고 사용하는 방식으로 수정이 필요해보입니다!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

와 맞네요 감사합니당

String oneOfCarName = separatedCarNames.nextToken().trim();
Cars.add(oneOfCarName);
}
else {
throw new IllegalArgumentException("다섯 글자가 넘어요");
}
}
}

public void setTrial(){
trial = Integer.parseInt(Console.readLine());
}

public int getTrial(){
return trial;
}
}
13 changes: 13 additions & 0 deletions src/main/java/racingcar/view/output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package racingcar.view;

public class output{

public static void CarNameRequestMessage(){
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
}

public static void trialRequestMessage(){
System.out.println("시도할 회수는 몇회인가요?");
}

}