-
Notifications
You must be signed in to change notification settings - Fork 55
[김도훈_BackEnd] 2주차 과제 제출합니다. #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
d52adc5
2d5e6e0
ae581f9
4ca3b0f
ee8ff0e
cde6b87
d30087b
e657adb
3744597
679a445
e7b1003
4fc7ffa
9f0c122
f9d7fdf
30607b2
cd45b64
7b50757
4da2c20
656f8bc
f9873be
1748a1e
5bd731c
08ebdf4
d4af3bc
5a6e949
0607e36
6087109
f6c0496
8be2536
8e49bce
a695e83
160fcdc
a757a09
36208d2
42b4169
b893357
99a4904
bcb64f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [기능 요구 사항] | ||
| ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ | ||
| ■ 자동차 전진 또는 정지 | ||
| - 전진 조건: 0~9사이 무작위 값이 4 이상인 경우 | ||
|
|
||
| ■ 자동차 이름 부여 | ||
| - 각 자동차 이름은 쉼표(,)를 기준으로 구분 | ||
| - 이름은 5자 이하만 가능, (잘못된 값 입력시 IllegalArgumentException을 발생시킨 후 앱 종료) | ||
| ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ | ||
| ㄴ Car 클래스 | ||
|
|
||
| ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ | ||
| ■ 경주 게임 진행 횟수 입력 힘수 | ||
| - 진행 횟수는 음수일 수 없음 (잘못된 값 입력시 IllegalArgumentException을 발생시킨 후 앱 종료) | ||
|
|
||
| ■ 실시간 경주 게임 진행 상황 출력 함수 | ||
| - 전진하는 자동차 출력시 이름도 출력 | ||
|
|
||
| ■ 경주 게임 종료 함수 | ||
| - 우승자 출력 | ||
| - 우승자는 한 명 이상 가능 | ||
| - 우승자 여러 명일 경우 쉼표(,)를 기준으로 구분 | ||
| - 진행 횟수동안 가장 많이 전진한 자동차가 우승 | ||
|
|
||
| ■ 경주 게임 진행 메인 함수 | ||
| - 시도 횟수 입력받기 | ||
| - 시도 횟수 동안 게임 진행 | ||
| - 우승자 출력 | ||
| ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ | ||
| ㄴ Racing 클래스 | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Car car = new Car(); | ||
| Racing Game = new Racing(); | ||
|
|
||
| car.get_Car_Names(); | ||
|
||
| Game.Start(car); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| class Car { | ||
| private static final int MOVING_FORWARD = 4; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 MOVING_FORWARD와 start/end의 변수 명명을 구분하신 이유가 있으신가요?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바 네이밍 규칙 준수하겠습니다. |
||
| private static final int start = 0; | ||
| private static final int end = 9; | ||
| protected String[] Car_Names; | ||
| protected int[] Move_count; | ||
|
|
||
| // 자동차 수 만큼 int 배열 정의 | ||
| protected void get_Players_Move() { Move_count = new int[Car_Names.length]; } | ||
|
||
|
|
||
| // 자동차 전진 및 정지 | ||
| protected void Move() { | ||
| for (int i = 0; i < Car_Names.length; i++) { | ||
| int random = Randoms.pickNumberInRange(start, end); | ||
| if (random >= MOVING_FORWARD) { Move_count[i] += 1; } | ||
| } | ||
| } | ||
|
|
||
| protected void get_Car_Names() { | ||
| // 이름 입력 | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String Car_String = Console.readLine(); | ||
| Car_Names = Car_String.split(","); | ||
| String Car_Name; | ||
|
|
||
| // 이름 예외 처리 | ||
| for (int i = 0; i < Car_Names.length; i++) { | ||
| Car_Name = Car_Names[i]; | ||
| if (Car_Name.length() > 5) { throw new IllegalArgumentException("이름은 5자 이하만 가능합니다."); } | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
|
|
||
| public class Racing extends Car { | ||
|
||
| private int round; | ||
|
|
||
| // 시도 횟수 입력 함수 | ||
| public void get_round() { | ||
| // 시도 횟수 입력 | ||
| System.out.println("시도할 횟수는 몇회인가요?"); | ||
| String input = Console.readLine(); | ||
| round = Integer.parseInt(input); | ||
|
|
||
| // 입력 예외 처리 | ||
| if (round < 0) { throw new IllegalArgumentException("시도 횟수는 음수일 수 없습니다."); } | ||
|
||
| } | ||
|
|
||
| // 게임 진행 상황 출력 함수 | ||
| public void display(Car Players) { | ||
| for (int i = 0; i < Players.Car_Names.length; i++) { | ||
| System.out.printf("%s : %s\n", Players.Car_Names[i], "-".repeat(Players.Move_count[i])); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| // 게임 종료 및 우승자 출력 함수 | ||
| public void End(Car Players) { | ||
| ArrayList<String> Winner = new ArrayList<String>(); | ||
| int max = Players.Move_count[0]; | ||
|
|
||
| // 최대 이동횟수 찾기 | ||
| for (int i = 0; i < Players.Car_Names.length; i++) { | ||
| for (int j = i + 1; j < Players.Car_Names.length; j++) { | ||
| if (max <= Players.Move_count[j]) { | ||
| max = Players.Move_count[j]; | ||
| } | ||
| } | ||
| } | ||
| // 우승자 리스트 추가 | ||
| for (int i = 0; i < Players.Car_Names.length; i++) { | ||
| if (Players.Move_count[i] == max) { | ||
| Winner.add(Players.Car_Names[i]); | ||
| } | ||
| } | ||
| System.out.print("최종 우승자 : "); | ||
| if (Winner.size() > 1) { | ||
| String Winners = String.join(", ", Winner); | ||
| System.out.print(Winners); | ||
| } | ||
| else { System.out.print(Winner.get(0)); } | ||
|
|
||
| } | ||
|
|
||
| // 게임 진행 메인 함수 | ||
| public void Start(Car Players) { | ||
| get_round(); | ||
| Players.get_Players_Move(); | ||
|
|
||
| System.out.println("실행 결과"); | ||
| for (int i = 0; i < round; i++) { | ||
| Players.Move(); | ||
| display(Players); | ||
| } | ||
| End(Players); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
car와 Game에서 각각 소문자/대문자를 사용하신 이유가 궁금합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사실 이름에 대해서는 크게 신경을 쓰지 않은 것 같습니다. 자바 네이밍 규칙 준수하겠습니다.