Files
kotlin-kvasnickiy/works/lab-06/scr/Main.kt
T
2026-11-24 17:34:55 +03:00

85 lines
2.3 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
fun main() {
showTask(description = "Print logs.")
val damage: Int = calcDamage(10, 3.5)
val status: String = checkStatus(123)
printBattleLog("Player #1", "Player #2", damage, status)
showTask(2, "Using functions with default parameters.")
useTrainerCard("Test name")
useTrainerCard("Test name", 100000)
useTrainerCard("Test name", 1000, 10)
showTask(3, "The function takes another function as a parameter.")
print(
String.format(
"Current hp: %d",
processTurn(100, getString("Enter the value for the variable 'damage'", "damage").toInt()) {hp -> hp + 100}
)
)
}
//============= Fun for tasks
//==========================
// Task 1
fun calcDamage(power: Int, multiplier: Double = 1.0): Int = (power * multiplier).toInt()
fun checkStatus(hp: Int): String = if (hp > 0) "В строю" else "Нокаут"
fun printBattleLog(attacker: String, defender: String, damage: Int, status: String) =
println(
String.format(
"\n|LOGGER:|==============================\n%-15s|%-15s|%-15s\n%-15s|%-15s|%-15s\nStatus: $status\n=======================================\n",
"attacker",
"defender",
"damage",
attacker,
defender,
damage
)
)
// Task 2
fun useTrainerCard(cardName: String, healAmount: Int = 20, drawCards: Int = 0) =
println(
String.format(
"\n|USING TRAINER|===========================\n%-15s|%-15s|%s\n%-15s|%15s|%10s\n==========================================\n",
"Name",
"Heal amount",
"Draw cards",
cardName,
healAmount,
drawCards
)
)
// Task 3
fun applyHealEffect(hp: Int): Int = hp + 100
fun processTurn(hp: Int, damage: Int, effect: (Int) -> Int): Int = effect(hp - damage)
//============= System fun
//==========================
fun br(iterations: Byte = 1){
for (i in 1..<iterations)
println()
}
fun showTask(number: Byte = 1, description: String = ""){
if (number != 1.toByte())
br(3)
println("=== Task № $number ===")
if (description != "")
println("'''$description'''")
}
fun getString(question: String, variable: String): String{
print("$question\n$variable: ")
val value = readlnOrNull() ?: "null"
br(2)
return value
}