diff --git a/works/lab-08/scr/Main.kt b/works/lab-08/scr/Main.kt index f3c444f..ba1822f 100644 --- a/works/lab-08/scr/Main.kt +++ b/works/lab-08/scr/Main.kt @@ -1,55 +1,152 @@ +import kotlin.ranges.contains + data class Pokemon( val name: String, - var hp: Short + var hp: Short, + val maxHp: Short = 200, + var level: Byte = 1 ){ init{ require(name.isNotBlank()) {"Name cannot be is empty."} - require(hp in 0..999) {"Hp must bi in range from 0 to 999"} + 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 MAX_HP_VALUE: Short = 999 + 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) { + + 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() + println(pikachu); br() pikachu.takeDamage(30) - println(pikachu) - println("Status: ${pikachu.isAlive()}") - br() + println("$pikachu\nStatus: ${pikachu.isAlive()}"); br() pikachu.takeDamage(80) - println(pikachu) - println("Status: ${pikachu.isAlive()}") + println("$pikachu\nStatus: ${pikachu.isAlive()}") -showTask(2) +showTask(2, " // Поле у экземпляра нельзя изменть, так как оно является read-only (val).\n" + + " // Переменную pikachu - так как она является неизменяемой (val)\n" + + "\n" + + " // team - переменная, хранящая ссылку на область в памяти, где хранятся данные. В этой области можно добавлять и изменять элементы.\n" + + " // Нельзя перезаписывать саму ссылку.") pikachu.hp = 50 - // Поле у экземпляра нельзя изменть, так как оно является read-only (val). - // Переменную pikachu - так как она является неизменяемой (val) - // team - переменная, хранящая ссылку на область в памяти, где хранятся данные. В этой области можно добавлять и изменять элементы. - // Нельзя перезаписывать саму ссылку. -showTask(3) +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) + }