-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
142 lines (125 loc) · 4.11 KB
/
ai.js
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
138
139
140
141
142
function getBestMove() {
if (aiDifficulty === 'easy') {
return getRandomMove();
} else if (aiDifficulty === 'medium') {
return Math.random() < 0.6 ? getBestMoveHard() : getRandomMove();
} else {
// First move random
if (gameBoard.every(cell => cell === '')) {
return getRandomMove();
} else {
return getBestMoveHard();
}
}
}
function getRandomMove() {
const availableMoves = gameBoard.reduce((acc, cell, index) => {
if (cell === '') acc.push(index);
return acc;
}, []);
return availableMoves[Math.floor(Math.random() * availableMoves.length)];
}
function getBestMoveHard() {
let bestScore = -Infinity;
let bestMove;
for (let i = 0; i < gameBoard.length; i++) {
if (gameBoard[i] === '') {
gameBoard[i] = aiSymbol;
let score = minimax(gameBoard, 0, false, -Infinity, Infinity);
gameBoard[i] = '';
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
return bestMove;
}
function minimax(board, depth, isMaximizing, alpha, beta) {
if (depth === aiMaxDepth || checkWinForMinimax(board, depth) !== null) {
return evaluateBoard(board, depth);
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = aiSymbol;
let score = minimax(board, depth + 1, false, alpha, beta);
board[i] = '';
bestScore = Math.max(score, bestScore);
alpha = Math.max(alpha, bestScore);
if (beta <= alpha) break; // Élagage alpha-beta
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = playerSymbol;
let score = minimax(board, depth + 1, true, alpha, beta);
board[i] = '';
bestScore = Math.min(score, bestScore);
beta = Math.min(beta, bestScore);
if (beta <= alpha) break; // Élagage alpha-beta
}
}
return bestScore;
}
}
function checkWinForMinimax(board, depth) {
for (let condition of winConditions) {
if (condition.every(index => board[index] === aiSymbol)) {
return 100 * (aiMaxDepth - depth + 1);
}
if (condition.every(index => board[index] === playerSymbol)) {
return -100 * (aiMaxDepth - depth + 1);
}
}
if (board.every(cell => cell !== '')) {
return 0;
}
return null;
}
// heuristics functions
function evaluateBoard(board, depth) {
const result = checkWinForMinimax(board, depth);
if (result !== null) {
return result;
}
let score = 0;
const lines = getLines(board);
for (const line of lines) {
const aiCount = line.filter(cell => cell === aiSymbol).length;
const playerCount = line.filter(cell => cell === playerSymbol).length;
if (aiCount === 0 && playerCount > 0) {
score -= Math.pow(2, playerCount);
} else if (playerCount === 0 && aiCount > 0) {
score += Math.pow(2, aiCount);
}
}
return score;
}
function getLines(board) {
const size = Math.sqrt(board.length);
const lines = [];
// Lignes horizontales et verticales
for (let i = 0; i < size; i++) {
const row = [];
const col = [];
for (let j = 0; j < size; j++) {
row.push(board[i * size + j]);
col.push(board[j * size + i]);
}
lines.push(row, col);
}
// Diagonales
const diag1 = [];
const diag2 = [];
for (let i = 0; i < size; i++) {
diag1.push(board[i * size + i]);
diag2.push(board[i * size + (size - 1 - i)]);
}
lines.push(diag1, diag2);
return lines;
}