https://github.com/nemeslaszlo/tictactoe_ai_minimax
Simple Tic Tac Toe game on 3x3 table with a Minimax algorithm implementation. Using Vanilla Javascript and p5.js for visualization and practise.
https://github.com/nemeslaszlo/tictactoe_ai_minimax
minimax-algorithm tic-tac-toe visualization
Last synced: over 1 year ago
JSON representation
Simple Tic Tac Toe game on 3x3 table with a Minimax algorithm implementation. Using Vanilla Javascript and p5.js for visualization and practise.
- Host: GitHub
- URL: https://github.com/nemeslaszlo/tictactoe_ai_minimax
- Owner: NemesLaszlo
- Created: 2020-01-11T09:35:55.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-11T09:42:58.000Z (over 6 years ago)
- Last Synced: 2025-01-29T08:44:08.388Z (over 1 year ago)
- Topics: minimax-algorithm, tic-tac-toe, visualization
- Language: JavaScript
- Homepage: https://nemeslaszlo.github.io/TicTacToe_AI_Minimax/
- Size: 582 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TicTacToe_AI_Minimax
Simple Tic Tac Toe game on 3x3 table with a Minimax algorithm implementation. Using Vanilla Javascript and p5.js for visualization and practise.
#### Source links and others:
https://en.wikipedia.org/wiki/Minimax
https://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html
##### Minimax function:
```javascript
function minimax(board, depth, isMaximizing) {
let result = checkWinner();
if (result !== null) {
return scores[result];
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '') {
board[i][j] = ai;
let score = minimax(board, depth + 1, false);
board[i][j] = '';
bestScore = max(score, bestScore);
}
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '') {
board[i][j] = human;
let score = minimax(board, depth + 1, true);
board[i][j] = '';
bestScore = min(score, bestScore);
}
}
}
return bestScore;
}
}
```