Skip to content

Commit

Permalink
feat(frontend): implement automated play
Browse files Browse the repository at this point in the history
  • Loading branch information
DecentM committed Jan 12, 2024
1 parent a702809 commit c2303e2
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 137 deletions.
10 changes: 4 additions & 6 deletions apps/frontend/src/lib/bot-worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import * as Sentry from '@sentry/vue'
import {
findBestMove,
getBoardScore,
Openings,
} from '@decentm/allegiance-chess-bot'
import { Bot, getBoardScore, Openings } from '@decentm/allegiance-chess-bot'
import {
AfenPreset,
Board,
Expand Down Expand Up @@ -94,6 +90,8 @@ if (sentryEnabled) {
})
}

const bot = new Bot()

onmessage = async (messageEvent: MessageEvent<BotWorkerMessage>) => {
try {
const message = messageEvent.data
Expand Down Expand Up @@ -134,7 +132,7 @@ onmessage = async (messageEvent: MessageEvent<BotWorkerMessage>) => {
}

case 'bot-move': {
const botResult = findBestMove(board, 3)
const botResult = bot.findBestMove(3)
const move = board.executeMoveIndex(botResult.index)

const response: BotWorkerResponse = {
Expand Down
55 changes: 55 additions & 0 deletions packages/bot/clinic/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AfenPreset, Board } from '@decentm/allegiance-chess-core'
import { randomBytes } from 'node:crypto'

import { Bot } from '../src/engine'

const b = new Board()

b.importAFEN(AfenPreset.VanillaDefault)

const _seed = randomBytes(8).toString('hex')

console.log('Running performance benchmark...')
const start = performance.now()

let MOVES = 4

const history: number[] = []

const bot = new Bot()

// eslint-disable-next-line no-constant-condition
while (MOVES > 0) {
const moveStart = performance.now()

const { index, score, seed } = bot.findBestMove(3, _seed)

Check warning on line 25 in packages/bot/clinic/benchmark.ts

View workflow job for this annotation

GitHub Actions / lint

'score' is assigned a value but never used

Check warning on line 25 in packages/bot/clinic/benchmark.ts

View workflow job for this annotation

GitHub Actions / lint

'seed' is assigned a value but never used

if (index === -1) {
console.log('No moves!')
break
}

const move = b.executeMoveIndex(index)

Check warning on line 32 in packages/bot/clinic/benchmark.ts

View workflow job for this annotation

GitHub Actions / lint

'move' is assigned a value but never used

history.push(performance.now() - moveStart)
console.log('Move', MOVES, 'complete in', history.at(-1))
MOVES--
// const openings = Openings.getNamesByFen(b.toAFEN({ sections: ['positions'] }))

// console.clear()
// console.log(seed, Notation.writeNode(move), score, '\n')
// console.log(b.toAFEN(), '\n')
// console.log(
// 'openings:',
// openings.length < 10 ? openings.join(', ') : openings.length,
// '\n'
// )
// console.log(b.dump(), '\n')
// console.log(b.getMoveHistory())
}

console.log('Benchmark done in', performance.now() - start)
console.log(
'Average move time:',
history.reduce((prev, cur) => prev + cur, 0) / history.length
)
16 changes: 9 additions & 7 deletions packages/bot/clinic/scenario-1.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Board, Notation } from '@decentm/allegiance-chess-core'
import { findBestMove } from '../src/engine'
import { Notation } from '@decentm/allegiance-chess-core'
import { Bot } from '../src/engine'

const b = new Board()
const bot = new Bot()

b.importAFEN('rnbqkbnr/ppp1pppp/8/3p4/8/2N5/PPPPPPPP/R1BQKBNR w KQkq d6 0 1')
b.importMoveHistory('1. b1c3 d7d5')
bot.board.importAFEN(
'rnbqkbnr/ppp1pppp/8/3p4/8/2N5/PPPPPPPP/R1BQKBNR w KQkq d6 0 1'
)
bot.board.importMoveHistory('1. b1c3 d7d5')

const moves = b.getValidMoves()
const moves = bot.board.getValidMoves()

console.log('Searching', moves.length, 'moves')

const { index, score } = findBestMove(b, 3)
const { index, score } = bot.findBestMove(3)

console.log(
'Best move is move',
Expand Down
7 changes: 3 additions & 4 deletions packages/bot/clinic/self-play.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AfenPreset, Board, Notation } from '@decentm/allegiance-chess-core'
import { randomBytes } from 'node:crypto'

import { TranspositionTable, findBestMove } from '../src/engine'
import { Bot } from '../src/engine'
import * as Openings from '../src/engine/openings'

const b = new Board()
Expand All @@ -10,7 +10,7 @@ b.importAFEN(AfenPreset.VanillaDefault)

const _seed = randomBytes(8).toString('hex')

const transpositionTable: TranspositionTable = new Map()
const bot = new Bot()

// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -25,7 +25,7 @@ while (true) {
}
}

const { index, score, seed } = findBestMove(b, 3, transpositionTable, _seed)
const { index, score, seed } = bot.findBestMove(3, _seed)

if (index === -1) {
console.log('No moves!')
Expand All @@ -43,7 +43,6 @@ while (true) {
openings.length < 10 ? openings.join(', ') : openings.length,
'\n'
)
console.log('TT size:', transpositionTable.size, '\n')
console.log(b.dump(), '\n')
console.log(b.getMoveHistory())
}
17 changes: 17 additions & 0 deletions packages/bot/src/engine/competency.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'ava'

import { Bot } from '.'

test('saves the knight', (t) => {
t.timeout(Number.POSITIVE_INFINITY)

const bot = new Bot()

bot.board.importAFEN(
'rnbqkbnr/ppppp3/8/5p>p1/6N1/7p/PPPPPP1P/RNBQKR>R1 w Qkq - 1 8'
)

const result = bot.findBestMove(3)

t.true(result.path?.split(',')[0].startsWith('Ng4'))
})
5 changes: 0 additions & 5 deletions packages/bot/src/engine/index.spec.ts

This file was deleted.

Loading

0 comments on commit c2303e2

Please sign in to comment.