-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
import { Chess } from '../../cjsmin/src/chess'; | ||
import { FileReaderGame } from '../types'; | ||
|
||
export async function getPiecePromotionInfo(games: FileReaderGame[]) { | ||
console.time('Task 7: getPiecePromotionInfo'); | ||
let ambigPiecePromotedToMap = {}; | ||
let promotingPieceMap = {}; | ||
|
||
for (const game of games) { | ||
const chess = new Chess(); | ||
chess.loadPgn(game.moves); | ||
const chessHistory = chess.history(); | ||
import { | ||
Pawns, | ||
Piece, | ||
PrettyMove, | ||
PromotablePiece, | ||
} from '../../cjsmin/src/chess'; | ||
import { Metric } from './metric'; | ||
|
||
export class PromotionMetric implements Metric { | ||
//uas pawns mapped to count of promotions to each piece type | ||
//prettier-ignore | ||
promotionMap: { | ||
[key in Pawns]: { | ||
[key in PromotablePiece]: number; | ||
} | ||
}; | ||
|
||
for (const moveInfo of chessHistory) { | ||
if (moveInfo.originalString.includes('=')) { | ||
// REGEX to only capture the piece type | ||
const piecePromotedTo = moveInfo.originalString | ||
.split('=')[1] | ||
.match(/[a-zA-Z]/)[0]; | ||
constructor() { | ||
this.clear(); | ||
} | ||
|
||
const promotingPiece = moveInfo.uas; | ||
clear(): void { | ||
let promotionMap = {}; | ||
//prettier-ignore | ||
for (const pawn of ["pa", "pb", "pc", "pd", "pe", "pf", "pg", "ph", 'PA', 'PB', 'PC', 'PD', 'PE', 'PF', 'PG', 'PH']) { | ||
promotionMap[pawn] = { q: 0, r: 0, b: 0, n: 0 }; | ||
} | ||
this.promotionMap = promotionMap as any; | ||
} | ||
|
||
processGame(game: { move: PrettyMove; board: Piece[] }[], gameLink?: string) { | ||
for (const { move } of game) { | ||
// TODO: we can use flags instead of includes('=) | ||
if (move.originalString.includes('=')) { | ||
// update ambigPiecePromotedToMap | ||
if (!ambigPiecePromotedToMap[piecePromotedTo]) { | ||
ambigPiecePromotedToMap[piecePromotedTo] = 0; | ||
} | ||
ambigPiecePromotedToMap[piecePromotedTo]++; | ||
|
||
// update promotingPieceMap | ||
if (!promotingPieceMap[promotingPiece]) { | ||
promotingPieceMap[promotingPiece] = 0; | ||
} | ||
promotingPieceMap[promotingPiece]++; | ||
this.promotionMap[move.uas][move.promotion]++; | ||
} | ||
} | ||
} | ||
|
||
// promotions facts | ||
console.log('PROMOTIONS FACTS:'); | ||
console.log( | ||
'How often a piece is promoted to different ambiguous piece types:' | ||
), | ||
console.table(ambigPiecePromotedToMap); | ||
console.log('How often unambiguous piece is promoted:'), | ||
console.table(promotingPieceMap); | ||
console.log('=============================================================='); | ||
console.log('\n'); | ||
|
||
console.timeEnd('Task 7: getPiecePromotionInfo'); | ||
return { | ||
ambigPiecePromotedToMap, | ||
promotingPieceMap, | ||
}; | ||
aggregate() {} | ||
|
||
logResults(): void { | ||
// promotions facts | ||
console.log('PROMOTIONS FACTS:'); | ||
console.log( | ||
'How often a piece is promoted to different ambiguous piece types:' | ||
), | ||
console.table(this.promotionMap); | ||
console.log('How often unambiguous piece is promoted:'), | ||
console.table(this.promotionMap); | ||
console.log( | ||
'==============================================================' | ||
); | ||
console.log('\n'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters