-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevelProposer.swift
200 lines (153 loc) · 7.72 KB
/
LevelProposer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// LevelProposer.swift
// Slide the Box
//
// Created by Alex Morgan on 31/12/2016.
// Copyright © 2016 Alex Morgan. All rights reserved.
//
import SpriteKit
class LevelProposer {
var numBlocksX = Int()
var numBlocksY = Int()
let numbersSelectorKey = "Number of Numbers"
let arrowsSelectorKey = "Number of Arrows"
var blocksReal = Array<Array<Int>>()
init() {
}
init(numBlocksX: Int, numBlocksY: Int) {
self.numBlocksX = numBlocksX
self.numBlocksY = numBlocksY
}
func propose(difficulty: DifficultyCriteria, useNumbers: Bool, useArrows: Bool) -> Level {
let level = Level(numBlocksX: numBlocksX, numBlocksY: numBlocksY)
var startPosition = Array<Int>()
var endPosition = Array<Int>()
var numBlocks = Int()
var numNumbers = Int()
var numArrows = Int()
(blocksReal, startPosition, endPosition, numBlocks, numNumbers, numArrows) = proposeBlocksReal(difficulty: difficulty, useNumbers: useNumbers, useArrows: useArrows)
level.setBlocksReal(blocks: blocksReal)
level.setStartPosition(position: startPosition)
level.setEndPosition(position: endPosition)
level.setNumBlockBlocks(number: numBlocks)
level.setNumNumberBlocks(number: numNumbers)
level.setNumArrowBlocks(number: numArrows)
return level
}
func proposeBlocksReal(difficulty: DifficultyCriteria, useNumbers: Bool, useArrows: Bool) -> (blocksReal: Array<Array<Int>>,
startPosition: Array<Int>, endPosition: Array<Int>, numBlocks: Int, numNumbers: Int, numArrows: Int) {
var blocksReal: Array<Array<Int>> = Array(repeating: Array(repeating: 0, count: numBlocksX), count: numBlocksY)
// Place random starting location
let startPosition = [Int(arc4random_uniform(UInt32(numBlocksX))),
Int(arc4random_uniform(UInt32(numBlocksY)))]
blocksReal[startPosition[1]][startPosition[0]] = 8
// Place random end location
var trialPosition = Array<Int>()
var freePositionFound = false
while(!freePositionFound) {
trialPosition = [Int(arc4random_uniform(UInt32(numBlocksX))),
Int(arc4random_uniform(UInt32(numBlocksY)))]
if blocksReal[trialPosition[1]][trialPosition[0]] == 0 {
freePositionFound = true
}
}
let endPosition = trialPosition
blocksReal[endPosition[1]][endPosition[0]] = 9
// Place N block blocks randomly
let numBlocks = generateNumBlockBlocks(difficulty: difficulty)
blocksReal = addBlockBlocks(blocksReal: blocksReal, numBlocks: numBlocks)
// Place N number blocks randomly
var numNumbers = 0
if useNumbers {
numNumbers = generateNumNumberBlocks(difficulty: difficulty)
blocksReal = addNumberBlocks(blocksReal: blocksReal, numNumbers: numNumbers)
}
// Place N arrow blocks randomly
var numArrows = 0
if useArrows {
numArrows = generateNumNumberBlocks(difficulty: difficulty)
blocksReal = addArrowBlocks(blocksReal: blocksReal, numArrows: numArrows)
}
return (blocksReal: blocksReal, startPosition: startPosition, endPosition: endPosition, numBlocks: numBlocks, numNumbers: numNumbers, numArrows: numArrows)
}
func addBlockBlocks(blocksReal: Array<Array<Int>>, numBlocks: Int) -> Array<Array<Int>> {
var newBlocksReal = blocksReal
var trialPosition = Array<Int>()
// Place N blocks randomly
var blocksPlaced = 0
while(blocksPlaced < numBlocks) {
trialPosition = [Int(arc4random_uniform(UInt32(numBlocksX))),
Int(arc4random_uniform(UInt32(numBlocksY)))]
if newBlocksReal[trialPosition[1]][trialPosition[0]] == 0 {
newBlocksReal[trialPosition[1]][trialPosition[0]] = 1
blocksPlaced += 1
}
}
return newBlocksReal
}
func addNumberBlocks(blocksReal: Array<Array<Int>>, numNumbers: Int) -> Array<Array<Int>> {
var newBlocksReal = blocksReal
var trialPosition = Array<Int>()
// Place N random numbers randomly
var blocksPlaced = 0
while(blocksPlaced < numNumbers) {
trialPosition = [Int(arc4random_uniform(UInt32(numBlocksX))),
Int(arc4random_uniform(UInt32(numBlocksY)))]
if newBlocksReal[trialPosition[1]][trialPosition[0]] == 0 {
// Number: 0 -> Code: 10
// Number: 1 -> Code: 11
// Number: 2 -> Code: 12
// Number: 3 -> Code: 13
let maxNumber = 3
let numberValue = Int(arc4random_uniform(UInt32(maxNumber+1)))
newBlocksReal[trialPosition[1]][trialPosition[0]] = 10+numberValue
blocksPlaced += 1
}
}
return newBlocksReal
}
func addArrowBlocks(blocksReal: Array<Array<Int>>, numArrows: Int) -> Array<Array<Int>> {
var newBlocksReal = blocksReal
var trialPosition = Array<Int>()
// Place N arrows randomly
var blocksPlaced = 0
while(blocksPlaced < numArrows) {
trialPosition = [Int(arc4random_uniform(UInt32(numBlocksX))),
Int(arc4random_uniform(UInt32(numBlocksY)))]
if newBlocksReal[trialPosition[1]][trialPosition[0]] == 0 {
// Number: 0 -> Code: up
// Number: 1 -> Code: down
// Number: 2 -> Code: left
// Number: 3 -> Code: right
let maxNumber = 3
let numberValue = Int(arc4random_uniform(UInt32(maxNumber+1)))
newBlocksReal[trialPosition[1]][trialPosition[0]] = 20+numberValue
blocksPlaced += 1
}
}
return newBlocksReal
}
func generateNumBlockBlocks(difficulty: DifficultyCriteria) -> Int {
// let levelNumBlocks = (numBlocksX*numBlocksY)/8
let levelMaxBlocks = difficulty.getPercentOfBlockBlocksCriteria() * Float(numBlocksX * numBlocksY)
let levelNumBlockBlocks = Int(arc4random_uniform(UInt32(levelMaxBlocks)-3)+3)
// var levelNumBlocks = Int()
// var percentOfBlockBlocks = Float()
// repeat {
// levelNumBlocks = randomGaussian(mean: Float(numBlocksX*numBlocksY)/6,
// stddev: Float(numBlocksX*numBlocksY)/8)
// percentOfBlockBlocks = Float(levelNumBlocks) / Float(numBlocksX * numBlocksY)
// } while (percentOfBlockBlocks <= difficulty.getPercentOfBlockBlocksCriteria())
return levelNumBlockBlocks
}
func generateNumNumberBlocks(difficulty: DifficultyCriteria) -> Int {
return UserDefaults.standard.integer(forKey: numbersSelectorKey)
// let maxNumNumberBlocks = 4
// let levelNumBlocks = Int(arc4random_uniform(UInt32(maxNumNumberBlocks))+1)
//
// return levelNumBlocks
}
func generateNumArrowBlocks(difficulty: DifficultyCriteria) -> Int {
return UserDefaults.standard.integer(forKey: arrowsSelectorKey)
}
}