Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList
- import util.Random
- import java.util.LinkedList
- case class XY(var x: Int, var y: Int)
- class Vertex(var x: Int, var y: Int, var c: Char) {
- var previousVertex: Vertex = this
- var lengthToGoal: Int = java.lang.Integer.MAX_VALUE
- }
- class View(tilesStr: String) {
- private var vertexes: ArrayList[Vertex] = new ArrayList()
- var size: Int = Math.sqrt(tilesStr.length).toInt
- for (i <- 0 until tilesStr.length) {
- vertexes.add(new Vertex(i % size, i / size, tilesStr.charAt(i)))
- }
- def getChar(x: Int, y: Int): Char = vertexes.get(y * size + x).c
- def getVertex(x: Int, y: Int): Vertex = vertexes.get(y * size + x)
- def setVertex(x: Int, y: Int, c: Char): Unit = {
- vertexes.get(y * size + x).c = c
- }
- }
- object BreadthFirst {
- def getNextDirectionVertex(vertex: Vertex): Vertex = {
- var result = vertex
- while (result.previousVertex != result.previousVertex.previousVertex) result = result.previousVertex
- result
- }
- def apply(view: View): XY = {
- val queue: LinkedList[Vertex] = new LinkedList[Vertex]()
- val size: Int = view.size
- val startingPosition: Vertex = view.getVertex(size / 2, size / 2)
- startingPosition.lengthToGoal = 0
- queue.add(startingPosition)
- while (!queue.isEmpty) {
- val currentTile: Vertex = queue.pollFirst()
- val availableMoves = Array(XY(-1,-1),XY(-1,0),XY(-1,1),XY(0,-1),XY(0,1),XY(1,-1),XY(1,0),XY(1,1))
- for(i <- 0 until availableMoves.length){
- val checking = availableMoves(i)
- if(currentTile.x + checking.x >= 0 && currentTile.x + checking.x < size &&
- currentTile.y + checking.y >= 0 && currentTile.y + checking.y < size) {
- val nextTile: Vertex = view.getVertex(currentTile.x + checking.x, currentTile.y + checking.y)
- if (!(checking.x == 0 && checking.y == 0)) {
- if (nextTile.c != 'W' && nextTile.c != '?' &&
- nextTile.lengthToGoal > currentTile.lengthToGoal + 1) {
- nextTile.previousVertex = currentTile
- nextTile.lengthToGoal = currentTile.lengthToGoal + 1
- if (nextTile.c == 'P' || nextTile.c == 'B') {
- val nextDirectionTile = getNextDirectionVertex(nextTile)
- val nextX: Int = nextDirectionTile.x - startingPosition.x
- val nextY: Int = nextDirectionTile.y - startingPosition.y
- return new XY(nextX, nextY)
- }
- queue.add(nextTile)
- }
- }
- }
- }
- }
- val rnd = new Random()
- val dx = rnd.nextInt(3)-1
- val dy = rnd.nextInt(3)-1
- new XY(dx, dy)
- }
- }
- class ControlFunction {
- val rnd = new Random()
- def respond(input: String): String = {
- val (opcode, paramMap) = CommandParser(input)
- if( opcode == "React" ) {
- val viewString = paramMap("view")
- var view = new View(viewString)
- var direction = BreadthFirst(view)
- val generation = paramMap("generation").toInt
- val moveDirection = direction.x + ":" + direction.y;
- if ( generation == 0 ) {
- if( paramMap("energy").toInt >= 100 && rnd.nextDouble() < 0.05)
- {
- val dx = rnd.nextInt(3)-1
- val dy = rnd.nextInt(3)-1
- val direction = dx + ":" + dy
- "Spawn(direction=" + direction + ",heading=" + direction + ")"
- }
- else "Move(direction=" + moveDirection + ")|Status(text=Harvesting)"
- }
- else {
- val heading = paramMap("heading")
- val viewString = paramMap("view")
- var view = new View(viewString)
- val size: Int = view.size
- val botPosition: Vertex = view.getVertex(size / 2, size / 2)
- val availableEnemy = Array(XY(-1,-1),XY(-1,0),XY(-1,1),XY(0,-1),XY(0,1),XY(1,-1),XY(1,0),XY(1,1))
- for(i <- 0 until availableEnemy.length){
- val checking = availableEnemy(i)
- val nextVertex: Vertex = view.getVertex(botPosition.x + checking.x, botPosition.y + checking.y)
- if (nextVertex.c == 'b') {
- "Explode()"
- }
- }
- "Move(direction=" + heading + ")"
- }
- } else ""
- }
- }
- object CommandParser {
- def apply(command: String) = {
- def splitParam(param: String) = {
- val segments = param.split('=')
- if( segments.length != 2 )
- throw new IllegalStateException("invalid key/value pair: " + param)
- (segments(0),segments(1))
- }
- val segments = command.split('(')
- if( segments.length != 2 )
- throw new IllegalStateException("invalid command: " + command)
- val params = segments(1).dropRight(1).split(',')
- val keyValuePairs = params.map( splitParam ).toMap
- (segments(0), keyValuePairs)
- }
- }
- class ControlFunctionFactory {
- def create = new ControlFunction().respond _
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement