Skip to content

Instantly share code, notes, and snippets.

@loganmzz
Created May 16, 2017 19:25
Show Gist options
  • Save loganmzz/2bd3f258b295b542aea528ffb753b90e to your computer and use it in GitHub Desktop.
Save loganmzz/2bd3f258b295b542aea528ffb753b90e to your computer and use it in GitHub Desktop.
MKTD Banana Island
let myName = "Capucin"
var isOver = false
public init() {
// Nothing
}
public func play(_ game:Game, _ teamId:Int) -> Direction {
print("Team ID: \(teamId)")
// search current position
var myPosition: (Int, Int) = (0, 0)
let boardDimensions = game.getBoardGame().getDimensions()
for x in 0...boardDimensions.x {
for y in 0...boardDimensions.y {
if game.getBoardGame().getElementAt(x: x, y: y) == teamId {
myPosition = (x, y)
}
}
}
//print("My position: \(myPosition)")
//
var nodes = [Node]()
nodes.append(Node(myPosition, nil, false))
var isOver = false
while (!isOver) {
let newNodes = iteration(game, nodes)
let bananas = newNodes.filter({$0.isBanana})
isOver = !bananas.isEmpty
nodes = isOver ? bananas : newNodes
}
print("Found: \(nodes)")
return nodes[randomNumber(nodes.count)].firstDirection!
}
func iteration(_ game:Game, _ nodes: [Node]) -> [Node] {
//print("Iteration")
var newNodes = [Node]()
for node in nodes {
let deltas: [(Int,Int,Direction)] = [(-1, 0, .West), (+1, 0, .East), (0, -1, .North), (0, +1, .South)]
for delta in deltas {
let location = (node.position.0+delta.0, node.position.1+delta.1)
let direction = node.firstDirection ?? delta.2
if let cell = game.getBoardGame().getElementAt(x: location.0, y: location.1) {
if (cell < 2) {
//print("Add node:\(location)")
newNodes.append(Node(location, direction, cell == 1))
}
}
}
}
return newNodes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment