Skip to content
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
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
}
rootProject.name = 'java-baseball'

include 'src:main:Kotlin'
21 changes: 21 additions & 0 deletions src/main/Kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
kotlin("jvm") version "2.1.20"
}

group = "camp.nextstep.edu"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions src/main/Kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package camp.nextstep.edu

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
fun main() {
val name = "Kotlin"
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
println("Hello, " + name + "!")

for (i in 1..5) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
println("i = $i")
}
}
95 changes: 95 additions & 0 deletions src/main/Kotlin/src/main/kotlin/racinggame/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package racinggame

fun main() {
Application().start()
}

class Application {
fun start() {
val carNames = InputView.readCarNames()
val tryCount = InputView.readTryCount()

val cars = Cars(carNames.map { Car(it) })
val game = RacingGame(cars)

println("\n실행 결과")
repeat(tryCount) {
game.race()
ResultView.printRaceResult(cars)
}

ResultView.printWinners(cars.findWinners())
}
}

object InputView {
fun readCarNames(): List<String> {
while (true) {
try {
println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)")
val input = readln()
return input.split(",").map { it.trim() }.also {
require(it.all { name -> name.length in 1..5 }) { "[ERROR] 이름은 1자 이상 5자 이하만 가능합니다." }
}
} catch (e: IllegalArgumentException) {
println(e.message)
}
}
}

fun readTryCount(): Int {
while (true) {
try {
println("시도할 회수는 몇회인가요?")
val input = readln()
return input.toInt().also {
require(it > 0) { "[ERROR]-회수는 1 이상이어야 합니다." }
}
} catch (e: Exception) {
println("[ERROR] 올바른 숫자를 입력해주세요.")
}
}
}
}

object ResultView {
fun printRaceResult(cars: Cars) {
cars.all().forEach {
println("${it.name} : ${"-".repeat(it.position)}")
}
println()
}

fun printWinners(winners: List<Car>) {
println("최종 우승자 : ${winners.joinToString(", ") { it.name }}")
}
}

class RacingGame(private val cars: Cars) {
fun race() {
cars.moveAll()
}
}

class Cars(private val cars: List<Car>) {
fun moveAll() {
cars.forEach { it.move(RandomGenerator.generate()) }
}

fun all(): List<Car> = cars

fun findWinners(): List<Car> {
val max = cars.maxOf { it.position }
return cars.filter { it.position == max }
}
}

class Car(val name: String, var position: Int = 0) {
fun move(condition: Int) {
if (condition >= 4) position++
}
}

object RandomGenerator {
fun generate(): Int = (0..9).random()
}