Skip to content

Commit

Permalink
reducers 追加
Browse files Browse the repository at this point in the history
  • Loading branch information
hey-cube committed Oct 21, 2019
1 parent 09223f9 commit be72914
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 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;
}

0 comments on commit be72914

Please sign in to comment.