Created
January 8, 2021 20:42
-
-
Save Damercy/e1324bfe41a5ae6f92046bc971943adb to your computer and use it in GitHub Desktop.
Kotlin sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground code link - https://pl.kotl.in/8XKnzisvF | |
/** | |
* You can edit, run, and share this code. | |
* play.kotlinlang.org | |
*/ | |
fun main() { | |
val tim = Player(name="Tim",level=4,lives=8) | |
val louise = Player("Louise",2,5,1000) | |
var weapon:Weapon = Weapon("Axe",50) | |
tim.weapon = weapon | |
louise.weapon = weapon | |
tim.weapon = Weapon("Spear",52) | |
tim.lootItems.add(Loot("Red Dragon",LootType.POTION,5.0)) | |
tim.lootItems.add(Loot("Silver Delight",LootType.WEAPON,25.5)) | |
println(tim) | |
} | |
class Player(val name:String,var level:Int = 1, var lives:Int = 3, var score:Long = 0){ | |
var weapon:Weapon = Weapon("Fist",5) | |
var lootItems:ArrayList<Loot> = ArrayList<Loot>() | |
fun showLootItems():String{ | |
var lootItemsString:String="" | |
for(loot in lootItems) | |
lootItemsString+=loot | |
return lootItemsString | |
} | |
override fun toString():String{ | |
return """ | |
name: $name, | |
level: $level, | |
lives: $lives, | |
score: $score, | |
weapon: $weapon, | |
loot: ${showLootItems()} | |
""" | |
} | |
} | |
class Weapon(val name:String,var damageInflicted:Int){ | |
override fun toString():String{ | |
return """ | |
weaponName: $name, | |
weaponDamage: $damageInflicted | |
""" | |
} | |
} | |
enum class LootType{ | |
POTION,COIN,WEAPON | |
} | |
class Loot(val name:String, val type:LootType,var value:Double){ | |
override fun toString():String{ | |
return """ | |
lootName: $name, | |
lootType: $type, | |
lootValue: $value | |
""" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment