Tetris/TetrisKit/Tetromino.swift

69 lines
1022 B
Swift

//
// Tetromino.swift
// TetrisKit
//
// Created by Shadowfacts on 10/13/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import Foundation
public enum Tetromino: CaseIterable {
case i, o, t, j, l, s, z
public var shape: [[Bool]] {
return Tetromino.shapes[self]!
}
}
extension Tetromino {
static var shapes: [Tetromino: [[Bool]]] = [
.i: parseShape("""
XXXX
----
----
----
"""),
.o: parseShape("""
XX
XX
"""),
.t: parseShape("""
XXX
-X-
---
"""),
.j: parseShape("""
XXX
X--
---
"""),
.l: parseShape("""
XXX
--X
---
"""),
.s: parseShape("""
XX-
-XX
---
"""),
.z: parseShape("""
-XX
XX-
---
""")
]
static func parseShape(_ str: String) -> [[Bool]] {
let lines = str.split(separator: "\n")
return lines.map { line in line.map { char in char == "X" } }
}
}
extension Tetromino {
static func random() -> Tetromino {
return allCases[Int.random(in: 0..<allCases.count)]
}
}