Clean-up and more testing

This commit is contained in:
Sage Vaillancourt 2021-01-03 13:32:58 -05:00
parent 4b38e44a3a
commit 0ba0c2850f
3 changed files with 122 additions and 53 deletions

View File

@ -35,5 +35,8 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"homepage": "./" "homepage": "./",
"devDependencies": {
"react-test-renderer": "^17.0.1"
}
} }

View File

@ -169,7 +169,9 @@ class Board extends React.Component {
text = text.replace(/[\n]+/g, ''); text = text.replace(/[\n]+/g, '');
const squares = text.substring(1); const squares = text.substring(1);
return { return {
hand: null, hand: {
heldPiece: null,
},
blackIsNext: text[0].toUpperCase() === 'B', blackIsNext: text[0].toUpperCase() === 'B',
squares: squares.split('').map(c => { squares: squares.split('').map(c => {
const type = c.toLowerCase(); const type = c.toLowerCase();
@ -236,8 +238,7 @@ class Board extends React.Component {
doReset() { doReset() {
this.setState(this.getSetting(SHUFFLING_ENABLED) ? this.setState(this.getSetting(SHUFFLING_ENABLED) ?
this.shuffledBackRowState() : this.shuffledBackRowState() : this.originalState());
this.originalState());
this.setState({ this.setState({
showPopup: false, showPopup: false,
}); });
@ -314,7 +315,7 @@ class Board extends React.Component {
return 1; return 1;
} }
}; };
function addBunch(xFunc, yFunc, isUp) { const addBunch = (xFunc, yFunc, isUp) => {
for (let i = 1; i < 8; i++) { for (let i = 1; i < 8; i++) {
if(tryAddMove(xFunc(i), yFunc(i)) !== 0) { if(tryAddMove(xFunc(i), yFunc(i)) !== 0) {
break; break;
@ -470,8 +471,8 @@ class Board extends React.Component {
for(const move of moves) { for(const move of moves) {
// Copy the board // Copy the board
const board = this.clone(); const board = this.clone();
board.state.squares[board.getIndex(move.x, move.y)] = board.state.squares[i]; const moveIndex = board.getIndex(move.x, move.y);
board.state.squares[i].isEmpty(); board.state.squares[moveIndex] = board.state.squares[i];
const check = board.inCheck(checkedKing); const check = board.inCheck(checkedKing);
if (check == null || check.color !== checkedKing.color) { if (check == null || check.color !== checkedKing.color) {
return false; return false;
@ -517,19 +518,18 @@ class Board extends React.Component {
const move = this.isValidMove(from, to) const move = this.isValidMove(from, to)
if (move) { if (move) {
if (move.passant) { if (move.passant) {
squares[this.getIndex(move.passant.x, move.passant.y)] = new Piece(EMPTY, EMPTY); const i = this.getIndex(move.passant.x, move.passant.y);
squares[i] = new Piece(EMPTY, EMPTY);
} }
if (move.castle) { if (move.castle) {
// .castle holds the position where the rook should end up // .castle holds the position where the rook should end up
// King moved left // King moved left
const rookX = move.castle[0] > move.x ? 0 : 7; const rookX = move.castle[0] > move.x ? 0 : 7;
console.log("Replace "); console.log("Replace " + move.castle + " with " + [rookX, move.castle[1]]);
console.log(move.castle); const rookStart = this.getIndex(rookX, move.castle[1]);
console.log("With "); const rookLanding = this.getIndex(move.castle[0], move.castle[1]);
console.log([rookX, move.castle[1]]); squares[rookLanding] = squares[rookStart];
squares[this.getIndex(move.castle[0], move.castle[1])] = squares[rookStart] = new Piece(EMPTY, EMPTY);
squares[this.getIndex(rookX, move.castle[1])];
squares[this.getIndex(rookX, move.castle[1])] = new Piece(EMPTY, EMPTY);
} }
// Remove existing passantable states // Remove existing passantable states
squares.forEach(square => { squares.forEach(square => {
@ -703,4 +703,5 @@ class Board extends React.Component {
} }
export default Board; export default Board;
export {Piece};
export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY }; export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY };

View File

@ -1,8 +1,43 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import renderer from 'react-test-renderer';
import Board from './board'; import Board from './board';
import * as game from './board'; import * as game from './board';
it('detects friendship and enemyship', () => {
const black = new game.Piece(game.BLACK, game.KING);
const black2 = new game.Piece(game.BLACK, game.QUEEN);
const white = new game.Piece(game.WHITE, game.KING);
const white2 = new game.Piece(game.WHITE, game.QUEEN);
const empty = new game.Piece(game.EMPTY, game.EMPTY);
const empty2 = new game.Piece(game.EMPTY, game.EMPTY);
const truths = [
black.isFriendOf(black2),
white.isFriendOf(white2),
black.isEnemyOf(white),
white.isEnemyOf(black),
];
for (const truth of truths) {
expect(truth).toBe(true);
}
const lies = [
black.isFriendOf(white), black.isFriendOf(empty),
white.isFriendOf(black), white.isFriendOf(empty),
empty.isFriendOf(white), empty.isFriendOf(black), empty.isFriendOf(empty2),
black.isEnemyOf(black2), black.isEnemyOf(empty),
white.isEnemyOf(white2), white.isEnemyOf(empty),
empty.isEnemyOf(white), empty.isEnemyOf(black), empty.isEnemyOf(empty2),
];
for (const lie of lies) {
expect(lie).toBe(false);
}
});
it('is created correctly', () => { it('is created correctly', () => {
const board = new Board(); const board = new Board();
const rows = ['B', const rows = ['B',
@ -16,6 +51,9 @@ it('is created correctly', () => {
'RNBQKBNR' 'RNBQKBNR'
]; ];
expect(board.textFromState()).toBe(rows.join('')); expect(board.textFromState()).toBe(rows.join(''));
for (const square of board.state.squares) {
expect(square.hasntMoved()).toBe(true);
}
}); });
it('is created from text correctly', () => { it('is created from text correctly', () => {
@ -49,3 +87,30 @@ it('detects an obvious checkmate', () => {
expect(inCheck.type).toEqual(game.KING); expect(inCheck.type).toEqual(game.KING);
expect(inCheck.color).toEqual(game.BLACK); expect(inCheck.color).toEqual(game.BLACK);
}); });
it('detects basic castling', () => {
const startRows = ['B',
'____k___',
'________',
'________',
'________',
'________',
'________',
'PPPPPPPP',
'R___K__R'
];
// const endRows = ['B',
// '____k___',
// '________',
// '________',
// '________',
// '________',
// '________',
// 'PPPPPPPP',
// 'R____RK_'
// ];
let board = new Board({text: startRows.join('')});
let move = board.isValidMove(60, 62);
expect(move.x).toBe(6);
expect(move.y).toBe(7);
});