From be72914aacb56e2c61dcd42c89f5d9cee7d15e1a Mon Sep 17 00:00:00 2001 From: hey-cube Date: Sun, 28 Jul 2019 17:35:12 +0900 Subject: [PATCH] =?UTF-8?q?reducers=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/reducers.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/reducers.js diff --git a/src/reducers.js b/src/reducers.js new file mode 100644 index 0000000..6d1bc76 --- /dev/null +++ b/src/reducers.js @@ -0,0 +1,68 @@ +import { combineReducers } from "redux"; +import { CLICK_SQUARE, JUMP_TO_PAST } from "./actions"; + +const initialState = { + history: [ + { + squares: Array(9).fill(null) + } + ], + stepNumber: 0, + xIsNext: true +}; + +function game(state = initialState, action) { + switch (action.type) { + case CLICK_SQUARE: + const history = state.history.slice(0, state.stepNumber + 1); + const current = history[history.length - 1]; + const squares = current.squares.slice(); + if (calculateWinner(squares) || squares[action.index]) { + return state; + } + squares[action.index] = state.xIsNext ? "X" : "O"; + return { + history: history.concat([ + { + squares: squares + } + ]), + stepNumber: history.length, + xIsNext: !state.xIsNext + }; + + case JUMP_TO_PAST: + return { + ...state, + stepNumber: action.step, + xIsNext: action.step % 2 === 0 + }; + + default: + return state; + } +} + +export const app = combineReducers({ game }); + +// ======================================== + +function calculateWinner(squares) { + const lines = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [2, 4, 6] + ]; + for (let i = 0; i < lines.length; i++) { + const [a, b, c] = lines[i]; + if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { + return squares[a]; + } + } + return null; +}