Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to swift3 #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If you want to check out a more advanced Swift project, please take a look at **
Instructions
------------

You will need Xcode 7 to run the project. Run it in the simulator or on an actual device.
You will need Xcode 8 to run the project. Run it in the simulator or on an actual device.

Tap the button to play the game. Swipe to move the tiles.

Expand Down
8 changes: 7 additions & 1 deletion swift-2048.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
1AE8BEF2193EF1C7007592BA /* AppearanceProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AE8BEF1193EF1C7007592BA /* AppearanceProvider.swift */; };
1AE8BEF4193EF260007592BA /* NumberTileGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AE8BEF3193EF260007592BA /* NumberTileGame.swift */; };
1AE8BEF6193EF4B6007592BA /* TileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AE8BEF5193EF4B6007592BA /* TileView.swift */; };
1AEF46D01BAE736400119D81 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1AEF46CF1BAE736400119D81 /* Launch Screen.storyboard */; settings = {ASSET_TAGS = (); }; };
1AEF46D01BAE736400119D81 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1AEF46CF1BAE736400119D81 /* Launch Screen.storyboard */; };
1AF8F0B619403AA400A7049E /* AccessoryViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF8F0B519403AA400A7049E /* AccessoryViews.swift */; };
1AF8F0B81941841000A7049E /* AuxiliaryModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF8F0B71941841000A7049E /* AuxiliaryModels.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -207,9 +207,11 @@
TargetAttributes = {
1AC563DC193DCE6000402286 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0820;
};
1AC563EE193DCE6000402286 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0820;
TestTargetID = 1AC563DC193DCE6000402286;
};
};
Expand Down Expand Up @@ -394,6 +396,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "f3nghuang.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -406,6 +409,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "f3nghuang.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -426,6 +430,7 @@
METAL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = "f3nghuang.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUNDLE_LOADER)";
};
name = Debug;
Expand All @@ -443,6 +448,7 @@
METAL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "f3nghuang.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUNDLE_LOADER)";
};
name = Release;
Expand Down
12 changes: 6 additions & 6 deletions swift-2048/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Expand Down
14 changes: 7 additions & 7 deletions swift-2048/AppearanceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import UIKit

protocol AppearanceProviderProtocol: class {
func tileColor(value: Int) -> UIColor
func numberColor(value: Int) -> UIColor
func tileColor(_ value: Int) -> UIColor
func numberColor(_ value: Int) -> UIColor
func fontForNumbers() -> UIFont
}

class AppearanceProvider: AppearanceProviderProtocol {

// Provide a tile color for a given value
func tileColor(value: Int) -> UIColor {
func tileColor(_ value: Int) -> UIColor {
switch value {
case 2:
return UIColor(red: 238.0/255.0, green: 228.0/255.0, blue: 218.0/255.0, alpha: 1.0)
Expand All @@ -34,17 +34,17 @@ class AppearanceProvider: AppearanceProviderProtocol {
case 128, 256, 512, 1024, 2048:
return UIColor(red: 237.0/255.0, green: 207.0/255.0, blue: 114.0/255.0, alpha: 1.0)
default:
return UIColor.whiteColor()
return UIColor.white
}
}

// Provide a numeral color for a given value
func numberColor(value: Int) -> UIColor {
func numberColor(_ value: Int) -> UIColor {
switch value {
case 2, 4:
return UIColor(red: 119.0/255.0, green: 110.0/255.0, blue: 101.0/255.0, alpha: 1.0)
default:
return UIColor.whiteColor()
return UIColor.white
}
}

Expand All @@ -53,6 +53,6 @@ class AppearanceProvider: AppearanceProviderProtocol {
if let font = UIFont(name: "HelveticaNeue-Bold", size: 20) {
return font
}
return UIFont.systemFontOfSize(20)
return UIFont.systemFont(ofSize: 20)
}
}
38 changes: 19 additions & 19 deletions swift-2048/Models/AuxiliaryModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

/// An enum representing directions supported by the game model.
enum MoveDirection {
case Up, Down, Left, Right
case up, down, left, right
}

/// An enum representing a movement command issued by the view controller as the result of the user swiping.
Expand All @@ -22,40 +22,40 @@ struct MoveCommand {
/// An enum representing a 'move order'. This is a data structure the game model uses to inform the view controller
/// which tiles on the gameboard should be moved and/or combined.
enum MoveOrder {
case SingleMoveOrder(source: Int, destination: Int, value: Int, wasMerge: Bool)
case DoubleMoveOrder(firstSource: Int, secondSource: Int, destination: Int, value: Int)
case singleMoveOrder(source: Int, destination: Int, value: Int, wasMerge: Bool)
case doubleMoveOrder(firstSource: Int, secondSource: Int, destination: Int, value: Int)
}

/// An enum representing either an empty space or a tile upon the board.
enum TileObject {
case Empty
case Tile(Int)
case empty
case tile(Int)
}

/// An enum representing an intermediate result used by the game logic when figuring out how the board should change as
/// the result of a move. ActionTokens are transformed into MoveOrders before being sent to the delegate.
enum ActionToken {
case NoAction(source: Int, value: Int)
case Move(source: Int, value: Int)
case SingleCombine(source: Int, value: Int)
case DoubleCombine(source: Int, second: Int, value: Int)
case noAction(source: Int, value: Int)
case move(source: Int, value: Int)
case singleCombine(source: Int, value: Int)
case doubleCombine(source: Int, second: Int, value: Int)

// Get the 'value', regardless of the specific type
func getValue() -> Int {
switch self {
case let .NoAction(_, v): return v
case let .Move(_, v): return v
case let .SingleCombine(_, v): return v
case let .DoubleCombine(_, _, v): return v
case let .noAction(_, v): return v
case let .move(_, v): return v
case let .singleCombine(_, v): return v
case let .doubleCombine(_, _, v): return v
}
}
// Get the 'source', regardless of the specific type
func getSource() -> Int {
switch self {
case let .NoAction(s, _): return s
case let .Move(s, _): return s
case let .SingleCombine(s, _): return s
case let .DoubleCombine(s, _, _): return s
case let .noAction(s, _): return s
case let .move(s, _): return s
case let .singleCombine(s, _): return s
case let .doubleCombine(s, _, _): return s
}
}
}
Expand All @@ -68,7 +68,7 @@ struct SquareGameboard<T> {

init(dimension d: Int, initialValue: T) {
dimension = d
boardArray = [T](count:d*d, repeatedValue:initialValue)
boardArray = [T](repeating: initialValue, count: d*d)
}

subscript(row: Int, col: Int) -> T {
Expand All @@ -85,7 +85,7 @@ struct SquareGameboard<T> {
}

// We mark this function as 'mutating' since it changes its 'parent' struct.
mutating func setAll(item: T) {
mutating func setAll(_ item: T) {
for i in 0..<dimension {
for j in 0..<dimension {
self[i, j] = item
Expand Down
Loading