Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1613bb7
docs: update manual.md
Jaehooni Mar 14, 2023
494a07d
feat: move function
Jaehooni Mar 14, 2023
71039f0
docs: add new manual
Jaehooni Mar 14, 2023
0c861ab
feat: add game method
Jaehooni Mar 14, 2023
64e0e9f
feat: add make method
Jaehooni Mar 14, 2023
dfe3b19
docs: delete manual item
Jaehooni Mar 14, 2023
79e996e
fix: typo
Jaehooni Mar 14, 2023
e10a9cc
feat: add new func to show output
Jaehooni Mar 15, 2023
9b3f2c8
feat: add new error find features
Jaehooni Mar 15, 2023
4178bf0
docs: simpify fix sentence
Jaehooni Mar 15, 2023
f10bbe7
docs: simplify fix sentence
Jaehooni Mar 15, 2023
7635771
refactor: move input feature to InputCheck.java
Jaehooni Mar 15, 2023
78a914e
feat: make execute code
Jaehooni Mar 15, 2023
7a8542e
refactor: make project convention style same as naver java convention
Jaehooni Mar 15, 2023
7254c16
refactor(replace declaration type):
Jaehooni Mar 21, 2023
f73573a
refactor: Separate print func from Car.java
Jaehooni Mar 21, 2023
6330ace
refactor: Separate func from Car.java
Jaehooni Mar 21, 2023
6e6c23b
refactor: just fix some codes
Jaehooni Mar 21, 2023
1ba30dd
feat: make new instance parameter
Jaehooni Mar 21, 2023
88db2a1
style: change parameter name
Jaehooni Mar 21, 2023
bfa1c4e
style: change parameter name
Jaehooni Mar 24, 2023
d90073c
refactor: separate sort function from findMaxValue(...)
Jaehooni Mar 24, 2023
5a31b44
refactor: add new function to start()
Jaehooni Mar 24, 2023
aa47529
refactor: simple fix
Jaehooni Mar 24, 2023
f5c5154
refactor: move files to utility directory
Jaehooni Mar 25, 2023
b00e2b0
refactor: move files to utility directory
Jaehooni Mar 25, 2023
3de28a2
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
4a7544c
feat: overriding toString()
Jaehooni Mar 25, 2023
582d57f
refactor: move sort func to Sort.java
Jaehooni Mar 25, 2023
7909af2
feat: white space
Jaehooni Apr 9, 2023
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: 5 additions & 3 deletions src/main/java/racingcar/Application.java
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();
}
}
27 changes: 21 additions & 6 deletions src/main/java/racingcar/Car.java
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;
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/racingcar/Game.java
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();

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/

Copy link
Author

Choose a reason for hiding this comment

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

조언 감사합니다! 말씀 주신 부분 참고해서 변수 명 수정토록 하겠습니다

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);

Choose a reason for hiding this comment

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

불필요한 반복문 사용을 줄이기 위해 forEach 메서드를 사용한 점이 좋은 것 같습니다. 저도 활용해봐야겠네요!

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다! :)

Output.printProcedure(this.carList);
}
}

public void start() {
run();
Output.printResult(this.carList);
}
}
49 changes: 49 additions & 0 deletions src/main/java/racingcar/InputCheck.java
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] 시도 횟수는 자연수인 숫자여야 한다.");
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/racingcar/Output.java
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", "-");
Copy link

Choose a reason for hiding this comment

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

char 배열과 replace를 이용해서 거리를 나타내는 문자열을 만드는 점이 인상 깊었었습니다.

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다 :)

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());
}

Choose a reason for hiding this comment

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

findMaxValue 메서드가 정렬과 최댓값 반환 두 동작을 수행하고 있는데, 메서드 이름은 최댓값 반환에 대해서만 나타나 있는 것 같습니다.
메서드의 구현을 읽지 않고도 어떤 로직으로 동작하는지 이해할 수 있게 바뀌면 좋을 것 같습니다.

Copy link
Author

Choose a reason for hiding this comment

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

미처 파악하지 못한 부분 말씀주셔서 감사합니다..! 수정토록 하겠습니다


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();
}
}
10 changes: 10 additions & 0 deletions src/main/java/racingcar/manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
자동차 경주 게임 기능 구현
======================

🗒️기능 목록
--------
### 1. 자동차가 주어진 Random 값에 따라 전진 혹은 멈추는 기능
### 2. 입력받은 이동 횟수만큼 1의 함수를 실행시키는 기능
### 3. 입력이 잘못되었을때 에러를 발생시키고 다시 입력받게 하는 기능
### 4. 입력으로 주어진 이름을 기반으로 Car Object를 생성하는 기능
### 5. 결과를 출력하는 기능