mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Separate out game state into its own component
This commit is contained in:
parent
5556de00b0
commit
d86d94dc70
@ -6,6 +6,7 @@ import classNames from "classnames";
|
|||||||
import ChessBoardSquare from "./chess-board-square";
|
import ChessBoardSquare from "./chess-board-square";
|
||||||
import RankLabels from "./rank-labels";
|
import RankLabels from "./rank-labels";
|
||||||
import FileLabels from "./file-labels";
|
import FileLabels from "./file-labels";
|
||||||
|
import GameState from "./game-state";
|
||||||
|
|
||||||
class ChessBoard extends React.Component {
|
class ChessBoard extends React.Component {
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
@ -74,12 +75,6 @@ class ChessBoard extends React.Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
get gameState() {
|
|
||||||
const { store } = this.props;
|
|
||||||
console.log(store.getState().state);
|
|
||||||
return store.getState().state;
|
|
||||||
}
|
|
||||||
|
|
||||||
get boardClass() {
|
get boardClass() {
|
||||||
const turn = this.getTurn();
|
const turn = this.getTurn();
|
||||||
const player = this.getPlayer();
|
const player = this.getPlayer();
|
||||||
@ -88,6 +83,8 @@ class ChessBoard extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const { store } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={this.boardClass}>
|
<div className={this.boardClass}>
|
||||||
<RankLabels />
|
<RankLabels />
|
||||||
@ -97,9 +94,7 @@ class ChessBoard extends React.Component {
|
|||||||
{this.renderSquares()}
|
{this.renderSquares()}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="board-game-state">
|
<GameState store={store} />
|
||||||
{this.gameState}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import classNames from "classnames";
|
|
||||||
|
|
||||||
class FileLabels extends React.Component {
|
class FileLabels extends React.Component {
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
}
|
|
||||||
|
|
||||||
get fileLabels() {
|
get fileLabels() {
|
||||||
return ["a", "b", "c", "d", "e", "f", "g", "h"];
|
return ["a", "b", "c", "d", "e", "f", "g", "h"];
|
||||||
}
|
}
|
||||||
|
|||||||
37
assets/js/components/game-state.js
Normal file
37
assets/js/components/game-state.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import React from "react";
|
||||||
|
import classNames from "classnames";
|
||||||
|
|
||||||
|
class GameState extends React.Component {
|
||||||
|
get gameStates() {
|
||||||
|
return {
|
||||||
|
"checkmate": "Checkmate!",
|
||||||
|
"stalemate": "Stalemate",
|
||||||
|
"check": "Check",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get gameState() {
|
||||||
|
const { store } = this.props;
|
||||||
|
return store.getState().state;
|
||||||
|
}
|
||||||
|
|
||||||
|
get friendlyGameState() {
|
||||||
|
return this.gameStates[this.gameState];
|
||||||
|
}
|
||||||
|
|
||||||
|
get gameStateClass() {
|
||||||
|
const state = this.gameState;
|
||||||
|
|
||||||
|
return classNames("board-game-state", state);
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div className={this.gameStateClass}>
|
||||||
|
{this.friendlyGameState}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default GameState;
|
||||||
@ -1,12 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import classNames from "classnames";
|
|
||||||
|
|
||||||
class RankLabels extends React.Component {
|
class RankLabels extends React.Component {
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
}
|
|
||||||
|
|
||||||
get rankLabels() {
|
get rankLabels() {
|
||||||
return [1, 2, 3, 4, 5, 6, 7, 8];
|
return [1, 2, 3, 4, 5, 6, 7, 8];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user