import kotlin.ranges.contains data class Pokemon( val name: String, var hp: Short, val maxHp: Short = 200, var level: Byte = 1 ){ init{ require(name.isNotBlank()) {"Name cannot be is empty."} require(maxHp in SYS_MIN_HP_VALUE .. SYS_MAX_HP_VALUE) {"Max HP cannot be less than $SYS_MIN_HP_VALUE and greater than $SYS_MAX_HP_VALUE."} require(hp in SYS_MIN_HP_VALUE..maxHp) {"Hp cannot be less than $SYS_MIN_HP_VALUE and greater than $maxHp"} require(level in SYS_MIN_LEVEL_VALUE .. SYS_MAX_LEVEL_VALUE) { "Level cannot be less than $SYS_MAX_LEVEL_VALUE. and greater than $SYS_MAX_LEVEL_VALUE"} } companion object{ const val SYS_MIN_HP_VALUE: Short = 0 const val SYS_MAX_HP_VALUE: Short = 999 const val SYS_MAX_LEVEL_VALUE: Short = 999 const val SYS_MIN_LEVEL_VALUE: Short = 1 fun wild(name: String): Pokemon = Pokemon(name, hp = 30, maxHp = 30, level = 3) } fun isAlive(): Boolean = hp > 0 fun takeDamage(amount: Short): Short { amount.coerceAtLeast(0) val oldHP: Short = hp hp = (hp - amount).coerceAtLeast(0).toShort() return (oldHP - hp).toShort() } fun heal(amount: Short): Short { amount.coerceAtLeast(0) val oldHP: Short = hp hp = (hp + amount).coerceAtMost(maxHp.toInt()).toShort() return (hp - oldHP).toShort() } } data class TotalPokemon( val name: String, val hp: Short ){ init{ require(name.isNotBlank()) {"Name cannot be is empty."} require(hp in SYS_MIN_HP_VALUE .. SYS_MAX_HP_VALUE) {"Max HP cannot be less than $SYS_MIN_HP_VALUE and greater than $SYS_MAX_HP_VALUE."} //require(level in SYS_MIN_LEVEL_VALUE .. SYS_MAX_LEVEL_VALUE) { "Level cannot be less than $SYS_MAX_LEVEL_VALUE. and greater than $SYS_MAX_LEVEL_VALUE"} } companion object{ const val SYS_MIN_HP_VALUE: Short = 0 const val SYS_MAX_HP_VALUE: Short = 999 const val SYS_MAX_LEVEL_VALUE: Short = 999 const val SYS_MIN_LEVEL_VALUE: Short = 1 fun createPokemonOrNull(name: String?, hpRaw: String?): TotalPokemon? { if(name == null || hpRaw.isNullOrBlank() || hpRaw.toShortOrNull() == null) return null return TotalPokemon(name, hpRaw.toShort()) } fun wild(name: String): Pokemon = Pokemon(name, hp = 30, maxHp = 30, level = 3) } } fun main() { val pikachu = Pokemon("Pikachu", 100) showTask() println(pikachu); br() pikachu.takeDamage(30) println("$pikachu\nStatus: ${pikachu.isAlive()}"); br() pikachu.takeDamage(80) println("$pikachu\nStatus: ${pikachu.isAlive()}") showTask(2, " // Поле у экземпляра нельзя изменть, так как оно является read-only (val).\n" + " // Переменную pikachu - так как она является неизменяемой (val)\n" + "\n" + " // team - переменная, хранящая ссылку на область в памяти, где хранятся данные. В этой области можно добавлять и изменять элементы.\n" + " // Нельзя перезаписывать саму ссылку.") pikachu.hp = 50 showTask(3, "Create the objects.") Pokemon("Pikachu", 100) // Pokemon("", 100) // Pokemon("Mewtwo", 5000) showTask(4) println(""" Take damage (+20): ${Pokemon("Bulbasaur", hp = 30, maxHp = 100).takeDamage(20)}! Heal hp (+100): ${Pokemon("Bulbasaur", hp = 30, maxHp = 100).heal(0)}! """.trimIndent()) showTask(5) val pokemonList:MutableList = mutableListOf( Pokemon("Name1", 100), Pokemon("Name2", 32), Pokemon("Name3", 7), Pokemon("Name4", 0), Pokemon("Name5", 30) ) println(""" Count: ${pokemonList.count()} Is alive: ${pokemonList.joinToString(separator = ", ") { it.name }} (${pokemonList.count { it.isAlive() }}) Total HP: ${pokemonList.map { it.hp }.sum() } Max hp: ${pokemonList.lastOrNull() {it.hp == it.maxHp} ?.name ?: "nobody"} Dead: ${pokemonList.firstOrNull() {!it.isAlive()}?.name ?: "nobody"} AVG hp: ${pokemonList.map { it.hp }.average().takeIf {!it.isNaN()} ?: "0.0"} """.trimIndent()) pokemonList.filter {it.isAlive()}.forEach {it.takeDamage(20)} println(pokemonList.joinToString(separator="\n")); br() val emptyList:MutableList = mutableListOf() println(emptyList.maxByOrNull { it.hp }?.hp ?: "List Is empty") showTask(6) val pokemon1: Pokemon = Pokemon("Name1", 100) val pokemon2: Pokemon = Pokemon("Name1", 100) val pokemon3: Pokemon = pokemon1.copy(hp = 3) println(""" pokemon1 == pokemon2 : ${pokemon1 == pokemon2} pokemon1 HASH: ${pokemon1.hashCode()} pokemon2 HASH: ${pokemon2.hashCode()} Copy: ${pokemon3} """.trimIndent()) showTask(7, "Create the objects.") // val a = Pokemon("Pikachu") // Не введено обязательное поле val b = Pokemon("Machamp", hp = 80, maxHp = 120) // Pokemon("Machamp", hp = 80, maxHp = 120, level = 1) // val c = Pokemon("Mewtwo", level = 70) // Не введено обязательное поле val d = Pokemon.wild("Pidgey") // wild showTask(8) } //============= 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.isNotEmpty()) println("'''$description'''") } fun getString(question: String, variable: String, default: String = "null", useStrict: Boolean = true): String{ print("$question\n$variable: ") var str: String = readlnOrNull() ?: default if (useStrict) if (str.isEmpty()) str = default br(2) return str } fun showInfo(cardRaw: String, damageRaw: Int, bonusRaw: Float, finalDamage: Float) = print( """ ====================================== | INFO | ====================================== """.trimIndent() + String.format( "%-16s%s\n%-16s%d\n%-16s%.1f\n%-16s%.1f\n======================================\n", "Card name:", cardRaw, "Base damage:", damageRaw, "Bonus:", bonusRaw, "Final damage:", finalDamage ) )