-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallGrid.swift
79 lines (64 loc) · 1.95 KB
/
SmallGrid.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// SmallGrid.swift
// DropTetris
//
// Created by Kevin Yue on 12/1/14.
// Copyright (c) 2014 Kevin Yue. All rights reserved.
//
import Foundation
import SpriteKit
class SmallGrid: GridWithTransformations {
let myGridDims:GridDimension
init(){
let subGridSize = Layout.gridSqSize * Rules.subGridN
myGridDims = GridDimension(numRows: Rules.subGridN, numCols: Rules.subGridN,
gridSize: subGridSize,
origin: CGPoint.topRight - Layout.screenMarginSize.toVector() - subGridSize.toVector().half());
super.init(d: myGridDims)
populate()
}
func resetPieces(){
clearAllPieces()
populate()
}
func populate(){
var spawnProb = Difficulty.getSpawnProb()
for sq : GridSq in sqDict.values{
if(self.random(spawnProb)){
sq.addPiece()
}
}
if(piecesInGrid() == 0){
populate()
}
center()
}
func clearAllPieces(){
for sq : GridSq in sqDict.values{
sq.removePiece()
}
}
func piecesInGrid() -> Int{
var numPieces:Int = 0
for sq : GridSq in sqDict.values{
if(sq.occupied()){numPieces+=1}
}
return numPieces
}
func pieceAtSq(coord:Coordinate) -> Piece{
assert(sqDict[coord] != nil, "ERROR: pieceAtSq entered invalid coord")
assert(sqDict[coord]!.contains != nil, "ERROR: no piece at that coordinate")
var p:Piece = sqDict[coord]!.contains!
return p
}
private func random(probability:Double) -> Bool{
let aHighNum:Int = Int(1000000)
if(Double(arc4random_uniform(UInt32(aHighNum) )) < (probability * aHighNum)){
return true
}
return false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}