-
Notifications
You must be signed in to change notification settings - Fork 7
[8주차] ysy #53
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: main
Are you sure you want to change the base?
[8주차] ysy #53
Changes from 13 commits
2da042e
6555d2a
bbd117d
ea017bf
0df622d
e834702
2cac2e8
c05f489
a0e7017
79e22bd
24fef7c
8c84718
201fda2
84e4e2c
ac1279e
6564893
433cff1
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,48 @@ | ||
|
|
||
| # Created by .ignore support plugin (hsz.mobi) | ||
| ### Java template | ||
| # Compiled class file | ||
| *.class | ||
|
|
||
| # Log file | ||
| *.log | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
|
|
||
| .DS_Store | ||
| .gradle | ||
| /build/ | ||
| !gradle/wrapper/gradle-wrapper.jar | ||
| /out/ | ||
| /target/ | ||
|
|
||
| ### STS ### | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| bin/ | ||
|
|
||
| ### IntelliJ IDEA ### | ||
| .idea | ||
| *.iws | ||
| *.iml | ||
| *.ipr |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import utils.GameUtils; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| GameUtils gameUtils = new GameUtils(); | ||
| gameUtils.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package inpututils; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private static final Scanner SCANNER = new Scanner(System.in); | ||
|
|
||
| public static String getCarName() { | ||
| System.out.print("경주 할 자동차 이름 : "); | ||
| return SCANNER.nextLine(); | ||
| } | ||
|
|
||
| public static int getTryNumber() { | ||
| System.out.print("시도할 횟수 : "); | ||
| try { | ||
| return SCANNER.nextInt(); | ||
| } catch (NumberFormatException e) { | ||
| throw new NumberFormatException("[ERROR] 시도 횟수는 숫자여야 한다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package outpututils; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class OutputView { | ||
| private OutputView() { | ||
| } | ||
|
|
||
| public static void printCarName(String name) { | ||
| System.out.print(name + " : "); | ||
| } | ||
|
|
||
| public static void printMove() { | ||
| System.out.print("-"); | ||
| } | ||
|
|
||
| public static void printWinner(ArrayList<String> winner) { | ||
| //TODO:마지막에 , 나오지 않도록 수정 필요 | ||
| winner.stream() | ||
| .forEach(w -> System.out.print(w + ",")); | ||
| } | ||
| } | ||
|
Comment on lines
+17
to
+22
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,25 @@ | ||
| package racingcar; | ||
|
|
||
| import utils.GameUtils; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
|
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. 생성시점에 초기화 해 주는 방법도 좋은 방법일 것 같습니다
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. 프로그래밍 요구사항-Car객체에서 해당 부분이 고정인 것 같아 그대로썼는데, 생성시점에 초기화 해주는 방법이라는 것이 어떤 것인지 잘 이해가 가지 않습니다! |
||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int plusPosition(){ | ||
| return position++; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package racingcar; | ||
|
|
||
| import outpututils.OutputView; | ||
| import utils.RandomUtils; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Cars { | ||
| private static List<Car> carList; | ||
|
|
||
| private static final int START_NUMBER = 0; | ||
| private static final int END_NUMBER = 9; | ||
|
|
||
| private static final int BOUNDARY_NUMBER = 4; | ||
|
|
||
| private static ArrayList<String> winner; | ||
|
|
||
| public Cars(List<Car> carList) { | ||
| this.carList = carList; | ||
| } | ||
|
|
||
| public static boolean isMove(int randomNumber) { | ||
| if (randomNumber >= BOUNDARY_NUMBER) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void playGame() { | ||
| for (int j = 0; j < carList.size(); j++) { | ||
|
|
||
| OutputView.printCarName(carList.get(j).getName()); | ||
|
|
||
| if (isMove(RandomUtils.nextInt(START_NUMBER, END_NUMBER))) { | ||
| carList.get(j).plusPosition(); | ||
| } | ||
| for (int k = 0; k < carList.get(j).getPosition(); k++) { | ||
| OutputView.printMove(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public static int getMax() { | ||
| int max = carList.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .getAsInt(); | ||
| return max; | ||
| } | ||
|
|
||
| public static void setWinnerList(int max) { | ||
| winner = new ArrayList<String>(); | ||
| for (int i = 0; i < carList.size(); i++) { | ||
| if (max == carList.get(i).getPosition()) { | ||
| winner.add(carList.get(i).getName()); | ||
| } | ||
| } | ||
| } | ||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static ArrayList<String> getWinnerList() { | ||
| return winner; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package utils; | ||
|
|
||
| import inpututils.InputView; | ||
| import outpututils.OutputView; | ||
| import racingcar.Car; | ||
| import racingcar.Cars; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class GameUtils { | ||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static List<Car> makeCarList(String[] splitResult) { | ||
| return Arrays.stream(splitResult) | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static void run() { | ||
|
|
||
| String inputName = InputView.getCarName(); | ||
| int inputCount = InputView.getTryNumber(); | ||
| String[] splitResult = SplitString.splitString(inputName); | ||
|
|
||
| Cars cars = new Cars(makeCarList(splitResult)); | ||
|
|
||
| for (int i = 0; i < inputCount; i++) { | ||
| Cars.playGame(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| Cars.setWinnerList(Cars.getMax()); | ||
| OutputView.printWinner(Cars.getWinnerList()); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package utils; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomUtils { | ||
| private static final Random RANDOM = new Random(); | ||
|
|
||
| private RandomUtils() { | ||
| } | ||
|
|
||
| public static int nextInt(final int startInclusive, final int endInclusive) { | ||
| if (startInclusive > endInclusive) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| if (startInclusive < 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if (startInclusive == endInclusive) { | ||
| return startInclusive; | ||
| } | ||
| return RANDOM.nextInt(endInclusive - startInclusive + 1) + startInclusive; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package utils; | ||
|
|
||
| public class SplitString { | ||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private SplitString() { | ||
| } | ||
|
|
||
| public static String[] splitString(String input) { | ||
| return input.split(","); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.