Skip to content

Commit

Permalink
ai: restore old version to see if its better. it's not
Browse files Browse the repository at this point in the history
  • Loading branch information
TeemuKoivisto committed Aug 9, 2023
1 parent d97917f commit 1ed34ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
29 changes: 18 additions & 11 deletions packages/ai/src/ai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { Board } from './board'
import { Board } from './board2'
import { getCellValue } from './cell'
import { Board } from './board'
// import { Board } from './board2'
import { Cell, getCellValue } from './cell'

interface Options {
maxDepth: number
Expand All @@ -13,7 +13,7 @@ let iterations = 0
const cachedComputations = new Map<string, number>()

export function computeAi(b: Board, aiNumber: number, searchDepth: number) {
let chosenCell: number | undefined
let chosenCell: Cell | undefined
let bestValue = Number.NEGATIVE_INFINITY
const opts = {
maxDepth: 10,
Expand All @@ -29,7 +29,7 @@ export function computeAi(b: Board, aiNumber: number, searchDepth: number) {
value = cached
} else {
value = minimax(c, b, searchDepth, false, -Infinity, Infinity, aiNumber, opts)
cachedComputations.set(b.code, value)
// cachedComputations.set(b.code, value)
b.set_cell_owner(c, 0)
}
if (value > bestValue) {
Expand All @@ -46,14 +46,16 @@ export function computeAi(b: Board, aiNumber: number, searchDepth: number) {
6
)} per iteration`
)
const aiMove = getCellValue(chosenCell)
// const aiMove = getCellValue(chosenCell)
const aiMove = chosenCell
console.log(`best: ${aiMove.x} ${aiMove.x} ${bestValue} at iterations ${iterations} \n`)
b.set_cell_owner(chosenCell, aiNumber)
return b.update_cell_adjancies(chosenCell, aiNumber)
}

export function minimax(
cell: number,
// cell: number,
cell: Cell,
board: Board,
depth: number,
isMaximizing: boolean,
Expand All @@ -65,18 +67,24 @@ export function minimax(
iterations += 1
board.set_cell_owner(cell, player)
const cached = cachedComputations.get(board.code)
if (cached !== undefined) {
if (cached) {
return cached
}
const won = board.update_cell_adjancies(cell, player)
let value: number
let value = NaN
if (won) {
value = opts.humanPlayer === player ? -1000 - depth : 1000 + depth
} else if (board.is_full()) {
value = opts.humanPlayer === player ? -100 - depth : 100 + depth
} else if (depth === 0) {
value = 0
} else if (isMaximizing) {
}
if (!isNaN(value)) {
cachedComputations.set(board.code, value)
return value
}

if (isMaximizing) {
value = Number.NEGATIVE_INFINITY
board.get_available_moves().some(c => {
value = Math.max(
Expand All @@ -99,6 +107,5 @@ export function minimax(
return beta <= alpha
})
}
cachedComputations.set(board.code, value)
return value
}
10 changes: 5 additions & 5 deletions packages/ai/src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class Board {
return this.cells[x + y * this.size]
}

set_cell_owner(x: number, y: number, player: number) {
const idx = x + y * this.size
set_cell_owner(c: Cell, player: number) {
const idx = c.x + c.y * this.size
this.cells[idx].owner = player
if (player !== 0) {
this.available -= 1
Expand Down Expand Up @@ -171,16 +171,16 @@ export class Board {
return adjacent
}

update_cell_adjancies(x: number, y: number, player: number) {
update_cell_adjancies(cell: Cell, player: number) {
let bestInRow = 0
for (let i = 0; i < 4; i += 1) {
const dir = i as Adjacency
const cells = player !== 0 ? this.get_adjacent_cells(x, y, player, dir) : []
const cells = player !== 0 ? this.get_adjacent_cells(cell.x, cell.y, player, dir) : []
const adjacent_count = player === 0 ? 0 : cells.length + 1
for (const c of cells) {
this.cells[c.x + c.y * this.size].adjacency[dir] = adjacent_count
}
this.cells[x + y * this.size].adjacency[dir] = adjacent_count
this.cells[cell.x + cell.y * this.size].adjacency[dir] = adjacent_count
if (adjacent_count > bestInRow) {
bestInRow = adjacent_count
}
Expand Down
33 changes: 27 additions & 6 deletions packages/ai/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get, writable } from 'svelte/store'
// import { Board } from './board'
import { Board } from './board2'
import { Board } from './board'
// import { Board } from './board2'
import { computeAi } from './ai'

export interface PlayOptions {
Expand Down Expand Up @@ -29,17 +29,38 @@ export const gameActions = {
this.evaluateAiMove()
}
},
// playerSelectCell(x: number, y: number) {
// const b = get(board)
// const c = b.get_cell_at(x, y)
// const cell = b.get_cell_value_at(x, y)
// if (cell.owner !== 0) {
// return
// }
// const playerNumber = get(player) === 'x' ? 1 : 2
// let ended = true
// b.set_cell_owner(c, playerNumber)
// if (b.update_cell_adjancies(c, playerNumber)) {
// gameStatus.set(playerNumber === 1 ? 'x-won' : 'o-won')
// } else if (b.is_full()) {
// gameStatus.set('tie')
// } else {
// ended = false
// }
// board.set(b)
// if (!ended) {
// this.evaluateAiMove()
// }
// },
playerSelectCell(x: number, y: number) {
const b = get(board)
const c = b.get_cell_at(x, y)
const cell = b.get_cell_value_at(x, y)
const cell = b.get_cell_at(x, y)
if (cell.owner !== 0) {
return
}
const playerNumber = get(player) === 'x' ? 1 : 2
let ended = true
b.set_cell_owner(c, playerNumber)
if (b.update_cell_adjancies(c, playerNumber)) {
b.set_cell_owner(cell, playerNumber)
if (b.update_cell_adjancies(cell, playerNumber)) {
gameStatus.set(playerNumber === 1 ? 'x-won' : 'o-won')
} else if (b.is_full()) {
gameStatus.set('tie')
Expand Down

0 comments on commit 1ed34ed

Please sign in to comment.