Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions ui/learn/src/levelCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as timeouts from './timeouts';
import { failure, levelStart, levelEnd, take, move as moveSound } from './sound';
import makeChess, { type ChessCtrl } from './chess';
import makeScenario, { type Scenario } from './scenario';
import { type SquareName, makeSquare, makeUci, opposite } from 'chessops';
import { type SquareName, makeSquare, makeUci, opposite, parseSquare } from 'chessops';
import type { CgMove } from './chessground';
import { PromotionCtrl } from './promotionCtrl';
import { type Prop, prop } from 'lib';
Expand Down Expand Up @@ -135,9 +135,16 @@ export class LevelCtrl {
failure();
return true;
};
const enemyRoleToBeCaptured = (orig: SquareName, dest: SquareName): Role | undefined => {
const destSquare = parseSquare(dest);
const pieceBeingMoved = chess.instance.board.get(parseSquare(orig));
const enPassant = destSquare === chess.instance.epSquare && pieceBeingMoved?.role === 'pawn';
return enPassant ? 'pawn' : chess.instance.board.get(destSquare)?.role;
};

return (orig: SquareName, dest: SquareName, prom?: PromotionRole) => {
vm.nbMoves++;
const enemyRoleCaptured = enemyRoleToBeCaptured(orig, dest);
const move = chess.move(orig, dest, prom);
if (move) this.setFen(chess.fen(), blueprint.color, new Map());
else {
Expand All @@ -148,6 +155,8 @@ export class LevelCtrl {
redraw();
return;
}
// Todo: refactor so that flows for scenario / non-scenario / apple level get their own functions?
// E.g., currently the semantics for `took` vary depending on the flow we're in.
let took = false,
inScenario,
captured = false;
Expand All @@ -156,9 +165,8 @@ export class LevelCtrl {
items.remove(makeSquare(move.to));
took = true;
});
const pieceAtKey = chess.instance.board.get(move.to);
if (!took && pieceAtKey && blueprint.pointsForCapture && pieceAtKey.role !== 'king') {
vm.score += blueprint.showPieceValues ? pieceValue(pieceAtKey.role) : capture;
if (!took && enemyRoleCaptured && blueprint.pointsForCapture && enemyRoleCaptured !== 'king') {
vm.score += blueprint.showPieceValues ? pieceValue(enemyRoleCaptured) : capture;
took = true;
}
this.setCheck();
Expand Down