-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_max.js
More file actions
137 lines (124 loc) · 4.24 KB
/
min_max.js
File metadata and controls
137 lines (124 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Define the Tic-Tac-Toe board as a 3x3 array
let board = [
['', '', ''],
['', '', ''],
['', '', '']
];
// Define the players
const HUMAN = 'X';
const AI = 'O';
// Function to check if the game is over
function isGameOver(board) {
// Check rows, columns, and diagonals for a win
for (let i = 0; i < 3; i++) {
if (board[i][0] !== '' && board[i][0] === board[i][1] && board[i][1] === board[i][2]) {
return true; // Row win
}
if (board[0][i] !== '' && board[0][i] === board[1][i] && board[1][i] === board[2][i]) {
return true; // Column win
}
}
if (board[0][0] !== '' && board[0][0] === board[1][1] && board[1][1] === board[2][2]) {
return true; // Diagonal win
}
if (board[0][2] !== '' && board[0][2] === board[1][1] && board[1][1] === board[2][0]) {
return true; // Diagonal win
}
// Check for a tie (no empty spaces left)
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '') {
return false; // Game is not over
}
}
}
return true; // Tie
}
// Function to evaluate the board
function evaluate(board) {
// Check rows, columns, and diagonals for a win
for (let i = 0; i < 3; i++) {
if (board[i][0] === board[i][1] && board[i][1] === board[i][2]) {
if (board[i][0] === AI) return +10; // AI wins
else if (board[i][0] === HUMAN) return -10; // Human wins
}
if (board[0][i] === board[1][i] && board[1][i] === board[2][i]) {
if (board[0][i] === AI) return +10; // AI wins
else if (board[0][i] === HUMAN) return -10; // Human wins
}
}
if (board[0][0] === board[1][1] && board[1][1] === board[2][2]) {
if (board[0][0] === AI) return +10; // AI wins
else if (board[0][0] === HUMAN) return -10; // Human wins
}
if (board[0][2] === board[1][1] && board[1][1] === board[2][0]) {
if (board[0][2] === AI) return +10; // AI wins
else if (board[0][2] === HUMAN) return -10; // Human wins
}
return 0; // No winner yet
}
// Minimax algorithm implementation
function minimax(board, depth, isMaximizing) {
let score = evaluate(board);
// If the game is over, return the score
if (score === 10 || score === -10) {
return score;
}
// If it's a tie, return 0
if (isGameOver(board)) {
return 0;
}
// If it's the AI's turn (maximizing player)
if (isMaximizing) {
let best = -Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '') {
board[i][j] = AI;
best = Math.max(best, minimax(board, depth + 1, !isMaximizing));
board[i][j] = ''; // Undo the move
}
}
}
return best;
}
// If it's the human's turn (minimizing player)
else {
let best = Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '') {
board[i][j] = HUMAN;
best = Math.min(best, minimax(board, depth + 1, !isMaximizing));
board[i][j] = ''; // Undo the move
}
}
}
return best;
}
}
// Function to find the best move for the AI
function findBestMove(board) {
let bestVal = -Infinity;
let bestMove = { row: -1, col: -1 };
// Traverse all cells and evaluate minimax for each empty cell
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '') {
board[i][j] = AI;
let moveVal = minimax(board, 0, false);
board[i][j] = ''; // Undo the move
// If the value of the current move is better than the best value, update best
if (moveVal > bestVal) {
bestMove.row = i;
bestMove.col = j;
bestVal = moveVal;
}
}
}
}
return bestMove;
}
// Example usage
let bestMove = findBestMove(board);
console.log(`Best move for AI: Row = ${bestMove.row}, Col = ${bestMove.col}`);