Yuta Koshizawa
April 12, 2018
610

RPGで学ぶSwiftの値型 / Learning Value Types in Swift with RPG

Osaka Mix Leap Study #10 - try! Swift Tokyo Report

April 12, 2018

Transcript

1. RPGͰֶͿSwi'ͷ஋ܕ
Yuta Koshizawa @koher

2. ม਺
let maxHP = 153 // ࠷େHP
var hp = maxHP // ݱࡏHP

3. ԋࢉ
// (߈ܸྗ - ๷ޚྗ) ÷ 2 ͰμϝʔδΛܭࢉ͢Δ৔߹
let damage = (character.attack - target.defense) / 2
print("\(character.name)ͷ͜͏͖͛ɻ")
print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")

4. ৚݅෼ذ
hp += 80
// ࠷େHPΛ௒͑ͯճ෮͠ͳ͍Α͏ʹ͢Δ
if hp > maxHP {
hp = maxHP
}

5. ഑ྻͱ܁Γฦ͠
// ΩϟϥΫλʔͷ഑ྻͰύʔςΟΛද͢
var party: [Character] = [...]
// ϧʔϓͰύʔςΟͷΩϟϥΫλʔΛॱ൪ʹऔಘ
for character in party {
print(character.name)
print("HP \(character.hp)")
print("MP \(character.mp)")
print()
}

6. Α͋͘Δϓϩάϥϛϯάॳ৺ऀ޲͚ͷ՝୊
// 1͔Β100·Ͱͷ߹ܭΛٻΊͳ͍͞
var sum = 0
for n in 1...100 {
sum += n
}
print(sum)

7. ۩ମྫͰֶ΅͏ʹ΋ྫΛཧղ͢Δͷ͕େม

8. RPGΛ୊ࡐʹϓϩάϥϛϯάΛֶͿ
×
ॳ৺ऀ

9. RPGΛ୊ࡐʹϓϩάϥϛϯάΛֶͿ
×
ܦݧऀ

10. Swi\$ ͷҰ൪ͷಛ௃͸ԿͰ͔͢ʁ

11. ͨͱ͑͹ɺ஋ܕத৺ͷݴޠͰ͋Δ͜ͱ

12. Swi\$ ඪ४ϥΠϒϥϦ͸΄΅͢΂ͯ஋ܕ
struct Int { ... }
struct Double { ... }
struct Bool { ... }
struct String { ... }
struct Array { ... }
struct Dictionary { ... }
enum Optional { ... }

13. ࢀরܕͷίϨΫγϣϯʢ Python ʣ
# Python
a = [2, 3, 4]
b = a
a[2] = 5
print(a) # [2, 3, 5]
print(b) # [2, 3, 5]

14. ஋ܕͷίϨΫγϣϯʢ Swi% ʣ
// Swift
var a = [2, 3, 4]
var b = a
a[2] = 5
print(a) // [2, 3, 5]
print(b) // [2, 3, 4]

15. Swi\$ ͷ஋ܕ × SWIFT QUEST

16. ஋ܕ͸↓ͷ͍͍ͱ͜ͲΓ
ϛϡʔλϒϧΫϥε
ΠϛϡʔλϒϧΫϥε

17. ϛϡʔλϒϧΫϥε
class Character {
let name: String // ໊લ
var hp: Int // ̝̥
var mp: Int // ̢̥
let attack: Int // ߈ܸྗ
let defense: Int // ๷ޚྗ
...
}

18. ༐ऀ
let hero = Character(
name: "Ώ͏͠Ό",
hp: 153,
mp: 25,
attack: 162,
defense: 97
)

19. ϛϡʔλϒϧΫϥε
let hero = Character(name: "Ώ͏͠Ό", ...)
let archfiend = Character(name: "·͓͏", ...)
print("\(archfiend.name)ͷ͜͏͖͛ɻ")
let damage = (archfiend.attack - hero.defense) / 2
hero.hp -= damage
print("\(hero.name)ʹ\(damage)ͷμϝʔδʂ")

20. ϛϡʔλϒϧΫϥε
class Character {
let name: String // ໊લ
var hp: Int // ̝̥
var mp: Int // ̢̥
let attack: Int // ߈ܸྗ
let defense: Int // ๷ޚྗ
...
}

