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
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
자동차게임 기능 구현 목록\
1.사용자 이름 입력받기(,로 나눠서 입력)\
\# 5자 이상일 경우 에러발생\
\# 빈칸일 경우 에러발생\
2.게임 횟수 입력받기\
3.0~9 사이의 값을 난수로 발생시켜 4이상일 경우 움직이게 한다.\
4.정해진 횟수만큼 게임 실행 후 최종 가장 많이 움직인 플레이어를 우승자로 한다.\
\# 공동우승 가능(,로 구분하여 출력)
74 changes: 73 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
package racingcar;

import camp.nextstep.edu.missionutils.*;
import java.util.Arrays;
public class Application {

public void forward(String[] cars){
for (int i =0;i < cars.length; i++){

}
Comment on lines +7 to +10
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 static void main(String[] args) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

main()에서 입력, 검증, 게임 진행, 우승자 출력까지 모두 담당하고 있어 역할별 메서드로 나누면 가독성이 더 좋아질 것 같습니다.

// TODO: 프로그램 구현

//플레이어 자동차 이름 입력받기
System.out.print("경주할 자동차 이름을 입력하세요 (,로 구분하고 5자 이하) : ");
String carsinput = Console.readLine();
String[] cars = carsinput.split(",");

//이름 예외처리

for(int i =0;i < cars.length;i++){ //5자 이하로
if (cars[i].length() > 5){
throw new IllegalArgumentException();
}

if(cars[i].isBlank()){ //빈칸
throw new IllegalArgumentException();
}
}
//게임 횟수 입력
System.out.print("게임 횟수를 입력하세요 : ");
int game = Integer.parseInt(Console.readLine());
//횟수 예외처리
if (game <1){
throw new IllegalArgumentException();
}

//경주 게임 출력

String[] race = new String[cars.length];
Arrays.fill(race,"");
int[] count = new int[cars.length];
Comment on lines +41 to +43
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<game;i++){
for (int j = 0; j < cars.length; j++) {
int num = Randoms.pickNumberInRange(0, 9);
System.out.print(cars[j] + " : ");
if (num >= 4) {race[j] += "-";System.out.println(race[j]);count[j]++;}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

한 줄에 여러 동작이 함께 들어가 있어, 줄을 나누면 읽기 훨씬 편해질 것 같습니다.

else System.out.println(race[j]);

}
System.out.println();
}

//우승자 출력
System.out.print("최종 우승자 : ");
int s = cars.length;
for (int i = 0 ; i < s ; i++){

for (int j = 0; j < s; j++){
if (j == i)continue;
if ( count[j] > count[i]){
System.out.print(cars[j]);
break;
}
else if(count[j] == count[i]){
System.out.print(cars[j]+","+cars[i]);
break;
}


}
}




Comment on lines +70 to +77
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

불필요한 공백들은 제거해주세요:)

}
}