Skip to content

Commit

Permalink
fix uas refactor (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
EllAchE authored Nov 4, 2023
1 parent c431134 commit 16992e2
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 492 deletions.
62 changes: 24 additions & 38 deletions cjsmin/src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const DEFAULT_POSITION =
export type Piece = {
color: Color;
type: PieceType;
unambiguousSymbol: UnambiguousPieceSymbol;
uas: UnambiguousPieceSymbol;
};

type Capture = {
Expand All @@ -172,7 +172,7 @@ export type InternalMove = {
from: number;
to: number;
piece: PieceType;
unambiguousSymbol: UnambiguousPieceSymbol;
uas: UnambiguousPieceSymbol;
capture?: Capture;
promotion?: PieceType;
flags: number;
Expand Down Expand Up @@ -612,7 +612,7 @@ function addMove(
from: number,
to: number,
piece: PieceType,
unambiguousSymbol: UnambiguousPieceSymbol,
uas: UnambiguousPieceSymbol,
capture: Capture | undefined = undefined,
flags: number = BITS.NORMAL
) {
Expand All @@ -628,7 +628,7 @@ function addMove(
piece,
capture,
promotion,
unambiguousSymbol,
uas: uas,
flags: flags | BITS.PROMOTION,
});
}
Expand All @@ -639,7 +639,7 @@ function addMove(
to,
piece,
capture,
unambiguousSymbol,
uas: uas,
flags,
});
}
Expand Down Expand Up @@ -809,10 +809,10 @@ export class Chess {
from: square,
to: this._epSquare,
piece: PAWN,
unambiguousSymbol: this._board[square]?.unambiguousSymbol,
uas: this._board[square]?.uas,
capture: {
type: PAWN,
uas: this._board[square]?.unambiguousSymbol,
uas: this._board[square]?.uas,
},
flags: BITS.EP_CAPTURE,
});
Expand Down Expand Up @@ -867,7 +867,7 @@ export class Chess {
square: Square
) {
//@ts-ignore this breaks for non init
const unambiguousSymbol = SQUARE_TO_STARTING_POSITION_MAP[
const uas = SQUARE_TO_STARTING_POSITION_MAP[
square
] as UnambiguousPieceSymbol;

Expand All @@ -894,7 +894,7 @@ export class Chess {
this._board[sq] = {
type: type as PieceType,
color: color as Color,
unambiguousSymbol,
uas: uas,
};

if (type === KING) {
Expand Down Expand Up @@ -1252,14 +1252,7 @@ export class Chess {
// single square, non-capturing
to = from + PAWN_OFFSETS[us][0];
if (!this._board[to]) {
addMove(
moves,
us,
from,
to,
PAWN,
this._board[from].unambiguousSymbol
);
addMove(moves, us, from, to, PAWN, this._board[from].uas);

// double square
to = from + PAWN_OFFSETS[us][1];
Expand All @@ -1270,7 +1263,7 @@ export class Chess {
from,
to,
PAWN,
this._board[from].unambiguousSymbol,
this._board[from].uas,
undefined,
BITS.BIG_PAWN
);
Expand All @@ -1289,10 +1282,10 @@ export class Chess {
from,
to,
PAWN,
this._board[from].unambiguousSymbol,
this._board[from].uas,
{
type: this._board[to].type,
uas: this._board[to].unambiguousSymbol,
uas: this._board[to].uas,
},
BITS.CAPTURE
);
Expand All @@ -1310,7 +1303,7 @@ export class Chess {
from,
to,
PAWN,
this._board[from].unambiguousSymbol,
this._board[from].uas,
{
type: PAWN,
uas: uas as UnambiguousPieceSymbol,
Expand All @@ -1331,14 +1324,7 @@ export class Chess {
if (to & 0x88) break;

if (!this._board[to]) {
addMove(
moves,
us,
from,
to,
type,
this._board[from].unambiguousSymbol
);
addMove(moves, us, from, to, type, this._board[from].uas);
} else {
// own color, stop loop
if (this._board[to].color === us) break;
Expand All @@ -1349,10 +1335,10 @@ export class Chess {
from,
to,
type,
this._board[from].unambiguousSymbol,
this._board[from].uas,
{
type: this._board[to].type,
uas: this._board[to].unambiguousSymbol,
uas: this._board[to].uas,
},
BITS.CAPTURE
);
Expand Down Expand Up @@ -1392,7 +1378,7 @@ export class Chess {
this._kings[us],
castlingTo,
KING,
this._board[this._kings[us]].unambiguousSymbol,
this._board[this._kings[us]].uas,
undefined,
BITS.KSIDE_CASTLE
);
Expand All @@ -1418,7 +1404,7 @@ export class Chess {
this._kings[us],
castlingTo,
KING,
this._board[this._kings[us]].unambiguousSymbol,
this._board[this._kings[us]].uas,
undefined,
BITS.QSIDE_CASTLE
);
Expand Down Expand Up @@ -1484,7 +1470,7 @@ export class Chess {
this._board[move.to] = {
type: move.promotion,
color: us,
unambiguousSymbol: this._board[move.to].unambiguousSymbol,
uas: this._board[move.to].uas,
};
}

Expand Down Expand Up @@ -1605,15 +1591,15 @@ export class Chess {
this._board[index] = {
type: PAWN,
color: them,
unambiguousSymbol: move.capture.uas,
uas: move.capture.uas,
};
} else {
// regular capture
this._board[move.to] = {
type: move.capture.type,
color: them,
// TODO: Implement unambiguousSymbol
unambiguousSymbol: move.capture.uas,
uas: move.capture.uas,
};
}
}
Expand Down Expand Up @@ -1956,7 +1942,7 @@ export class Chess {
to: toAlgebraic,
flags: prettyFlags,
piece,
umabiguousSymbol: uglyMove.unambiguousSymbol,
umabiguousSymbol: uglyMove.uas,
};

if (capture) {
Expand All @@ -1975,7 +1961,7 @@ export class Chess {
to: move.to,
flags: move.flags,
piece: move.piece,
uas: uglyMove.unambiguousSymbol,
uas: uglyMove.uas,
originalString,
};

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getAverageDistance,
getMoveDistanceSetOfGames,
} from './metrics/distances';
import { getGameWithMostMoves, getPieceLevelMoveInfo } from './metrics/metrics';
import { getGameWithMostMoves, getPieceLevelMoveInfo } from './metrics/moves';
import { getPiecePromotionInfo } from './metrics/promotions';
import { FileReaderGame } from './types';

Expand Down
95 changes: 58 additions & 37 deletions src/metrics/captures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,53 +69,74 @@ export function trackCaptures(boardMap: BoardMap, moves: PrettyMove[]) {
}
}

function uapMap() {
/**
* A utitily function to create an object with unambiguous piece symbols as keys
*/
function createUAPMap<T>(object: T): { [key: string]: T } {
const map = {};
for (const uap of ALL_UNAMBIGUOUS_PIECE_SYMBOLS) {
map[uap] = { killStreaks: 0 };
map[uap] = { ...object };
}
return map;
}

function getMaxKillStreak(
uapMap: any,
moves: PrettyMove[],
startingIndex: 0 | 1 // assume games have at least 2 moves
) {
let i = startingIndex;
let streakLength = 0;
let streakPiece: UnambiguousPieceSymbol;

while (i < moves.length) {
const move = moves[i];
if (move.capture) {
if (streakLength === 0) {
streakPiece = move.uas;
streakLength++;
} else if (streakPiece === move.uas) {
streakLength++;
} else {
uapMap[streakPiece].killStreaks = Math.max(
uapMap[streakPiece].killStreaks,
streakLength
);
streakLength = 0;
export class KillStreakMetric implements Metric {
killStreakMap: {
[key: string]: {
killStreaks: number;
};
};

constructor() {
this.killStreakMap = createUAPMap({ killStreaks: 0 });
}

logResults(): void {}

aggregate() {
return this.killStreakMap;
}

clear(): void {
this.killStreakMap = createUAPMap({ killStreaks: 0 });
}

getMaxKillStreak(
game: { move: PrettyMove; board: Piece[] }[],
startingIndex: 0 | 1 // assume games have at least 2 moves
) {
let i = startingIndex;
let streakLength = 0;
let streakPiece: UnambiguousPieceSymbol;

while (i < game.length) {
const move = game[i].move;
if (move.capture) {
if (streakLength === 0) {
streakPiece = move.uas;
streakLength++;
} else if (streakPiece === move.uas) {
streakLength++;
} else {
this.killStreakMap[streakPiece].killStreaks = Math.max(
this.killStreakMap[streakPiece].killStreaks,
streakLength
);
streakLength = 0;
}
}
i += 2;
}
i += 2;
this.killStreakMap[streakPiece].killStreaks = Math.max(
this.killStreakMap[streakPiece].killStreaks,
streakLength
);
}
uapMap[streakPiece].killStreaks = Math.max(
uapMap[streakPiece].killStreaks,
streakLength
);
}

export function getBWKillStreaks(moves: PrettyMove[]) {
const tracker = uapMap();
getMaxKillStreak(tracker, moves, 0);
getMaxKillStreak(tracker, moves, 1);

return tracker;
processGame(game: { move: PrettyMove; board: Piece[] }[]) {
this.getMaxKillStreak(game, 0);
this.getMaxKillStreak(game, 1);
}
}

export class KDRatioMetric implements Metric {
Expand Down
19 changes: 19 additions & 0 deletions src/metrics/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// calculates how many games in the dataset
export function countGamesInDataset(datasetPath: string): number {
const fs = require('fs');
const path = require('path');

let data = fs.readFileSync(
path.join(__dirname, '../data/10.10.23_test_set'),
'utf8'
);
let games = data.split('\n[Event');
// If the first game doesn't start with a newline, add 1 back to the count
if (data.startsWith('[Event')) {
console.log(`Number of games: ${games.length}`);
return games.length;
} else {
console.log(`Number of games: ${games.length - 1}`); // Subtract 1 because the first split item will be an empty string
return games.length - 1;
}
}
21 changes: 0 additions & 21 deletions src/metrics/metrics.ts → src/metrics/moves.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { Chess } from '../../cjsmin/src/chess';
import { FileReaderGame } from '../types';

// calculates how many games in the dataset
export function countGamesInDataset(datasetPath: string): number {
const fs = require('fs');
const path = require('path');

let data = fs.readFileSync(
path.join(__dirname, '../data/10.10.23_test_set'),
'utf8'
);
let games = data.split('\n[Event');
// If the first game doesn't start with a newline, add 1 back to the count
if (data.startsWith('[Event')) {
console.log(`Number of games: ${games.length}`);
return games.length;
} else {
console.log(`Number of games: ${games.length - 1}`); // Subtract 1 because the first split item will be an empty string
return games.length - 1;
}
}

export async function getGameWithMostMoves(games: FileReaderGame[]) {
console.time('Task 5: getGameWithMostMoves');
let maxNumMoves = 0;
Expand Down
Loading

0 comments on commit 16992e2

Please sign in to comment.