1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Move a piece!

This commit is contained in:
Daniel Barber 2016-12-08 16:27:07 +00:00
parent 5ca7520012
commit d5860ffcfa
2 changed files with 23 additions and 10 deletions

View File

@ -1,21 +1,17 @@
import defaultState from "store/default-state";
import movePiece from "reducers/move-piece";
const chessBoardReducer = (state = defaultState, action) => {
switch (action.type) {
case "MOVE_PIECE":
var piece = state.board[from.rank][from.file];
state.board[to.rank][to.file] = piece;
state.board[from.rank][from.file] = null;
return state;
const newState = {
board: movePiece(state.board, action.from, action.to),
selectedSquare: null
}
var newBoard = state.board.map((item, index) => {
});
return Object.assign({}, state, {
board: newBoard
});
return Object.assign({}, state, newState);
case "SELECT_PIECE":
console.log("Action fired");
return Object.assign({}, state, { selectedSquare: action.coords });
default:

View File

@ -0,0 +1,17 @@
import Immutable from "immutable";
import { Map } from "immutable";
const movePiece = (board, from, to) => {
const newBoard = Immutable.fromJS(board);
const piece = board[from.rank][from.file];
const boardChange = Map([
[to.rank, Map([[to.file, piece]])]
]).mergeDeep(Map([
[from.rank, Map([[from.file, null]])]
]));
return newBoard.mergeDeep(boardChange).toJS();
}
export default movePiece;