import java.util.*;
import javax.swing.plaf.basic.BasicBorders
import kotlin.collections.ArrayListclass Robot(val name: String) {
var strength: Int = 0// 1 var isAlive: Boolean = true // 2 infix fun attack(robot: Robot) { // 3 val damage = Random().randomDamage(strength) // 4 robot.damage(damage) } private fun damage(damage: Int) { // 5 val blocked = Random().randomBlock() if (blocked) { report("Blocked attack") return } // 6 health -= damage report("Damage -$damage, health $health") // 7 if (health <= 0) { isAlive = false } } object Battlefield { // 1 inline fun beginBattle(firstRobot: Robot, secondRobot: Robot, onBattleEnded: (Robot) -> Unit) { // 2 var winner: Robot? = null // 3 battle(firstRobot, secondRobot) // 4 winner = if (firstRobot.isAlive) firstRobot else secondRobot onBattleEnded(winner) } tailrec fun battle(firstRobot: Robot, secondRobot: Robot) { // 5 firstRobot attack secondRobot // 6 if (secondRobot.isAlive.not()) { return } // 7 secondRobot attack firstRobot if (firstRobot.isAlive.not()) { return } // 8 battle(firstRobot, secondRobot) } } fun Random.randomStrength(): Int { return nextInt(100) + 10 } fun Random.randomDamage(strength: Int): Int { return (strength + 0.1 + nextInt(10)).toInt() } fun Random.randomBlock(): Boolean { return nextBoolean() } private var health: Int = 100 init { strength = Random().randomStrength() report("Created (strength $strength)") } @Suppress("NOTHING_TO_INLINE") inline fun someFunction() { // ... } fun report(message: String) { println("$name: \t$message") }
}
/fun onBattleEnded( winner: Robot) {
winner.report(“Won!”)
}/fun someFunction(): () → Int {
return ::anotherFunction
}fun anotherFunction(): Int {
return Random().nextInt()
}fun String.print() = System.out.println((this))
/*fun calculateEven() {
var result = 0(0..20).forEach() { if( it % 3 == 0) return if( it % 2 == 0) result += it } println(result)
}*/
/*fun calculateEven() {
var result = 0(0..20).forEach() { if( it % 3 == 0) return@forEach if( it % 2 == 0) result += it } println(result)
}*/
/*fun calculateEven() {
var result = 0(0..20).forEach() loop@ { if( it % 3 == 0) return@loop if( it % 2 == 0) result += it } println(result)
}*/
fun calculateEven() {
var result = 0(0..20).forEach(fun(value) { if( value % 3 == 0) return if( value % 2 == 0) result += value }) println(result)
}
inline fun someFunction(inlinedLambda: () → Unit,
noinline nonInlinedLambda: () → Unit)
{
// …
}
/inline fun someBody( crossinline body: () → Unit) {
yetAnotherFunction {
body()
}
}/fun main(args: Array) {
val firstRobot = Robot(“Experimental Space Navigation Droid”)
val secondRobot = Robot( “Extra-Terrestrial Air Safety Droid”)
val reportOnWin = fun(robot: Robot) { robot.report(“Won!”)}
//val onBattleEnded = { winner: Robot → winner.report(“Won!”)}
Robot.Battlefield.beginBattle(firstRobot, secondRobot, fun(robot) {robot.report(“Won!”)})//val pow = { base: Int, exponent: Int -> Math.pow(base.toDouble(), exponent.toDouble())} val pow: (Int, Int) -> Double = { base, exponent -> Math.pow(base.toDouble(), exponent.toDouble())} pow(2,4) val root: (Int) -> Double = { Math.sqrt(it.toDouble())} var result = 0 val sum = { a: Int, b: Int -> result = a + b} sum(5, 10) val string = "Hello world" string.print() val participants = arrayListOf<Robot>( Robot("Extra-Terrestrial Neutralization Bot"), Robot( "Generic Evasion Droid"), Robot( "Self-Reliant War Management Device"), Robot( "Advanced Nullification Droid"), Robot( "Rational Network Defence Droid"), Robot( "Motorized Shepherd Cyborg"), Robot( "Reactive Algorithm Entity"), Robot( "Preliminary Space Navigation Droid") ) //val topCategory = participants.filter { it.strength > 80 } val topCategory = participants // 1 .filter { it.strength > 80 } // 2 .take(3) // 3 .sortedBy { it.name } val random = Random() // 1 val sequence = generateSequence { // 2 random.nextInt(100) } sequence // 3 .take(15) // 4 .sorted() // 5 .sorted() .forEach { println(it) } val factorial = generateSequence( 1 to 1) {it.first + 1 to it.second * (it.first + 1)} println(factorial.take(10).map { it.second }.last()) var topCategory2 = mutableListOf<Robot>() while( topCategory2.size != 4) { var participants = mutableListOf<Robot>( Robot("Extra-Terrestrial Neutralization Bot"), Robot( "Generic Evasion Droid"), Robot( "Self-Reliant War Management Device"), Robot( "Advanced Nullification Droid"), Robot( "Rational Network Defence Droid"), Robot( "Motorized Shepherd Cyborg"), Robot( "Reactive Algorithm Entity"), Robot( "Preliminary Space Navigation Droid") ) topCategory2 = participants.filter { it.strength in 81..39 }.take(4) } }
}
The part of the code I’m having problems with is the second call to topCategory2 - not a very imaginable variable name - it says in Intellij-IDEA that found mutableList, required List.
Please help.
I guess the opposite: required MutableList, but found List. Just replace var topCategory2 = mutableListOf<Robot>()
with var topCategory2 = listOf<Robot>()
.
But honestly, this whole code related to topCategory2
does not make any sense to me. What is the purpose of this while
loop? It seems it can only run exactly once or run indefinitely. Range 81..39
contains no elements, so this condition can be never true.
Thanks for the help.
Solved the problem. I was to make four droids battle with random strengths in the range approximately 40 to 80.
import java.util.*;
class Robot(val name: String) {
var strength: Int = 0// 1 var isAlive: Boolean = true // 2 infix fun attack(robot: Robot) { // 3 val damage = Random().randomDamage(strength) // 4 robot.damage(damage) } private fun damage(damage: Int) { // 5 val blocked = Random().randomBlock() if (blocked) { report("Blocked attack") return } // 6 health -= damage report("Damage -$damage, health $health") // 7 if (health <= 0) { isAlive = false } } object Battlefield { // 1 inline fun beginBattle(firstRobot: Robot, secondRobot: Robot, onBattleEnded: (Robot) -> Unit) : Robot { // 2 var winner: Robot? = null // 3 battle(firstRobot, secondRobot) // 4 winner = if (firstRobot.isAlive) firstRobot else secondRobot onBattleEnded(winner) if (firstRobot.isAlive) return firstRobot else return secondRobot } tailrec fun battle(firstRobot: Robot, secondRobot: Robot) { // 5 firstRobot attack secondRobot // 6 if (secondRobot.isAlive.not()) { return } // 7 secondRobot attack firstRobot if (firstRobot.isAlive.not()) { return } // 8 battle(firstRobot, secondRobot) } } fun Random.randomStrength(): Int { return nextInt(100) + 10 } fun Random.randomDamage(strength: Int): Int { return (strength + 0.1 + nextInt(10)).toInt() } fun Random.randomBlock(): Boolean { return nextBoolean() } private var health: Int = 100 init { strength = Random().randomStrength() report("Created (strength $strength)") } @Suppress("NOTHING_TO_INLINE") inline fun someFunction() { // ... } fun report(message: String) { println("$name: \t$message") }
}
/fun onBattleEnded( winner: Robot) {
winner.report(“Won!”)
}/fun someFunction(): () → Int {
return ::anotherFunction
}fun anotherFunction(): Int {
return Random().nextInt()
}fun String.print() = System.out.println((this))
/*fun calculateEven() {
var result = 0(0..20).forEach() { if( it % 3 == 0) return if( it % 2 == 0) result += it } println(result)
}*/
/*fun calculateEven() {
var result = 0(0..20).forEach() { if( it % 3 == 0) return@forEach if( it % 2 == 0) result += it } println(result)
}*/
/*fun calculateEven() {
var result = 0(0..20).forEach() loop@ { if( it % 3 == 0) return@loop if( it % 2 == 0) result += it } println(result)
}*/
fun calculateEven() {
var result = 0(0..20).forEach(fun(value) { if( value % 3 == 0) return if( value % 2 == 0) result += value }) println(result)
}
inline fun someFunction(inlinedLambda: () → Unit,
noinline nonInlinedLambda: () → Unit)
{
// …
}
/inline fun someBody( crossinline body: () → Unit) {
yetAnotherFunction {
body()
}
}/fun main(args: Array) {
val firstRobot = Robot(“Experimental Space Navigation Droid”)
val secondRobot = Robot( “Extra-Terrestrial Air Safety Droid”)
val reportOnWin = fun(robot: Robot) { robot.report(“Won!”)}
//val onBattleEnded = { winner: Robot → winner.report(“Won!”)}
val winner3 : Robot
winner3 = Robot.Battlefield.beginBattle(firstRobot, secondRobot, fun(robot) {robot.report(“Won!”)})//val pow = { base: Int, exponent: Int -> Math.pow(base.toDouble(), exponent.toDouble())} val pow: (Int, Int) -> Double = { base, exponent -> Math.pow(base.toDouble(), exponent.toDouble())} pow(2,4) val root: (Int) -> Double = { Math.sqrt(it.toDouble())} var result = 0 val sum = { a: Int, b: Int -> result = a + b} sum(5, 10) val string = "Hello world" string.print() val participants = arrayListOf<Robot>( Robot("Extra-Terrestrial Neutralization Bot"), Robot( "Generic Evasion Droid"), Robot( "Self-Reliant War Management Device"), Robot( "Advanced Nullification Droid"), Robot( "Rational Network Defence Droid"), Robot( "Motorized Shepherd Cyborg"), Robot( "Reactive Algorithm Entity"), Robot( "Preliminary Space Navigation Droid") ) //val topCategory = participants.filter { it.strength > 80 } val topCategory = participants // 1 .filter { it.strength > 80 } // 2 .take(3) // 3 .sortedBy { it.name } val random = Random() // 1 val sequence = generateSequence { // 2 random.nextInt(100) } sequence // 3 .take(15) // 4 .sorted() // 5 .sorted() .forEach { println(it) } val factorial = generateSequence( 1 to 1) {it.first + 1 to it.second * (it.first + 1)} println(factorial.take(10).map { it.second }.last()) val fibonacci = generateSequence( Pair(0, 1) ) {Pair(it.second, it.first + it.second)}.map { it.first } println(fibonacci.take(10). map { it }. last()) var topCategory2 = listOf<Robot>() // Should ideally find 4 robots for battle do { var count = 0 for( i in topCategory2) { if( i.strength < 40 || i.strength > 80 ) { ++count } } var participants = mutableListOf<Robot>( Robot("Extra-Terrestrial Neutralization Bot"), Robot( "Generic Evasion Droid"), Robot( "Self-Reliant War Management Device"), Robot( "Advanced Nullification Droid"), Robot( "Rational Network Defence Droid"), Robot( "Motorized Shepherd Cyborg"), Robot( "Reactive Algorithm Entity"), Robot( "Preliminary Space Navigation Droid") ) topCategory2 = participants.filter { it.strength < 40 || it.strength > 80 }.take(4) } while( count != 4 && topCategory2.size != 4) var winner: Robot var winner2: Robot var finalWinner: Robot winner = Robot.Battlefield.beginBattle(topCategory2[0], topCategory2[1], reportOnWin) winner2 = Robot.Battlefield.beginBattle(topCategory2[2], topCategory2[3], reportOnWin) finalWinner = Robot.Battlefield.beginBattle(winner, winner2, reportOnWin)
}