Save 37% off PRO during our Black Friday Sale! »

リファクタリング・チャレンジ リバーシ編

リファクタリング・チャレンジ リバーシ編

06641ec93c5914e0db74cddff2ec6004?s=128

Yuki Yasoshima

May 16, 2020
Tweet

Transcript

  1. :VLJ:BTPTIJNB!ZBTP@TBO ϦϑΝΫλϦϯάɾνϟϨϯδ Ϧόʔγฤ IUUQTHJUIVCDPNPCKFDUJWFBVEJPSFWFSTJJPT

  2. ԿΛϦϑΝΫλϦϯά͢Δͷ͔ w ͦΜͳʹ'BUͰ΋ͳ͍ͷͰ͸ʁ w ήʔϜͷίʔυͱͯͪ͠ΌΜͱ੔ཧ͞Ε͍ͯͳ͍ํ͕໰୊ʁ w ࣌ؒ࣠ํ޲ͷঢ়ଶ؅ཧ͕࠷΋ॏཁ w ઃܭύλʔϯ͸ΏΔ;ΘͰ w

    ֎෦ϥΠϒϥϦ͸࢖Θͳ͍ɻ7JFXʹ͸खΛ͚ͭͳ͍
  3. Ͳ͏΍ͬͯίʔυΛ੔ཧ͢Δ͔ w ίʔυΛ͋Δ΂͖ܗʹ͢Δʢύλʔϯʹ͸ΊΔͷͰ͸ͳ͍ʣ w ᐆດ͞Λͳ͘͢ʢܕ΍ؔ਺ͷ࡞ΓํͳͲʣ w ҙຯ͕Θ͔ΔΑ͏ʹ͢Δʢ໊લͷ෇͚ํɺॲཧͷྲྀΕͳͲʣ w ͭ໰୊Λղܾ͢Ε͹ผͷ໰୊΋ղܾ͠΍͘͢ͳΔ w

    Ͳ͜·Ͱ΍Δ͔͸ཧ૝ͱ࣌ؒͱͷ݉Ͷ߹͍
  4. ঢ়ଶͷ؅ཧ

  5. େ·͔ͳঢ়ଶભҠ ήʔϜ݁Ռ ։࢝ 6*଴ͪ ૢ࡞଴ͪ ύε଴ͪ ഑ஔத Ϧηοτ MBVODIJOH QBTTJOH

    PQFSBUJOH QMBDJOH SFTVMU ή ʔ Ϝ ऴ ྃ ήʔϜத ४උ׬ྃ ࣍ͷϓϨʔϠ͚ͩ ஔ͚ͳ͍ ύε͢Δ ϓϨʔϠަ୅ ஔ͘ ͲͪΒ΋ ஔ͚ͳ͍ ϓϨʔϠަ୅
  6. ήʔϜͷঢ়ଶΛFOVNͰఆٛ enum State { case launching(side: Side) case operating(side: Side,

    player: Player) case passing(side: Side) case placing(side: Side, positions: [Position]) case result(Result) case resetting, branching, next } w ڞ༻ܕͷFOVNͳΒ֤εςʔτ͝ͱʹඞཁͳ஋͚ͩΛ࣋ͯΔ
  7. શମͷߏ੒

  8. Ϋϥεߏ੒ 7JFX$POUSPMMFS 1SFTFOUFS *OUFSBDUPS ݺͼग़͠ ݺͼग़͠ ௨஌ ௨஌ %BUB4UPSF อଘ

    ಡΈࠐΈ w *OUFSBDUPSήʔϜͷঢ়ଶΛ؅ཧɻΞϓϦͷத৺ w 1SFTFOUFS*OUFSBDUPSͱ7$ͷڮ౉͠
  9. ΦϒδΣΫτͷࢀর 7JFX$POUSPMMFS 1SFTFOUFS *OUFSBDUPS 4USPOH 8FBL w ສ͕Ұ7$͕κϯϏʹͳͬͯ΋*OUFSBDUPSͱܨ͕ͬͯͳ͚Ε͹༨ܭͳΠϕϯτ ΛແࢹͰ͖Δ γϯάϧτϯ

  10. ࡉ͔͍ίʔυͷ੔ཧͷྫ

  11. OFYU5VSO ϦϑΝΫλϦϯάલ func nextTurn() { guard var turn = self.turn

    else { return } turn.flip() if validMoves(for: turn).isEmpty { if validMoves(for: turn.flipped).isEmpty { self.turn = nil // ...ॲཧ͍Ζ͍Ζ } else { self.turn = turn // ...ॲཧ͍Ζ͍Ζ } } else { self.turn = turn // ...ॲཧ͍Ζ͍Ζ } } w UVSO͕OJMͰདྷͯ΋ྑ͍ͷ͔ʁ w UVSOͷঢ়ଶ͕Θ͔Γʹ͍͘ w ෼ذͨ͠ޙͷॲཧ͕ଟ͍
  12. OFYU5VSO ϦϑΝΫλϦϯάத w UVSOΛTJEFʹͯ͠OJMΛ࠷ ॳ͔Βআ֎ w DVSSFOUͱOFYUʹ෼͚Δ w CPBSEͰWBMJE.PWFT w

    ෼ذͨ͠ޙͷॲཧ͸࣍ͷ εςʔτΛฦ͚ͩ͢ func nextTurnState(from currentSide: Side) -> State { let nextSide = currentSide.flipped if board.validMoves(for: nextSide).isEmpty { if board.validMoves(for: currentSide).isEmpty { return .result(board.result()) } else { return .passing(side: nextSide) } } else { return .operating(side: nextSide, player: player(for: nextSide)) } }
  13. OFYU5VSO ϦϑΝΫλϦϯάޙ w WBMJE.PWFTJT&NQUZ ΛDBO1MBDFʹஔ͖׵͑ w ωετΛઙ͘͢Δ func nextTurnState(from currentSide:

    Side) -> State { let nextSide = currentSide.flipped if board.canPlace(for: nextSide) { return .operating(side: nextSide, player: player(for: nextSide)) } else if board.canPlace(for: currentSide) { return .passing(side: nextSide) } else { return .resulting(board.result()) } }
  14. ·ͱΊ w Կ͸ͳ͘ͱ΋ঢ়ଶ؅ཧ w ঢ়ଶ͕؅ཧͰ͖Ε͹໰୊΍ߟྀෆ଍Λݟ͚ͭ΍͍͢ w ᐆດ͞Λল͘ɺҙຯ͕௨͡Α͏ʹ͢Δ w 7JFX$POUSPMMFS͸7JFXΛ؅ཧ͢Δ͚ͩɻΞϓϦͷຊମ͸ผ IUUQTHJUIVCDPNPCKFDUJWFBVEJPSFWFSTJJPT