-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
57 lines (46 loc) · 1016 Bytes
/
Contents.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
import UIKit
class Spaceship {
var name: String = ""
var health = 100
var position = 0
func moveLeft() {
position -= 1
}
func moveRight() {
position += 1
}
func wasHit() {
health -= 5
}
}
class Fighter: Spaceship {
var weapon = ""
var remainingFirePower = 5
func fire() {
if remainingFirePower > 0 {
remainingFirePower -= 1
} else {
print("You have no more fire power.")
}
}
}
class ShieldedShip: Fighter {
var shieldStrength = 25
override func wasHit() {
if (shieldStrength > 0) {
shieldStrength = shieldStrength - 5
} else {
super.wasHit()
}
}
}
let defender = ShieldedShip()
defender.name = "Defender"
defender.weapon = "Cannon"
defender.moveRight()
print(defender.position)
defender.fire()
print(defender.remainingFirePower)
defender.wasHit()
print(defender.shieldStrength)
print(defender.health)