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

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

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. ԋࢉ // (߈ܸྗ - ๷ޚྗ) ÷ 2 ͰμϝʔδΛܭࢉ͢Δ৔߹ let damage

    = (character.attack - target.defense) / 2 print("\(character.name)ͷ͜͏͖͛ɻ") print("\(target.name)ʹ\(damage)ͷμϝʔδʂ")
  2. ഑ྻͱ܁Γฦ͠ // ΩϟϥΫλʔͷ഑ྻͰύʔςΟΛද͢ var party: [Character] = [...] // ϧʔϓͰύʔςΟͷΩϟϥΫλʔΛॱ൪ʹऔಘ

    for character in party { print(character.name) print("HP \(character.hp)") print("MP \(character.mp)") print() }
  3. Swi$ ඪ४ϥΠϒϥϦ͸΄΅͢΂ͯ஋ܕ struct Int { ... } struct Double {

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

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

    4] var b = a a[2] = 5 print(a) // [2, 3, 5] print(b) // [2, 3, 4]
  6. ϛϡʔλϒϧΫϥε class Character { let name: String // ໊લ var

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

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

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

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

    ໊લ var hp: Int // ̝̥ var mp: Int // ̢̥ let attack: Int // ߈ܸྗ let defense: Int // ๷ޚྗ ... }
  13. ஋ܕʢ 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)ͷμϝʔδʂ")
  14. ϛϡʔλϒϧΫϥε 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)
  15. ஋ܕʢ 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)
  16. ஋ܕʢ 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)
  17. ϛϡʔλϒϧΫϥε 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)
  18. ϛϡʔλϒϧΫϥε 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)!) // }
  19. ஋ܕʢ 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)!) // }
  20. ॓୊ ஋ܕͰεϚʔτʹॻ͘ʹ͸Ͳ͏͢Ε͹Α͍Ͱ͠ΐ͏ʁ // ϛϡʔλϒϧΫϥε performAttack(by: character, to: any(of: friendParty.members)!) //

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