From 5d97cec891fd2c38af0a9ac8d8e16970b4c49d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-H9O9ENN=5C=EC=9D=B4=EC=83=81=EC=A7=80?= Date: Wed, 1 Nov 2023 23:29:20 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb..cca583f40 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,40 @@ +๐Ÿš€ ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ + + +์ดˆ๊ฐ„๋‹จ ์ž๋™์ฐจ ๊ฒฝ์ฃผ ๊ฒŒ์ž„์„ ๊ตฌํ˜„ + +์ฃผ์–ด์ง„ ํšŸ์ˆ˜ ๋™์•ˆ n๋Œ€์˜ ์ž๋™์ฐจ๋Š” ์ „์ง„ ๋˜๋Š” ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค. +๊ฐ ์ž๋™์ฐจ์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค. +val carNames +์ „์ง„ํ•˜๋Š” ์ž๋™์ฐจ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์ž๋™์ฐจ ์ด๋ฆ„์„ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค. +print("$name : ") +println("-".repeat(position)) +์ž๋™์ฐจ ์ด๋ฆ„์€ ์‰ผํ‘œ(,)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉฐ ์ด๋ฆ„์€ 5์ž ์ดํ•˜๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. +isCarNamesValid +์‚ฌ์šฉ์ž๋Š” ๋ช‡ ๋ฒˆ์˜ ์ด๋™์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. +val tryCount = Console.readLine()!!.toInt() +์ „์ง„ํ•˜๋Š” ์กฐ๊ฑด์€ 0์—์„œ 9 ์‚ฌ์ด์—์„œ ๋ฌด์ž‘์œ„ ๊ฐ’์„ ๊ตฌํ•œ ํ›„ ๋ฌด์ž‘์œ„ ๊ฐ’์ด 4 ์ด์ƒ์ผ ๊ฒฝ์šฐ์ด๋‹ค. +cars.forEach { it.move(Randoms.pickNumberInRange(0, 9)) } +์ž๋™์ฐจ ๊ฒฝ์ฃผ ๊ฒŒ์ž„์„ ์™„๋ฃŒํ•œ ํ›„ ๋ˆ„๊ฐ€ ์šฐ์Šนํ–ˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค€๋‹ค. +์šฐ์Šน์ž๋Š” ํ•œ ๋ช… ์ด์ƒ์ผ ์ˆ˜ ์žˆ๋‹ค. +์šฐ์Šน์ž๊ฐ€ ์—ฌ๋Ÿฌ ๋ช…์ผ ๊ฒฝ์šฐ ์‰ผํ‘œ(,)๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ํ•œ๋‹ค. +val winners = cars.filter { it.position == cars.maxOf { car -> car.position } } +์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ IllegalArgumentException์„ ๋ฐœ์ƒ์‹œํ‚จ ํ›„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•œ๋‹ค. +throw IllegalArgumentException + +์ถ”๊ฐ€๋œ ์š”๊ตฌ ์‚ฌํ•ญ + +indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ 3์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. +2๊นŒ์ง€๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด while๋ฌธ ์•ˆ์— if๋ฌธ์ด ์žˆ์œผ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์ด๋‹ค. +ํžŒํŠธ: indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ ์ค„์ด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. +ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ํ•˜๋„๋ก ์ตœ๋Œ€ํ•œ ์ž‘๊ฒŒ ๋งŒ๋“ค์–ด๋ผ. +JUnit 5์™€ AssertJ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณธ์ธ์ด ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก์ด ์ •์ƒ ๋™์ž‘ํ•จ์„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ํ™•์ธํ•œ๋‹ค. +ํ…Œ์ŠคํŠธ ๋„๊ตฌ ์‚ฌ์šฉ๋ฒ•์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด test/kotlin/study๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. + + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ + +camp.nextstep.edu.missionutils์—์„œ ์ œ๊ณตํ•˜๋Š” Randoms ๋ฐ Console API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. +Random ๊ฐ’ ์ถ”์ถœ์€ camp.nextstep.edu.missionutils.Randoms์˜ pickNumberInRange()๋ฅผ ํ™œ์šฉํ•œ๋‹ค. +์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ camp.nextstep.edu.missionutils.Console์˜ readLine()์„ ํ™œ์šฉํ•œ๋‹ค. \ No newline at end of file From a62e596c13b7dc92e09c7e77ae3ebbb3ab385537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-H9O9ENN=5C=EC=9D=B4=EC=83=81=EC=A7=80?= Date: Wed, 1 Nov 2023 23:30:24 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[=EB=AF=B8=EC=85=98=20-=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=EC=B0=A8=20=EA=B2=BD=EC=A3=BC]=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/racingcar/Application.kt | 44 +++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/racingcar/Application.kt b/src/main/kotlin/racingcar/Application.kt index 0d8f3a79d..9abc3ee6a 100644 --- a/src/main/kotlin/racingcar/Application.kt +++ b/src/main/kotlin/racingcar/Application.kt @@ -1,5 +1,47 @@ package racingcar +import camp.nextstep.edu.missionutils.Console +import camp.nextstep.edu.missionutils.Randoms + fun main() { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + println("๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)") + val carNames = Console.readLine()!!.split(",") + if (!isCarNamesValid(carNames)) { + throw IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ 5์ž ์ดํ•˜๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.") + } + + println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ์ธ๊ฐ€์š”?") + val tryCount = Console.readLine()!!.toInt() + if (tryCount <= 0) { + throw IllegalArgumentException("์‹œ๋„ ํšŸ์ˆ˜๋Š” 1 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.") + } + + val cars = carNames.map { Car(it) } + + println("\n์‹คํ–‰ ๊ฒฐ๊ณผ") + repeat(tryCount) { + cars.forEach { it.move(Randoms.pickNumberInRange(0, 9)) } + println() + } + + val winners = cars.filter { it.position == cars.maxOf { car -> car.position } } + println("์ตœ์ข… ์šฐ์Šน์ž : ${winners.joinToString(", ") { it.name }}") +} + +class Car(val name: String) { + var position = 0 + private set + + fun move(randomNumber: Int) { + if (randomNumber >= 4) { + position++ + } + print("$name : ") + println("-".repeat(position)) + + } +} + +fun isCarNamesValid(carNames: List): Boolean { + return carNames.all { it.length <= 5 } } From bc74c9fb1ed3a4413d53ab211b60e127a3002a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-H9O9ENN=5C=EC=9D=B4=EC=83=81=EC=A7=80?= Date: Wed, 1 Nov 2023 23:55:10 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/racingcar/ApplicationTest.kt | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/racingcar/ApplicationTest.kt b/src/test/kotlin/racingcar/ApplicationTest.kt index 2cb36835c..a8d639542 100644 --- a/src/test/kotlin/racingcar/ApplicationTest.kt +++ b/src/test/kotlin/racingcar/ApplicationTest.kt @@ -6,8 +6,25 @@ 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 +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.AfterEach +import java.io.ByteArrayOutputStream +import java.io.PrintStream class ApplicationTest : NsTest() { + private val outputBuffer = ByteArrayOutputStream() + private val originalOutput = System.out + + @BeforeEach + fun setUpOutput() { + System.setOut(PrintStream(outputBuffer)) + } + + @AfterEach + fun restoreOutput() { + System.setOut(originalOutput) + } + @Test fun `์ „์ง„ ์ •์ง€`() { assertRandomNumberInRangeTest( @@ -22,7 +39,8 @@ class ApplicationTest : NsTest() { @Test fun `์ด๋ฆ„์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ`() { assertSimpleTest { - assertThrows { runException("pobi,javaji", "1") } + val exception = assertThrows { runException("pobi,javaji", "1") } + assertThat(exception.message).isEqualTo("์ž๋™์ฐจ ์ด๋ฆ„์€ 5์ž ์ดํ•˜๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.") } }