-
Notifications
You must be signed in to change notification settings - Fork 9
장재훈 - 자동차 게임 구현 #2
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: Jaehooni
Are you sure you want to change the base?
Changes from 20 commits
1613bb7
494a07d
71039f0
0c861ab
64e0e9f
dfe3b19
79e996e
e10a9cc
9b3f2c8
4178bf0
f10bbe7
7635771
78a914e
7a8542e
7254c16
f73573a
6330ace
6e6c23b
1ba30dd
88db2a1
bfa1c4e
d90073c
5a31b44
aa47529
f5c5154
b00e2b0
3de28a2
4a7544c
582d57f
7909af2
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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package racingcar; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO 구현 진행 | ||
| } | ||
| public static void main(String[] args) { | ||
| Game game = new Game(); | ||
| game.make(); | ||
| game.start(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,27 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return this.position; | ||
| } | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| // 추가 기능 구현 | ||
| void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
| this.position += 1; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Game { | ||
| private List<Car> carList = new ArrayList<>(); | ||
|
|
||
| public void make() { | ||
| List<String> names = InputCheck.inputCarNames(); | ||
| for (String name : names) { | ||
| this.carList.add(new Car(name)); | ||
| } | ||
| } | ||
|
|
||
| private void run() { | ||
| int num = InputCheck.inputRound(); | ||
| for (int i = 0; i < num; i++) { | ||
| this.carList.forEach(Car::move); | ||
|
||
| Output.printProcedure(this.carList); | ||
| } | ||
| } | ||
|
|
||
| public void start() { | ||
| run(); | ||
| Output.printResult(this.carList); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public final class InputCheck { | ||
|
|
||
| public static List<String> inputCarNames() { | ||
| while (true) { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)"); | ||
| List<String> names = Arrays.asList(Console.readLine().split(",")); | ||
| try { | ||
| names.forEach(InputCheck::checkName); | ||
| return names; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void checkName(String name) { | ||
| if (name.trim().length() == 0 || name.trim().length() > 5) { | ||
| throw new IllegalArgumentException("[ERROR] 이름은 1~5자 사이로 작성하라."); | ||
| } | ||
| } | ||
|
|
||
| public static int inputRound() { | ||
| while (true) { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| try { | ||
| String input = Console.readLine(); | ||
| checkRound(input); | ||
| return Integer.parseInt(input); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void checkRound(String input) { | ||
| String pattern = "^[0-9]+$"; | ||
| if (!Pattern.matches(pattern, input)) { | ||
| throw new IllegalArgumentException("[ERROR] 시도 횟수는 자연수인 숫자여야 한다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Output { | ||
| public static void printProcedure(List<Car> carList) { | ||
| System.out.println("실행 결과"); | ||
| for (Car car : carList) { | ||
| String distance = new String(new char[car.getPosition()]).replace("\0", "-"); | ||
|
||
| System.out.printf("%s : %s%n", car.getName(), distance); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| static void printResult(List<Car> carList) { | ||
| List<String> winner = new ArrayList<>(); | ||
| int maxValue = findMaxValue(carList); | ||
|
|
||
| for (Car car : carList) { | ||
| if (car.getPosition() < maxValue) { | ||
| break; | ||
| } | ||
| winner.add(car.getName()); | ||
| } | ||
|
||
|
|
||
| System.out.printf("최종 우승자 : %s", String.join(", ", winner)); | ||
| } | ||
|
|
||
| private static int findMaxValue(List<Car> carList) { | ||
| carList.sort((x, y) -> y.getPosition() - x.getPosition()); | ||
| return carList.get(0).getPosition(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 자동차 경주 게임 기능 구현 | ||
| ====================== | ||
|
|
||
| 🗒️기능 목록 | ||
| -------- | ||
| ### 1. 자동차가 주어진 Random 값에 따라 전진 혹은 멈추는 기능 | ||
| ### 2. 입력받은 이동 횟수만큼 1의 함수를 실행시키는 기능 | ||
| ### 3. 입력이 잘못되었을때 에러를 발생시키고 다시 입력받게 하는 기능 | ||
| ### 4. 입력으로 주어진 이름을 기반으로 Car Object를 생성하는 기능 | ||
| ### 5. 결과를 출력하는 기능 |
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.
변수명에 자료형이 들어가지 않는 것이 더 좋다고 알고 있습니다! names처럼 복수형으로 표현하는 것은 어떨까요?
참고 : https://tecoble.techcourse.co.kr/post/2020-04-24-variable_naming/
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.
조언 감사합니다! 말씀 주신 부분 참고해서 변수 명 수정토록 하겠습니다