Add basic score calculation

This commit is contained in:
Shadowfacts 2019-10-16 17:49:16 -04:00
parent 3c9f551542
commit 9a765b7eda
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 19 additions and 1 deletions

View File

@ -34,6 +34,8 @@ struct ContentView: View {
} else { } else {
Rectangle().foregroundColor(.clear).frame(width: 50, height: 50) Rectangle().foregroundColor(.clear).frame(width: 50, height: 50)
} }
Text("Score")
Text(verbatim: self.controller.score.description)
} }
.padding(.leading, 8) .padding(.leading, 8)
BoardView(board: self.$controller.board, currentPiece: self.$controller.currentPiece, droppedPiece: self.$controller.currentPieceAtDropPoint) BoardView(board: self.$controller.board, currentPiece: self.$controller.currentPiece, droppedPiece: self.$controller.currentPieceAtDropPoint)

View File

@ -27,6 +27,9 @@ public class GameController: ObservableObject {
@Published public var nextTetrominoes: [Tetromino] = [.random(), .random(), .random()] @Published public var nextTetrominoes: [Tetromino] = [.random(), .random(), .random()]
@Published public var heldTetromino: Tetromino? @Published public var heldTetromino: Tetromino?
@Published public var score = 0
var previousPieceWasTetris = false
public var ended: Bool { public var ended: Bool {
return (0..<width).first(where: { board[$0, 0] }) != nil return (0..<width).first(where: { board[$0, 0] }) != nil
} }
@ -56,16 +59,29 @@ public class GameController: ObservableObject {
} }
func clearLines() { func clearLines() {
var cleared = 0
var row = height - 1 var row = height - 1
while row >= 0 { while row >= 0 {
if board.rowFull(row) { if board.rowFull(row) {
board.tiles.remove(at: row) board.tiles.remove(at: row)
cleared += 1
} }
row -= 1 row -= 1
} }
for _ in 0..<height - board.tiles.count { for _ in 0..<cleared {
board.tiles.insert(Array(repeating: nil, count: width), at: 0) board.tiles.insert(Array(repeating: nil, count: width), at: 0)
} }
if cleared == 4 {
score += previousPieceWasTetris ? 1200 : 800
previousPieceWasTetris = true
} else if cleared == 3 {
score += 500
} else if cleared == 2 {
score += 300
} else if cleared == 1 {
score += 100
}
} }
public func step() { public func step() {