-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevelSolution.swift
206 lines (156 loc) · 5.85 KB
/
LevelSolution.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
//
// LevelSolution.swift
// Slide the Box
//
// Created by Alex Morgan on 01/01/2017.
// Copyright © 2017 Alex Morgan. All rights reserved.
//
import SpriteKit
class LevelSolution {
var numBlocksX = Int()
var numBlocksY = Int()
var blocksExploration = Array<Array<Int>>()
var calculatedRoute = Array<Array<Array<Array<Int>>>>()
var solved = Bool()
var solvable = Bool()
var stuckable = Bool()
var numNumbersUsed = Int()
var numArrowsUsed = Int()
var infiniteArrowLoop = Bool()
init() {
}
init(numBlocksX: Int, numBlocksY: Int) {
self.numBlocksX = numBlocksX
self.numBlocksY = numBlocksY
solved = false
solvable = false
stuckable = true
numNumbersUsed = 0
numArrowsUsed = 0
infiniteArrowLoop = false
blocksExploration = Array(repeating: Array(repeating: 0, count: numBlocksX), count: numBlocksY)
calculatedRoute = Array(repeating: Array(repeating: Array<Array<Int>>(), count: numBlocksX), count: numBlocksY)
}
init(otherObject: LevelSolution) {
self.numBlocksX = otherObject.numBlocksX
self.numBlocksY = otherObject.numBlocksY
self.blocksExploration = otherObject.blocksExploration
self.calculatedRoute = otherObject.calculatedRoute
self.solved = otherObject.solved
self.solvable = otherObject.solvable
self.stuckable = otherObject.stuckable
self.numNumbersUsed = otherObject.numNumbersUsed
self.numArrowsUsed = otherObject.numArrowsUsed
self.infiniteArrowLoop = otherObject.infiniteArrowLoop
}
func copy() -> LevelSolution {
return LevelSolution(otherObject: self)
}
func randomEnemyPosition(notIn: Array<Array<Int>>, level: Level) -> Array<Int> {
var potentialPositions: Array<Array<Int>>
var (minExpStage, _) = level.twoDimMax(array: getBlocksExploration())
repeat {
potentialPositions = getPotentialEnemyPositions(array: level.getSolution().getBlocksExploration(), greaterThan: minExpStage, level: level)
potentialPositions = removeFromArray(array: potentialPositions, remove: notIn)
minExpStage -= 1
} while (potentialPositions.count == 0)
let randomPositionIndex = Int(arc4random_uniform(UInt32(potentialPositions.count)))
return potentialPositions[randomPositionIndex]
}
func getPotentialEnemyPositions(array: Array<Array<Int>>, greaterThan: Int, level: Level) -> Array<Array<Int>> {
var potentialPositions = Array<Array<Int>>()
var row = 0
var col = 0
for curRow in getBlocksExploration() {
for expStage in curRow {
if (expStage > greaterThan) && (level.getBlocksRealValue(position: [col, row]) == 0) {
potentialPositions.append([col, row])
}
col += 1
}
col = 0
row += 1
}
return potentialPositions
}
func removeFromArray(array: Array<Array<Int>>, remove: Array<Array<Int>>) -> Array<Array<Int>> {
var potentialPositions = Array<Array<Int>>()
var found: Bool
for position in array {
found = false
for toRemove in remove {
if position == toRemove {
found = true
break
}
}
if !found {
potentialPositions.append(position)
}
}
return potentialPositions
}
func isSolved() -> Bool {
return solved
}
func setSolved(isSolved: Bool) {
self.solved = isSolved
}
func isSolvable() -> Bool {
return solvable
}
func setSolvable(isSolvable: Bool) {
self.solvable = isSolvable
}
func isStuckable() -> Bool {
return stuckable
}
func setStuckable(isStuckable: Bool) {
self.stuckable = isStuckable
}
func getBlocksExploration() -> Array<Array<Int>> {
return self.blocksExploration
}
func getBlocksExplorationValue(position: Array<Int>) -> Int {
return self.blocksExploration[position[1]][position[0]]
}
func setBlocksExploration(blocks: Array<Array<Int>>) {
self.blocksExploration = blocks
}
func setBlocksExplorationValue(position: Array<Int>, value: Int) {
self.blocksExploration[position[1]][position[0]] = value
}
func resetBlocksExploration() {
self.blocksExploration = Array(repeating: Array(repeating: 0, count: numBlocksX), count: numBlocksY)
}
func getCalculatedRoute() -> Array<Array<Array<Array<Int>>>> {
return self.calculatedRoute
}
func setCalculatedRoute(calculatedRoute: Array<Array<Array<Array<Int>>>>) {
self.calculatedRoute = calculatedRoute
}
func setCalculatedRouteValue(position: Array<Int>, value: Array<Array<Int>>) {
self.calculatedRoute[position[1]][position[0]] = value
}
func resetCalculatedRoute() {
self.calculatedRoute = Array(repeating: Array(repeating: Array<Array<Int>>(), count: numBlocksX), count: numBlocksY)
}
func getNumNumbersUsed() -> Int {
return self.numNumbersUsed
}
func setNumNumbersUsed(value: Int) {
self.numNumbersUsed = value
}
func getNumArrowsUsed() -> Int {
return self.numArrowsUsed
}
func setNumArrowsUsed(value: Int) {
self.numArrowsUsed = value
}
func isInfiniteArrowLoop() -> Bool {
return self.infiniteArrowLoop
}
func setInfiniteArrowLoop(value: Bool) {
self.infiniteArrowLoop = value
}
}