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

[자동차 게임] 임가희 미션 제출합니다. #230

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**
!**/src/test/**
local.properties

### macOS ###
.DS_Store
Expand Down
33 changes: 33 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 자동차 경주 게임

## 기능 목록

- [x] 경주 할 자동차 이름을 입력한다. Input#inputCarName()
- [x] 자동차 이름은 쉼표(,)를 기준으로 구분한다.
- [x] 잘못된 값을 입력한 경우 IllegalArgumentException을 발생시킨다. Input#nameCheck()
- [x] 이름은 5자 이하만 가능하다.
- [x] 값을 입력해야 한다.
- [x] 시도할 횟수를 입력한다. Input#inputGameNumber()
- [x] 잘못된 값을 입력한 경우 IllegalArgumentException을 발생시킨다. Input#gameNumberCheck()
- [] 값을 입력해야 한다.
- [] 숫자를 입력해야 한다.
- [x] 1이상을 입력해야 한다.
- [x] 0에서 9 사이에서 무작위 값을 생성한다. RandomNumber#createRandomNumber()
- [x] 무작위 값이 4 이상일 경우 전진한다. Move#moveCar()
- [x] 각각의 자동차의 전진한 횟수를 비교한다. Moven#compareMoveNumber()
- [x] 가장 많이 전진한 자동차가 우승한다.
- [x] 우승자는 한명 이상일 수 있다.
- [x] 우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다.

## 기능 요구 사항

초간단 자동차 경주 게임을 구현한다.

주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다.
각 자동차에 이름을 부여할 수 있다. 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다.
자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다.
사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다.
전진하는 조건은 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상일 경우이다.
자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다.
우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다.
사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다.
24 changes: 23 additions & 1 deletion src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package racingcar

fun main() {
// TODO: 프로그램 구현
println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)")
val user = Input()
val carName = user.inputCarName()

println("시도할 횟수는 몇 회인가요?")
val count = user.inputGameNumber()

println("실행 결과")
var car: MutableList<Car> = mutableListOf()
setCar(carName, car)

val move = Move(car)
repeat(count) {
val carMove = move.moveCar()
}
val result = move.compareMoveNumber()

}

fun setCar(carName: MutableList<String>, car: MutableList<Car>) {
for (i in carName) {
car.add(Car(name = i.toString()))
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/racingcar/Car.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racingcar

class Car(var name: String) {
var distance = 0
fun forward() {
distance++
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/racingcar/Input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar

import camp.nextstep.edu.missionutils.Console

class Input {
fun inputCarName(): MutableList<String> {
val carName: MutableList<String> = Console.readLine().split(",").toMutableList()
nameCheck(carName)
return carName
}

fun inputGameNumber(): Int {
val gameNumber = Console.readLine().toInt()
gameNumberCheck(gameNumber)
return gameNumber
}

private fun nameCheck(carName: List<String>) {
for (carName in carName) {
if (carName.length > 5) {
throw IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.")
}
if (carName.isEmpty()) {
throw IllegalArgumentException("자동차 이름을 입력해 주세요.")
}
}
}

private fun gameNumberCheck(gameNumber: Int) {
if (gameNumber < 1) {
throw IllegalArgumentException("1 이상의 숫자를 입력해 주세요")
}
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/racingcar/Move.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package racingcar

class Move(private val cars: MutableList<Car>) {

val randomNumber = RandomNumber()

fun moveCar() {
for (car in this.cars) {
val randomNum = randomNumber.createRandomNumber()
if (randomNum >= 4) {
car.forward()
}
println("${car.name} : ${"-".repeat(car.distance)}")
}
println()
}

fun compareMoveNumber() {
val maxDistance = cars.maxOf { it.distance }
println(maxDistance)
val sameDistance = cars.filter { maxDistance == it.distance }
val winner = sameDistance.joinToString(",") { it.name }
println("최종 우승자 : $winner")
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/racingcar/RandomNumber.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package racingcar

import camp.nextstep.edu.missionutils.Randoms

class RandomNumber {
fun createRandomNumber(): Int {
return Randoms.pickNumberInRange(0, 9)
}
}