2020-05-24 21:56:11 -06:00
|
|
|
//
|
|
|
|
// Privyet.swift
|
|
|
|
// Privyet
|
|
|
|
//
|
|
|
|
// Created by Amy Bowersox on 5/24/20.
|
|
|
|
// Copyright © 2020 Erbosoft Metaverse Design Solutions. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
let NumColumns = 10
|
|
|
|
let NumRows = 20
|
|
|
|
|
|
|
|
let StartingColumn = 4
|
|
|
|
let StartingRow = 0
|
|
|
|
|
|
|
|
let PreviewColumn = 12
|
|
|
|
let PreviewRow = 1
|
|
|
|
|
|
|
|
let PointsPerLine = 10
|
|
|
|
let LevelThreshold = 500
|
|
|
|
|
|
|
|
protocol PrivyetDelegate {
|
|
|
|
// Invoked when the current round of Privyet ends
|
|
|
|
func gameDidEnd(privyet: Privyet)
|
|
|
|
|
|
|
|
// Invoked after a game has begun
|
|
|
|
func gameDidBegin(privyet: Privyet)
|
|
|
|
|
|
|
|
// Invoked when the falling shape has become part of the game board
|
|
|
|
func gameShapeDidLand(privyet: Privyet)
|
|
|
|
|
|
|
|
// Invoked when the falling shape has changed its location
|
|
|
|
func gameShapeDidMove(privyet: Privyet)
|
|
|
|
|
|
|
|
// invoked when the falling shape has changed its location after being dropped
|
|
|
|
func gameShapeDidDrop(privyet: Privyet)
|
|
|
|
|
|
|
|
// invoked when the game has reached a new level
|
|
|
|
func gameDidLevelUp(privyet: Privyet)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Privyet {
|
2020-05-27 11:10:50 -06:00
|
|
|
var bucket: Array2D<Block>
|
2020-05-24 21:56:11 -06:00
|
|
|
var nextShape: Shape?
|
|
|
|
var fallingShape: Shape?
|
|
|
|
var delegate: PrivyetDelegate?
|
|
|
|
|
|
|
|
var score = 0
|
|
|
|
var level = 1
|
|
|
|
|
|
|
|
init() {
|
|
|
|
fallingShape = nil
|
|
|
|
nextShape = nil
|
2020-05-27 11:10:50 -06:00
|
|
|
bucket = Array2D<Block>(columns: NumColumns, rows: NumRows)
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Starts the game by initializing game data
|
2020-05-24 21:56:11 -06:00
|
|
|
func beginGame() {
|
|
|
|
if (nextShape == nil) {
|
|
|
|
nextShape = Shape.random(startingColumn: PreviewColumn, startingRow: PreviewRow)
|
|
|
|
}
|
|
|
|
delegate?.gameDidBegin(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Advances the current falling shape and the "next" shape. Returns both these shapes as a tuple.
|
2020-05-24 21:56:11 -06:00
|
|
|
func newShape() -> (fallingShape: Shape?, nextShape: Shape?) {
|
|
|
|
fallingShape = nextShape
|
|
|
|
nextShape = Shape.random(startingColumn: PreviewColumn, startingRow: PreviewRow)
|
|
|
|
fallingShape?.moveTo(column: StartingColumn, row: StartingRow)
|
|
|
|
|
|
|
|
guard detectIllegalPlacement() == false else {
|
|
|
|
nextShape = fallingShape
|
|
|
|
nextShape!.moveTo(column: PreviewColumn, row: PreviewRow)
|
|
|
|
endGame()
|
|
|
|
return (nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (fallingShape, nextShape)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Returns true if the falling shape is in an "illegal" placement (outside bucket bounds or "colliding"
|
|
|
|
// with existing blocks in the bucket).
|
2020-05-24 21:56:11 -06:00
|
|
|
func detectIllegalPlacement() -> Bool {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for block in shape.blocks {
|
|
|
|
if block.column < 0 || block.column >= NumColumns || block.row < 0 || block.row >= NumRows {
|
|
|
|
return true
|
2020-05-27 11:10:50 -06:00
|
|
|
} else if bucket[block.column, block.row] != nil {
|
2020-05-24 21:56:11 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// "Settle" the falling shape by transferring its blocks to the bucket.
|
2020-05-24 21:56:11 -06:00
|
|
|
func settleShape() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for block in shape.blocks {
|
2020-05-27 11:10:50 -06:00
|
|
|
bucket[block.column, block.row] = block
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
fallingShape = nil
|
|
|
|
delegate?.gameShapeDidLand(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Returns true if the falling block is "landing" (at the bottom of the bucket or directly above existing blocks)
|
2020-05-24 21:56:11 -06:00
|
|
|
func detectTouch() -> Bool {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for bottomBlock in shape.bottomBlocks {
|
2020-05-27 11:10:50 -06:00
|
|
|
if bottomBlock.row == NumRows - 1 || bucket[bottomBlock.column, bottomBlock.row + 1] != nil {
|
2020-05-24 21:56:11 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Ends the current game
|
2020-05-24 21:56:11 -06:00
|
|
|
func endGame() {
|
|
|
|
score = 0
|
|
|
|
level = 1
|
|
|
|
delegate?.gameDidEnd(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Detect and remove completed lines. Returns a tuple of two arrays of arrays:
|
|
|
|
// linesRemoved - The blocks that will be removed as a result of completed lines.
|
|
|
|
// fallenBlocks - The blocks that will drop to a lower line in the bucket when the above blocks are removed.
|
|
|
|
// If no lines are to be removed, returns a pair of empty lists.
|
2020-05-24 21:56:11 -06:00
|
|
|
func removeCompletedLines() -> (linesRemoved: Array<Array<Block>>, fallenBlocks: Array<Array<Block>>) {
|
|
|
|
var removedLines = Array<Array<Block>>()
|
|
|
|
for row in (1..<NumRows).reversed() {
|
|
|
|
var rowOfBlocks = Array<Block>()
|
|
|
|
for column in 0..<NumColumns {
|
2020-05-27 11:10:50 -06:00
|
|
|
guard let block = bucket[column, row] else {
|
2020-05-24 21:56:11 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rowOfBlocks.append(block)
|
|
|
|
}
|
|
|
|
if rowOfBlocks.count == NumColumns {
|
2020-05-27 11:10:50 -06:00
|
|
|
// filled line detected, remove it
|
2020-05-24 21:56:11 -06:00
|
|
|
removedLines.append(rowOfBlocks)
|
|
|
|
for block in rowOfBlocks {
|
2020-05-27 11:10:50 -06:00
|
|
|
bucket[block.column, block.row] = nil
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if removedLines.count == 0 {
|
2020-05-27 11:10:50 -06:00
|
|
|
return ([], []) // no lines filled
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
2020-05-27 11:10:50 -06:00
|
|
|
|
|
|
|
// Advance score and game level as appropriate
|
2020-05-24 21:56:11 -06:00
|
|
|
let pointsEarned = removedLines.count * PointsPerLine * level
|
|
|
|
score += pointsEarned
|
|
|
|
if score >= level * LevelThreshold {
|
|
|
|
level += 1
|
|
|
|
delegate?.gameDidLevelUp(privyet: self)
|
|
|
|
}
|
2020-05-27 11:10:50 -06:00
|
|
|
|
2020-05-24 21:56:11 -06:00
|
|
|
var fallenBlocks = Array<Array<Block>>()
|
|
|
|
for column in 0..<NumColumns {
|
|
|
|
var fallenBlocksArray = Array<Block>()
|
|
|
|
for row in (1..<removedLines[0][0].row).reversed() {
|
2020-05-27 11:10:50 -06:00
|
|
|
guard let block = bucket[column, row] else {
|
2020-05-24 21:56:11 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var newRow = row
|
2020-05-27 11:10:50 -06:00
|
|
|
while (newRow < NumRows - 1 && bucket[column, newRow + 1] == nil) {
|
2020-05-24 21:56:11 -06:00
|
|
|
newRow += 1
|
|
|
|
}
|
|
|
|
block.row = newRow
|
2020-05-27 11:10:50 -06:00
|
|
|
bucket[column, row] = nil
|
|
|
|
bucket[column, newRow] = block
|
2020-05-24 21:56:11 -06:00
|
|
|
fallenBlocksArray.append(block)
|
|
|
|
}
|
|
|
|
if fallenBlocksArray.count > 0 {
|
|
|
|
fallenBlocks.append(fallenBlocksArray)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (removedLines, fallenBlocks)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Removes all blocks from the bucket (at end of game). Returns all the removed blocks.
|
2020-05-24 21:56:11 -06:00
|
|
|
func removeAllBlocks() -> Array<Array<Block>> {
|
|
|
|
var allBlocks = Array<Array<Block>>()
|
|
|
|
for row in 0..<NumRows {
|
|
|
|
var rowOfBlocks = Array<Block>()
|
|
|
|
for column in 0..<NumColumns {
|
2020-05-27 11:10:50 -06:00
|
|
|
guard let block = bucket[column, row] else {
|
2020-05-24 21:56:11 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rowOfBlocks.append(block)
|
2020-05-27 11:10:50 -06:00
|
|
|
bucket[column, row] = nil
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
allBlocks.append(rowOfBlocks)
|
|
|
|
}
|
|
|
|
return allBlocks
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Drop the falling shape down as far as it will go.
|
2020-05-24 21:56:11 -06:00
|
|
|
func dropShape() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
while detectIllegalPlacement() == false {
|
|
|
|
shape.lowerShapeByOneRow()
|
|
|
|
}
|
|
|
|
shape.raiseShapeByOneRow()
|
|
|
|
delegate?.gameShapeDidDrop(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Let the falling shape fall by one row.
|
2020-05-24 21:56:11 -06:00
|
|
|
func letShapeFall() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shape.lowerShapeByOneRow()
|
|
|
|
if detectIllegalPlacement() {
|
|
|
|
shape.raiseShapeByOneRow()
|
|
|
|
if detectIllegalPlacement() {
|
2020-05-27 11:10:50 -06:00
|
|
|
endGame() // shape was in illegal place to start, this ends the game
|
2020-05-24 21:56:11 -06:00
|
|
|
} else {
|
2020-05-27 11:10:50 -06:00
|
|
|
settleShape() // shape can land
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delegate?.gameShapeDidMove(privyet: self)
|
|
|
|
if detectTouch() {
|
|
|
|
settleShape()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Rotate the falling shape clockwise by 90 degrees.
|
2020-05-24 21:56:11 -06:00
|
|
|
func rotateShape() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shape.rotateClockwise()
|
|
|
|
guard detectIllegalPlacement() == false else {
|
|
|
|
shape.rotateCounterClockwise()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delegate?.gameShapeDidMove(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Move the falling shape left by one column.
|
2020-05-24 21:56:11 -06:00
|
|
|
func moveShapeLeft() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shape.shiftLeftByOneColumn()
|
|
|
|
guard detectIllegalPlacement() == false else {
|
|
|
|
shape.shiftRightByOneColumn()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delegate?.gameShapeDidMove(privyet: self)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Move the falling shape right by one column.
|
2020-05-24 21:56:11 -06:00
|
|
|
func moveShapeRight() {
|
|
|
|
guard let shape = fallingShape else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shape.shiftRightByOneColumn()
|
|
|
|
guard detectIllegalPlacement() == false else {
|
|
|
|
shape.shiftLeftByOneColumn()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delegate?.gameShapeDidMove(privyet: self)
|
|
|
|
}
|
|
|
|
}
|