Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

3c031541ed3d92869414857dfef853de?s=128

Yuta Koshizawa

April 12, 2018
Tweet

Transcript

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

  2. None
  3. None
  4. None
  5. None
  6. ม਺ let maxHP = 153 // ࠷େHP var hp =

    maxHP // ݱࡏHP
  7. ԋࢉ // (߈ܸྗ - ๷ޚྗ) ÷ 2 ͰμϝʔδΛܭࢉ͢Δ৔߹ let damage

    = (character.attack - target.defense) / 2 print("\(character.name)ͷ͜͏͖͛ɻ") print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
  8. ৚݅෼ذ hp += 80 // ࠷େHPΛ௒͑ͯճ෮͠ͳ͍Α͏ʹ͢Δ if hp > maxHP

    { hp = maxHP }
  9. ഑ྻͱ܁Γฦ͠ // ΩϟϥΫλʔͷ഑ྻͰύʔςΟΛද͢ var party: [Character] = [...] // ϧʔϓͰύʔςΟͷΩϟϥΫλʔΛॱ൪ʹऔಘ

    for character in party { print(character.name) print("HP \(character.hp)") print("MP \(character.mp)") print() }
  10. Α͋͘Δϓϩάϥϛϯάॳ৺ऀ޲͚ͷ՝୊ // 1͔Β100·Ͱͷ߹ܭΛٻΊͳ͍͞ var sum = 0 for n in

    1...100 { sum += n } print(sum)
  11. ۩ମྫͰֶ΅͏ʹ΋ྫΛཧղ͢Δͷ͕େม

  12. None
  13. RPGΛ୊ࡐʹϓϩάϥϛϯάΛֶͿ × ॳ৺ऀ

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

  15. None
  16. Swi$ ͷҰ൪ͷಛ௃͸ԿͰ͔͢ʁ

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

  18. Swi$ ඪ४ϥΠϒϥϦ͸΄΅͢΂ͯ஋ܕ struct Int { ... } struct Double {

    ... } struct Bool { ... } struct String { ... } struct Array<Element> { ... } struct Dictionary<Key, Value> { ... } enum Optional<Wrapped> { ... }
  19. ࢀরܕͷίϨΫγϣϯʢ Python ʣ # Python a = [2, 3, 4]

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

    4] var b = a a[2] = 5 print(a) // [2, 3, 5] print(b) // [2, 3, 4]
  21. Swi$ ͷ஋ܕ × SWIFT QUEST

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

  23. ϛϡʔλϒϧΫϥε class Character { let name: String // ໊લ var

    hp: Int // ̝̥ var mp: Int // ̢̥ let attack: Int // ߈ܸྗ let defense: Int // ๷ޚྗ ... }
  24. ༐ऀ let hero = Character( name: "Ώ͏͠Ό", hp: 153, mp:

    25, attack: 162, defense: 97 )
  25. ϛϡʔλϒϧΫϥε 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)ͷμϝʔδʂ")
  26. ϛϡʔλϒϧΫϥε class Character { let name: String // ໊લ var

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

    hp: Int // ̝̥ let mp: Int // ̢̥ let attack: Int // ߈ܸྗ let defense: Int // ๷ޚྗ ... }
  28. ΠϛϡʔλϒϧΫϥε 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)ͷμϝʔδʂ")
  29. ΠϛϡʔλϒϧΫϥε class Character { let name: String // ໊લ let

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

    ໊લ var hp: Int // ̝̥ var mp: Int // ̢̥ let attack: Int // ߈ܸྗ let defense: Int // ๷ޚྗ ... }
  31. ஋ܕʢ 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)ͷμϝʔδʂ")
  32. ϛϡʔλϒϧΫϥε 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)
  33. ΠϛϡʔλϒϧΫϥε // লུ

  34. ஋ܕʢ 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)
  35. ஋ܕʢ 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)
  36. ϛϡʔλϒϧΫϥε 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)
  37. ϛϡʔλϒϧΫϥε class Party { let members: [Character] ... }

  38. ϛϡʔλϒϧΫϥε 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)!) // }
  39. ஋ܕʢ 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)!) // }
  40. ॓୊ ஋ܕͰεϚʔτʹॻ͘ʹ͸Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏ʁ // ϛϡʔλϒϧΫϥε performAttack(by: character, to: any(of: friendParty.members)!) //

    // ஋ܕʢ struct ʣ performAttack(by: character, to: &any(of: enemyParty.members)!) // h"ps:/ /git.io/vx7jW
  41. None