From 68fab068d9e1d3cf3d28acd2eb56087ea037bab3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 16 Oct 2019 21:39:30 -0400 Subject: [PATCH] Use 7-bag for tetromino sequence generation --- TetrisKit/GameController.swift | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/TetrisKit/GameController.swift b/TetrisKit/GameController.swift index 83a37d0..00b104f 100644 --- a/TetrisKit/GameController.swift +++ b/TetrisKit/GameController.swift @@ -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..