-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevel.swift
312 lines (247 loc) · 9.87 KB
/
Level.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// Level.swift
// Slide the Box
//
// Created by Alex Morgan on 31/12/2016.
// Copyright © 2016 Alex Morgan. All rights reserved.
//
import SpriteKit
class Level {
var numBlocksX = Int()
var numBlocksY = Int()
var numBlockBlocks = Int()
var numNumberBlocks = Int()
var numArrowBlocks = Int()
var startPosition = Array<Int>()
var endPosition = Array<Int>()
var enemyPositions = Array<Array<Int>>()
var blocksReal = Array<Array<Int>>()
var levelSolution = LevelSolution()
init() {
}
init(numBlocksX: Int, numBlocksY: Int) {
self.numBlocksX = numBlocksX
self.numBlocksY = numBlocksY
blocksReal = Array(repeating: Array(repeating: 0, count: numBlocksX), count: numBlocksY)
levelSolution = LevelSolution(numBlocksX: numBlocksX, numBlocksY: numBlocksY)
}
init(otherObject: Level) {
self.numBlocksX = otherObject.numBlocksX
self.numBlocksY = otherObject.numBlocksY
self.numBlockBlocks = otherObject.numBlockBlocks
self.numNumberBlocks = otherObject.numNumberBlocks
self.numArrowBlocks = otherObject.numArrowBlocks
self.startPosition = otherObject.startPosition
self.endPosition = otherObject.endPosition
self.enemyPositions = otherObject.enemyPositions
self.blocksReal = otherObject.blocksReal
self.levelSolution = otherObject.getSolution().copy()
}
func copy() -> Level {
return Level(otherObject: self)
}
func calculateMove(position: Array<Int>, direction: String, blockType: String)
-> (positions: Array<Array<Int>>, numMoves: Array<Int>, endBlock: Bool, infiniteArrowLoop: Bool) {
var finished = false
var col = position[0]
var row = position[1]
var numMoves = 0
var endBlock = false
var positions = Array<Array<Int>>()
var newPosition = position
positions.append(newPosition)
var numMovesArray = Array<Int>()
var hitNumber = false
var numberRemaining = 0
var currentDirection = direction
var updateDirection = String()
var hitArrow = false
var hitArrowNum = 0
var infiniteArrowLoop = false
if (blocksReal[row][col] >= 11 && blocksReal[row][col] <= 18) { // Number block. No Number 0 block
hitNumber = true
numberRemaining = blocksReal[row][col] - 10
}
repeat {
numMoves = 0
hitArrow = false
finished = false
while(!finished) {
if hitNumber == true {
if numberRemaining > 0 {
numberRemaining -= 1
} else {
finished = true
break
}
}
(col, row) = incrementPosition(col: col, row: row, direction: currentDirection, amount: 1)
if (row >= 0 && row < numBlocksY) && (col >= 0 && col < numBlocksX) {
if blocksReal[row][col] == 0 || blocksReal[row][col] == 8 || blocksReal[row][col] == 7 { // Empty or start Blocks
numMoves += 1
} else if blocksReal[row][col] == 9 && blockType == "player" { // End Block
numMoves += 1
finished = true
endBlock = true
} else if blocksReal[row][col] == 9 && blockType == "enemy" { // treat as blank
numMoves += 1
} else if (blocksReal[row][col] >= 10 && blocksReal[row][col] <= 18) { // Number block
hitNumber = true
numberRemaining = blocksReal[row][col] - 10
numMoves += 1
} else if (blocksReal[row][col] >= 20 && blocksReal[row][col] <= 23) { // Arrow block
hitArrow = true
hitArrowNum += 1
updateDirection = getArrowDirection(blockType: blocksReal[row][col])
numMoves += 1
finished = true
} else if blocksReal[row][col] == 1 { // End block
finished = true
} else {
finished = true
}
} else {
finished = true
}
}
newPosition = incrementPosition(position: newPosition, direction: currentDirection, amount: numMoves)
positions.append(newPosition)
numMovesArray.append(numMoves)
if hitArrow {
currentDirection = updateDirection
}
if hitArrowNum > 10 {
infiniteArrowLoop = true
break
}
} while(hitArrow)
return (positions: positions, numMoves: numMovesArray, endBlock: endBlock, infiniteArrowLoop: infiniteArrowLoop)
}
func addEnemies(numOfEnemies: Int) {
enemyPositions = Array<Array<Int>>()
for _ in 1...numOfEnemies {
let enemyPosition = levelSolution.randomEnemyPosition(notIn: enemyPositions, level: self)
blocksReal[enemyPosition[1]][enemyPosition[0]] = 7
enemyPositions.append(enemyPosition)
}
}
func twoDimMax(array: Array<Array<Int>>) -> (value: Int, index: Array<Int>) {
var globalMax = 0
var globalIndex = Array<Int>()
var row = 0
for curRow in array {
let tempMax = curRow.max()
if (tempMax! > globalMax) {
globalMax = tempMax!
globalIndex = [curRow.firstIndex(of: tempMax!)!,row]
}
row += 1
}
return (globalMax, globalIndex)
}
func positionInBounds(position: Array<Int>) -> Bool {
if (position[0] >= 0 && position[0] < blocksReal[0].count &&
position[1] >= 0 && position[1] < blocksReal.count) {
return true
} else {
return false
}
}
func isPositionOnBoundary(position: Array<Int>) -> Bool {
if (position[0] == 0 || position[0] == (numBlocksX-1)) ||
(position[1] == 0 || position[1] == (numBlocksY-1)) {
return true
} else {
return false
}
}
func incrementPosition(col: Int, row: Int, direction: String, amount: Int) -> (col: Int, row: Int) {
let startPosition = [col, row]
let newPosition = incrementPosition(position: startPosition, direction: direction, amount: amount)
return (col: newPosition[0], row: newPosition[1])
}
func incrementPosition(position: Array<Int>, direction: String, amount: Int) -> Array<Int> {
var newPosition = position
if direction == "up" {
newPosition[1] -= amount
} else if direction == "down" {
newPosition[1] += amount
} else if direction == "left" {
newPosition[0] -= amount
} else if direction == "right" {
newPosition[0] += amount
}
return newPosition
}
func getSolution() -> LevelSolution {
return self.levelSolution
}
func getBlocksReal() -> Array<Array<Int>> {
return self.blocksReal
}
func getBlocksRealValue(position: Array<Int>) -> Int {
return self.blocksReal[position[1]][position[0]]
}
func setBlocksReal(blocks: Array<Array<Int>>) {
self.blocksReal = blocks
}
func getStartPosition() -> Array<Int> {
return self.startPosition
}
func setStartPosition(position: Array<Int>) {
self.startPosition = position
}
func getEndPosition() -> Array<Int> {
return self.endPosition
}
func setEndPosition(position: Array<Int>) {
self.endPosition = position
}
func getBlockSpaceRatio() -> Float {
return Float(0)
}
func getMinMoves() -> Int {
if self.levelSolution.isSolved() {
return self.levelSolution.getBlocksExplorationValue(position: getEndPosition()) - 1
} else {
return -1
}
}
func getNumBlockBlocks() -> Int {
return self.numBlockBlocks
}
func setNumBlockBlocks(number: Int) {
self.numBlockBlocks = number
}
func getNumBlocks() -> Int {
return self.numBlocksX * self.numBlocksY
}
func getNumNumberBlocks() -> Int {
return self.numNumberBlocks
}
func setNumNumberBlocks(number: Int) {
self.numNumberBlocks = number
}
func getNumArrowBlocks() -> Int {
return self.numArrowBlocks
}
func setNumArrowBlocks(number: Int) {
self.numArrowBlocks = number
}
func getArrowDirection(blockType: Int) -> String {
var arrowDirection = ""
if blockType == 20 {
arrowDirection = "up"
} else if blockType == 21 {
arrowDirection = "down"
} else if blockType == 22 {
arrowDirection = "left"
} else if blockType == 23 {
arrowDirection = "right"
}
return arrowDirection
}
func getEnemyPositions() -> Array<Array<Int>> {
return self.enemyPositions
}
}