shogiops
Version:
Shogi rules and operations
65 lines • 2.96 kB
JavaScript
import { bishopAttacks, goldAttacks, kingAttacks, pawnAttacks, rookAttacks, silverAttacks, } from '../attacks.js';
import { Board } from '../board.js';
import { Hands } from '../hands.js';
import { SquareSet } from '../square-set.js';
import { opposite } from '../util.js';
import { Position } from './position.js';
import { standardDropDests, standardMoveDests } from './shogi.js';
export class Minishogi extends Position {
constructor() {
super('minishogi');
}
static default() {
const pos = new this();
pos.board = minishogiBoard();
pos.hands = Hands.empty();
pos.turn = 'sente';
pos.moveNumber = 1;
return pos;
}
static from(setup, strict) {
const pos = new this();
pos.fromSetup(setup);
return pos.validate(strict).map((_) => pos);
}
squareAttackers(square, attacker, occupied) {
const defender = opposite(attacker), board = this.board;
return board.color(attacker).intersect(rookAttacks(square, occupied)
.intersect(board.roles('rook', 'dragon'))
.union(bishopAttacks(square, occupied).intersect(board.roles('bishop', 'horse')))
.union(goldAttacks(square, defender).intersect(board.roles('gold', 'tokin', 'promotedsilver')))
.union(silverAttacks(square, defender).intersect(board.role('silver')))
.union(pawnAttacks(square, defender).intersect(board.role('pawn')))
.union(kingAttacks(square).intersect(board.roles('king', 'dragon', 'horse'))));
}
squareSnipers(square, attacker) {
const empty = SquareSet.empty();
return rookAttacks(square, empty)
.intersect(this.board.roles('rook', 'dragon'))
.union(bishopAttacks(square, empty).intersect(this.board.roles('bishop', 'horse')))
.intersect(this.board.color(attacker));
}
moveDests(square, ctx) {
return standardMoveDests(this, square, ctx);
}
dropDests(piece, ctx) {
return standardDropDests(this, piece, ctx);
}
}
const minishogiBoard = () => {
const occupied = new SquareSet([0x1001f, 0x100000, 0x1f, 0x0, 0x0, 0x0, 0x0, 0x0]);
const colorIter = [
['sente', new SquareSet([0x0, 0x100000, 0x1f, 0x0, 0x0, 0x0, 0x0, 0x0])],
['gote', new SquareSet([0x1001f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0])],
];
const roleIter = [
['rook', new SquareSet([0x10, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0])],
['bishop', new SquareSet([0x8, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0])],
['gold', new SquareSet([0x2, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0])],
['silver', new SquareSet([0x4, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0])],
['pawn', new SquareSet([0x10000, 0x100000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0])],
['king', new SquareSet([0x1, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0])],
];
return Board.from(occupied, colorIter, roleIter);
};
//# sourceMappingURL=minishogi.js.map