-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
67 lines (56 loc) · 1.42 KB
/
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
57
58
59
60
61
62
63
64
65
66
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 wepon = ""
var remainingFirePower = 5
func fire() {
if (remainingFirePower > 0) {
remainingFirePower = remainingFirePower - 1
} else {
print("You have no more fire power.")
}
}
}
let destroyer = Fighter()
destroyer.wepon = "Laser"
destroyer.remainingFirePower = 10
destroyer.name = "Destroyer"
print(destroyer.position)
destroyer.moveRight()
print(destroyer.position)
// print(falcon.wepon) won't work as falcon is an instance of the Spaceship class which does not have a wepon variable defined.
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)
destroyer.fire()
print(destroyer.remainingFirePower)