Skip to content
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

[자동차 경주] 이도형 미션 제출합니다 #232

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
36 changes: 36 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## 기능 목록
#### Preset.kt
- 자동차 이름을 입력 받아 자동차를 배정하는 함수 - fun presetCars()
- 시도 횟수를 제대로 입력했는지 확인하는 함수 - fun checkValidInput()
#### CarGame.kt
- 시도할 횟수를 입력 받아 게임을 진행하는 함수 - fun repeatMove()
- 0에서 9 사이 무작위 값을 구한뒤 4 이상인지 판단하는 함수 - fun checkAdvance()
- 각 자동차 별로 이동시키는 함수 - fun moveCar()
- 이동 결과를 출력하는 함수 - fun printMoveStatus()
#### Podium.kt
- 결과에 최종 우승자를 표시하는 함수 - fun showWinners()

---

## 기능 요구 사항
#### 사용자의 입력
- 사용자는 각 자동차의 이름을 입력할 수 있다
- 사용자는 몇 번의 이동을 할 것인지 입력할 수 있다.
#### 자동차
- 자동차의 이름은 쉼표로 기준으로 구분한다.
- 이름은 5자 이하만 가능하다
#### 이동
- 전진 조건은 0에서 9 사이의 무작의 값을 구한 뒤 4 이상이어야 한다
- 전진은 한 칸 씩 가능하다
#### 최종 결과
- 게임 완료 후 우승자가 누군지 알려줘야 한다
- 우승자는 한 명이상일 수 있다
- 우승자가 여러 명일 때 쉼표를 이용하여 구분한다
#### 입력 오류
- 사용자가 몇 번의 이동을 할 것인지 입력할 때 1 이상의 정수가 아니라면 IllegalArgumentException을 발생 시킨다

---

## 오류 해결
- carsLocation 리스트의 위치 재정의
- 글자수가 5 이상인 자동차 오류 반환
17 changes: 16 additions & 1 deletion src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package racingcar
import camp.nextstep.edu.missionutils.Console

fun main() {
// TODO: 프로그램 구현
// 자동차 이름 입력 메시지
println("경주할 자동차 이름을 입력하세오.(이름은 쉼표(,) 기준으로 구분)")
// 자동차의 이름을 String으로 받는 carsNameInput
val carsNameInput = Console.readLine()
// ,로 구분된 자동차 리스트를 carsList에 저장
val carsList: List<String> = presetCars(carsNameInput)

// 시도 횟수 입력 메시지
println("시도할 횟수는 몇 회인가요?")
// 시도 입력을 받을 String형 trialInput
val trialInput = Console.readLine()
// 제대로 된 입력인지 체크하여 맞다면 trial에 저장
val trial = checkValidInput(trialInput)

repeatMove(trial, carsList)
}
52 changes: 52 additions & 0 deletions src/main/kotlin/racingcar/CarGame.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package racingcar
import camp.nextstep.edu.missionutils.Randoms

fun repeatMove(trial: Int, carsList: List<String>) {
// 자동차의 위치를 나타낼 바뀔수 있는 List인 carsLocation 생성
var carsLocation = MutableList(carsList.size) {0}

// trial만큼 자동차 이동 반복
repeat(trial) {
carsLocation = moveCar(carsList, carsLocation)
}
showWinners(carsList, carsLocation)
}

fun checkAdvance(): Boolean {
// 0에서 9까지의 정수 중 무작위 값 추출
val randomNumber = Randoms.pickNumberInRange(0,9)

// 무작위 값이 4이상 일 경우 true 반환
if (randomNumber >= 4)
return true

// 무작위 값이 4보다 작을 경우 false 반환
else
return false
}

fun moveCar(carsList: List<String>, carsLocation: MutableList<Int>): MutableList<Int> {
// 전체 carsLocation을 살펴보고
repeat(carsLocation.size) {
// checkAdvance를 통과하면 한 칸 전진
if (checkAdvance() == true)
carsLocation[it] += 1
}

// 현재 위치 표시
printMoveStatus(carsList, carsLocation)

return carsLocation
}

fun printMoveStatus(carsList: List<String>, carsLocation: List<Int>) {
// 모든 차를 출력
repeat(carsList.size) {
// '차 이름 : '
print(carsList[it] + " : ")
repeat(carsLocation[it]) {
print("-") // 이동 거리 출력
}
println() // 줄바꿈
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/racingcar/Podium.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racingcar

fun showWinners(carsList: List<String>, carsLocation: List<Int>) {
// 최종 우승자 메시지 출력
print("최종 우승자 : ")

var printed = false

// 최대 값 (우승자의 거리) 추출
val max = carsLocation.max()
repeat(carsList.size) {
// 최대 값과 거리가 같다면 출력
if (carsLocation[it] == max) {

// 앞에 이미 출력된게 있다면 ,를 넣는다
if (printed == true)
print(", ")

print(carsList[it])
printed = true
}
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/racingcar/Preset.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar

fun presetCars(carsNameInput: String): List<String> {
// 입력받은 자동차들의 이름을 ,에 따라 구분하여 List에 저장
val splitedCars = carsNameInput.split(",".toRegex()).toList()

// 만약 이 중 글자 수가 5 이상인 자동차가 있다면 오류 반환
repeat(splitedCars.size) {
if (splitedCars[it].length >= 5)
throw IllegalArgumentException()
}

// 이름에 따라 구분된 자동차가 들어가나 List 반환
return splitedCars
}

fun checkValidInput(trialInput: String): Int {
// 시도를 받을 정수형 trial
val trial: Int

// 정수형이 안되면 오류 반환
if (trialInput.toIntOrNull() == null)
throw IllegalArgumentException()

// 정수형이 된다면 Int로 변환
else
trial = trialInput.toInt()

// 이동 횟수가 1 이상의 정수가 아니라면 오류 반환
if (trial < 0)
throw IllegalArgumentException()

return trial
}