-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0d9919
commit c25c491
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package racingcar | ||
|
||
import camp.nextstep.edu.missionutils.Randoms | ||
|
||
class Car(val name: String) { | ||
var position: Int = 0 | ||
private set | ||
|
||
fun move() { | ||
if (Randoms.pickNumberInRange(0, 9) >= 4) { | ||
position++ | ||
} | ||
} | ||
|
||
fun getPositionMarker(): String = "-".repeat(position) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package racingcar | ||
|
||
class RacingGame(private val cars: List<Car>, private val tryCount: Int) { | ||
|
||
fun start() { | ||
repeat(tryCount) { | ||
cars.forEach { it.move() } | ||
printCurrentPositions() | ||
} | ||
} | ||
|
||
private fun printCurrentPositions() { | ||
cars.forEach { car -> | ||
println("${car.name} : ${car.getPositionMarker()}") | ||
} | ||
println() | ||
} | ||
|
||
fun getWinners(): String { | ||
val maxPosition = cars.maxOf { it.position } | ||
return cars.filter { it.position == maxPosition } | ||
.joinToString(", ") { it.name } | ||
} | ||
} |