Some cleanup
This commit is contained in:
parent
b3ffa54a8e
commit
45a109f8f3
61
src/index.js
61
src/index.js
|
@ -67,16 +67,15 @@ function isKing(piece) {
|
|||
}
|
||||
|
||||
function Square(props) {
|
||||
let bg = {
|
||||
backgroundImage: `url(${Images[props.value]})`,
|
||||
backgroundSize: `100%`,
|
||||
backgroundColor: props.bg,
|
||||
};
|
||||
return (
|
||||
<button
|
||||
className="square"
|
||||
onClick={props.onClick}
|
||||
style={bg}
|
||||
style={{
|
||||
backgroundImage: `url(${Images[props.value]})`,
|
||||
backgroundSize: `100%`,
|
||||
backgroundColor: props.bgColor,
|
||||
}}
|
||||
>
|
||||
</button>
|
||||
);
|
||||
|
@ -92,7 +91,6 @@ class Board extends React.Component {
|
|||
heldPiece: null,
|
||||
},
|
||||
};
|
||||
this.reset();
|
||||
}
|
||||
|
||||
setHand(hand) {
|
||||
|
@ -138,7 +136,7 @@ class Board extends React.Component {
|
|||
return [x, y];
|
||||
}
|
||||
|
||||
squareAt(x, y) {
|
||||
pieceAt(x, y) {
|
||||
let i = x + (y * 8);
|
||||
if (i < 64 && x < 8 && y < 8) {
|
||||
return this.state.squares[x + (y * 8)];
|
||||
|
@ -148,19 +146,19 @@ class Board extends React.Component {
|
|||
}
|
||||
|
||||
whiteAt(x, y) {
|
||||
let square = this.squareAt(x, y);
|
||||
let square = this.pieceAt(x, y);
|
||||
if (square == null) {
|
||||
return false;
|
||||
}
|
||||
return square < Pieces.BlackPawn;
|
||||
return isWhite(square);
|
||||
}
|
||||
|
||||
blackAt(x, y) {
|
||||
let square = this.squareAt(x, y);
|
||||
let square = this.pieceAt(x, y);
|
||||
if (square == null) {
|
||||
return false;
|
||||
}
|
||||
return square >= Pieces.BlackPawn;
|
||||
return isBlack(square);
|
||||
}
|
||||
|
||||
isEnemyOf(piece, x, y) {
|
||||
|
@ -172,7 +170,7 @@ class Board extends React.Component {
|
|||
var i;
|
||||
var moves = [];
|
||||
let tryAddMove = (x, y) => {
|
||||
if (this.squareAt(x, y) == null) {
|
||||
if (this.pieceAt(x, y) == null) {
|
||||
moves.push([x, y]);
|
||||
// Keep searching
|
||||
return 0;
|
||||
|
@ -188,9 +186,9 @@ class Board extends React.Component {
|
|||
let shift = pieceIsBlack ? -1 : 1;
|
||||
let startLine = pieceIsBlack ? 6 : 1;
|
||||
|
||||
if (this.squareAt(x, y + shift) == null) {
|
||||
if (this.pieceAt(x, y + shift) == null) {
|
||||
moves.push([x, y + shift]);
|
||||
if (y === startLine && this.squareAt(x, y + (shift * 2)) == null) {
|
||||
if (y === startLine && this.pieceAt(x, y + (shift * 2)) == null) {
|
||||
moves.push([x, y + (shift * 2)]);
|
||||
}
|
||||
}
|
||||
|
@ -295,14 +293,14 @@ class Board extends React.Component {
|
|||
let moves = this.getValidMoves(i);
|
||||
for(var j = 0; j < moves.length; j++) {
|
||||
if(moves[j][0] === blackKing[0] && moves[j][1] === blackKing[1]) {
|
||||
return true;
|
||||
return Pieces.BlackKing;
|
||||
} else if(moves[j][0] === whiteKing[0] && moves[j][1] === whiteKing[1]) {
|
||||
return true;
|
||||
return Pieces.WhiteKing;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
checkmate() {
|
||||
|
@ -382,7 +380,7 @@ class Board extends React.Component {
|
|||
<Square
|
||||
value={this.state.squares[i]}
|
||||
onClick={() => this.handleClick(i)}
|
||||
bg={bgColor}
|
||||
bgColor={bgColor}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
@ -404,10 +402,8 @@ class Board extends React.Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const winner = calculateWinner(this.state.squares);
|
||||
let checkMsg = this.inCheck() ? "Check! " : "";
|
||||
let status = winner ?
|
||||
'Winner: ' + winner :
|
||||
const status =
|
||||
checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn";
|
||||
|
||||
var texttext =
|
||||
|
@ -421,7 +417,6 @@ class Board extends React.Component {
|
|||
{this.row(5)}
|
||||
{this.row(6)}
|
||||
{this.row(7)}
|
||||
{this.inCheck()}
|
||||
</div>
|
||||
;
|
||||
|
||||
|
@ -453,23 +448,3 @@ ReactDOM.render(
|
|||
<Game />,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
||||
function calculateWinner(squares) {
|
||||
const lines = [
|
||||
[0, 1, 2],
|
||||
[3, 4, 5],
|
||||
[6, 7, 8],
|
||||
[0, 3, 6],
|
||||
[1, 4, 7],
|
||||
[2, 5, 8],
|
||||
[0, 4, 8],
|
||||
[2, 4, 6],
|
||||
];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const [a, b, c] = lines[i];
|
||||
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
|
||||
return squares[a];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue