Tetris/TetrisKit/GameBoard.swift

55 lines
1.3 KiB
Swift

//
// GameBoard.swift
// TetrisKit
//
// Created by Shadowfacts on 10/13/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import Foundation
public struct GameBoard {
public let width: Int
public let height: Int
public internal(set) var tiles: [[Tetromino?]]
public init(width: Int, height: Int) {
self.width = width
self.height = height
tiles = (1...height).map { _ in
Array(repeating: nil, count: width)
}
}
public subscript(x: Int, y: Int) -> Bool {
return self.tiles[y][x] != nil
}
public subscript(x: Int, y: Int) -> Tetromino? {
return self.tiles[y][x]
}
public mutating func set(piece: GamePiece) {
let (left, top) = piece.topLeft
for y in 0..<piece.tiles.count where y + top < height {
for x in 0..<piece.tiles.first!.count where x + left < width {
if piece.tiles[y][x] {
self.tiles[y + top][x + left] = piece.tetromino
}
}
}
}
mutating func clear(tiles: [(Int, Int)]) {
for (x, y) in tiles {
self.tiles[y][x] = nil
}
}
func rowFull(_ row: Int) -> Bool {
return tiles[row].allSatisfy({ $0 != nil })
}
}