Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import java.util.ArrayList
  2. import util.Random
  3. import java.util.LinkedList
  4.  
  5. case class XY(var x: Int, var y: Int)
  6.  
  7. class Vertex(var x: Int, var y: Int, var c: Char) {
  8.  
  9. var previousVertex: Vertex = this
  10.  
  11. var lengthToGoal: Int = java.lang.Integer.MAX_VALUE
  12.  
  13. }
  14.  
  15. class View(tilesStr: String) {
  16.  
  17. private var vertexes: ArrayList[Vertex] = new ArrayList()
  18.  
  19. var size: Int = Math.sqrt(tilesStr.length).toInt
  20.  
  21. for (i <- 0 until tilesStr.length) {
  22. vertexes.add(new Vertex(i % size, i / size, tilesStr.charAt(i)))
  23. }
  24.  
  25. def getChar(x: Int, y: Int): Char = vertexes.get(y * size + x).c
  26.  
  27. def getVertex(x: Int, y: Int): Vertex = vertexes.get(y * size + x)
  28.  
  29. def setVertex(x: Int, y: Int, c: Char): Unit = {
  30. vertexes.get(y * size + x).c = c
  31. }
  32.  
  33. }
  34.  
  35. object BreadthFirst {
  36. def getNextDirectionVertex(vertex: Vertex): Vertex = {
  37. var result = vertex
  38. while (result.previousVertex != result.previousVertex.previousVertex) result = result.previousVertex
  39. result
  40. }
  41.  
  42. def apply(view: View): XY = {
  43. val queue: LinkedList[Vertex] = new LinkedList[Vertex]()
  44. val size: Int = view.size
  45. val startingPosition: Vertex = view.getVertex(size / 2, size / 2)
  46. startingPosition.lengthToGoal = 0
  47. queue.add(startingPosition)
  48. while (!queue.isEmpty) {
  49. val currentTile: Vertex = queue.pollFirst()
  50. 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))
  51. for(i <- 0 until availableMoves.length){
  52. val checking = availableMoves(i)
  53. if(currentTile.x + checking.x >= 0 && currentTile.x + checking.x < size &&
  54. currentTile.y + checking.y >= 0 && currentTile.y + checking.y < size) {
  55. val nextTile: Vertex = view.getVertex(currentTile.x + checking.x, currentTile.y + checking.y)
  56. if (!(checking.x == 0 && checking.y == 0)) {
  57. if (nextTile.c != 'W' && nextTile.c != '?' &&
  58. nextTile.lengthToGoal > currentTile.lengthToGoal + 1) {
  59. nextTile.previousVertex = currentTile
  60. nextTile.lengthToGoal = currentTile.lengthToGoal + 1
  61. if (nextTile.c == 'P' || nextTile.c == 'B') {
  62. val nextDirectionTile = getNextDirectionVertex(nextTile)
  63. val nextX: Int = nextDirectionTile.x - startingPosition.x
  64. val nextY: Int = nextDirectionTile.y - startingPosition.y
  65. return new XY(nextX, nextY)
  66. }
  67.  
  68. queue.add(nextTile)
  69. }
  70. }
  71. }
  72. }
  73. }
  74. val rnd = new Random()
  75. val dx = rnd.nextInt(3)-1
  76. val dy = rnd.nextInt(3)-1
  77. new XY(dx, dy)
  78. }
  79. }
  80.  
  81. class ControlFunction {
  82. val rnd = new Random()
  83. def respond(input: String): String = {
  84. val (opcode, paramMap) = CommandParser(input)
  85. if( opcode == "React" ) {
  86. val viewString = paramMap("view")
  87. var view = new View(viewString)
  88. var direction = BreadthFirst(view)
  89. val generation = paramMap("generation").toInt
  90. val moveDirection = direction.x + ":" + direction.y;
  91. if ( generation == 0 ) {
  92. if( paramMap("energy").toInt >= 100 && rnd.nextDouble() < 0.05)
  93. {
  94. val dx = rnd.nextInt(3)-1
  95. val dy = rnd.nextInt(3)-1
  96. val direction = dx + ":" + dy
  97. "Spawn(direction=" + direction + ",heading=" + direction + ")"
  98. }
  99. else "Move(direction=" + moveDirection + ")|Status(text=Harvesting)"
  100. }
  101. else {
  102. val heading = paramMap("heading")
  103. val viewString = paramMap("view")
  104. var view = new View(viewString)
  105. val size: Int = view.size
  106. val botPosition: Vertex = view.getVertex(size / 2, size / 2)
  107. 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))
  108. for(i <- 0 until availableEnemy.length){
  109. val checking = availableEnemy(i)
  110. val nextVertex: Vertex = view.getVertex(botPosition.x + checking.x, botPosition.y + checking.y)
  111. if (nextVertex.c == 'b') {
  112. "Explode()"
  113. }
  114. }
  115. "Move(direction=" + heading + ")"
  116. }
  117. } else ""
  118. }
  119. }
  120.  
  121.  
  122. object CommandParser {
  123. def apply(command: String) = {
  124. def splitParam(param: String) = {
  125. val segments = param.split('=')
  126. if( segments.length != 2 )
  127. throw new IllegalStateException("invalid key/value pair: " + param)
  128. (segments(0),segments(1))
  129. }
  130.  
  131. val segments = command.split('(')
  132. if( segments.length != 2 )
  133. throw new IllegalStateException("invalid command: " + command)
  134.  
  135. val params = segments(1).dropRight(1).split(',')
  136. val keyValuePairs = params.map( splitParam ).toMap
  137. (segments(0), keyValuePairs)
  138. }
  139. }
  140.  
  141. class ControlFunctionFactory {
  142. def create = new ControlFunction().respond _
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement