Use 7-bag for tetromino sequence generation

This commit is contained in:
Shadowfacts 2019-10-16 21:39:30 -04:00
parent 0cecbfc5ae
commit 68fab068d9
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 22 additions and 2 deletions

View File

@ -24,9 +24,11 @@ public class GameController: ObservableObject {
}
}
@Published public var currentPieceAtDropPoint: GamePiece?
@Published public var nextTetrominoes: [Tetromino] = [.random(), .random(), .random()]
@Published public var nextTetrominoes: [Tetromino] = []
@Published public var heldTetromino: Tetromino?
var currentBag: [Tetromino] = []
@Published public var score = 0
var previousPieceWasTetris = false
@ -41,13 +43,26 @@ public class GameController: ObservableObject {
public func start() {
state = .playing(.normal)
generateBag()
nextPiece()
}
func nextPiece() {
let tetromino = nextTetrominoes.removeFirst()
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
nextTetrominoes.append(.random())
if currentBag.isEmpty {
generateBag()
}
nextTetrominoes.append(currentBag.removeFirst())
}
func generateBag() {
currentBag = Array(0..<Tetromino.allCases.count).shuffled().map { Tetromino.allCases[$0] }
if nextTetrominoes.isEmpty {
nextTetrominoes = Array(currentBag[0..<3])
currentBag.removeFirst(3)
}
}
func finalizePiece() {
@ -77,10 +92,15 @@ public class GameController: ObservableObject {
previousPieceWasTetris = true
} else if cleared == 3 {
score += 500
previousPieceWasTetris = false
} else if cleared == 2 {
score += 300
previousPieceWasTetris = false
} else if cleared == 1 {
score += 100
previousPieceWasTetris = false
} else {
previousPieceWasTetris = false
}
}