Skip to content

Commit

Permalink
Fix tile calls
Browse files Browse the repository at this point in the history
The first index was being ignored, causing a flower puzzle to fail
because it couldn't grab its neighbor.

Code: 3:2Y2Cw60Aw2Sw0Sw+C
  • Loading branch information
alicerunsonfedora committed Jan 12, 2025
1 parent 2de8034 commit 59909bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Sources/PuzzleKit/Taiji/PKTaijiPuzzle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,26 @@ extension PKTaijiPuzzle: PKGrid {

public func tile(above coordinate: PKGridCoordinate) -> PKTaijiTile? {
let index = coordinate.aboveOrNil()?.toIndex(relativeTo: self)
guard let index, index >= 1, index < tiles.count else { return nil }
guard let index, index >= 0, index < tiles.count else { return nil }
return self.tiles[index]
}

public func tile(before coordinate: PKGridCoordinate) -> PKTaijiTile? {
let index = coordinate.beforeOrNil()?.toIndex(relativeTo: self)
guard let index, index >= 1, index < tiles.count else { return nil }
guard let index, index >= 0, index < tiles.count else { return nil }
return self.tiles[index]
}

public func tile(after coordinate: PKGridCoordinate) -> PKTaijiTile? {
let index = coordinate.after(stoppingAt: self.width)?.toIndex(relativeTo: self)
guard let index, index >= 1, index < tiles.count else { return nil }
guard let index, index >= 0, index < tiles.count else { return nil }
return self.tiles[index]
}

public func tile(below coordinate: PKGridCoordinate) -> PKTaijiTile? {
let height = tiles.count / width
let index = coordinate.below(stoppingAt: height)?.toIndex(relativeTo: self)
guard let index, index >= 1, index < tiles.count else { return nil }
guard let index, index >= 0, index < tiles.count else { return nil }
return self.tiles[index]
}
}
Expand Down
27 changes: 18 additions & 9 deletions Tests/PuzzleKitTests/Taiji/PKTaijiPuzzleValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Testing
@testable import PuzzleKit

private typealias VError = PKTaijiPuzzleValidatorError

@Suite("Taiji Puzzle Validator")
struct PKTaijiPuzzleValidatorTests {
@Test("Symbol lookup table constructs")
Expand Down Expand Up @@ -36,17 +38,24 @@ struct PKTaijiPuzzleValidatorTests {
#expect(validator.regionMap[.init(x: 6, y: 4)] == 5)
}

@Test("Flower constraints")
func validationFlowerConstraints() async throws {
let okFlowers = try PKTaijiPuzzle(decoding: "6:V02+B2X22W02+B2+G2+B202Y202Z2202+B20")
#expect(throws: Never.self) {
try okFlowers.validate().get()
@Test("Flower constraints", arguments: [
("6:V02+B2X22W02+B2+G2+B202Y202Z2202+B20", nil),
("3:2Y2Cw60Aw2Sw0Sw+C", nil),
("6:V02+B2X22W02+B2+G2+B202Y222Z2202+B20", VError.invalidPetalCount(.init(x: 2, y: 5)))
])
func validationFlowerConstraints(code: String, error: PKTaijiPuzzleValidatorError?) async throws {
let puzzle = try PKTaijiPuzzle(decoding: code)

if let error {
#expect(throws: error.self) {
try puzzle.validate(options: .whatTheTaiji).get()
}
} else {
#expect(throws: Never.self) {
try puzzle.validate(options: .whatTheTaiji).get()
}
}

let notOkFlowers = try PKTaijiPuzzle(decoding: "6:V02+B2X22W02+B2+G2+B202Y222Z2202+B20")
#expect(throws: PKTaijiPuzzleValidatorError.invalidPetalCount(.init(x: 2, y: 5))) {
try notOkFlowers.validate().get()
}
}

@Test("Diamond constraints (WTT)")
Expand Down

0 comments on commit 59909bb

Please sign in to comment.