diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..a6c4543d --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,31 @@ +public class Car { + private final String name; + private final int MOVE = 4; + private int position = 0; + + public Car(String name) { + this.name = name; + } + + public void moveCar(int randomCount) { + if (randomCount >= MOVE) { + this.position += 1; + } + } + + public int getPosition() { + return this.position; + } + + public String getName() { + return this.name; + } + + public void printRaceSituation() { + System.out.print(name + " : "); + for (int i = 0; i < position; i++) { + System.out.print("-"); + } + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/RacingGame.java b/src/main/java/RacingGame.java new file mode 100644 index 00000000..4b50b962 --- /dev/null +++ b/src/main/java/RacingGame.java @@ -0,0 +1,201 @@ +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; + + +public class RacingGame implements RacingGameInterface { + private Scanner sc = new Scanner(System.in); + private Random random = new Random(); + private String playerName = ""; + private ArrayList playerNameArrayList = new ArrayList(); + private ArrayList carList = new ArrayList(); + private ArrayList winnerList = new ArrayList(); + private int moveCount = 0; + private final int ASCII_CODE_ZERO = 48; + private final int ASCII_CODE_NINE = 57; + + public String inputPlayerName() { + boolean nameNotInput = true; + + while (nameNotInput) { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,)기준으로 구분)"); + playerName = sc.nextLine(); + + if (playerName.length() != 0) { + nameNotInput = false; + } + } + + return playerName; + } + + /** + * String타입으로 입력 받은 자동차 이름을 콤마(,)를 기준으로 나누어 playerNameArrayList에 담는 메서드 + * + * @param inputPlayerName()로 받은 자동차 이름 + * @return 콤마(,)를 기준으로 이름을 잘라내어 담은 리스트 + */ + public ArrayList convertStringNameToArrayList(String name) { + String[] nameList = name.split(","); + playerNameArrayList.clear(); + + for (int i = 0; i < nameList.length; i++) { + nameList[i] = nameList[i].trim(); + + // 공백으로 입력된 자동차 이름은 리스트에 담지 않는다. + if (nameList[i].length() == 0) { + continue; + } + + playerNameArrayList.add(nameList[i]); + } // end for + + return playerNameArrayList; + } + + public boolean checkPlayerNameLength(ArrayList nameList) { + final int MINIMUM_CAR_NAME = 5; + boolean wrongLength = false; + + for (int i = 0; i < nameList.size(); i++) { + if (nameList.get(i).length() > MINIMUM_CAR_NAME) { + System.out.println("자동차 이름은 5자 이하여야 합니다."); + wrongLength = true; + return wrongLength; + } + } + + return wrongLength; + } + + /** + * inputPlayerName()로 자동차의 이름을 입력받고 각각의 자동차 이름이 5자 이하인지 확인해 최종적으로 자동차 이름을 + * playerNameArrayList에 저장합니다. + */ + public ArrayList setPlayerName() { + boolean validatedPlayerName = true; + + while (validatedPlayerName) { + playerName = inputPlayerName(); + playerNameArrayList = convertStringNameToArrayList(playerName); + validatedPlayerName = checkPlayerNameLength(playerNameArrayList); + } + + return playerNameArrayList; + } + + /** + * 입력받은 이름을 생성자의 인자로 넘겨 Car객체를 생성합니다. + * + * @param 자동차의 이름이 담겨있는 ArrayList + * @return 자동차의 이름을 생성자의 인자로 넘겨 만든 자동차 객체들의 리스트 + */ + public ArrayList makePlayers(ArrayList nameArrayList) { + for (int i = 0; i < nameArrayList.size(); i++) { + carList.add(new Car(nameArrayList.get(i))); + } + + return carList; + } + + public int inputMoveCount() { + boolean wrongInput = true; + String moveCountStr = "0"; + + while (wrongInput) { + System.out.println("시도할 회수는 몇회인가요?"); + moveCountStr = sc.nextLine(); + wrongInput = checkNumberOrNot(moveCountStr); + } + + moveCount = Integer.parseInt(moveCountStr); + + return moveCount; + } + + public boolean checkNumberOrNot(String moveCountStr) { + boolean isWrongInputNumber = false; + + for (int i = 0; i < moveCountStr.length(); i++) { + if (moveCountStr.charAt(i) < ASCII_CODE_ZERO || moveCountStr.charAt(i) > ASCII_CODE_NINE) { + System.out.println("시도할 회수로 숫자를 입력해야합니다."); + isWrongInputNumber = true; + break; + } + } + + if (isWrongInputNumber == false) { + isWrongInputNumber = checkMoveCountInputOrNot(moveCountStr); + } + + return isWrongInputNumber; + } + + public boolean checkMoveCountInputOrNot(String moveCountStr) { + boolean isNotInput = false; + + if (moveCountStr.length() == 0) { + System.out.println("시도할 회수가 입력되지 않았습니다."); + isNotInput = true; + return isNotInput; + } + + if (Integer.parseInt(moveCountStr) == 0) { + System.out.println("레이스를 위해 0 이상의 숫자를 입력해주세요."); + isNotInput = true; + } + + return isNotInput; + } + + public void startRace() { + playerNameArrayList = setPlayerName(); + carList = makePlayers(playerNameArrayList); + moveCount = inputMoveCount(); + + // 입력 받은 경기 회수만큼 경기를 진행해 car 클래스의 position을 변경하고 결과를 출력합니다. + for (int i = 0; i < moveCount; i++) { + for (int j = 0; j < carList.size(); j++) { + carList.get(j).moveCar(random.nextInt(10)); + carList.get(j).printRaceSituation(); + } + + System.out.println("\n\n"); + } + } + + public ArrayList judgeWinner(ArrayList carList) { + int maxPosition = 0; + + for (int i = 0; i < carList.size(); i++) { + if (carList.get(i).getPosition() > maxPosition) { + maxPosition = carList.get(i).getPosition(); + } + } + + for (int i = 0; i < carList.size(); i++) { + if (carList.get(i).getPosition() == maxPosition) { + winnerList.add(carList.get(i)); + } + } + + return winnerList; + } + + public void printWinner() { + winnerList = judgeWinner(carList); + + for (int i = 0; i < winnerList.size() - 1; i++) { + System.out.print(winnerList.get(i).getName() + ", "); + } + + System.out.println(winnerList.get(winnerList.size() - 1).getName() + "가 최종 우승했습니다."); + } + + public static void main(String[] args) { + RacingGame rg = new RacingGame(); + rg.startRace(); + rg.printWinner(); + } + +} \ No newline at end of file diff --git a/src/main/java/RacingGameInterface.java b/src/main/java/RacingGameInterface.java new file mode 100644 index 00000000..7adbe4b6 --- /dev/null +++ b/src/main/java/RacingGameInterface.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; + +public interface RacingGameInterface { + + /** 자동차 이름 입력 받기 */ + abstract String inputPlayerName(); + abstract ArrayList convertStringNameToArrayList(String name); + abstract boolean checkPlayerNameLength(ArrayList nameList); + abstract ArrayList setPlayerName(); + + /** 자동차 객체 생성 */ + abstract ArrayList makePlayers(ArrayList nameArrayList); + + /** 실행 회수 입력 받기 */ + abstract int inputMoveCount(); + abstract boolean checkNumberOrNot(String moveCountStr); + abstract boolean checkMoveCountInputOrNot(String moveCountStr); + + /** 승자 판정 */ + abstract ArrayList judgeWinner(ArrayList carList); + + /** 게임 시작 */ + abstract void startRace(); + + /** 우승자 출력 */ + public void printWinner(); + +}