-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_5_RevisingPreviousIdea.swift
152 lines (109 loc) · 4.04 KB
/
Day_5_RevisingPreviousIdea.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Day 5: I changed a learnign web to LinkedIn Learnign
- Type Safety and Inference
- Swift Operators
- Understanding Strings
- Working with Strings
- Type Conversions
- Booleans and Logical Operators
- Introduction Optional
- Some challenge
*/
// -------------------- Type Safety and Inference --------------------
// Type inference
var currentHealth = 90
// Type annotation
var characterName: String
characterName = "Kobe Brooke"
// Type annotation + inference
var maxHealth: Int = 100
// Multiple variables - type annotation shorthand
var numberOne, numberTwo, numberThree: Int
// -------------------- Swift Operators --------------------
// Arithmetic (+, -, /, *)
// Compound assignments (+=, -=, *=, /=, %=)
// Modulo (%)
/*
Comparisons (==, !=, >, <, >=, <=)
Logical operators (!, &&, ||)
Ranges (1...5 or 1..<5)
*/
// -------------------- Understanding Strings --------------------
// Declaring strings
var activeQuest : String = "Retrieving the Cart!"
// Concatenation
var questDifficulty = "Nightmare"
var questInfo = activeQuest + " -> " + questDifficulty
questInfo += "!"
// String interpolation
let questInfo_Interpolated = "I'm not sure you are ready to \(activeQuest) yet, it's level"
// -------------------- Working with Strings --------------------
// Test variable
var dialogue = "The Innkeeper's Haven"
// String data
dialogue.count
dialogue.isEmpty
dialogue.contains("s")
// Append and Insert
dialogue.append(contentsOf: ", weary travelers")
// Remove and Split
//dialogue.removeLast()
//dialogue.removeFirst()
//dialogue.removeAll()
dialogue.split(separator: ",")
print(dialogue)
// -------------------- Type Conversions --------------------
// Test variables
var currentGold_Double = 5.999999
// Explicit conversions
var currentGold_Integer: Int = Int(currentGold_Double)
var currentGold_String: String = String(currentGold_Integer)
// Inferred conversion with operators
var bankDeposit = 37 + 5.892
var bankDeposit_Explicit = currentGold_Double + Double(currentGold_Integer)
// -------------------- Booleans and Logical Operators --------------------
// Test variable
var isActive: Bool = false
var canMove: Bool = false
// Logical operators (!, &&, ||)
isActive = !isActive
var playerControl_and = canMove && isActive
var playerControl_or = canMove || isActive
// -------------------- Introduction Optional --------------------
// Creating optionals
var itemGather: String? = "Pickaxe"
var isExchangable: Bool?
// Forced unwrapping
print(itemGather!)
"""
Chapter Challenge: Player Stats
Tasks:
1 ) Create 2 variables called characterName and weaponClassification and assign them string values of your choice.
2 ) Use the += operator to add a nickname string onto characterName.
3 ) Create a variable called currentMana and assign it a decimal value.
4 ) Create another variable called manaPercentage and assign it currentMana divided by 100, then explicitly convert it to a string.
5 ) Create a variable called debugStats and use string interpolation to lay out your character stats in a creative way. (HINT: use \n to create line breaks)
6 ) Create 2 boolean variables called questAccepted and canQuest respectively and assign them values of your choice.
7 ) Use the AND (&&) operator to evaluate if questAccepted and canQuest are both true and store it in a variable called questStatus.
8 ) Add an interpolated string that includes questStatus to debugStats using the append method and print out debugStats.
"""
// Solution :
// 1
var characterName = "Adam"
var weaponClassification = "Carry"
// 2
characterName += " Erving"
// 3
var currentMana: Double = 78.422212312
// 4
var manaPercentage: String = String(currentMana / 100)
// 5
var debugStats = "The brave \(characterName) with \(currentMana) \nMana : The percentage of mana is evaluated to be \(manaPercentage)"
// 6
var questAccepted: Bool = true
var canQuest: Bool = true
// 7
var questStatus = questAccepted && canQuest
// 8
debugStats.append(contentsOf: "\nQuest accepted status : \(questAccepted)\nAbility to quest status : \(canQuest)")
print(debugStats)