cg85-v2/Sources/Np85Snake/Board/BoardPart.swift
2025-10-03 01:58:56 +05:30

84 lines
2.3 KiB
Swift

public enum BoardPart: String {
case empty = "."
case apple = "$"
case legalHead = "0" // my head
case illegalHead = "O" // other snake head
case junctionHead = "" // t-crossing
case upRightHead = "" // turn right, or turn down
case rightLeftHead = "" // left or right
case downRightHead = "" // turn right, or turn up
case upLeftHead = "" // turn left, or turn down
case upDownHead = "" // up or down
case downLeftHead = "" // turn left, or turn up
// same as above but not connected to head
case junction = ""
case upRight = ""
case rightLeft = ""
case downRight = ""
case upLeft = ""
case upDown = ""
case downLeft = ""
var isHead: Bool {
self == .legalHead || self == .illegalHead
}
var isSafe: Bool {
self == .empty || self == .apple || self == .legalHead // || self.isJunction || self.isJumpable
}
var isNeck: Bool {
self == .junctionHead || self == .upDownHead || self == .rightLeftHead || self == .upRightHead
|| self == .upLeftHead || self == .downRightHead || self == .downLeftHead
}
var isJunction: Bool {
self == .junctionHead || self == .junction
}
var isJumpable: Bool {
self == .upDown || self == .rightLeft
}
func normalize() -> BoardPart {
switch self {
case .junctionHead: .junction
case .upDownHead: .upDown
case .rightLeftHead: .rightLeft
case .upLeftHead: .upLeft
case .upRightHead: .upRight
case .downLeftHead: .downLeft
case .downRightHead: .downRight
default: self
}
}
func connects(to other: BoardPart, in direction: Vertex) -> Bool {
if self.isHead && !other.isNeck {
return false
}
switch other.normalize() {
case .junction:
return true
case .upDown:
return direction == Vertex.up || direction == Vertex.down
case .rightLeft:
return direction == Vertex.left || direction == Vertex.right
case .upRight:
return direction == Vertex.up || direction == Vertex.right
case .upLeft:
return direction == Vertex.up || direction == Vertex.left
case .downRight:
return direction == Vertex.down || direction == Vertex.right
case .downLeft:
return direction == Vertex.down || direction == Vertex.left
default:
return false
}
}
}