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

[자동차 경주] 김유정 미션 제출합니다. #207

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 구현할 기능
1. 경주할 자동차 이름을 입력받는다
2. 경주할 숫자를 입력받는다
3. 실행 결과를 출력하는 기능
- 각 차들은 0~9의 랜덤 값이 4 이상일 경우에만 전진
4. 게임 종료 후 우승자 출력 기능
6 changes: 4 additions & 2 deletions src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package racingcar

import racingcar.controller.GameController

fun main() {
// TODO: 프로그램 구현
}
GameController().start()
}
40 changes: 40 additions & 0 deletions src/main/kotlin/racingcar/controller/GameController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package racingcar.controller

import camp.nextstep.edu.missionutils.Console
import racingcar.global.InformationMessage
import racingcar.service.CarService
import racingcar.service.RaceService

class GameController {
var carService = CarService()
var raceService = RaceService()

fun start() {
createCarList()
createRaceNumber()
println()
playRace()
gameResult()
}

fun createCarList() {
println(InformationMessage.INPUT_CARS_NAME.message)
val input = Console.readLine().split(",")
carService.createCarList(input)
}

fun createRaceNumber() {
println(InformationMessage.ATTEMPT_NUMBER.message)
val input = Console.readLine()
raceService.createRaceNumber(input)
}

fun playRace() {
println(InformationMessage.EXECUTION_RESULT.message)
raceService.playRace()
}
fun gameResult() {
val winner = carService.getWinner()
println(InformationMessage.FINAL_WINNER.message + winner)
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/racingcar/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package racingcar.exception

enum class ErrorCode(val message: String) {
CHARACTERS_NUMBER_EXCEEDED("5글자 이하의 이름만 가능합니다."),
NOT_NUMBER("숫자만 입력해 주세요.")
}
17 changes: 17 additions & 0 deletions src/main/kotlin/racingcar/exception/InputException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package racingcar.exception

class InputException {
fun validateCarName(cars: List<String>) {
for(car in cars) {
require(car.length <= 5) {ErrorCode.CHARACTERS_NUMBER_EXCEEDED.message}
}
}

fun validateAttemptNumber(number: String): Int {
try {
return number.toInt()
} catch (e: Exception) {
throw IllegalArgumentException(ErrorCode.NOT_NUMBER.message)
}
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/racingcar/global/InformationMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racingcar.global

enum class InformationMessage(val message: String) {
INPUT_CARS_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"),
ATTEMPT_NUMBER("시도할 횟수는 몇 회인가요?"),
EXECUTION_RESULT("실행 결과"),
FINAL_WINNER("최종 우승자 : ")
}
10 changes: 10 additions & 0 deletions src/main/kotlin/racingcar/model/Car.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package racingcar.model

data class Car(
val name: String,
var location: Int
) {
fun move() {
this.location++
}
}
33 changes: 33 additions & 0 deletions src/main/kotlin/racingcar/service/CarService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package racingcar.service

import camp.nextstep.edu.missionutils.Randoms
import racingcar.exception.InputException
import racingcar.model.Car

class CarService {
companion object {
lateinit var cars: List<Car>
}

fun createCarList(carNames: List<String>) {
InputException().validateCarName(carNames)
Companion.cars = carNames.map { Car(it, 0) }
}

fun getCarList(): List<Car> {
return Companion.cars
}

fun moveCar() {
Companion.cars.forEach { car ->
val random = Randoms.pickNumberInRange(0, 9)
if(random >= 4) car.move()
}
}

fun getWinner(): String {
val largestNumber = Companion.cars.maxOf(Car::location)
return Companion.cars.filter { it.location == largestNumber }
.joinToString(separator = ", ") { car -> car.name }
}
}
26 changes: 26 additions & 0 deletions src/main/kotlin/racingcar/service/RaceService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package racingcar.service

import racingcar.exception.InputException
import racingcar.model.Car

class RaceService {
val carService = CarService()

var raceNumber: Int = 0
val locationExpression = "-"

fun createRaceNumber(raceNumber: String) {
this.raceNumber = InputException().validateAttemptNumber(raceNumber)
}

fun playRace() {
for(i in 0..<raceNumber) {
carService.moveCar()
val cars = carService.getCarList()
cars.forEach(fun(car: Car) {
println(String.format("%s : %s", car.name, locationExpression.repeat(car.location)))
})
println()
}
}
}