Last active
February 19, 2019 16:18
-
-
Save jamesrochabrun/fd267bfbf7a125e6d69ed44e77886df9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
//STRUCTS | |
struct Point { | |
//stored properties | |
let x: Int | |
let y: Int | |
//INITIALIZER METHODS | |
init(x: Int, y:Int) { | |
self.x = x | |
self.y = y | |
} | |
///returns the surrounding points in range of | |
//// the current one | |
func points(inRange range: Int = 1) -> [Point] { | |
var results = [Point]() | |
let lowerBoundOfXRange = x - range | |
let upperBoundOfXRange = x + range | |
let lowerBoundOfXYRange = y - range | |
let upperBoundOfYRange = y + range | |
for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange { | |
for yCoordinate in lowerBoundOfXYRange...upperBoundOfYRange { | |
let coordinatePoint = Point(x:xCoordinate, y: yCoordinate) | |
results.append(coordinatePoint) | |
} | |
} | |
return results | |
} | |
} | |
//instance of the struct | |
let coordinatePoint = Point(x:0,y:0) | |
print(coordinatePoint.points()) | |
//OTHER STRUCT | |
struct Player { | |
let firstName: String | |
let lastName: String | |
func fullName() -> String { | |
let fullName = firstName + " " + lastName | |
return fullName | |
} | |
} | |
let player = Player(firstName: "lau", lastName: "boo") | |
player.fullName() | |
//other struct //coding challenge | |
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) { | |
self.red = red | |
self.green = green | |
self.blue = blue | |
self.alpha = alpha | |
self.description = ("red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)") | |
} | |
} | |
//CLASSES | |
//Enemies | |
class Enemy { | |
//fixed value example | |
var life : Int = 2 | |
let position: Point | |
init(position: Point) { | |
self.position = position | |
} | |
func decreaseLife(by factor:Int) { | |
life -= factor | |
} | |
} | |
//Subclass rules | |
class SuperEnemy: Enemy { | |
//1 )assign values to any new variables or constants | |
let isSuper: Bool = true | |
override init(position: Point) { | |
//2) provide values to any variable or constant of the super class in this case we passthe position | |
// as a parameter so the posiiton property of the superclass acquires a value | |
super.init(position:position) | |
self.life = 50 | |
} | |
} | |
//TOWERS | |
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 fire(at enemy: Enemy) { | |
if isInRange(of: enemy) { | |
enemy.decreaseLife(by: strength) | |
print("gotcha") | |
} else { | |
print("out of range") | |
} | |
} | |
//helper method | |
func isInRange(of enemy:Enemy) -> Bool { | |
let availablePositions = position.points(inRange: range) | |
for point in availablePositions { | |
if enemy.position.x == point.x && enemy.position.y == point.y { | |
return true | |
} | |
} | |
return false | |
} | |
} | |
class laserTower:Tower { | |
override init(x:Int, y:Int) { | |
super.init(x: x, y: y) | |
self.range = 100 | |
self.strength = 100 | |
} | |
override func fire(at enemy: Enemy) { | |
while enemy.life >= 0 { | |
enemy.decreaseLife(by: strength) | |
} | |
print("enemy destriyed") | |
} | |
} | |
let position = Point(x:0,y:0) | |
let tower = Tower(x:1, y:1) | |
let lazerTower = laserTower(x: 2, y: 2) | |
let enemy = Enemy.init(position: position) | |
let superEnemy = SuperEnemy(position: position) | |
tower.fire(at: enemy) | |
tower.fire(at: superEnemy) | |
lazerTower.fire(at: superEnemy) | |
lazerTower.fire(at: enemy) | |
//sub CLASS CODE CHALLENGE 1 oveeride class/ subclasse | |
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 | |
override init(withDoors doors: Int, andWheels wheels: Int) { | |
super.init(withDoors: doors, andWheels: wheels) | |
} | |
} | |
let someCar = Car(withDoors: 3, andWheels: 3) | |
//sub CLASS CODE CHALLENGE 2 override methods / subclass | |
class Person { | |
let firstName: String | |
let lastName: String | |
init(firstName: String, lastName: String) { | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
func fullName() -> String { | |
return "\(firstName) \(lastName)" | |
} | |
} | |
let person = Person(firstName: "alex", lastName: "becker") | |
person.fullName() | |
class Doctor: Person { | |
override init(firstName: String, lastName: String) { | |
super.init(firstName: firstName,lastName: lastName) | |
} | |
override func fullName() -> String { | |
return "Dr. \(firstName) \(lastName)" | |
} | |
} | |
let doctor = Doctor(firstName: "alex", lastName: "becker") | |
doctor.fullName() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment