generated from admin/template
lab-08
This commit is contained in:
@@ -0,0 +1,103 @@
|
|||||||
|
data class Pokemon(
|
||||||
|
val name: String,
|
||||||
|
var hp: Short
|
||||||
|
){
|
||||||
|
init{
|
||||||
|
require(name.isNotBlank()) {"Name cannot be is empty."}
|
||||||
|
require(hp in 0..999) {"Hp must bi in range from 0 to 999"}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
const val MAX_HP_VALUE: Short = 999
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isAlive(): Boolean = hp > 0
|
||||||
|
fun takeDamage(amount: Short) {
|
||||||
|
amount.coerceAtLeast(0)
|
||||||
|
hp = (hp - amount).coerceAtLeast(0).toShort()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val pikachu = Pokemon("Pikachu", 100)
|
||||||
|
|
||||||
|
showTask()
|
||||||
|
println(pikachu)
|
||||||
|
br()
|
||||||
|
|
||||||
|
pikachu.takeDamage(30)
|
||||||
|
println(pikachu)
|
||||||
|
println("Status: ${pikachu.isAlive()}")
|
||||||
|
br()
|
||||||
|
|
||||||
|
pikachu.takeDamage(80)
|
||||||
|
println(pikachu)
|
||||||
|
println("Status: ${pikachu.isAlive()}")
|
||||||
|
|
||||||
|
showTask(2)
|
||||||
|
pikachu.hp = 50
|
||||||
|
// Поле у экземпляра нельзя изменть, так как оно является read-only (val).
|
||||||
|
// Переменную pikachu - так как она является неизменяемой (val)
|
||||||
|
|
||||||
|
// team - переменная, хранящая ссылку на область в памяти, где хранятся данные. В этой области можно добавлять и изменять элементы.
|
||||||
|
// Нельзя перезаписывать саму ссылку.
|
||||||
|
|
||||||
|
showTask(3)
|
||||||
|
Pokemon("Pikachu", 100)
|
||||||
|
// Pokemon("", 100)
|
||||||
|
// Pokemon("Mewtwo", 5000)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//============= 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
|
||||||
|
)
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user