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 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:
|
||||
|
||||
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