-
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 14 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,11 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO 구현 진행 | ||
| } | ||
| public static void main(String[] args) { | ||
| ArrayList<Car> cars = Car.make(); | ||
| Car.game(cars); | ||
| Car.printResult(cars); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,66 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| 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 void move() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
| this.position += 1; | ||
| } | ||
| } | ||
|
|
||
| public static void game(ArrayList<Car> cars) { | ||
| int num = InputCheck.inputRound(); | ||
| for (int i = 0; i < num; i++) { | ||
| System.out.println("실행 결과"); | ||
| for (Car car : cars) { | ||
| car.move(); | ||
| car.printProgress(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public static ArrayList<Car> make() { | ||
| ArrayList<String> names = InputCheck.inputCarNames(); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
| for (String name : names) { | ||
| cars.add(new Car(name)); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public void printProgress() { | ||
| String distance = new String(new char[this.position]).replace("\0", "-"); | ||
| System.out.printf("%s : %s%n", this.name, distance); | ||
| } | ||
|
|
||
| public static void printResult(ArrayList<Car> cars) { | ||
| String winner = ""; | ||
| int winnerScore = -1; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
| cars.sort((x, y) -> y.getPosition() - x.getPosition()); | ||
|
||
| for (Car car : cars) { | ||
| if (car.getPosition() >= winnerScore) { | ||
| winner += car.name + ", "; | ||
| winnerScore = car.position; | ||
| } | ||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| System.out.printf("최종 우승자 : %s", winner.substring(0, winner.length() - 2)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class InputCheck { | ||
| public InputCheck() { | ||
| } | ||
|
||
|
|
||
| public static ArrayList<String> inputCarNames() { | ||
| while (true) { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)"); | ||
| ArrayList<String> names = new ArrayList<>(Arrays.asList(Console.readLine().split(","))); | ||
| try { | ||
| iterateCars(names); | ||
| return names; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println("[ERROR] 이름은 5자 이하만 가능하다."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void iterateCars(ArrayList<String> names) { | ||
| for (String name : names) { | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } | ||
|
||
|
|
||
| public static int inputRound() { | ||
| while (true) { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| try { | ||
| String input = Console.readLine(); | ||
| match(input); | ||
| return Integer.parseInt(input); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println("[ERROR] 시도 횟수는 자연수인 숫자여야 한다."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void match(String input) { | ||
| String pattern = "^[0-9]+$"; | ||
| if (!Pattern.matches(pattern, input)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } | ||
| 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.
JAVA 에서는 List와 같은 Interface 타입을 변수의 타입으로 사용하길 권장하고 있습니다. 한번 찾아보시면 좋을것 같아요!
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.
감사합니다! 아직 자바 코드 작성 경험이 적어 생기는 문제점인 것 같습니다 코드 수정 과정에서 수정하겠습니다 :)