Use 7-bag for tetromino sequence generation
This commit is contained in:
parent
0cecbfc5ae
commit
68fab068d9
|
@ -24,9 +24,11 @@ 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] = []
|
||||||
@Published public var heldTetromino: Tetromino?
|
@Published public var heldTetromino: Tetromino?
|
||||||
|
|
||||||
|
var currentBag: [Tetromino] = []
|
||||||
|
|
||||||
@Published public var score = 0
|
@Published public var score = 0
|
||||||
var previousPieceWasTetris = false
|
var previousPieceWasTetris = false
|
||||||
|
|
||||||
|
@ -41,13 +43,26 @@ public class GameController: ObservableObject {
|
||||||
|
|
||||||
public func start() {
|
public func start() {
|
||||||
state = .playing(.normal)
|
state = .playing(.normal)
|
||||||
|
generateBag()
|
||||||
nextPiece()
|
nextPiece()
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextPiece() {
|
func nextPiece() {
|
||||||
let tetromino = nextTetrominoes.removeFirst()
|
let tetromino = nextTetrominoes.removeFirst()
|
||||||
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
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() {
|
func finalizePiece() {
|
||||||
|
@ -77,10 +92,15 @@ public class GameController: ObservableObject {
|
||||||
previousPieceWasTetris = true
|
previousPieceWasTetris = true
|
||||||
} else if cleared == 3 {
|
} else if cleared == 3 {
|
||||||
score += 500
|
score += 500
|
||||||
|
previousPieceWasTetris = false
|
||||||
} else if cleared == 2 {
|
} else if cleared == 2 {
|
||||||
score += 300
|
score += 300
|
||||||
|
previousPieceWasTetris = false
|
||||||
} else if cleared == 1 {
|
} else if cleared == 1 {
|
||||||
score += 100
|
score += 100
|
||||||
|
previousPieceWasTetris = false
|
||||||
|
} else {
|
||||||
|
previousPieceWasTetris = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue