Skip to content

Commit 6b4bd6d

Browse files
author
Oleksii Oliinyk
committed
Update Swift tools version to 6.0, refactor bounds checking methods, and enhance documentation for grid index neighbors.
1 parent 81df19e commit 6b4bd6d

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.6
1+
// swift-tools-version:6.0
22
import PackageDescription
33

44
let package = Package(
@@ -11,5 +11,5 @@ let package = Package(
1111
.target(name: "Match3Kit"),
1212
.testTarget(name: "Match3KitTests", dependencies: ["Match3Kit"])
1313
],
14-
swiftLanguageVersions: [.v5]
14+
swiftLanguageModes: [.v6]
1515
)

Sources/Match3Kit/Controller.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public struct Pattern: Hashable {
4949

5050
for i in columns {
5151
for j in rows {
52-
let figureIndices = indices.lazy.map { Index(column: $0.column + i,
53-
row: $0.row + j) }
52+
let figureIndices = indices.map {
53+
Index(column: $0.column + i,
54+
row: $0.row + j)
55+
}
5456
if containedIn.isSuperset(of: figureIndices) {
5557
return Set(figureIndices)
5658
}
@@ -213,7 +215,7 @@ public final class Controller<Filling: GridFilling, GeneratorType: Generator<Fil
213215
public func findPossibleSwap() -> (Index, Index)? {
214216
for index in grid.allIndices() {
215217
for neighbour in index.neighbors
216-
where grid.size.isOnBounds(neighbour) && hasMatchesExcanging(index, and: neighbour) {
218+
where grid.size.isInBounds(neighbour) && hasMatchesExcanging(index, and: neighbour) {
217219
return (index, neighbour)
218220
}
219221
}

Sources/Match3Kit/Grid.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ public struct Size: Hashable, Codable {
3434
self.rows = rows
3535
}
3636

37+
/// Checks if an index is within the bounds of the grid.
38+
///
39+
/// - Parameter index: The index to check.
40+
/// - Returns: `true` if the index is within the bounds of the grid, `false` otherwise.
3741
@inlinable
38-
public func isOnBounds(_ index: Index) -> Bool {
42+
public func isInBounds(_ index: Index) -> Bool {
3943
index.column > leftBound && index.column < rightBound &&
4044
index.row > lowerBound && index.row < upperBound
4145
}
@@ -106,13 +110,35 @@ public struct Index: Hashable, Codable, CustomStringConvertible {
106110
Index(column: column - 1, row: row)
107111
}
108112

113+
/// Returns an array of immediate neighbors of the current index.
114+
///
115+
/// The neighbors are the indices immediately adjacent to the current index,
116+
/// including the upper, lower, right, and left neighbors.
117+
///
118+
/// Example usage:
119+
/// ```
120+
/// let index = Index(column: 2, row: 2)
121+
/// let neighbors = index.neighbors
122+
/// print(neighbors) // Prints: "[Index(column: 1, row: 2), Index(column: 3, row: 2), Index(column: 2, row: 1), Index(column: 2, row: 3)]"
123+
/// ```
109124
@inlinable
110125
public var neighbors: [Index] {
111126
[left, upper, right, lower]
112127
}
113128

129+
/// Returns an array of diagonal neighbors of the current index.
130+
///
131+
/// The diagonal neighbors are the indices diagonally adjacent to the current index,
132+
/// including the upper-left, upper-right, lower-left, and lower-right neighbors.
133+
///
134+
/// Example usage:
135+
/// ```
136+
/// let index = Index(column: 2, row: 2)
137+
/// let diagonalNeighbors = index.diagonalNeighbors
138+
/// print(diagonalNeighbors) // Prints: "[Index(column: 1, row: 1), Index(column: 3, row: 3), Index(column: 1, row: 3), Index(column: 3, row: 1)]"
139+
/// ```
114140
@inlinable
115-
public var crossNeighbors: [Index] {
141+
public var diagonalNeighbors: [Index] {
116142
[Index(column: column - 1, row: row - 1),
117143
Index(column: column + 1, row: row + 1),
118144
Index(column: column - 1, row: row + 1),
@@ -255,10 +281,10 @@ public struct Grid<Filling>: Codable where Filling: GridFilling {
255281
columns[index.column][index.row] = cell
256282
}
257283

258-
public mutating func remove(cells indicies: Set<Index>) -> Set<Index> {
259-
let cells = indicies.map(cell(at:))
284+
public mutating func remove(cells indices: Set<Index>) -> Set<Index> {
285+
let cells = indices.map(cell(at:))
260286
var removedIndices = Set<Index>()
261-
removedIndices.reserveCapacity(indicies.count)
287+
removedIndices.reserveCapacity(indices.count)
262288
for i in columns.indices {
263289
let start = columns[i].stablePartition { cells.contains($0) }
264290
let columnIndices = columns[i][start...].indices.map { Index(column: i, row: $0) }

Sources/Match3Kit/Matcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ open class Matcher<Filling: GridFilling> {
101101
sequence: some Sequence<Index>
102102
) -> [Index] {
103103
sequence.prefix {
104-
grid.size.isOnBounds($0) && match(cell: grid.cell(at: $0), with: cell)
104+
grid.size.isInBounds($0) && match(cell: grid.cell(at: $0), with: cell)
105105
}
106106
}
107107
}

Tests/Match3KitTests/GridTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ final class SizeTests: XCTestCase {
2828
func testIsOnBounds() {
2929
let size = Size(columns: 5, rows: 10)
3030
// Testing within bounds
31-
XCTAssertTrue(size.isOnBounds(Index(column: 0, row: 0)))
32-
XCTAssertTrue(size.isOnBounds(Index(column: 4, row: 9)))
31+
XCTAssertTrue(size.isInBounds(Index(column: 0, row: 0)))
32+
XCTAssertTrue(size.isInBounds(Index(column: 4, row: 9)))
3333
// Testing at bounds
34-
XCTAssertFalse(size.isOnBounds(Index(column: -1, row: 0)))
35-
XCTAssertFalse(size.isOnBounds(Index(column: 5, row: 0)))
36-
XCTAssertFalse(size.isOnBounds(Index(column: 0, row: -1)))
37-
XCTAssertFalse(size.isOnBounds(Index(column: 0, row: 10)))
34+
XCTAssertFalse(size.isInBounds(Index(column: -1, row: 0)))
35+
XCTAssertFalse(size.isInBounds(Index(column: 5, row: 0)))
36+
XCTAssertFalse(size.isInBounds(Index(column: 0, row: -1)))
37+
XCTAssertFalse(size.isInBounds(Index(column: 0, row: 10)))
3838
// Testing out of bounds
39-
XCTAssertFalse(size.isOnBounds(Index(column: -2, row: 0)))
40-
XCTAssertFalse(size.isOnBounds(Index(column: 6, row: 0)))
41-
XCTAssertFalse(size.isOnBounds(Index(column: 0, row: -2)))
42-
XCTAssertFalse(size.isOnBounds(Index(column: 0, row: 11)))
39+
XCTAssertFalse(size.isInBounds(Index(column: -2, row: 0)))
40+
XCTAssertFalse(size.isInBounds(Index(column: 6, row: 0)))
41+
XCTAssertFalse(size.isInBounds(Index(column: 0, row: -2)))
42+
XCTAssertFalse(size.isInBounds(Index(column: 0, row: 11)))
4343
}
4444
}
4545

@@ -67,7 +67,7 @@ final class IndexTests: XCTestCase {
6767

6868
func testCrossNeighbors() {
6969
let index = Index(column: 5, row: 10)
70-
XCTAssertEqual(index.crossNeighbors, [
70+
XCTAssertEqual(index.diagonalNeighbors, [
7171
Index(column: 4, row: 9),
7272
Index(column: 6, row: 11),
7373
Index(column: 4, row: 11),

0 commit comments

Comments
 (0)