mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Move a piece!
This commit is contained in:
parent
5ca7520012
commit
d5860ffcfa
@ -1,21 +1,17 @@
|
|||||||
import defaultState from "store/default-state";
|
import defaultState from "store/default-state";
|
||||||
|
import movePiece from "reducers/move-piece";
|
||||||
|
|
||||||
const chessBoardReducer = (state = defaultState, action) => {
|
const chessBoardReducer = (state = defaultState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case "MOVE_PIECE":
|
case "MOVE_PIECE":
|
||||||
var piece = state.board[from.rank][from.file];
|
const newState = {
|
||||||
state.board[to.rank][to.file] = piece;
|
board: movePiece(state.board, action.from, action.to),
|
||||||
state.board[from.rank][from.file] = null;
|
selectedSquare: null
|
||||||
return state;
|
}
|
||||||
|
|
||||||
var newBoard = state.board.map((item, index) => {
|
return Object.assign({}, state, newState);
|
||||||
});
|
|
||||||
return Object.assign({}, state, {
|
|
||||||
board: newBoard
|
|
||||||
});
|
|
||||||
|
|
||||||
case "SELECT_PIECE":
|
case "SELECT_PIECE":
|
||||||
console.log("Action fired");
|
|
||||||
return Object.assign({}, state, { selectedSquare: action.coords });
|
return Object.assign({}, state, { selectedSquare: action.coords });
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
17
app/reducers/move-piece.js
Normal file
17
app/reducers/move-piece.js
Normal 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;
|
||||||
Loading…
Reference in New Issue
Block a user