Tetris/TetrisKit/GameBoard.swift

53 lines
1.2 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: [[Bool]]
public init(width: Int, height: Int) {
self.width = width
self.height = height
tiles = (1...height).map { _ in
Array(repeating: false, count: width)
}
}
public subscript(x: Int, y: Int) -> Bool {
return self.tiles[y][x]
}
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] = true
}
}
}
}
public mutating func set(tiles: [(Int, Int)]) {
for (x, y) in tiles {
self.tiles[y][x] = true
}
}
mutating func unset(tiles: [(Int, Int)]) {
for (x, y) in tiles {
self.tiles[y][x] = false
}
}
}