Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created October 4, 2014 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiksy/29d44cedf07942a95884 to your computer and use it in GitHub Desktop.
Save daiksy/29d44cedf07942a95884 to your computer and use it in GitHub Desktop.
CodingGame Training
import math._
import scala.util._
/**
* The code below will read all the game information for you.
* On each game turn, information will be available on the standard input, you will be sent:
* -> the total number of visible enemies
* -> for each enemy, its name and distance from you
* The system will wait for you to write an enemy name on the standard output.
* Once you have designated a target:
* -> the cannon will shoot
* -> the enemies will move
* -> new info will be available for you to read on the standard input.
**/
object Player {
def main(args: Array[String]) {
// game loop
while(true) {
val count = readInt // The number of current enemy ships within range
val targets = for{i <- 0 until count
Array(enemy, _dist) = readLine split " "
dist = _dist.toInt
} yield (dist -> enemy)
// Write an action using println
// To debug: Console.err.println("Debug messages...")
println(targets.sortBy(_._1).map(_._2).head) // The name of the most threatening enemy (HotDroid is just one example)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment