Skip to content

Commit

Permalink
Messing around with the move parser
Browse files Browse the repository at this point in the history
  • Loading branch information
neelkapse committed May 20, 2023
1 parent 508bf12 commit 9a6b063
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 65 deletions.
2 changes: 1 addition & 1 deletion js/hive-db/src/db/playGameMoves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export function playGameMove(
): Promise<GameMoveResponse> {
const uri = `/api/game/${game.gid}/play`;
// TODO: Neel: need to play around with this more and figure out any conversion
return postJSON(uri, { Turn: ['.', '${move.notation}'] }, authToken);
return postJSON(uri, { Turn: [move.notation, '.'] }, authToken);
}
2 changes: 1 addition & 1 deletion js/hive-db/src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function newGameFromBackendGame(backendGame: BackendGame): Game {
options,
players,
meta,
state: newGameState()
state
};
}

Expand Down
1 change: 1 addition & 0 deletions js/hive-db/src/game/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface GameState {
export const newGameState = (notation?: string): GameState => {
if (!notation) notation = '';
const moves = getGameMoves(notation);
console.log(moves);
return {
notation: notation,
turn: moves.length % 2 === 0 ? 'w' : 'b',
Expand Down
2 changes: 2 additions & 0 deletions js/hive-lib/src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export function buildBoard(moves: Move[], upTo?: number): GameBoard {
// Extract the data we need from the move object
const { tileId, refId, dir } = move;

console.log(draft);

if (isEmpty(draft)) {
// The first tile placed on the board is always at (0, 0)
_placeTile(draft, tileId, { q: 0, r: 0 });
Expand Down
118 changes: 59 additions & 59 deletions js/hive-lib/src/notation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,65 +87,65 @@ describe('notation parsing', () => {
}));
});

describe('_parseTurnNotation', () => {
test('turn 3: white pass, black has not moved', () =>
expect(_parseTurnNotation('3. x')).toEqual({
notation: '3. x',
index: 3,
white: {
notation: 'x',
tileId: 'x',
refId: 'x',
dir: -1
}
}));
test('turn 150: white ant 2 to top right of black queen, game end', () =>
expect(_parseTurnNotation('150. wA2 bQ/#')).toEqual({
notation: '150. wA2 bQ/#',
index: 150,
white: {
notation: 'wA2 bQ/#',
tileId: 'wA2',
refId: 'bQ',
dir: 1,
end: true
}
}));
test('turn 10: white mosquito on top of black beetle 2, black pass', () =>
expect(_parseTurnNotation('10. wM bB2, x')).toEqual({
notation: '10. wM bB2, x',
index: 10,
white: {
notation: 'wM bB2',
tileId: 'wM',
refId: 'bB2',
dir: 0
},
black: {
notation: 'x',
tileId: 'x',
refId: 'x',
dir: -1
}
}));
test('turn 73: white grasshopper 1 to bottom right of black queen, black ladybug to left of white pillbug', () =>
expect(_parseTurnNotation('73. wG1 bQ\\, bL -wP')).toEqual({
notation: '73. wG1 bQ\\, bL -wP',
index: 73,
white: {
notation: 'wG1 bQ\\',
tileId: 'wG1',
refId: 'bQ',
dir: 3
},
black: {
notation: 'bL -wP',
tileId: 'bL',
refId: 'wP',
dir: 5
}
}));
});
// describe('_parseTurnNotation', () => {
// test('turn 3: white pass, black has not moved', () =>
// expect(_parseTurnNotation('3. x')).toEqual({
// notation: '3. x',
// index: 3,
// white: {
// notation: 'x',
// tileId: 'x',
// refId: 'x',
// dir: -1
// }
// }));
// test('turn 150: white ant 2 to top right of black queen, game end', () =>
// expect(_parseTurnNotation('150. wA2 bQ/#')).toEqual({
// notation: '150. wA2 bQ/#',
// index: 150,
// white: {
// notation: 'wA2 bQ/#',
// tileId: 'wA2',
// refId: 'bQ',
// dir: 1,
// end: true
// }
// }));
// test('turn 10: white mosquito on top of black beetle 2, black pass', () =>
// expect(_parseTurnNotation('10. wM bB2, x')).toEqual({
// notation: '10. wM bB2, x',
// index: 10,
// white: {
// notation: 'wM bB2',
// tileId: 'wM',
// refId: 'bB2',
// dir: 0
// },
// black: {
// notation: 'x',
// tileId: 'x',
// refId: 'x',
// dir: -1
// }
// }));
// test('turn 73: white grasshopper 1 to bottom right of black queen, black ladybug to left of white pillbug', () =>
// expect(_parseTurnNotation('73. wG1 bQ\\, bL -wP')).toEqual({
// notation: '73. wG1 bQ\\, bL -wP',
// index: 73,
// white: {
// notation: 'wG1 bQ\\',
// tileId: 'wG1',
// refId: 'bQ',
// dir: 3
// },
// black: {
// notation: 'bL -wP',
// tileId: 'bL',
// refId: 'wP',
// dir: 5
// }
// }));
// });

describe('_parseGameNotation', () => {
test('a game with two completed turns (game0)', () =>
Expand Down
30 changes: 26 additions & 4 deletions js/hive-lib/src/notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export function _buildTurnNotation(
* @return An array of *Turn* objects.
*/
export function _parseGameNotation(notation: string): Turn[] {
return notation.split(/\s(?=\d+\.)/g).map(_parseTurnNotation);
return notation
.split(';')
.map((turnNotation) => _parseTurnNotation(turnNotation));
}

/**
Expand All @@ -135,11 +137,20 @@ export function _parseGameNotation(notation: string): Turn[] {
* @param notation A turn notation string.
* @return A *Turn* object.
*/
// export function _parseTurnNotation(notation: string, index: number): Turn {
// const moves = notation.split(' ');
// return {
// notation,
// index: index + 1,
// white: _parseMoveNotation(moves[0]),
// black: _parseMoveNotation(moves[1])
// };
// }
export function _parseTurnNotation(notation: string): Turn {
const sepLocation = notation.indexOf('.');
const indexString = notation.slice(0, sepLocation);
const placementsString = notation.slice(sepLocation + 1);
const placements = placementsString.split(',');
const placements = placementsString.split(' ');
return {
notation,
index: parseInt(indexString),
Expand All @@ -159,7 +170,10 @@ export function _parseMoveNotation(notation?: string): Move | undefined {

// Split notation into moving tile and reference tile portions
notation = notation.trim();
const [tileId, refNotation] = notation.split(/\s/g);
let [tileId, refNotation] = notation.split(/\s/g);
console.log(notation);
console.log(tileId);
console.log(refNotation);

// Check for and return a passing move
if (tileId === 'x')
Expand All @@ -171,11 +185,19 @@ export function _parseMoveNotation(notation?: string): Move | undefined {
};

// Parse the reference notation to get the reference tile and direction
const { refId, dir, end } =
let { refId, dir, end } =
refNotation !== undefined
? _parseReferenceNotation(refNotation) // only when there is one
: { refId: tileId, dir: 0, end: false }; // first move notation

// // TODO: is this the right way to do this?
// // handle first move notation
// if (tileId === '.') {
// refId = tileId;
// dir = 0;
// end = false;
// }

// Return a playing move
return {
notation,
Expand Down

0 comments on commit 9a6b063

Please sign in to comment.