cg85-v2/Sources/Np85Snake/Board/SnakeCommand.swift

22 lines
608 B
Swift

public enum SnakeCommand: String {
case rightOne = "r"
case leftOne = "l"
case upOne = "u"
case downOne = "d"
case rightTwo = "R"
case leftTwo = "L"
case upTwo = "U"
case downTwo = "D"
case repeatLast = "."
static func fromDirection(_ direction: Vertex, far: Bool = false) -> SnakeCommand {
switch direction {
case Vertex.down: far ? upTwo : upOne
case Vertex.up: far ? downTwo : downOne
case Vertex.right: far ? rightTwo : rightOne
case Vertex.left: far ? leftTwo : leftOne
default: [upOne, downOne, rightOne, leftOne].randomElement() ?? .repeatLast
}
}
}