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

[구민주] 프리코스 미션 제출합니다. #5551

Open
wants to merge 3 commits into
base: master
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.7.21'
}

version '1.0'
Expand All @@ -11,6 +12,7 @@ repositories {
dependencies {
testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

java {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import kotlin.random.Random

data class Car(val name: String="", var pos: Int=0)

fun main(){
//자동차 이름 입력
val cars: Array<Car> = inputCarName()

//횟수 입력
val trialCnt = inputTrialCnt()

race(cars,trialCnt)

printWinnerCar(cars)
}

fun inputCarName(): Array<Car>{ //자동차 명 입력
print("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n")
val carNames = readLine()?.split(",") ?: throw IllegalArgumentException("[ERROR]: 자동차 이름을 입력해 주세요")
return carNames.map{ Car(it,0)}.toTypedArray()
}

fun inputTrialCnt(): Int{ //시도 횟수 입력
print("시도할 횟수는 몇회인가요?\n")
return readLine()?.toInt() ?: throw IllegalArgumentException("[ERROR]: 횟수를 입력해주세요")
}

fun calculateCarMovement(car: Car){ //전진 판단 계산
//랜덤 값 생성 후 4이상이면 전진
if(4 <= Random.nextInt(10))
car.pos++
}

fun race(cars: Array<Car>, trialCnt: Int){ //경주
println("실행 결과")
repeat(trialCnt){
cars.forEach { car->
calculateCarMovement(car)

println("${car.name}, ${"-".repeat(car.pos)}")
}
println()
}
println()
}

fun printWinnerCar(cars: Array<Car>){
val maxPos = cars.maxByOrNull { it.pos }?.pos
val winners = cars.filter {it.pos == maxPos}.joinToString(", ") { it.name }

println("최종 우승자 : ${winners}")
}