Crossmate/Models/Game.swift (9483B)
1import Foundation
2import Observation
3
4
5
6
7
8
9@MainActor
10@Observable
11final class Game {
12 let puzzle: Puzzle
13 var squares: [[Square]]
14 @ObservationIgnored private let fillableCellCount: Int
15 private var filledCellCount: Int = 0
16 private var wrongCellCount: Int = 0
17
18 init(puzzle: Puzzle) {
19 self.puzzle = puzzle
20
21
22
23 self.fillableCellCount = puzzle.cells.reduce(0) { count, row in
24 count + row.filter { !$0.isBlock && $0.solution != nil && !$0.expectsBlank }.count
25 }
26 self.squares = Array(
27 repeating: Array(repeating: Square(), count: puzzle.width),
28 count: puzzle.height
29 )
30 }
31
32
33
34
35
36
37
38
39
40
41 func setLetter(_ letter: String, atRow row: Int, atCol col: Int, pencil: Bool, authorID: String? = nil) {
42 let cell = puzzle.cells[row][col]
43 guard !cell.isBlock else { return }
44 guard !squares[row][col].mark.isRevealed else { return }
45 let oldEntry = squares[row][col].entry
46 let newEntry = letter.uppercased()
47 squares[row][col].entry = newEntry
48 squares[row][col].mark = pencil ? .pencil(checked: nil) : .none
49 if newEntry != oldEntry {
50 squares[row][col].letterAuthorID = authorID
51 }
52 noteEntryChange(from: oldEntry, to: newEntry, for: cell)
53 }
54
55
56 func clearLetter(atRow row: Int, atCol col: Int) {
57 let cell = puzzle.cells[row][col]
58 guard !cell.isBlock else { return }
59 guard !squares[row][col].mark.isRevealed else { return }
60 let oldEntry = squares[row][col].entry
61 squares[row][col].entry = ""
62 squares[row][col].mark = .none
63 squares[row][col].letterAuthorID = nil
64 noteEntryChange(from: oldEntry, to: "", for: cell)
65 }
66
67
68
69
70
71 func applyCellState(_ letter: String, mark: CellMark, authorID: String?, atRow row: Int, atCol col: Int) {
72 let cell = puzzle.cells[row][col]
73 guard !cell.isBlock else { return }
74 let oldEntry = squares[row][col].entry
75 let newEntry = letter.uppercased()
76 squares[row][col].entry = newEntry
77 squares[row][col].mark = mark
78 squares[row][col].letterAuthorID = authorID
79 noteEntryChange(from: oldEntry, to: newEntry, for: cell)
80 }
81
82
83
84
85
86
87
88
89
90
91
92 func checkCells(_ cells: [Puzzle.Cell]) {
93 for cell in cells {
94 guard !cell.isBlock else { continue }
95 guard cell.solution != nil else { continue }
96 let entry = squares[cell.row][cell.col].entry
97 guard !entry.isEmpty else { continue }
98 guard !squares[cell.row][cell.col].mark.isRevealed else { continue }
99
100 let result: CheckResult = cell.accepts(entry) ? .right : .wrong
101 switch squares[cell.row][cell.col].mark {
102 case .pencil:
103 squares[cell.row][cell.col].mark = result == .right
104 ? .pen(checked: .right)
105 : .pencil(checked: .wrong)
106 case .none, .pen:
107 squares[cell.row][cell.col].mark = .pen(checked: result)
108 case .revealed:
109 break
110 }
111 }
112 }
113
114 func checkPuzzle() {
115 checkCells(puzzle.cells.flatMap { $0 })
116 }
117
118
119
120
121
122
123 func revealCells(_ cells: [Puzzle.Cell]) {
124 for cell in cells {
125 guard !cell.isBlock else { continue }
126 guard let solution = cell.solution else { continue }
127 let expected = solution.uppercased()
128 let oldEntry = squares[cell.row][cell.col].entry
129 if cell.accepts(oldEntry) { continue }
130 squares[cell.row][cell.col].entry = expected
131 squares[cell.row][cell.col].mark = .revealed
132 squares[cell.row][cell.col].letterAuthorID = nil
133 noteEntryChange(from: oldEntry, to: expected, for: cell)
134 }
135 }
136
137 func revealPuzzle() {
138 revealCells(puzzle.cells.flatMap { $0 })
139 }
140
141
142
143
144 func clearCells(_ cells: [Puzzle.Cell]) {
145 for cell in cells {
146 guard !cell.isBlock else { continue }
147 guard !squares[cell.row][cell.col].mark.isRevealed else { continue }
148 let oldEntry = squares[cell.row][cell.col].entry
149 squares[cell.row][cell.col].entry = ""
150 squares[cell.row][cell.col].mark = .none
151 squares[cell.row][cell.col].letterAuthorID = nil
152 noteEntryChange(from: oldEntry, to: "", for: cell)
153 }
154 }
155
156 func clearPuzzle() {
157 clearCells(puzzle.cells.flatMap { $0 })
158 }
159
160
161
162 enum CompletionState: Sendable, Equatable {
163 case incomplete
164 case filledWithErrors
165 case solved
166 }
167
168
169
170
171
172 var completionState: CompletionState {
173 guard filledCellCount == fillableCellCount else { return .incomplete }
174 return wrongCellCount > 0 ? .filledWithErrors : .solved
175 }
176
177
178
179 func recomputeCompletionCache() {
180 filledCellCount = 0
181 wrongCellCount = 0
182 for r in 0..<puzzle.height {
183 for c in 0..<puzzle.width {
184 let cell = puzzle.cells[r][c]
185 guard !cell.isBlock, cell.solution != nil else { continue }
186 let entry = squares[r][c].entry
187 if !entry.isEmpty, !cell.expectsBlank { filledCellCount += 1 }
188 if isWrongEntry(entry, for: cell) { wrongCellCount += 1 }
189 }
190 }
191 }
192
193 private func noteEntryChange(from oldEntry: String, to newEntry: String, for cell: Puzzle.Cell) {
194 guard cell.solution != nil else { return }
195
196
197
198
199 if !cell.expectsBlank {
200 if oldEntry.isEmpty, !newEntry.isEmpty {
201 filledCellCount += 1
202 } else if !oldEntry.isEmpty, newEntry.isEmpty {
203 filledCellCount -= 1
204 }
205 }
206
207 let wasWrong = isWrongEntry(oldEntry, for: cell)
208 let isWrong = isWrongEntry(newEntry, for: cell)
209 if wasWrong, !isWrong {
210 wrongCellCount -= 1
211 } else if !wasWrong, isWrong {
212 wrongCellCount += 1
213 }
214 }
215
216 private func isWrongEntry(_ entry: String, for cell: Puzzle.Cell) -> Bool {
217 guard !entry.isEmpty, cell.solution != nil else { return false }
218 return !cell.accepts(entry)
219 }
220}