bofa doesn't crawl back anymore, yielding a saner movement pattern

This commit is contained in:
Armin Friedl 2017-11-06 01:52:44 +01:00
parent 2b0ef1ac82
commit dc6308e364
3 changed files with 45 additions and 6 deletions

3
bofa/Makefile Normal file
View file

@ -0,0 +1,3 @@
bot.zip: src/main/scala/bot/*.scala
cd src/main/scala/bot && sed -i 's/Boolean = true/Boolean = false/' DebugUtil.scala
cd src/main/scala && zip -r bot bot && mv bot.zip $(CURDIR)

View file

@ -15,25 +15,42 @@ final case object Right extends { val label = "right"; val deltaX = 1; val delta
final case object Up extends { val label = "up"; val deltaX = 0; val deltaY = -1 } with Direction final case object Up extends { val label = "up"; val deltaX = 0; val deltaY = -1 } with Direction
final case object Down extends { val label = "down"; val deltaX = 0; val deltaY = 1 } with Direction final case object Down extends { val label = "down"; val deltaX = 0; val deltaY = 1 } with Direction
object Scribble {
var lastFieldWeight = 0
var lastBofaField: Field = Field.empty
}
class Cortex { class Cortex {
def move(state: State, time: Int): Direction = { def move(state: State, time: Int): Direction = {
val directions: Seq[Direction] = possibleDirections(state.bofaField, state) val directions: Seq[Direction] = possibleDirections(state.bofaField, state)
val weightBoard = weightCodes(1000, decr, state)
val weightBoard = weightCodes(1000, decr, state)
preventFallBack(weightBoard, state)
val dir = getMaxDir(state.bofaField, directions, weightBoard, state) val dir = getMaxDir(state.bofaField, directions, weightBoard, state)
Scribble.lastBofaField = state.bofaField
weightBoard(state.bofaField.x)(state.bofaField.y) = -999 weightBoard(state.bofaField.x)(state.bofaField.y) = -999
printBoard(weightBoard) printBoard(weightBoard)
dir dir
} }
def decr(dist: Int, start: Int) = { def decr(dist: Int, start: Int) = {
(start/math.pow(2, (dist+1))).intValue() val d = (start/math.pow(2.5, (dist+1))).intValue()
if (d < 5) 5 else d
} }
def getMaxField(weightBoard: Array[Array[Int]], state: State): Field = { def getMaxField(weightBoard: Array[Array[Int]], state: State): Field = {
var maxField = (0,0) var maxField = (0,0)
for (x <- 0 until S.width; y <- 0 until S.height) { for (x <- 0 until S.width; y <- 0 until S.height) {
// generally, the highest is best
if (weightBoard(x)(y) > weightBoard(maxField _1)(maxField _2)) maxField = (x,y) if (weightBoard(x)(y) > weightBoard(maxField _1)(maxField _2)) maxField = (x,y)
// if equal prefer to go down, right b/c up/left is evil first but
if (weightBoard(x)(y) == weightBoard(maxField _1)(maxField _2)){
if (x > (maxField _1) || y > (maxField _2)) maxField = (x, y)
}
} }
state.board(maxField _1)(maxField _2) state.board(maxField _1)(maxField _2)
@ -54,11 +71,14 @@ class Cortex {
var (maxX, maxY) = (mod (field.x+maxDir.deltaX) (S.width), mod (field.y+maxDir.deltaY) (S.width)) var (maxX, maxY) = (mod (field.x+maxDir.deltaX) (S.width), mod (field.y+maxDir.deltaY) (S.width))
if (weightBoard(x)(y) > weightBoard(maxX)(maxY)) maxDir = d if (weightBoard(x)(y) > weightBoard(maxX)(maxY)) maxDir = d
} }
maxDir maxDir
} }
def manhattan(x: Int, y: Int)(a: Int, b: Int): Int = math.abs(x-a) + math.abs(y-b)
def possibleDirections(field: Field, state: State): Seq[Direction] = { def possibleDirections(field: Field, state: State): Seq[Direction] = {
val pd = new mutable.ListBuffer[Direction] val pd = new mutable.ListBuffer[Direction]
val board = state.board val board = state.board
@ -79,6 +99,22 @@ class Cortex {
pd pd
} }
def preventFallBack(weightBoard: Array[Array[Int]], state: State): Unit = {
val lastBofaField = Scribble.lastBofaField
if (lastBofaField != Field.empty){
// if something changed allow fallback
log(s"lastFieldWeight=${Scribble.lastFieldWeight}")
if (Scribble.lastFieldWeight == weightBoard(lastBofaField.x)(lastBofaField.y)){
Scribble.lastFieldWeight = weightBoard(state.bofaField.x)(state.bofaField.y)
val x: Int = weightBoard(lastBofaField.x)(lastBofaField.y)
weightBoard(lastBofaField.x)(lastBofaField.y) = (x - (0.1 * x)).intValue()
} else{
Scribble.lastFieldWeight = weightBoard(state.bofaField.x)(state.bofaField.y)
}
}
}
def weightCodes(start: Int, drop: (Int, Int) => Int, state: State): Array[Array[Int]] = { def weightCodes(start: Int, drop: (Int, Int) => Int, state: State): Array[Array[Int]] = {
val board = state.board val board = state.board
val codes = state.codes val codes = state.codes

View file

@ -2,9 +2,9 @@ package bot
object DebugUtil { object DebugUtil {
var state: Option[State] = None // we need this to get access to the current game round var state: Option[State] = None // we need this to get access to the current game round
val Dbg: Boolean = true val Dbg: Boolean = false
val Log: Boolean = true val Log: Boolean = false
val Time: Boolean = true val Time: Boolean = false
def dbg(msg: String) = if(Dbg) Console.err.println(s"$round $msg") def dbg(msg: String) = if(Dbg) Console.err.println(s"$round $msg")
def log(msg: String) = if(Log) Console.err.println(s"$round $msg") def log(msg: String) = if(Log) Console.err.println(s"$round $msg")