46 lines
849 B
Swift
46 lines
849 B
Swift
import Foundation
|
|
import Np85Snake
|
|
|
|
#if os(macOS) || os(iOS)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
@preconcurrency import Glibc
|
|
#elseif canImport(Musl)
|
|
import Musl
|
|
#elseif os(Windows)
|
|
import ucrt
|
|
#endif
|
|
|
|
public struct StdIO: BoardIO {
|
|
public func next() -> (any StringProtocol)? {
|
|
readLine(strippingNewline: true)
|
|
}
|
|
|
|
public func commit(text: String) {
|
|
print(text)
|
|
}
|
|
|
|
public func commit(command: SnakeCommand) {
|
|
print(command.rawValue)
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct Np85SnakeSkin {
|
|
static func main() {
|
|
setbuf(stdout, nil)
|
|
setbuf(stdin, nil)
|
|
let safeio = StdIO()
|
|
var io = safeio as any BoardIO
|
|
var board = try! Board.start(io: &io)
|
|
|
|
while true {
|
|
guard let result = try? board.parse(io: &io), result else {
|
|
return
|
|
}
|
|
board.calculate()
|
|
_ = board.commit(io: &io)
|
|
}
|
|
}
|
|
}
|