Advertisement
Guest User

Grok Scala - Dallas - Thor

a guest
Feb 9th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.10 KB | None | 0 0
  1. import math._
  2. import scala.util._
  3.  
  4. object Player {
  5.  
  6.     case class Coordinate(x: Int, y: Int)
  7.     case class Universe(lightOfPower: Coordinate, thorsPosition: Coordinate) {
  8.         def getDirection: (Universe, String) = {
  9.     val (we, x2) =
  10.       if (thorsPosition.x < lightOfPower.x)
  11.         ("E", thorsPosition.x + 1)
  12.       else
  13.         if (thorsPosition.x > lightOfPower.x)
  14.           ("W", thorsPosition.x - 1)
  15.         else
  16.           ("", thorsPosition.x)
  17.     val (ns, y2) =
  18.       if (thorsPosition.y < lightOfPower.y)
  19.         ("S", thorsPosition.y + 1)
  20.       else
  21.         if (thorsPosition.y > lightOfPower.y)
  22.           ("N", thorsPosition.y - 1)
  23.         else
  24.           ("", thorsPosition.y)
  25.     (copy(thorsPosition = Coordinate(x2, y2)), ns + we)
  26.         }
  27.     }
  28.    
  29.     def readInputInitial: Universe = {
  30.         val coordinates: Array[Int] =
  31.           for(i <- readLine split " ")
  32.             yield i.toInt
  33.         val result =
  34.           Universe(
  35.                 Coordinate(coordinates(0), coordinates(1))
  36.               , Coordinate(coordinates(2), coordinates(3))
  37.           )
  38.         Console.err.println("readInputInitial.result=" + result)
  39.         result
  40.     }
  41.    
  42.     def readInputLoop: Int = {
  43.         val result =
  44.           readInt
  45.         Console.err.println("readInputLoop.result=" + result)
  46.         result
  47.     }
  48.    
  49.     //def response(universe: Universe): (Coordinate, String) = {
  50.     //    //TODO: fill in the logic to figure out the actual logic to produce both the coordinate and the command
  51.     //    (Coordinate(universe.thor.x + 1, universe.thor.y + 1), "SE")
  52.     //}
  53.  
  54.     def main(args: Array[String]) {
  55.         var universe: Universe =
  56.           readInputInitial
  57.         // game loop
  58.         while(readInputLoop > 0) {
  59.             val (universeNew, command): (Universe, String) =
  60.               universe.getDirection
  61.             universe =
  62.               universeNew
  63.             Console.err.println(s"universe=$universe, command=$command")  
  64.             println(command) // A single line providing the move to be made: N NE E SE S SW W or NW
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement