-
Notifications
You must be signed in to change notification settings - Fork 411
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
๐ 3๋จ๊ณ - ์๋์ฐจ ๊ฒฝ์ฃผ #907
base: ajy9844
Are you sure you want to change the base?
Changes from all commits
79471b0
bb6825f
645cad3
4a2852b
52ae783
b85e840
3690e9d
6c6bfb5
f79be88
0d178d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package racingcar | ||
|
||
class Car { | ||
var position: Int = DEFAULT_POSITION | ||
private set | ||
|
||
fun move(value: Int) { | ||
if (value >= FORWARD_NUMBER) position++ | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_POSITION = 0 | ||
private const val FORWARD_NUMBER = 4 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package racingcar | ||
|
||
class InputView { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ ๊ณตํ๋ ํจ์๊ฐ ์ ์ ๋ฉ์๋๋ฐ์ ์๋ ๊ฒฝ์ฐ์๋ class ๋์ object๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ ์ ์์ต๋๋ค. |
||
companion object { | ||
fun view(): Pair<Int, Int> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. view()๊ฐ ๋ฌด์จ ๊ฐ์ ๋ฐํํ๋์ง ์ ์ ์์๊น์? ์ฌ์ฉํ๋ ๊ณณ์ ์ฝ๋๋ฅผ ์ค์ด๋ ค๊ณ ํ๋ ๊ฒ ๋ณด๋ค ์คํ๋ ค ๊ฐ๋
์ฑ์ด ๋จ์ด์ง ์ ์์ต๋๋ค. InputView.getNumberOfCars()
InputView.getTryCount()
... |
||
println("์๋์ฐจ ๋์๋ ๋ช ๋์ธ๊ฐ์?") | ||
val numberOfCars = readLine()!!.toInt() | ||
println("์๋ํ ํ์๋ ๋ช ํ์ธ๊ฐ์?") | ||
val count = readLine()!!.toInt() | ||
println("์คํ ๊ฒฐ๊ณผ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "์คํ ๊ฒฐ๊ณผ"๋ ResultView์์ ์ถ๋ ฅํด์ผ ํ์ง ์์๊น์? |
||
|
||
return numberOfCars to count | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racingcar | ||
|
||
fun main() { | ||
val racingGame = RacingGame() | ||
|
||
val (numberOfCars, count) = InputView.view() | ||
racingGame.set(numberOfCars, count) | ||
|
||
for (i in 1..count) { | ||
racingGame.run() | ||
ResultView.view(racingGame.carList) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package racingcar | ||
|
||
import kotlin.random.Random | ||
|
||
class RacingGame { | ||
lateinit var carList: List<Car> | ||
private set | ||
lateinit var randomNumberList: Array<IntArray> | ||
private set | ||
private var times: Int = 0 | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ง์ฐ์ด๊ธฐํ๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ์ด๋ค ์ฅ์ ์ด ์์๊น์? ํ๋ฒ์ ๊ฒฝ์ฃผ์์ ํ๋ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด ๋ด๋๋ก ํ๋ฉด ์ด๋จ๊น์? object RacingGame {
fun race(cars: List<Car>, times: Int, ...) { ... }
} |
||
|
||
fun set(numberOfCars: Int, count: Int) { | ||
val carMList: MutableList<Car> = mutableListOf() | ||
for (i in 1..numberOfCars) { | ||
carMList.add(Car()) | ||
} | ||
carList = carMList.toList() | ||
randomNumberList = Array(count) { IntArray(numberOfCars) } | ||
} | ||
|
||
fun run() { | ||
for (i in carList.indices) { | ||
val randomNumber = random() | ||
carList[i].move(randomNumber) | ||
randomNumberList[times][i] = randomNumber | ||
} | ||
times++ | ||
} | ||
|
||
private fun random(): Int { | ||
return Random.nextInt(MAX_RANDOM_NUMBER) | ||
} | ||
|
||
companion object { | ||
private const val MAX_RANDOM_NUMBER: Int = 9 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racingcar | ||
|
||
class ResultView { | ||
companion object { | ||
fun view(cars: List<Car>) { | ||
for (car in cars) { | ||
if (car.position == 0) println("x") | ||
else println("-".repeat(car.position)) | ||
} | ||
println() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package racingcar.feedback | ||
|
||
// ์์๋ฅผ ์ต์์ ์์ค๊ณผ ๋๋ฐ ๊ฐ์ฒด ์ค ๋ฌด์์ผ๋ก ์ ์ํ๋๋๋ ์์๋ฅผ ์ฌ์ฉํ๋ ํด๋์ค์ ๋ฒ์์ ๋ฐ๋ผ ๋ค๋ฅด๋ค! | ||
// ๋ง์ฝ์ ์์๋ฅผ ์ต์์ ์์ค์์ ์ ์ธํ๋ฉด ์ฝํ๋ฆฐ์ CarKt ์ด๋ผ๋ ํด๋์ค ํ์ผ์ ์์ฑํ์ฌ ๋ชจ์๋๋ค. | ||
// private const val ... | ||
|
||
object Numbers { | ||
// ์ต์์ ์์ค์ผ๋ก ์ ์ธํ์ ๋์ ๋จ์ ์ ๊ฐ๋ฐ์๊ฐ ์ง์ ์์ ์ด๋ฆ์ ์์์ผ ํ๋ค๋ ๊ฒ์ด๋ค. | ||
// ๊ทธ๋์ object ์์ ์์๋ฅผ ์ ์ธํด์ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค. | ||
const val MAXIMUM_NAME_LENGTH: Int = 5 | ||
} | ||
|
||
class Car(val name: String, position: Int = DEFAULT_POSITION) { | ||
var position: Int = position | ||
private set // ์ปค์คํ ๊ฒํฐ์ ์ธํฐ๋ ์์ฑ์์์ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ค! | ||
/* | ||
private var _position: Int = position | ||
val position: Int | ||
get() = _position | ||
*/ | ||
|
||
init { | ||
require(name.length <= Numbers.MAXIMUM_NAME_LENGTH) { "์๋์ฐจ ์ด๋ฆ์ 5๊ธ์๋ฅผ ๋๊ธธ ์ ์์ต๋๋ค." } | ||
} | ||
|
||
fun move() { | ||
position++ | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_POSITION: Int = 0 | ||
|
||
@JvmField | ||
val DEFAULT_CAR: Car = Car("") | ||
fun of(name: String): Car = Car(name) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@file:JvmName("NumberUtils") | ||
|
||
package racingcar.feedback | ||
|
||
const val FORWARD_NUMBER: Int = 4 | ||
|
||
fun calculate(text: String?): Int { | ||
require(!text.isNullOrBlank()) { "์ ๋ ฅ๊ฐ์ด null ๋๋ ๋น ๋ฌธ์์ด์ผ ์ ์์ต๋๋ค." } | ||
run(!text.isNullOrBlank()) { "์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค." } // ์ปค์คํ ์์ธ์ ๋ํด์ ์ง์ ๋ง๋ค์ด ์จ๋ณด์! | ||
// ... | ||
return 0 | ||
} | ||
|
||
fun run(value: Boolean, lazyMessage: () -> Any) { | ||
if (!value) { | ||
val message = lazyMessage() | ||
throw RuntimeException(message.toString()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package racingcar | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
class CarTest { | ||
@ParameterizedTest | ||
@ValueSource(ints = [4, 5, 6]) | ||
fun `์๋์ฐจ๋ ๋ฌด์์ ๊ฐ์ด 4 ์ด์์ผ ๊ฒฝ์ฐ ์ ์งํ๋ค`(input: Int) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์๋์ฐจ์ ๊ฒฝ์ฐ ์ ๋ ฅ๋ฐ์ ๊ฐ์ด "๋ฌด์์" ๋ผ๋ ์ฌ์ค์ ๊ด์ฌ์ด ์์๊น์? |
||
// given | ||
val car = Car() | ||
|
||
// when | ||
car.move(input) | ||
|
||
// then | ||
assertThat(car.position).isEqualTo(1) | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = [1, 2, 3]) | ||
fun `์๋์ฐจ๋ ๋ฌด์์ ๊ฐ์ด 4 ๋ฏธ๋ง์ผ ๊ฒฝ์ฐ ์ ์งํ๋ค`(input: Int) { | ||
// given | ||
val car = Car() | ||
|
||
// when | ||
car.move(input) | ||
|
||
// then | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package racingcar | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class RacingGameTest { | ||
@Test | ||
fun `๊ฒฝ์ฃผ ๊ฒ์์ ์ ๋ ฅ๊ฐ๋งํผ์ ์๋์ฐจ๋ฅผ ์์ฑํ๋ค`() { | ||
// given | ||
val racingGame = RacingGame() | ||
|
||
// when | ||
racingGame.set(3, 5) | ||
|
||
// then | ||
assertThat(racingGame.carList.size).isEqualTo(3) | ||
} | ||
|
||
@Test | ||
fun `๊ฒฝ์ฃผ ๊ฒ์์ ๋ชจ๋ ์๋์ฐจ์ ๋ํด ๋ฌด์์ ๊ฐ์ ๊ตฌํ์ฌ ์ด๋์ํจ๋ค`() { | ||
// given | ||
val racingGame = RacingGame() | ||
racingGame.set(3, 1) | ||
|
||
// when | ||
racingGame.run() | ||
|
||
// then | ||
val cars = racingGame.carList | ||
val numbers = racingGame.randomNumberList[0] | ||
|
||
for (i in cars.indices) { | ||
if (numbers[i] >= 4) assertThat(cars[i].position).isEqualTo(1) | ||
else assertThat(cars[i].position).isEqualTo(0) | ||
} | ||
} | ||
Comment on lines
+19
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํ
์คํธ ์ฝ๋์์๋ ๋ฌด์์๋ผ๋ ๋ถํ์ค์ฑ์ ํ
์คํธ ํ๋ ๊ฒ์ ์๋นํ ์ด๋ ต์ต๋๋ค. ์ฐจ๋์ด ์ด๋ ๊ฐ๋ฅํ์ง์ ๋ํ ์กฐ๊ฑด์ ์ ํ๊ณ , ๊ฐ๊ฐ ๊ตฌํํ๋๋ก ๋ง๋ค์ด์ ์๋์ฐจ์ ์ด๋ ์กฐ๊ฑด์ ํ ์คํธ์์ ์ ์ด๊ฐ ๊ฐ๋ฅํ๋๋ก ๋ง๋ค์ด ๋ณด์ธ์. // Hint
interface CarMoveable {
fun getMovementValue(): Int
}
class RandomCarMoveable: CarMoveable {
override fun getMovementValue(): Int { ... }
}
object AlwaysForwardCarMoveable: CarMoveable {
override fun getMovementValue(): Int { return 4 }
}
...
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package racingcar.feedback | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatExceptionOfType | ||
import org.junit.jupiter.api.Test | ||
|
||
class CarTest { | ||
@Test | ||
fun `์๋์ฐจ๋ฅผ ์์ฑํ๋ค`() { | ||
val car = Car("jason", 0) | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `๊ธฐ๋ณธ ์ธ์๋ฅผ ํ์ฉํ์ฌ ์๋์ฐจ๋ฅผ ์์ฑํ๋ค`() { | ||
val car = Car("jason") | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `์๋์ฐจ์ ์ด๋ฆ์ด 5๊ธ์๋ฅผ ์ด๊ณผํ๋ฉด ์ค๋ฅ๋ฅผ ๋ฐ์์ํจ๋ค`() { | ||
assertThatExceptionOfType(IllegalArgumentException::class.java).isThrownBy { | ||
Car("๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ") | ||
} | ||
} | ||
|
||
@Test | ||
fun `ํฉํ ๋ฆฌ ํจ์๋ฅผ ์ด์ฉํ์ฌ ์๋์ฐจ๋ฅผ ์์ฑํ๋ค`() { | ||
val car = Car.of("jason") | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `์ ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ค`() { | ||
val car = Car.DEFAULT_CAR | ||
assertThat(car.name).isEqualTo("") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racingcar.feedback; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class NumberUtilsTest { | ||
@Test | ||
void ์ฝํ๋ฆฐ์์() { | ||
assertThat(NumberUtils.FORWARD_NUMBER).isEqualTo(4); | ||
Car defaultCar = Car.DEFAULT_CAR; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฐจ๋์ ์ด๊ธฐ ์์น์ ๋ํ ํ ์คํธ๋ ์์ฑ ํด ๋ณด์ธ์.
์ด ๊ฒฝ์ฐ ์์ฑ์๋ฅผ ํตํด ์ฐจ๋์ ์ด๊ธฐ ์์น๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ํ๋ฉด ํ ์คํธ๋ฅผ ๋ณด๋ค ํจ์จ์ ์ผ๋ก ์์ฑํ ์ ์์ต๋๋ค.