-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.swift
241 lines (200 loc) · 5.92 KB
/
Board.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// Board.swift
// Tetris Engine
//
// Created by Martin Kiss on 17 Jan 2017.
// https://github.com/Tricertops/TetrisEngine
//
// The MIT License (MIT)
// Copyright © 2017 Martin Kiss
//
import Foundation
public struct Position: Serializable {
public var x: Int
public var y: Int
public init(x: Int, y: Int) {
self.x = x
self.y = y
}
public var above: Position {
return Position(x: x, y: y + 1)
}
public var below: Position {
return Position(x: x, y: y - 1)
}
public var left: Position {
return Position(x: x - 1, y: y)
}
public var right: Position {
return Position(x: x + 1, y: y)
}
typealias Representation = [Int]
func serialize() -> Representation {
return [x, y]
}
static func deserialize(_ array: Representation) -> Position? {
guard array.count == 2 else { return nil }
return Position(x: array[0], y: array[1])
}
}
final class Board: Serializable {
let width: Int
let height: Int
var cells: [Cell] = []
init(width: Int, height: Int) {
self.width = width
self.height = height
cells = []
clear()
}
func index(for position: Position) -> Int {
return position.y * width + position.x
}
func cell(at: Position) -> Cell {
guard at.x >= 0 else { return .outer }
guard at.y >= 0 else { return .outer }
guard at.x < width else { return .outer }
guard at.y < height else { return .outer }
return cells[index(for: at)]
}
func set(cell: Cell, at: Position) {
if case .outer = cell { return }
guard at.x >= 0 else { return }
guard at.y >= 0 else { return }
guard at.x < width else { return }
guard at.y < height else { return }
cells[index(for: at)] = cell
}
func clear() {
cells = Array(repeating: .empty, count: width * height)
}
func isFrozen(above: Int) -> Bool {
for y in above..<height {
for x in 0..<width {
if cell(at: Position(x: x, y: y)).isFree.not {
return yes
}
}
}
return no
}
func print() {
var lines: [String] = []
for y in 0..<height {
lines.insert("", at: 0)
for x in 0..<width {
let c = cell(at: Position(x: x, y: y))
lines[0] += c.visualDescription
}
}
Swift.print(lines.joined(separator: "\n"))
}
func findCompletedLines() -> IndexSet {
var lines = IndexSet()
for y in 0..<height {
if isCompleted(line: y) {
lines.insert(y)
}
}
return lines
}
func isCompleted(line: Int) -> Bool {
for x in 0..<width {
if cell(at: Position(x: x, y: line)).isCompleted.not {
return no
}
}
return yes
}
func remove(lines: IndexSet) {
for y in lines.reversed() {
for x in (0..<width).reversed() {
let position = Position(x: x, y: y)
cells.remove(at: index(for: position))
cells.append(.empty)
}
}
}
typealias Representation = [String: Any]
func serialize() -> Representation {
var dict: Representation = [:]
dict["width"] = width
dict["height"] = height
dict["cells"] = cells.map { $0.serialize() }
return dict
}
static func deserialize(_ dict: Representation) -> Board? {
guard let width = dict["width"] as? Int else { return nil }
guard let height = dict["height"] as? Int else { return nil }
let board = Board(width: width, height: height)
guard let cellsStrings = dict["cells"] as? [String] else { return nil }
guard cellsStrings.count == width * height else { return nil }
var cells: [Cell] = []
for s in cellsStrings {
guard let cell = Cell.deserialize(s) else { return nil }
cells.append(cell)
}
board.cells = cells
return board
}
}
public enum Cell: Serializable {
case empty
case frozen(shape: Block.Shape)
case falling(shape: Block.Shape)
case outer
public var isEmpty: Bool {
switch self {
case .empty: return yes
default: return no
}
}
var isFree: Bool {
switch self {
case .empty, .falling:
return yes
default:
return no
}
}
var isCompleted: Bool {
switch self {
case .frozen: return yes
default: return no
}
}
var visualDescription: String {
switch self {
case .empty: return "∙"
case .frozen: return "█"
case .falling: return "▓"
case .outer: return "╳"
}
}
typealias Representation = String
func serialize() -> Representation {
switch self {
case .empty: return " "
case .frozen(let shape): return shape.serialize().uppercased()
case .falling(let shape): return shape.serialize().lowercased()
case .outer: return "X"
}
}
static func deserialize(_ string: Representation) -> Cell? {
if string == " " {
return .empty
}
if string == "X" {
return .outer
}
if string == string.uppercased() {
guard let shape = Block.Shape.deserialize(string) else { return nil }
return .frozen(shape: shape)
}
if string == string.lowercased() {
guard let shape = Block.Shape.deserialize(string) else { return nil }
return .falling(shape: shape)
}
return nil
}
}