-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot1.scala
166 lines (149 loc) · 6.21 KB
/
bot1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class ControlFunction {
def respond(input: String): String = {
val (opcode, paramMap) = CommandParser(input)
if( opcode == "React" ) {
val generation = paramMap("generation").toInt
val time = paramMap("time").toInt
val energy = paramMap("energy").toInt
val view = View( paramMap("view") )
val previousDirectionStr = if (paramMap.contains("previousDirection")) paramMap("previousDirection") else "1:1"
val previousDirection = Pos.parse(previousDirectionStr)
if( generation == 0 ) {
if (paramMap.contains("forcedDirection")) {
val dir = Pos.parse(paramMap("forcedDirection"))
if (view.isFree(dir)) {
val stepsLeft = paramMap("stepsLeft").toInt
if (stepsLeft > 0) {
return "Move(direction=" + dir + ")" +
"|Set(stepsLeft=" + (stepsLeft - 1) +
",forcedDirection=" + dir +
",previousDirection=" + dir + ")"
}
}
}
val f = view.firstFood
if (f != None) {
val dir = view.directionTowardsPos(f.get)
if (view.isFree(dir)) {
"Status(text=Food)" +
"|Move(direction=" + dir + ")" +
"|Set(previousDirection=" + dir + ")"
} else {
if (view.isEnemy(dir)) {
val twisted = view.getFreeDirection(dir)
"Say(text=FLEE)" +
"|Status(text=ENEMY)" +
"|Move(direction=" + twisted + ")" +
"|Set(forcedDirection=" + twisted + ",stepsLeft=5,previousDirection=" + twisted + ")"
} else {
val twisted = view.getFreeDirection(dir)
"Say(text=WALL)" +
"|Status(text=Wall)" +
"|Move(direction=" + twisted + ")" +
"|Set(forcedDirection=" + twisted + ",stepsLeft=5,previousDirection=" + twisted + ")"
}
}
} else {
val twisted = view.getFreeDirection(previousDirection)
if (twisted != previousDirection) {
"Status(text=Scouting)" +
"|Move(direction=" + twisted + ")" +
"|Set(forcedDirection=" + twisted + ",stepsLeft=5,previousDirection=" + twisted + ")"
} else {
"Status(text=Scouting)" +
"|Move(direction=" + twisted + ")" +
"|Set(previousDirection=" + twisted + ")"
}
}
} else ""
} else ""
}
}
case class Pos(x:Int, y:Int) {
def direction = Pos( if(x==0) 0 else x/math.abs(x), if(y==0) 0 else y/math.abs(y) )
override def toString = x+":"+y
}
object Pos {
def parse(in:String) = {
val v = in.split(':').map(_.toInt)
Pos(v(0),v(1))
}
}
import util.Random
object Direction {
val dirs = "-1:-1 0:-1 1:-1 1:0 1:1 0:1 -1:1 -1:0".split(" ").map(Pos.parse(_))
def turn(dir:Pos, clockWise: Boolean) = {
val index = dirs.indexOf(dir)
if (index == -1) {
throw new IllegalArgumentException("Can't find index of " + dir + " in " + dirs)
}
if (clockWise) {
// clock wise
if (index == (dirs.length - 1)) {
dirs(0)
} else {
dirs(index + 1)
}
} else {
// counter clock wise
if (index == 0) {
dirs(dirs.length - 1)
} else {
dirs(index - 1)
}
}
}
}
case class View(data:String) {
val dimension = math.sqrt(data.length).toInt
val delta = ((dimension-1) / 2)
def firstFood:Option[Pos] = {
val food = data.view.zipWithIndex.filter(p => p._1 == 'P' || p._1 == 'B')
if (food.isEmpty)
None
else
Some( food.map(p => indexToPos(p._2)).minBy( distanceToPos(_) ) )
}
// should really look as far as possible in all directions and choose the most open
def getFreeDirection(currentDirection:Pos) = {
val random = new Random()
this._getFreeDirection(currentDirection, 0, random.nextBoolean)
}
def _getFreeDirection(currentDirection: Pos, turns: Int, turnClockWise: Boolean):Pos = {
if (turns == 8 || this.isFree(currentDirection)) {
currentDirection
} else {
this._getFreeDirection(Direction.turn(currentDirection, turnClockWise), turns + 1, turnClockWise)
}
}
def apply(pos:Pos) = data( posToIndex(pos) )
def isFree(pos:Pos) = "BP_".contains(this.apply(pos))
def isEnemy(pos:Pos) = "bs".contains(this.apply(pos))
def posToIndex(p:Pos) = (p.y+delta)*dimension + delta + p.x
def indexToPos(i:Int):Pos = Pos(i%dimension-delta, i/dimension-delta)
def distanceToPos(pos:Pos) = math.sqrt(pos.x*pos.x + pos.y*pos.y)
def directionTowardsPos(pos:Pos) = {
val x = if (pos.x == 0) 0 else math.abs(pos.x)/pos.x
val y = if (pos.y == 0) 0 else math.abs(pos.y)/pos.y
Pos(x, y)
}
}
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 _
}