-
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.
Merge pull request #3 from vkarchevskyi/feat/medium-bot
Feat: add medium bot
- Loading branch information
Showing
8 changed files
with
179 additions
and
49 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type Bot from './Bot' | ||
import type { | ||
Board, | ||
CurrentBoardIndex, | ||
MiniMaxMove, | ||
Position, | ||
Sign, | ||
SmallBoard, | ||
} from '@/TicTacToe/types.ts' | ||
import { checkWin } from '@/TicTacToe/GameController.ts' | ||
import { getRandomElement, min, max, getEmptyCellIndexes } from '@/TicTacToe/utils.ts' | ||
|
||
export default class MediumBot implements Bot { | ||
public constructor( | ||
private readonly board: Board, | ||
private readonly winnerBoard: SmallBoard, | ||
) {} | ||
|
||
public getMove(currentBoard: CurrentBoardIndex): Position { | ||
if (currentBoard === null) { | ||
const freeIndexes: number[] = getEmptyCellIndexes(this.winnerBoard) | ||
currentBoard = getRandomElement<number>(freeIndexes) | ||
} | ||
|
||
const index: number | undefined = this.miniMax( | ||
this.board[currentBoard], | ||
'O', | ||
getEmptyCellIndexes(this.board[currentBoard]).length, | ||
).index | ||
|
||
if (index === undefined) { | ||
throw new Error('Index must be present to make a minimax move') | ||
} | ||
|
||
return { | ||
smallBoard: currentBoard, | ||
row: Math.floor(index / 3), | ||
cell: index % 3, | ||
} | ||
} | ||
|
||
/** | ||
* Inspired by: | ||
* @see https://github.com/andersonpereiradossantos/tic-tac-toe-ai-minimax | ||
* */ | ||
private miniMax(board: SmallBoard, player: Sign, depth: number): MiniMaxMove { | ||
const empty = getEmptyCellIndexes(board) | ||
|
||
if (checkWin(this.winnerBoard, 'X')) { | ||
return { score: -1 } | ||
} | ||
if (checkWin(this.winnerBoard, 'O')) { | ||
return { score: 1 } | ||
} | ||
if (empty.length === 0 || depth === 0) { | ||
return { score: 0 } | ||
} | ||
|
||
depth-- | ||
|
||
const movePossibles: Array<{ index: number; score: number }> = [] | ||
|
||
for (let i = 0; i < empty.length; i++) { | ||
const newBoard = JSON.parse(JSON.stringify(board)) | ||
newBoard[Math.floor(empty[i] / 3)][empty[i] % 3] = player | ||
|
||
const result = this.miniMax(newBoard, player === 'O' ? 'X' : 'O', depth) | ||
movePossibles.push({ index: empty[i], score: result.score }) | ||
} | ||
|
||
let bestMove: number | null = null | ||
|
||
if (player === 'X') { | ||
let bestScore = -Infinity | ||
for (let i = 0; i < movePossibles.length; i++) { | ||
bestScore = max(bestScore, movePossibles[i].score) | ||
if (movePossibles[i].score === bestScore) { | ||
bestMove = i | ||
} | ||
} | ||
} else { | ||
let bestScore = Infinity | ||
for (let i = 0; i < movePossibles.length; i++) { | ||
bestScore = min(bestScore, movePossibles[i].score) | ||
if (movePossibles[i].score === bestScore) { | ||
bestMove = i | ||
} | ||
} | ||
} | ||
|
||
if (bestMove === null) { | ||
throw new Error('Best move not found') | ||
} | ||
|
||
return movePossibles[bestMove] | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { CurrentBoardIndex, Sign, SmallBoard } from '@/TicTacToe/types.ts' | ||
import { boardRowQuantity } from '@/TicTacToe/GameController.ts' | ||
|
||
export const getRandomElement = <T>(array: Array<T>): T => { | ||
return array[(array.length * Math.random()) | 0] | ||
} | ||
|
||
export const min = (a: number, b: number): number => { | ||
return a < b ? a : b | ||
} | ||
|
||
export const max = (a: number, b: number): number => { | ||
return a > b ? a : b | ||
} | ||
|
||
export const getEmptyCellIndexes = (board: SmallBoard): number[] => { | ||
const emptyCellIndexes: number[] = [] | ||
|
||
board.forEach((row: Sign[], index: number): void => { | ||
emptyCellIndexes.push( | ||
...row | ||
.map( | ||
(sign: Sign, rowIndex: number): CurrentBoardIndex => | ||
sign === '' ? rowIndex + boardRowQuantity * index : null, | ||
) | ||
.filter((index: CurrentBoardIndex) => index !== null), | ||
) | ||
}) | ||
|
||
return emptyCellIndexes | ||
} |
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