Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
16 changes: 16 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 기능 명세서

##

경주할 자동차 이름을 쉼표로 구분해 입력받음
5자 이하가 아닐 시 에러발생

사용자가 몇 번의 이동을 할 것인지 입력받음
숫자가 아닌걸 입력하면 에러 발생

입력받은 이동 횟수만큼 반복
각 자동차마다 0부터 9까지 무작위 숫자를 random()으로 뽑아서 5가 넘을경우 한칸 앞으로
현재 각각 자동차가 얼마나 앞으로 갔는지 출력

최종 우승자가 여러명이면 쉼표로 구분해 출력 아니면 그냥 출력

10 changes: 10 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@
public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
View view = new View();
Model model = new Model();

model.saveCarName(view.inputCarName());

model.saveNumberOfAttempts(view.inputNumberOfAttempts());

view.printResult(model);

view.printWinner(model);
}
}
64 changes: 64 additions & 0 deletions src/main/java/racingcar/Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.Random;
import java.util.StringTokenizer;

public class Model {
String[] car;
int[] len;
int numberOfAttempts;
int max = 0;

void saveCarName(String input){

StringTokenizer st = new StringTokenizer(input, ",");
car = new String[st.countTokens()];
len = new int[st.countTokens()];
for(int i=0; i<car.length; i++){
String str = st.nextToken();
if(str.length()>5) throw new IllegalArgumentException("5글자 이하로 작성");
Copy link
Contributor

Choose a reason for hiding this comment

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

사소한 거긴 한데 if 문은 한 줄이라도 괄호를 쳐주는 게 좋긴 합니다!

car[i] = str;
}

}

void saveNumberOfAttempts(String input){

try {
numberOfAttempts = Integer.parseInt(input);
} catch (Exception e){
System.out.println("숫자를 입력하세요");
}
Comment on lines +31 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

여기서는 아스키 코드를 이용해서 숫자 범위 안에 드는지 검사할 수 있을 듯! 근데 간단하게 짜려면 이렇게 짜도 되긴 합니당


}

StringBuilder race(){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < car.length; i++) {
int rand = Randoms.pickNumberInRange(0, 9);
if(rand>=4) len[i]++;
sb.append(car[i]).append(" : ")
.append("-".repeat(Math.max(0, len[i])))
.append("\n");
max = Math.max(len[i], max);
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분이 대충 경주 결과를 문자열로 나타내는 로직인 건 알겠는데, 정확히 어떤 과정을 통해 결과가 도출되는지 명확하지 않은 듯!

아마 최댓값을 넣는 등의 부분이 메서드 분리가 잘 안된 것도 있고 변수명이 직관적이지 않은 이유도 있는 것 같습니닷

ex. len이 length를 줄인 건 알겠는데 정확히 어떤 길이를 나타내는지? 명확하지 않음

}

return sb;
}

StringBuilder findWinner(){
StringBuilder sb = new StringBuilder();
boolean jointWinner = false;
for(int i=0; i<car.length; i++){
if(len[i]==max) {
if(jointWinner) sb.append(", ");
jointWinner = true;
sb.append(car[i]);
}
}
return sb;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

이 분은 뭔가 알고리즘 느낌으로 최적화된 코드인 것 같은데, 아마도 유지보수의 측면에서는 우승자를 고르고 바로 StringBuilder에 넣기보다는

전체 우승자 목록을 먼저 반환한다음, 최종적으로 StringBuilder에 넣는 게 좋을 듯합니다...!


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

import camp.nextstep.edu.missionutils.Console;

public class View {

String inputCarName(){
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,)기준으로 구분)");
return Console.readLine();
}

String inputNumberOfAttempts(){
System.out.println("시도할 회수는 몇회인가요?");
return Console.readLine();
}

void printResult(Model model){
System.out.println("\n실행 결과");
while (model.numberOfAttempts --> 0) {
System.out.println(model.race());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

MVC에서는 Controller 에서만 Model과 View가 만나고 View 코드 안에서는 Model과 관련된 부분이 웬만하면 없는 게 맞아서 사소하지만 model 객체가 아닌 model.race()를 직접 넣는 게 더 좋을 듯 합니다!


void printWinner(Model model){
System.out.print("최종 우승자 : ");
System.out.println(model.findWinner());
}

}