Add basic score calculation
This commit is contained in:
parent
3c9f551542
commit
9a765b7eda
|
@ -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)
|
||||||
|
|
|
@ -26,6 +26,9 @@ public class GameController: ObservableObject {
|
||||||
@Published public var currentPieceAtDropPoint: GamePiece?
|
@Published public var currentPieceAtDropPoint: GamePiece?
|
||||||
@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() {
|
||||||
|
|
Loading…
Reference in New Issue