Some cleanup

This commit is contained in:
Sage Vaillancourt 2020-12-28 22:51:01 -05:00
parent b3ffa54a8e
commit 45a109f8f3
1 changed files with 25 additions and 50 deletions

View File

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