-
Notifications
You must be signed in to change notification settings - Fork 6
java-racingcar(songpang) #3
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: songpang
Are you sure you want to change the base?
Changes from 53 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,76 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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을 제외한 모든 메서드들이 외부에서 호출될 필요가 없어보입니다.
Member
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.
클래스를 좀 더 분리해보는건 어떨까요? 🤔
Member
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인데 의도하신 건가요?
Member
Author
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로 변경했습니다!
Member
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 judgeAndMove(Car car, int randomNumber) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (randomNumber >= WINNER_CONDITION) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| car.moveForward(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. 핵데이 컨벤션 8.3 참고해주세요!
Suggested change
Member
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. 컨벤션을 확인해주세요 🙂 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<Car> inputNames() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String messageCode = Message.GeneralMessages.INPUT_NAMEOFCAR.getMessage(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printMessages(messageCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<String> names = new ArrayList<>(Arrays.asList(receiver.receiveName())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return makeCarList(names); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<Car> makeCarList(List<String> names) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<Car> cars = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < names.size(); i++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cars.add(new Car(names.get(i))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return cars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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
자바의 유명한 규칙중엔 program to interfaces not implementations 라는 부분이 있습니다. 객체 지향 기초 이야기 3, 유연함(Flexibility) 위 글 참고해보시면 좋을것 같아요 🙂 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int inputNumber() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String messageCode = Message.GeneralMessages.INPUT_COUNT.getMessage(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printMessages(messageCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return receiver.receiveNumber(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void launchAllRound(ArrayList<Car> cars, int countRound) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String messageCode = Message.GeneralMessages.DEFAULT_SPACE.getMessage(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < countRound; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (Car car : cars) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| judgeAndMove(car, generator.generateRandomNumber()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printProgress(car.getName(), car.getProgressWithSymbol()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printMessages(messageCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void run() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String messageCode = Message.GeneralMessages.OPERATION_RESULT.getMessage(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<Car> cars = inputNames(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int countRound = inputNumber(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printMessages(messageCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| launchAllRound(cars, countRound); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayList<Car> winner = Winner.checkWhoIsWinner(cars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.printWinner(Winner.makeWinnerToString(winner)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+78
to
+82
Member
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. 클린코드 책에 내려가기드 책에 내려가기 규칙이라는 것이 소개되어 있습니다. 코드를 신문 읽듯이 쭈욱 내려가면서 읽을 수 있어야 한다는 것입니다.
Member
Author
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. 생각해보니까 답글을 다 달지 않았네요 :(
Member
Author
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. 이 부분에 대해서 계속 고민하고 있는데 더 읽기 쉽게 배열할 수 있는 뚜렷한 방법이 떠오르지가 않네요. 어떤 방식으로 배열을 해야 읽기 쉽게 배열할 수 있을까요? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||
|
Member
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 클래스를 사용하면 난수 생성을 쉽게 할 수 있습니다.
Member
Author
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 valiNumber = "^[0-9]+$"; | ||
| private static final String commaInARow = "^.*(,,).*+$"; | ||
| private static final String characterOTN = "^[a-zA-Z,]+$"; | ||
|
songpang marked this conversation as resolved.
Outdated
|
||
| private static final Character COMMA = ','; | ||
| private static final String NOTHING = ""; | ||
|
|
||
| Printer printer; | ||
|
Member
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 Validator() { | ||
| printer = new Printer(); | ||
| } | ||
|
|
||
| public boolean validateName(String s) { | ||
|
Member
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(valiNumber); | ||
| } | ||
|
|
||
| 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(commaInARow, 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(characterOTN, 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(",")); | ||
|
Member
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()); | ||
| } | ||
| } | ||
|
songpang marked this conversation as resolved.
Outdated
|
||
|
|
||
| return winner.toString(); | ||
| } | ||
|
Comment on lines
+7
to
+17
Member
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 ArrayList<Car> checkWhoIsWinner(ArrayList<Car> cars) { | ||
| ArrayList<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.
현재 위치를
"-"로 표현된다는건 뷰의 관심사인것 같아요.현재의 콘솔 환경에선 "-", 웹 환경이라면 자동차의 위치에 맞는 이미지가 될수도 있지 않을까요?