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

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

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

Yuki Yasoshima

May 16, 2020
Tweet

More Decks by Yuki Yasoshima

Other Decks in Programming

Transcript

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

    PQFSBUJOH QMBDJOH SFTVMU ή ʔ Ϝ ऴ ྃ ήʔϜத ४උ׬ྃ ࣍ͷϓϨʔϠ͚ͩ ஔ͚ͳ͍ ύε͢Δ ϓϨʔϠަ୅ ஔ͘ ͲͪΒ΋ ஔ͚ͳ͍ ϓϨʔϠަ୅
  2. ήʔϜͷঢ়ଶΛ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ͳΒ֤εςʔτ͝ͱʹඞཁͳ஋͚ͩΛ࣋ͯΔ
  3. Ϋϥεߏ੒ 7JFX$POUSPMMFS 1SFTFOUFS *OUFSBDUPS ݺͼग़͠ ݺͼग़͠ ௨஌ ௨஌ %BUB4UPSF อଘ

    ಡΈࠐΈ w *OUFSBDUPSήʔϜͷঢ়ଶΛ؅ཧɻΞϓϦͷத৺ w 1SFTFOUFS*OUFSBDUPSͱ7$ͷڮ౉͠
  4. 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 ෼ذͨ͠ޙͷॲཧ͕ଟ͍
  5. 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)) } }
  6. 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()) } }