Skip to content

Commit

Permalink
Add Bestemshe variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Tucker committed Sep 10, 2024
1 parent 247e634 commit 5e04a08
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ function updatePocketPieces(
export function baseMove(state: HeadlessState, orig: cg.Key, dest: cg.Key): cg.Piece | boolean {
const origPiece = state.pieces.get(orig),
destPiece = state.pieces.get(dest);
if ((orig === dest && state.variant !== 'togyzkumalak') || !origPiece) return false;
if ((orig === dest && state.variant !== 'togyzkumalak' && state.variant !== 'bestemshe') || !origPiece) return false;
const captured = isCapture(state.variant, destPiece, origPiece);
if (dest === state.selected) unselect(state);
callUserFunction(state.events.move, orig, dest, captured);

switch (state.variant) {
case 'togyzkumalak':
case 'bestemshe':
setPieces(state, togyzkumalakUpdatePiecesFromMove(state.dimensions, state.pieces, orig, dest));
break;
case 'oware':
Expand Down Expand Up @@ -217,7 +218,8 @@ export function baseMove(state: HeadlessState, orig: cg.Key, dest: cg.Key): cg.P

function isCapture(variant: cg.Variant, destPiece: cg.Piece | undefined, origPiece: cg.Piece): cg.Piece | undefined {
switch (variant) {
case 'togyzkumalak': {
case 'togyzkumalak':
case 'bestemshe': {
//TODO account for wrapping around board (simimlar to oware capture)
const count = destPiece && Number(destPiece.role.split('-')[0].substring(1));
return destPiece && destPiece.playerIndex !== origPiece.playerIndex && count && (count === 2 || count % 2 === 1)
Expand Down Expand Up @@ -464,7 +466,7 @@ function isMovable(state: HeadlessState, orig: cg.Key): boolean {

export function canMove(state: HeadlessState, orig: cg.Key, dest: cg.Key): boolean {
return (
(orig !== dest || state.variant === 'togyzkumalak') &&
(orig !== dest || state.variant === 'togyzkumalak' || state.variant === 'bestemshe') &&
isMovable(state, orig) &&
(state.movable.free || !!state.movable.dests?.get(orig)?.includes(dest))
);
Expand Down
4 changes: 2 additions & 2 deletions src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { pos2key, NRanks, invNRanks } from './util';
import * as cg from './types';

export const initial: cg.FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR';
const commaFenVariants: cg.Variant[] = ['oware', 'togyzkumalak', 'backgammon', 'nackgammon'];
const mancalaFenVariants: cg.Variant[] = ['oware', 'togyzkumalak'];
const commaFenVariants: cg.Variant[] = ['oware', 'togyzkumalak', 'bestemshe', 'backgammon', 'nackgammon'];
const mancalaFenVariants: cg.Variant[] = ['oware', 'togyzkumalak', 'bestemshe'];

function roles(letter: string) {
return (letter.replace('+', 'p') + '-piece') as cg.Role;
Expand Down
1 change: 1 addition & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ function appendValue<K, V>(map: Map<K, V[]>, key: K, value: V): void {
function variantSpecificHighlightClass(variant: cg.Variant, k: cg.Key, orientation: cg.Orientation): string {
switch (variant) {
case 'togyzkumalak':
case 'bestemshe':
return k[1] === '1' ? ' p1' : ' p2';
case 'nackgammon':
case 'backgammon':
Expand Down
1 change: 1 addition & 0 deletions src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ function roleToSvgName(variant: cg.Variant, piece: DrawShapePiece): string {
return (piece.playerIndex === 'p1' ? 'b' : 'w') + piece.role[0].toUpperCase();
case 'oware':
case 'togyzkumalak':
case 'bestemshe':
return piece.role[0].split('-')[0].substring(1);
case 'nackgammon':
case 'backgammon':
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type Variant =
| 'amazons'
| 'oware'
| 'togyzkumalak'
| 'bestemshe'
| 'go9x9'
| 'go13x13'
| 'go19x19'
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const posToTranslateRel = (
pos,
orientation,
100,
v === 'togyzkumalak' ? 150 : v === 'backgammon' || v === 'nackgammon' ? 116 : 100,
v === 'togyzkumalak' || v === 'bestemshe' ? 150 : v === 'backgammon' || v === 'nackgammon' ? 116 : 100,
bt,
);

Expand Down
4 changes: 2 additions & 2 deletions src/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function renderWrap(element: HTMLElement, s: HeadlessState, relative: boo
);
container.appendChild(renderCoords(files.slice(0, s.dimensions.width).reverse(), 'files' + ' p2'));
}
} else if (s.variant === 'togyzkumalak') {
} else if (s.variant === 'togyzkumalak' || s.variant === 'bestemshe') {
if (s.orientation === 'p1') {
container.appendChild(renderCoords(ranks.slice(0, s.dimensions.width), 'files' + ' p1'));
container.appendChild(renderCoords(ranks.slice(0, s.dimensions.width), 'files' + ' p2'));
Expand Down Expand Up @@ -154,7 +154,7 @@ export function renderWrap(element: HTMLElement, s: HeadlessState, relative: boo

if (s.boardScores) {
const bd = s.dimensions;
if (s.variant === 'togyzkumalak') {
if (s.variant === 'togyzkumalak' || s.variant === 'bestemshe') {
const boardScores = invNRanks.slice(-bd.height).map(y =>
NRanks.slice(0, bd.width).map(x => {
const piece = s.pieces.get(pos2key([x, y]));
Expand Down

0 comments on commit 5e04a08

Please sign in to comment.