-
Notifications
You must be signed in to change notification settings - Fork 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
java-racingcar(songpang) #3
base: songpang
Are you sure you want to change the base?
Changes from all commits
0a5d963
216cb6d
ae04e19
a6e8c0f
804fe2d
a02977d
3ac806d
a973407
78bb06d
bb9c87b
07f6628
cdfc826
0ff13cc
6519379
7aaac17
b115436
8d840f7
74d8054
02b5186
6954307
305ea0d
fc3b773
e0fe4fa
2d34588
e1c98f3
38feeaa
c47f82f
2c62d0c
838a38b
1a10b46
d9fbd3a
932e5fe
ed3d8ac
e0b9f8d
0b4dd36
7bb1ace
c7d7fd1
66dd3b6
a595c43
0852319
422c3da
8d80c12
645be80
302139e
93dde0d
bfb6277
b87577f
9a31667
51d46b3
5d7064e
b3444f0
978faee
eb18b53
021f98d
c296369
e577df7
560f251
8f84168
0428c22
ed92c1f
83eaf8c
54109bd
506b630
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,8 @@ | ||
import domain.GamePlayer; | ||
|
||
public class Application { | ||
public static void main(String[] args){ | ||
GamePlayer gamePlayer = new GamePlayer(); | ||
gamePlayer.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||
package domain; | ||||||
|
||||||
import io.Message; | ||||||
import io.Printer; | ||||||
import io.Receiver; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.List; | ||||||
|
||||||
public class GamePlayer { | ||||||
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. run을 제외한 모든 메서드들이 외부에서 호출될 필요가 없어보입니다. 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.
클래스를 좀 더 분리해보는건 어떨까요? 🤔 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. 대부분의 메소드의 접근 제한자가 default인데 의도하신 건가요? 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. 처음에 public으로 설정했다가 동민님이 외부에서 사용하지 않는데 굳이 public을 사용한 이유가 있냐고 피드백을 주셔서 이에 대해 고민해 본 이후 default로 변경했습니다! 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. 아마 클래스 내부에서만 사용되는 메소드를 public으로 설정해서 그런 피드백을 주지 않았나 생각이 되네요 🙂 클래스 내부에서만 사용되는 메소드는 private, 외부에서 메세지를 전달받는 메소드는 public으로 설정하는 것이 좋을 것 같아요. |
||||||
private static final int WINNER_CONDITION = 4; | ||||||
|
||||||
private final Printer printer; | ||||||
private final Receiver receiver; | ||||||
private final Generator generator; | ||||||
|
||||||
public GamePlayer() { | ||||||
this.printer = new Printer(); | ||||||
this.receiver = new Receiver(); | ||||||
this.generator = new Generator(); | ||||||
} | ||||||
|
||||||
void moveAfterJudgement(Car car, int randomNumber) { | ||||||
if (judgeToMove(randomNumber)) { | ||||||
car.moveForward(); | ||||||
} | ||||||
} | ||||||
|
||||||
private boolean judgeToMove(int randomNumber) { | ||||||
return randomNumber >= WINNER_CONDITION; | ||||||
} | ||||||
|
||||||
List<Car> inputNames() { | ||||||
String messageCode = Message.GeneralMessages.INPUT_NAMEOFCAR.getMessage(); | ||||||
printer.printMessages(messageCode); | ||||||
List<String> names = new ArrayList<>(Arrays.asList(receiver.receiveName())); | ||||||
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.
Suggested change
|
||||||
|
||||||
return makeCarList(names); | ||||||
} | ||||||
|
||||||
int inputNumber() { | ||||||
String messageCode = Message.GeneralMessages.INPUT_COUNT.getMessage(); | ||||||
printer.printMessages(messageCode); | ||||||
|
||||||
return receiver.receiveNumber(); | ||||||
} | ||||||
|
||||||
List<Car> makeCarList(List<String> names) { | ||||||
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.
|
||||||
List<Car> cars = new ArrayList<>(); | ||||||
|
||||||
for (int i = 0; i < names.size(); i++) { | ||||||
cars.add(new Car(names.get(i))); | ||||||
} | ||||||
|
||||||
return cars; | ||||||
} | ||||||
|
||||||
void launchAllRound(List<Car> cars, int countRound) { | ||||||
String messageCode = Message.GeneralMessages.DEFAULT_SPACE.getMessage(); | ||||||
|
||||||
for (int i = 0; i < countRound; i++) { | ||||||
for (Car car : cars) { | ||||||
moveAfterJudgement(car, generator.generateRandomNumber()); | ||||||
printer.printProgress(car.getName(), car.getProgressWithSymbol()); | ||||||
} | ||||||
printer.printMessages(messageCode); | ||||||
} | ||||||
} | ||||||
|
||||||
public void run() { | ||||||
String messageCode = Message.GeneralMessages.OPERATION_RESULT.getMessage(); | ||||||
List<Car> cars = inputNames(); | ||||||
int countRound = inputNumber(); | ||||||
|
||||||
printer.printMessages(messageCode); | ||||||
launchAllRound(cars, countRound); | ||||||
|
||||||
List<Car> winner = Winner.checkWhoIsWinner(cars); | ||||||
printer.printWinner(Winner.makeWinnerToString(winner)); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package domain; | ||
|
||
public class Generator { | ||
private final int MAX_LIMIT_NUMBER = 10; //default value | ||
public int generateRandomNumber() { | ||
return (int) ((Math.random() * 10000) % MAX_LIMIT_NUMBER); | ||
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 API의 Random 클래스를 사용하면 난수 생성을 쉽게 할 수 있습니다. 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. 굳이 Random클래스를 사용하여 객체를 추가적으로 생성해야 한다는 것에 의문을 품어 Random 클래스를 사용하지 않았는데 지금와서 생각해보니 성능차이도 없거니와 모두가 주로 사용하는 클래스를 사용해야 가독성이 높아진다는 것을 깨달았습니다. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package domain; | ||
|
||
import io.Message; | ||
import io.Printer; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
public class Validator { | ||
private static final int MAX_NAME_SIZE = 5; | ||
private static final String VALID_NUMBER = "^[0-9]+$"; | ||
private static final String COMMA_IN_ROW = "^.*(,,).*+$"; | ||
private static final String CHARACTER = "^[a-zA-Z,]+$"; | ||
private static final Character COMMA = ','; | ||
private static final String NOTHING = ""; | ||
|
||
private final Printer printer; | ||
|
||
public Validator() { | ||
printer = new Printer(); | ||
} | ||
|
||
public boolean validateName(String s) { | ||
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. 약어를 사용하기보단 적절한 이름을 사용해주세요! |
||
return inputNothing(s) | ||
&& inputCommaInARow(s) | ||
&& inputCharactersOtherThanName(s) | ||
&& startWithComma(s) | ||
&& endWithComma(s) | ||
&& overSizeCharacters(s) | ||
&& inputSameName(s); | ||
} | ||
|
||
public boolean isValidNumber(String s) { | ||
return s.matches(VALID_NUMBER); | ||
} | ||
|
||
public boolean inputNothing(String s) { | ||
String messageCode = Message.ExceptionMessages.INPUT_NOTHING.getMessage(); | ||
|
||
if (s.equals(NOTHING)) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean inputCommaInARow(String s) { | ||
String messageCode = Message.ExceptionMessages.INPUT_COMMA_IN_A_ROW.getMessage(); | ||
|
||
if (Pattern.matches(COMMA_IN_ROW, s)) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean startWithComma(String s) { | ||
String messageCode = Message.ExceptionMessages.START_WITH_COMMA.getMessage(); | ||
|
||
if (s.charAt(0) == COMMA) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean endWithComma(String s) { | ||
String messageCode = Message.ExceptionMessages.END_WITH_COMMA.getMessage(); | ||
|
||
if (s.charAt(s.length() - 1) == COMMA) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean inputCharactersOtherThanName(String s) { | ||
String messageCode = Message.ExceptionMessages.INPUT_CHARACTERS_OTHER_THAN_NAME.getMessage(); | ||
|
||
if (!Pattern.matches(CHARACTER, s)) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean inputSameName(String s) { | ||
String messageCode = Message.ExceptionMessages.INPUT_SAME_NAME.getMessage(); | ||
List<String> carNames = Arrays.asList(s.split(",")); | ||
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.
|
||
int sizeOfNameList = carNames.size(); | ||
|
||
for (int i = 0; i < sizeOfNameList; i++) { | ||
if (carNames.subList(i + 1, sizeOfNameList).contains(carNames.get(i))) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean overSizeCharacters(String s) { | ||
String messageCode = Message.ExceptionMessages.OVER_SIZE_CHARACTERS.getMessage(); | ||
String[] splitName = s.split(","); | ||
|
||
for (String i : splitName) | ||
if (i.length() > MAX_NAME_SIZE) { | ||
printer.printMessages(messageCode); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Winner { | ||
public static String makeWinnerToString(List<Car> cars) { | ||
StringBuilder winner = new StringBuilder(cars.get(0).getName()); | ||
|
||
if (cars.size() > 1) { | ||
for (int i = 1; i < cars.size(); i++) { | ||
winner.append(", ").append(cars.get(i).getName()); | ||
} | ||
} | ||
|
||
return winner.toString(); | ||
} | ||
Comment on lines
+7
to
+17
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.
|
||
|
||
public static List<Car> checkWhoIsWinner(List<Car> cars) { | ||
List<Car> winner = new ArrayList<>(); | ||
int maxNumber = 0; | ||
|
||
for (Car car : cars) { | ||
if(car.isMaxNumber(maxNumber)){ | ||
winner.add(car); | ||
} | ||
if (car.isOverMaxNumber(maxNumber)) { | ||
maxNumber = initWinner(winner, car); | ||
} | ||
} | ||
|
||
return winner; | ||
} | ||
|
||
private static int initWinner(List<Car> winner, Car car) { | ||
int maxNumber = car.getPosition(); | ||
winner.clear(); | ||
winner.add(car); | ||
|
||
return maxNumber; | ||
} | ||
} |
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.
현재 위치를
"-"
로 표현된다는건 뷰의 관심사인것 같아요.현재의 콘솔 환경에선 "-", 웹 환경이라면 자동차의 위치에 맞는 이미지가 될수도 있지 않을까요?