Skip to content

Instantly share code, notes, and snippets.

@thanhluu
Created April 29, 2016 14:13
Show Gist options
  • Save thanhluu/30ed49897034438b562d587a78527862 to your computer and use it in GitHub Desktop.
Save thanhluu/30ed49897034438b562d587a78527862 to your computer and use it in GitHub Desktop.
Swift Struct & Class
let x1 = 0
let y1 = 0
let coordinate1: (x: Int, y: Int) = (0,0)
coordinate1.x
struct Point {
let x: Int
let y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
func surroundingPoints(withRange range: Int = 1) -> [Point] {
var results: [Point] = []
for xCoord in (x-range)...(x+range) {
for yCoord in (y-range)...(y+range) {
let coordinatePoint = Point(x: xCoord, y: yCoord)
results.append(coordinatePoint)
}
}
return results
}
}
let coordinatePoint = Point(x: 2, y: 2)
coordinatePoint.surroundingPoints()
// Bai Tap
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
// Add your code below
init( red: Double, green: Double, blue: Double, alpha: Double, description: String ) {
self.red = 86.0
self.green = 191.0
self.blue = 131.0
self.alpha = 1.0
self.description = description
}
}
// Game
class Enemy {
var life: Int = 2
let position: Point
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func descreaseHealth(factor: Int) {
life = life - factor // life -= factor
}
}
// Bai Tap
class Shape{
var numberOfSides: Int
init(sides numberOfSides: Int){
self.numberOfSides = numberOfSides
}
}
let someShape = Shape(sides: 3)
// Game
class Tower {
let position: Point
var range: Int = 1
var strength: Int = 1
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func fireAtEmemy(enemy: Enemy) {
if inRange(self.position, range: self.range, target: enemy.position) {
while enemy.life > 0 {
enemy.descreaseHealth(self.strength)
print("Enemy vanquished!")
}
} else {
print("Darn! The enemy is out of range")
}
}
func inRange(position: Point, range: Int, target: Point) -> Bool {
let availablePositions = position.surroundingPoints( withRange: range )
for point in availablePositions {
if (point.x == target.x) && (point.y == target.y) {
return true
}
}
return false
}
}
class SuperEnemy: Enemy {
let isSuper: Bool = true
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.life = 50
}
}
class LaserTower: Tower {
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.range = 100
self.strength = 100
}
override func fireAtEmemy(enemy: Enemy) {
while enemy.life >= 0 {
enemy.descreaseHealth(strength)
}
print("Enemy vanquished")
}
}
let tower = Tower(x: 0, y: 0)
let enemy1 = Enemy(x: 1, y: 1)
tower.fireAtEmemy(enemy1)
let laserTower = LaserTower(x: 0, y: 0)
let superEnemy = SuperEnemy(x: 20, y: 20)
laserTower.fireAtEmemy(superEnemy)
// Bai Tap
struct Location {
let latitude: Double
let longitude: Double
}
class Business {
let name: String = ""
let location: Location
init(name: String, latitude: Double, longitude: Double) {
self.location = Location(latitude: latitude, longitude: longitude)
}
}
let someBusiness = Business(name: "JOOM", latitude: 12.2, longitude: 13.3)
// Bai Tap
class Vehicle {
var numberOfDoors: Int
var numberOfWheels: Int
init(withDoors doors: Int, andWheels wheels: Int) {
self.numberOfDoors = doors
self.numberOfWheels = wheels
}
}
class Car: Vehicle {
var numberOfSeats: Int = 4
}
let someCar = Car( withDoors: 2, andWheels: 2 )
// Bai Tap
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
func getFullName() -> String {
return "\(firstName) \(lastName)"
}
}
class Doctor: Person {
override func getFullName() -> String {
return "Dr. \(lastName)"
}
}
let someDoctor = Doctor(firstName: "Sam", lastName: "Smith")
// Bai Tap
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
init( title: String, author: String, tag: Tag ) {
self.title = title
self.author = author
self.tag = tag
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post( title: "iOS Development", author: "Apple", tag: Tag(name: "swift") )
let postDescription = firstPost.description()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment