-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataStructures.swift
300 lines (268 loc) · 11.3 KB
/
DataStructures.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
//
// DataStructures.swift
// Edwin
//
// Created by Vegard Solheim Theriault on 28/02/15.
// Copyright (c) 2015 Wrong Bag. All rights reserved.
//
import Foundation
import UIKit
class GameResult: CustomStringConvertible {
var players: Players
var boardSize: Int
var turnDuration: Int
var won: Bool? // nil means draw. Only applicable for multiplayer games
var currentUserScore: Int
var opponentScore: Int! // Only applicable for multiplayer games
var currentUserDisplayName: String
var opponentDisplayName: String! // Only applicable for multiplayer games
var gameEndScreenshot: UIImage
// Use this for singleplayer games
init(
players: Players,
boardSize: Int,
turnDuration: Int,
currentUserScore: Int,
currentUserDisplayName: String,
gameEndScreenshot: UIImage)
{
self.players = players
self.boardSize = boardSize
self.turnDuration = turnDuration
self.currentUserScore = currentUserScore
self.currentUserDisplayName = currentUserDisplayName
self.gameEndScreenshot = gameEndScreenshot
}
// Use this for multiplayer games
convenience init(
players: Players,
boardSize: Int,
turnDuration: Int,
won: Bool?,
currentUserScore: Int,
opponentScore: Int,
currentUserDisplayName: String,
opponentDisplayName: String,
gameEndScreenshot: UIImage)
{
self.init(
players: players,
boardSize: boardSize,
turnDuration: turnDuration,
currentUserScore: currentUserScore,
currentUserDisplayName: currentUserDisplayName,
gameEndScreenshot: gameEndScreenshot)
self.won = won
self.opponentScore = opponentScore
self.opponentDisplayName = opponentDisplayName
}
var description: String {
get {
return "GameResult(players: \(players), boardSize: \(boardSize), turnDuration: \(turnDuration), won: \(won), currentUserScore: \(currentUserScore), opponentScore: \(opponentScore), currentUserDisplayName: \(currentUserDisplayName), opponentDisplayName: \(opponentDisplayName), gameEndScreenShot.size: \(gameEndScreenshot.size))"
}
}
}
class GameSetup<T: Evolvable>: CustomStringConvertible {
var players: Players
var setupForCreating: Bool
var dimension: Int
var turnDuration: Int
var firstTile: T!
var firstCoordinate: Coordinate!
var secondTile: T!
var secondCoordinate: Coordinate!
var opponentDisplayName: String! // Primarily used when joining a game
var gameServer: GameServerManager! // Primarily used when joining a game
init(
players: Players,
setupForCreating: Bool,
dimension: Int,
turnDuration: Int)
{
self.players = players
self.setupForCreating = setupForCreating
self.dimension = dimension
self.turnDuration = turnDuration
}
convenience init(
players: Players,
setupForCreating: Bool,
dimension: Int,
turnDuration: Int,
firstValue: T!,
firstCoordinate: Coordinate!,
secondValue: T!,
secondCoordinate: Coordinate!,
opponentDisplayName: String!,
gameServer: GameServerManager!)
{
self.init(players: players, setupForCreating: setupForCreating, dimension: dimension, turnDuration: turnDuration)
self.firstTile = firstValue
self.firstCoordinate = firstCoordinate
self.secondTile = secondValue
self.secondCoordinate = secondCoordinate
self.opponentDisplayName = opponentDisplayName
self.gameServer = gameServer
}
func isReady() -> Bool {
return firstTile != nil &&
firstCoordinate != nil &&
secondTile != nil &&
secondCoordinate != nil
}
var description: String {
get {
return "GameSetup(players: \(players), setupForCreating: \(setupForCreating), dimension: \(dimension), turnDuration: \(turnDuration), firstTile: \(firstTile), firstCoordinate: \(firstCoordinate), secondTile: \(secondTile), secondCoordinate: \(secondCoordinate), opponentDisplayName: \(opponentDisplayName))"
}
}
}
enum Players: CustomStringConvertible {
case Single
case Multi
var description: String {
get {
switch self {
case .Single:
return "Single Player"
case .Multi:
return "Multi Player"
}
}
}
}
enum MoveDirection: CustomStringConvertible {
case Up
case Down
case Left
case Right
var description: String {
get {
switch self {
case .Up:
return "Up"
case .Down:
return "Down"
case .Left:
return "Left"
case .Right:
return "Right"
}
}
}
}
protocol Evolvable: Equatable, CustomStringConvertible {
func evolve() -> Self?
static func getBaseValue() -> Self // Gets the lowest value
var scoreValue: Int { get } // The score increase that this piece should amount to
init(scoreValue: Int)
}
enum TileValue: Int, Evolvable {
case Two = 2
case Four = 4
case Eight = 8
case Sixteen = 16
case ThirtyTwo = 32
case SixtyFour = 64
case OneHundredAndTwentyEight = 128
case TwoHundredAndFiftySix = 256
case FiveHundredAndTwelve = 512
case OneThousandAndTwentyFour = 1024
case TwoThousandAndFourtyEight = 2048
case FourThousandAndNinetySix = 4096
case EightThousandOneHundredAndNinetyTwo = 8192
case SixteenThousandThreeHundredAndEightyFour = 16384
case ThirtyTwoThousandSevenHundredAndSixtyEight = 32768
case SixtyFiveThousandFiveHundredAndThirtySix = 65536
case OneHundredAndThirtyOneThousandAndSeventyTwo = 131072 // Seriously doubt anyone will get higher than this
func evolve() -> TileValue? {
switch self {
case .Two: return TileValue.Four
case .Four: return TileValue.Eight
case .Eight: return TileValue.Sixteen
case .Sixteen: return TileValue.ThirtyTwo
case .ThirtyTwo: return TileValue.SixtyFour
case .SixtyFour: return TileValue.OneHundredAndTwentyEight
case .OneHundredAndTwentyEight: return TileValue.TwoHundredAndFiftySix
case .TwoHundredAndFiftySix: return TileValue.FiveHundredAndTwelve
case .FiveHundredAndTwelve: return TileValue.OneThousandAndTwentyFour
case .OneThousandAndTwentyFour: return TileValue.TwoThousandAndFourtyEight
case .TwoThousandAndFourtyEight: return TileValue.FourThousandAndNinetySix
case .FourThousandAndNinetySix: return TileValue.EightThousandOneHundredAndNinetyTwo
case .EightThousandOneHundredAndNinetyTwo: return TileValue.SixteenThousandThreeHundredAndEightyFour
case .SixteenThousandThreeHundredAndEightyFour: return TileValue.ThirtyTwoThousandSevenHundredAndSixtyEight
case .ThirtyTwoThousandSevenHundredAndSixtyEight: return TileValue.SixtyFiveThousandFiveHundredAndThirtySix
case .SixtyFiveThousandFiveHundredAndThirtySix: return TileValue.OneHundredAndThirtyOneThousandAndSeventyTwo
case .OneHundredAndThirtyOneThousandAndSeventyTwo: return nil
}
}
// Should probably be a failable initializer
init(scoreValue: Int) {
self = TileValue(rawValue: scoreValue)!
}
static func getBaseValue() -> TileValue {
return TileValue.Two
}
var scoreValue: Int {
get {
return self.rawValue
}
}
var description: String {
get {
return "TileValue(\(self.rawValue))"
}
}
}
// Operator from the Equatable protocol which TileValue conforms to has to be defined globally
func ==(lhs: TileValue, rhs: TileValue) -> Bool {
return lhs.rawValue == rhs.rawValue
}
struct Coordinate: CustomStringConvertible, Equatable {
let x: Int
let y: Int
var description: String {
get {
return "Coordinate(x: \(x), y: \(y))"
}
}
}
// Operator from the Equatable protocol which Coordinate conforms to has to be defined globally
func ==(lhs: Coordinate, rhs: Coordinate) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
// This class should preferable by a struct, but Swift currently can't handle generic enums (see MoveAction)
// that have more than one associated value if one of the values are of the generic type. Having this be a
// class works because it is a reference type, and pointers have a fixed size. The size of value types
// (like structs and enums) can't be determined at compile time. Should change this back to a struct as soon
// as Apple implements "non-fixed multi-payload enum layout"
class GamePiece<T: Evolvable>: CustomStringConvertible {
let value: T
let position: Coordinate
init(value: T, position: Coordinate) {
self.value = value
self.position = position
}
var description: String {
get {
return "GamePiece(value: \(value), position: \(position))"
}
}
}
// These will be generated by the GameBoard and move all the way to the view so the changes can be properly animated
enum MoveAction<T: Evolvable>: CustomStringConvertible {
case Spawn(gamePiece: GamePiece<T>) // Spawns a new tile
case Move(from: Coordinate, to: Coordinate) // Moves a tile
case Merge(from: Coordinate, andFrom: Coordinate, toGamePiece: GamePiece<T>) // Merge two tiles
var description: String {
get {
switch self {
case let .Spawn(gamePiece):
return "Spawn(gamePiece: \(gamePiece)"
case let .Move(from, to):
return "Move(from: \(from), to: \(to))"
case let .Merge(from, to, toGamePiece):
return "Merge(from: \(from), and: \(to), toGamePiece: \(toGamePiece))"
}
}
}
}