diff --git a/src/components/Board.js b/src/components/Board.js index eb42e2d..f6a83c2 100644 --- a/src/components/Board.js +++ b/src/components/Board.js @@ -1,34 +1,32 @@ -import React, { Component } from 'react'; +import React from 'react'; import Square from './Square' import '../styles/board.css' -export default class Board extends Component { - files = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; - ranks = [1, 2, 3, 4, 5, 6, 7, 8]; +const files = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +const ranks = [1, 2, 3, 4, 5, 6, 7, 8]; - getPiece(f, r) { - return this.props.squares[this.ranks.indexOf(r)* 8 + this.files.indexOf(f)].piece; - } +function getPiece(squares, f, r) { + return squares[ranks.indexOf(r)* 8 + files.indexOf(f)].piece; +} - render() { - return ( - - - {this.ranks.slice().reverse().map(r => - - {this.files.map(f => - )} - )} - -
this.props.handleClick(f+r)}> - square.file===f && square.rank === r).length : false} /> -
- ); - } +export default function Board(props) { + return ( + + + {ranks.slice().reverse().map(r => + + {files.map(f => + )} + )} + +
props.handleClick(f+r)}> + square.file===f && square.rank === r).length : false} /> +
+ ); } diff --git a/src/containers/Root.js b/src/containers/Root.js deleted file mode 100644 index e8676d9..0000000 --- a/src/containers/Root.js +++ /dev/null @@ -1,62 +0,0 @@ -import React, { Component } from 'react'; -import Board from '../components/Board'; -import Chess from 'chess' - -export default class Root extends Component { - constructor(props) { - super(props) - this.state = { - gameClient: Chess.createSimple(), - selectedSquare: null - } - } - - getPiece(square) { - return this.state.gameClient.getStatus().board.getSquare(square).piece; - } - - getThreatenedSquares() { - return this.state.gameClient.validMoves - .filter(move => move.src.file+move.src.rank===this.state.selectedSquare) - .map(move => move.squares) - } - - handleClick(square) { - if(square === this.state.selectedSquare) { - this.setState({ - gameClient: this.state.gameClient, - selectedSquare: null - }); - return; - } - const selectedPiece = this.getPiece(square); - if(selectedPiece - && selectedPiece.side.name === this.state.gameClient.game.getCurrentSide().name) - this.setState({ - gameClient: this.state.gameClient, - selectedSquare: square - }) - else { - try { - this.state.gameClient.move(this.state.selectedSquare, square); - this.setState({ - gameClient: this.state.gameClient, - selectedSquare: null - }) - } catch (error) { - if(!error.includes("Move is invalid")) - throw error - } - } - } - - render() { - return ( - this.handleClick(square)} /> - ); - } -} diff --git a/src/containers/VisibleBoard.js b/src/containers/VisibleBoard.js index ce2c47c..044f32f 100644 --- a/src/containers/VisibleBoard.js +++ b/src/containers/VisibleBoard.js @@ -3,14 +3,6 @@ import { connect } from 'react-redux'; import {clickSquare} from '../actions'; import Board from '../components/Board'; -/** - this.handleClick(square)} /> - */ - function getThreatenedSquares(client, selected) { return client.validMoves .filter(move => move.src.file+move.src.rank===selected) diff --git a/src/reducers/game.js b/src/reducers/game.js index 3497dbd..d709583 100644 --- a/src/reducers/game.js +++ b/src/reducers/game.js @@ -30,7 +30,12 @@ function handleClick(state, square) { return state; } -export default (state = {client: Chess.createSimple(), selectedSquare: null}, action) => { +const initialState = { + client: Chess.createSimple(), + selectedSquare: null +}; + +export default (state = initialState, action) => { switch(action.type) { case 'CLICK_SQUARE': return handleClick(state, action.square);