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

[자동차 경주] 장민호 미션 제출합니다. #224

Open
wants to merge 2 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
14 changes: 14 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 기능 목록
## 경기 시작 전
- 쉼표(,)가 포함된 문자열에서 자동차 이름을 추출
- 이동 횟수를 입력
## 경기 중
- 무작위 값(0~9)을 추출하여 4와 비교
- 자동차의 현 위치 출력
## 경기 후
- 우승자 출력
- 우승자가 2명 이상일 경우 (,) 구분 출력
## Test코드
- 자동차 이름 이상값 test (ApplicationTest)
- 시도 횟수 이상값 test
- 공동 우승 test
5 changes: 4 additions & 1 deletion src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package racingcar

import racingcar.controller.RacingCarController

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

import camp.nextstep.edu.missionutils.Console
import camp.nextstep.edu.missionutils.Randoms
import racingcar.model.RacingCar
import racingcar.view.InputView
import racingcar.view.OutputView


class RacingCarController {
private val inputView = InputView()
private val outputView = OutputView()

fun play() {
inputView.submitCarNames()
val input = Console.readLine()
val carList = makeCarList(input)
outputView.startGameMessage()
playWithCarList(carList, inputView.askTimes(), outputView)
val winnerList = findWinner(carList)
outputView.noticeWinner(winnerList)
}

private fun makeCarList(input: String?): MutableList<RacingCar> {
val cars = input?.split(',')
if (cars != null) {
regexCheck(cars)
} else {
throw IllegalArgumentException()
}
val carList = mutableListOf<RacingCar>()
for (car in cars) {
carList.add(RacingCar(car, 0))
}
return carList
}

private fun regexCheck(cars: List<String>) {
for (car in cars) {
if (!car.matches(Regex("\\w{1,5}"))) {
throw IllegalArgumentException()
}
}
}

private fun moveForwardOrNot(carList: MutableList<RacingCar>) {
for (car in carList) {
val randomNumber = Randoms.pickNumberInRange(0, 9)
if (randomNumber >= 4) {
car.moveForward()
}
}
}

private fun playWithCarList(carList: MutableList<RacingCar>, times: Int, outputView: OutputView) {
for (i in 0 until times) {
moveForwardOrNot(carList)
outputView.printCurrentPlace(carList)
}
}

private fun findWinner(carList: MutableList<RacingCar>): List<RacingCar> {
val maxForward = carList.maxByOrNull { it.getForward() }?.getForward()
return carList.filter { it.getForward() == maxForward }
}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/racingcar/model/RacingCar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package racingcar.model

class RacingCar(private val carName: String, private var forward: Int = 0) {
fun moveForward() {
forward++
}

fun getCarName(): String {
return carName
}

fun getForward(): Int {
return forward
}

}
20 changes: 20 additions & 0 deletions src/main/kotlin/racingcar/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package racingcar.view

import camp.nextstep.edu.missionutils.Console

class InputView {
fun submitCarNames() = println(SUBMIT_CAR_NAMES_MESSAGE)
fun askTimes(): Int {
println(ASK_TIMES_MESSAGE)
val timesInput = Console.readLine().toInt()
if (timesInput < 1) {
throw IllegalArgumentException()
}
return timesInput
}

companion object {
const val SUBMIT_CAR_NAMES_MESSAGE = "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"
const val ASK_TIMES_MESSAGE = "시도할 횟수는 몇 회인가요?"
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/racingcar/view/OutputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package racingcar.view

import racingcar.model.RacingCar

class OutputView {
fun noticeWinner(winnerList: List<RacingCar>) {
println(WINNER_MESSAGE + winnerList.joinToString(", ") { it.getCarName() })
}

fun printCurrentPlace(carList: MutableList<RacingCar>) {
for (car in carList) {
val currentPlace = "-".repeat(car.getForward())
println(car.getCarName() + " : " + currentPlace)
}
println("\n")
}

fun startGameMessage() = println(START_GAME_MESSAGE)

companion object {
const val WINNER_MESSAGE = "최종 우승자 : "
const val START_GAME_MESSAGE = "실행 결과"
}
}
46 changes: 46 additions & 0 deletions src/test/kotlin/racingcar/RacingCarTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package racingcar

import camp.nextstep.edu.missionutils.test.Assertions
import camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest
import camp.nextstep.edu.missionutils.test.NsTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class RacingCarTest : NsTest() {
@Test
fun `시도 횟수 이상값 test`() {
Assertions.assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("bon,jovi", "가") }
}
Assertions.assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("bon,jovi", "hello") }
}
Assertions.assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("bon,jovi", "-1") }
}
Assertions.assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("bon,jovi", "0") }
}
}

@Test
fun `공동 우승`() {
assertRandomNumberInRangeTest(
{
run("bon,jovi", "3")
assertThat(output()).contains("최종 우승자 : bon, jovi")
},
5
)
}

public override fun runMain() {
main()
}

companion object {
private const val MOVING_FORWARD = 4
private const val STOP = 3
}
}