diff --git a/TetrisKit/GameBoard.swift b/TetrisKit/GameBoard.swift index 433f260..e0dd027 100644 --- a/TetrisKit/GameBoard.swift +++ b/TetrisKit/GameBoard.swift @@ -12,41 +12,43 @@ public struct GameBoard { public let width: Int public let height: Int - public internal(set) var tiles: [[Bool]] + 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: false, count: width) + 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] } - mutating func set(piece: GamePiece) { + public mutating func set(piece: GamePiece) { let (left, top) = piece.topLeft for y in 0.. Bool { + return tiles[row].allSatisfy({ $0 != nil }) } } diff --git a/TetrisKit/GameController.swift b/TetrisKit/GameController.swift index 2d7cb40..f8796b7 100644 --- a/TetrisKit/GameController.swift +++ b/TetrisKit/GameController.swift @@ -48,16 +48,15 @@ public class GameController: ObservableObject { } func clearLines() { - let fullRow = Array(repeating: true, count: width) var row = height - 1 while row >= 0 { - if board.tiles[row] == fullRow { + if board.rowFull(row) { board.tiles.remove(at: row) } row -= 1 } for _ in 0.. Color { + if currentPieceAt(col, row) { + return self.currentPiece!.tetromino.color + } else if droppedPieceAt(col, row) { + return .gray + } else { + return .clear + } + } + func currentPieceAt(_ col: Int, _ row: Int) -> Bool { guard let currentPiece = self.currentPiece else { return false } let (left, top) = currentPiece.topLeft diff --git a/TetrisUI/Tetromino+Color.swift b/TetrisUI/Tetromino+Color.swift new file mode 100644 index 0000000..8d5c09a --- /dev/null +++ b/TetrisUI/Tetromino+Color.swift @@ -0,0 +1,41 @@ +// +// Tetromino+Color.swift +// Tetris +// +// Created by Shadowfacts on 10/15/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import SwiftUI +import TetrisKit + +fileprivate extension Color { + static let tetrominoAqua = Color(red: 0, green: 214/255, blue: 211/255) + static let tetrominoBlue = Color(red: 0, green: 79/255, blue: 214/255) + static let tetrominoOrange = Color(red: 255/255, green: 128/255, blue: 0) + static let tetrominoYellow = Color(red: 255/255, green: 229/255, blue: 0) + static let tetrominoGreen = Color(red: 0, green: 214/255, blue: 71/255) + static let tetrominoPurple = Color(red: 168/255, green: 0, blue: 214/255) + static let tetrominoRed = Color(red: 214/255, green: 0, blue: 61/255) +} + +extension Tetromino { + var color: Color { + switch self { + case .i: + return .tetrominoAqua + case .o: + return .tetrominoYellow + case .t: + return .tetrominoPurple + case .j: + return .tetrominoOrange + case .l: + return .tetrominoBlue + case .s: + return .tetrominoRed + case .z: + return .tetrominoGreen + } + } +} diff --git a/TetrisUI/TilesView.swift b/TetrisUI/TilesView.swift index b9ec1aa..ef6764c 100644 --- a/TetrisUI/TilesView.swift +++ b/TetrisUI/TilesView.swift @@ -16,7 +16,7 @@ struct TilesView: View { GridView(rows: board.height, columns: board.width) { (col, row, size) in Rectangle() .frame(width: size, height: size) - .foregroundColor(self.board[col, row] ? Color.red : Color.black) + .foregroundColor(self.board[col, row]?.color ?? Color.clear) } } } @@ -24,7 +24,9 @@ struct TilesView: View { struct TilesView_Previews: PreviewProvider { @State static var board: GameBoard = { var b = GameBoard(width: 10, height: 16) - b.set(tiles: [(0, 15), (1, 15), (1, 14), (2, 14)]) + var piece = GamePiece(tetromino: .z) + piece.topLeft = (0, 14) + b.set(piece: piece) return b }()