21. ΠϛϡʔλϒϧΫϥε
class Character {
let name: String // ໊લ
let hp: Int // ̝̥
let mp: Int // ̢̥
let attack: Int // ߈ܸྗ
let defense: Int // ๷ޚྗ
...
}

22. ΠϛϡʔλϒϧΫϥε
var hero = Character(name: "Ώ͏͠Ό", ...)
var archfiend = Character(name: "·͓͏", ...)
print("\(archfiend.name)ͷ͜͏͖͛ɻ")
let damage = (archfiend.attack - hero.defense) / 2
hero = Character(
name: hero.name,
hp: hero.hp - damage,
mp: hero.mp,
attack: hero.attack,
defense: hero.defense
)
print("\(hero.name)ʹ\(damage)ͷμϝʔδʂ")

23. ΠϛϡʔλϒϧΫϥε
class Character {
let name: String // ໊લ
let hp: Int // ̝̥
let mp: Int // ̢̥
let attack: Int // ߈ܸྗ
let defense: Int // ๷ޚྗ
...
}

24. ஋ܕʢ struct ʣ
struct Character {
let name: String // ໊લ
var hp: Int // ̝̥
var mp: Int // ̢̥
let attack: Int // ߈ܸྗ
let defense: Int // ๷ޚྗ
...
}

25. ஋ܕʢ struct ʣ
var hero = Character(name: "Ώ͏͠Ό", ...)
var archfiend = Character(name: "·͓͏", ...)
print("\(archfiend.name)ͷ͜͏͖͛ɻ")
let damage = (archfiend.attack - hero.defense) / 2
hero.hp -= damage
print("\(hero.name)ʹ\(damage)ͷμϝʔδʂ")

26. ϛϡʔλϒϧΫϥε
func performAttack(by character: Character,
to target: Character) {
print("\(character.name)ͷ͜͏͖͛ɻ")
let damage = (character.attack - target.defense) / 2
target.hp -= damage
print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
print()
}
performAttack(by: hero, to: archfiend)
performAttack(by: archfiend, to: hero)

27. ΠϛϡʔλϒϧΫϥε
// লུ

28. ஋ܕʢ struct ʣ
func performAttack(by character: Character, to target: Character) -> Character {
var target = target
print("\(character.name)ͷ͜͏͖͛ɻ")
let damage = (character.attack - target.defense) / 2
target.hp -= damage
print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
print()
return target
}
archfiend = performAttack(by: hero, to: archfiend)
hero = performAttack(by: archfiend, to: hero)

29. ஋ܕʢ struct ʣ
func performAttack(by character: Character,
to target: inout Character) {
print("\(character.name)ͷ͜͏͖͛ɻ")
let damage = (character.attack - target.defense) / 2
target.hp -= damage
print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
print()
}
performAttack(by: hero, to: &archfiend)
performAttack(by: archfiend, to: &hero)

30. ϛϡʔλϒϧΫϥε
func performAttack(by character: Character,
to target: Character) {
print("\(character.name)ͷ͜͏͖͛ɻ")
let damage = (character.attack - target.defense) / 2
target.hp -= damage
print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
print()
}
performAttack(by: hero, to: archfiend)
performAttack(by: archfiend, to: hero)

31. ϛϡʔλϒϧΫϥε
class Party {
let members: [Character]
...
}

32. ϛϡʔλϒϧΫϥε
let friendParty = Party(members: [...])
let enemyParty = Party(members: [...])
for character in friendParty.members {
performAttack(by: character,
to: any(of: enemyParty.members)!) //
}
for character in enemyParty.members {
performAttack(by: character,
to: any(of: friendParty.members)!) //
}

33. ஋ܕʢ struct ʣ
var friendParty = Party(members: [...])
var enemyParty = Party(members: [...])
for character in friendParty.members {
performAttack(by: character,
to: &any(of: enemyParty.members)!) //
}
for character in enemyParty.members {
performAttack(by: character,
to: &any(of: friendParty.members)!) //
}

34. ॓୊
஋ܕͰεϚʔτʹॻ͘ʹ͸Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏ʁ
// ϛϡʔλϒϧΫϥε
performAttack(by: character, to: any(of: friendParty.members)!) //
// ஋ܕʢ struct ʣ
performAttack(by: character, to: &any(of: enemyParty.members)!) //
h"ps:/
/git.io/vx7jW