Skip to content

Instantly share code, notes, and snippets.

@pavelborisov
Created November 20, 2015 16:32
Show Gist options
  • Save pavelborisov/94ce6209f90c826fa80a to your computer and use it in GitHub Desktop.
Save pavelborisov/94ce6209f90c826fa80a to your computer and use it in GitHub Desktop.
import model.TileType._
import model.{Car, Game, Move, World}
import scala.collection.immutable.HashMap
final class MyStrategy extends Strategy {
var ways = HashMap.empty[(Int, Int), List[(Int, Int)]
override def move(self: Car, world: World, game: Game, move: Move) {
val matrix = Array.ofDim[Int]( world.width, world.height )
world.waypoints.toList.sliding(2).map { case ((sx :: sy :: Nil) :: (dx :: dy :: Nil) :: Nil) =>
matrix(sx)(sy) = 1
matrix(dx)(dy) = 1
for(
i <- 0 to world.width;
j <- 0 to world.height;
if( matrix(i)(j) == 0 )
) yield {
def f(hor:Int, ver:Int) = {
world.tilesXY(i + hor)(j + ver) match {
case CROSSROADS => 1
case VERTICAL | LEFT_HEADED_T | RIGHT_HEADED_T if ver != 0 => 1
case HORIZONTAL | TOP_HEADED_T | BOTTOM_HEADED_T if hor != 0 => 1
case LEFT_TOP_CORNER | LEFT_BOTTOM_CORNER | RIGHT_HEADED_T if( hor == 1 ) => 1
case RIGHT_TOP_CORNER | RIGHT_BOTTOM_CORNER | LEFT_HEADED_T if( hor == -1 ) => 1
case LEFT_TOP_CORNER | RIGHT_TOP_CORNER | BOTTOM_HEADED_T if (ver == 1 ) => 1
case LEFT_BOTTOM_CORNER | RIGHT_BOTTOM_CORNER | TOP_HEADED_T if(ver == -1) => 1
case _ => 0
}
matrix(i)(j) = matrix(i + hor)(j + ver) + {
world.tilesXY(i + hor)(j + ver) match {
case CROSSROADS => 1
case VERTICAL | LEFT_HEADED_T | RIGHT_HEADED_T if ver != 0 => 1
case HORIZONTAL | TOP_HEADED_T | BOTTOM_HEADED_T if hor != 0 => 1
case LEFT_TOP_CORNER | LEFT_BOTTOM_CORNER | RIGHT_HEADED_T if( hor == 1 ) => 1
case RIGHT_TOP_CORNER | RIGHT_BOTTOM_CORNER | LEFT_HEADED_T if( hor == -1 ) => 1
case LEFT_TOP_CORNER | RIGHT_TOP_CORNER | BOTTOM_HEADED_T if (ver == 1 ) => 1
case LEFT_BOTTOM_CORNER | RIGHT_BOTTOM_CORNER | TOP_HEADED_T if(ver == -1) => 1
case _ => 0
}
}
}
List(
(-1,-1), (0,-1), (1,-1),
(-1, 0), (1,0),
(-1, 1), (0, 1), (1,1)
).map{ case (dx,dy) => ((dx + i ), (dy + j)) }
.withFilter{ case (x,y) => if( )dx((dx + i ), (dy + j)) }
world.tilesXY match
}
case _ =>
}
ways
def weelturn = { R:Double => Math.asin( self.width / R ) }
def nextWaypoint:(Double,Double) = {
val cornerTileOffset = 0.25D * game.trackTileSize;
Some( ( world.tilesXY(self.nextWaypointX)(self.nextWaypointY) ) match {
case LEFT_TOP_CORNER => ( 1, 1)
case RIGHT_TOP_CORNER => ( -1, 1)
case LEFT_BOTTOM_CORNER => ( 1, -1)
case RIGHT_BOTTOM_CORNER => ( -1, -1)
case _ => (0, 0)
} ).map { case( dX, dY ) =>
List(dX, dY)
.map( _ * cornerTileOffset )
.map{ delta => { x:Double => (delta + ((x + 0.5D) * game.trackTileSize)) }} match
{
case x :: y :: Nil => (x(self.nextWaypointX), y(self.nextWaypointY))
case _ => ???
}
}.get
}
println( s"x: ${self.nextWaypointX}, y: ${self.nextWaypointY}" )
val (nextWaypointX, nextWaypointY) = nextWaypoint
val angleToWaypoint = self.angleTo( nextWaypointX, nextWaypointY );
val speedModule = Math.hypot(self.speedX, self.speedY);
move.wheelTurn = (angleToWaypoint * 32.0D / Math.PI);
move.enginePower = (0.75D);
if (speedModule * speedModule * Math.abs(angleToWaypoint) > 2.5D * 2.5D * Math.PI) {
move.brake = true;
}
}
}
/* move.enginePower = 1.0D
move.throwProjectile = true
move.spillOil = true
if (world.tick > game.initialFreezeDurationTicks) {
move.useNitro = true
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment