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

Yuta Koshizawa

April 12, 2018
Tweet

More Decks by Yuta Koshizawa

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. Swi$ ͷ஋ܕ × SWIFT QUEST

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)ͷμϝʔδʂ")

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)ͷμϝʔδʂ")

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)ͷμϝʔδʂ")

    View full-size slide

  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)

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

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

    View full-size slide

  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)!) //
    }

    View full-size slide

  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)!) //
    }

    View full-size slide

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

    View full-size slide