-
Notifications
You must be signed in to change notification settings - Fork 9
[노현욱] RacingCar Mission 구현 #6
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: rohsik2
Are you sure you want to change the base?
Changes from all commits
86d0ac4
ecb4486
476ee32
6d4091e
3cb0d19
ec3a7d7
517644e
a1979df
86a9bf2
29d7d7f
24ca6b5
b48267e
edd9e55
3859fe8
28e1162
be0d99b
e6c8dbe
7454c9f
5cd9fb1
cf54dff
baf4aa8
914fcfa
2b8d017
8b43b23
ebac380
3aa25fc
ea35570
7b2c003
d5094a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
package racingcar; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
public class Car { | ||
private static final int MOVE_RATE = 4; | ||
private final String name; | ||
private int position = 0; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
} | ||
|
||
// 추가 기능 구현 | ||
public void randomlyGoForward() { | ||
if(Randoms.pickNumberInRange(0, 9) >= MOVE_RATE) | ||
position++; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package racingcar; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
public class InputManager { | ||
private static final String CAR_NAME_DELIMITER = ","; | ||
|
||
public String[] getCarNames(){ | ||
String inputLine = Console.readLine(); | ||
return inputLine.split(CAR_NAME_DELIMITER); | ||
} | ||
|
||
public int getCount(){ | ||
String inputLine = Console.readLine(); | ||
try{ | ||
return Integer.parseInt(inputLine); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("[Error]숫자를 입력해주세요."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package racingcar; | ||
|
||
import java.util.List; | ||
|
||
public class OutputManager { | ||
|
||
int maxCarNameLen = 5; | ||
|
||
public void printCarNameInputMessage() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분). 최대 5글자"); | ||
} | ||
|
||
public void printTurnInputMessage() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
} | ||
|
||
public void printResultMessage() { | ||
System.out.println("실행 결과"); | ||
} | ||
|
||
public void printCarData(List<Car> cars) { | ||
for (Car car : cars) { | ||
System.out.println(getName(car.getName()) + " : " + getDash(car.getPosition())); | ||
} | ||
System.out.println(); | ||
} | ||
private String getName(String carName){ | ||
StringBuilder carNameBuilder = new StringBuilder(carName); | ||
while (carNameBuilder.length() < maxCarNameLen) { | ||
carNameBuilder.append(" "); | ||
} | ||
return carNameBuilder.toString(); | ||
|
||
} | ||
|
||
private String getDash(int position) { | ||
StringBuilder dash = new StringBuilder(); | ||
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. java 8에서는 repeat 기능을 못 써서 저는 String에다 char를 하나씩 붙였는데, StringBuilder를 써서 for loop를 돌려 문자열이 반복되게 만드는 방법도 있다는 걸 알게 되어 좋습니다! |
||
for (int i = 0; i < position; i++) { | ||
dash.append("-"); | ||
} | ||
return dash.toString(); | ||
} | ||
|
||
public void printWinner(List<Car> winnerCars) { | ||
System.out.print("최종 우승자 : "); | ||
for(Car winner : winnerCars){ | ||
System.out.print(winner.getName()); | ||
if(winner != winnerCars.get(winnerCars.size()-1)) | ||
System.out.print(", "); | ||
} | ||
System.out.println(); | ||
} | ||
Comment on lines
+44
to
+52
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. utf-8 인코딩은 하셨나요? 한글 잘 나오나요? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package racingcar; | ||
|
||
import static java.lang.Integer.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class RacingGame { | ||
|
||
private static final String CAR_NAME_DELIMITER = ","; | ||
private InputManager inputManager; | ||
private OutputManager outputManager; | ||
private List<Car> cars; | ||
private int maxTurnCount; | ||
|
||
public static void main(String[] args) { | ||
InputManager inputManager = new InputManager(); | ||
OutputManager outputManager = new OutputManager(); | ||
RacingGame game = new RacingGame(inputManager, outputManager); | ||
game.start(); | ||
} | ||
|
||
public RacingGame(InputManager inputManager, OutputManager outputManager) { | ||
this.inputManager = inputManager; | ||
this.outputManager = outputManager; | ||
} | ||
|
||
public void start() { | ||
initGame(); | ||
race(); | ||
reward(); | ||
} | ||
|
||
private void initGame() { | ||
outputManager.printCarNameInputMessage(); | ||
String[] carNames = inputManager.getCarNames(); | ||
checkDuplicateName(carNames); | ||
|
||
outputManager.printTurnInputMessage(); | ||
int turn = inputManager.getCount(); | ||
checkTurnValid(turn); | ||
|
||
|
||
cars = new ArrayList<>(); | ||
this.maxTurnCount = turn; | ||
for (String carName : carNames) { | ||
String trimed = carName.trim(); | ||
if(trimed.length() > 5) | ||
throw new IllegalArgumentException("[Error] 이름은 5글자 이하만 가능합니다."); | ||
cars.add(new Car(trimed)); | ||
} | ||
} | ||
|
||
private void race() { | ||
outputManager.printResultMessage(); | ||
for (int i = 0; i < maxTurnCount; i++) { | ||
for (Car car : cars) { | ||
car.randomlyGoForward(); | ||
} | ||
outputManager.printCarData(cars); | ||
} | ||
} | ||
|
||
private void reward() { | ||
List<Car> winnerCars = getWinnerCars(); | ||
outputManager.printWinner(winnerCars); | ||
} | ||
|
||
private void checkDuplicateName(String[] carNamesArray) { | ||
Set<String> carNamesSet = new HashSet<>(Arrays.asList(carNamesArray)); | ||
if (carNamesSet.size() != carNamesArray.length) { | ||
throw new IllegalArgumentException("[Error] 중복된 이름이 있습니다."); | ||
} | ||
} | ||
|
||
private void checkTurnValid(int turn) { | ||
if (turn < 1) { | ||
throw new IllegalArgumentException("[Error] 1 이상의 숫자를 입력해주세요."); | ||
} | ||
} | ||
|
||
private List<Car> getWinnerCars() { | ||
List<Car> winners = new ArrayList<>(); | ||
int maxPosition = 0; | ||
for (Car car : cars) { | ||
maxPosition = Math.max(maxPosition, car.getPosition()); | ||
} | ||
for (Car car : cars) { | ||
if (car.getPosition() == maxPosition) { | ||
winners.add(car); | ||
} | ||
} | ||
return winners; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### 기능 목록 | ||
1. 사용자의 입력을 받는 입력부 | ||
1. 동일한 자동차 이름이 존재하는지 여부를 확인 | ||
2. 입력받은 자동차 이름을 분리하여 자동차 객체를 생성하는 기능 | ||
3. 입력받은 시도 횟수를 저장하는 기능 | ||
1. 숫자가 입력이 되었는지 여부를 확인 | ||
4. 자동차 객체의 움직임을 결정하는 기능 | ||
5. 자동차 객체의 움직임을 출력하는 기능 | ||
6. 우승자를 결정하는 기능 |
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.
이건 왜 PRIVATE 이죠?