ia fonctionnelle mais random

test
Leo TUAILLON 2 years ago
parent 0e48bce381
commit e550c0160c

@ -92,7 +92,7 @@ class Matrix:
#Class : AI #Class : AI
class AI: class AI:
def __init__(self, level= 5, player=2): def __init__(self, level= 1, player=2):
self.level = level self.level = level
self.player = player self.player = player

@ -1,136 +1,162 @@
const cells = document.querySelectorAll('.cell'); class Matrix {
const resetButton = document.querySelector('#reset'); constructor() {
const aiButton = document.querySelector('#toggle-ai'); this.cases = Array(3).fill(null).map(() => Array(3).fill(0));
this.marked_case = 0;
let board = Array.from({ length: 9 }, () => 0);
let currentPlayer = 1;
let aiEnabled = true;
let gameMode = 'ai';
let running = true;
function handleClick(e) {
const cell = e.target;
const index = cells.findIndex(c => c === cell);
if (board[index] !== 0 || !running) {
return;
} }
makeMove(index); is_full() {
return this.marked_case === 9;
if (checkWinner(board) !== 0 || board.every(cell => cell !== 0)) {
running = false;
} }
if (running && gameMode === 'ai' && currentPlayer === 2) { mark(row, col, player) {
const { move } = minimax(board, false); if (this.cases[row][col] === 0) {
makeMove(move); this.cases[row][col] = player;
this.marked_case++;
if (checkWinner(board) !== 0 || board.every(cell => cell !== 0)) { return true;
running = false;
}
} }
return false;
} }
function makeMove(index) { check_win(player) {
board[index] = currentPlayer; const win_combinations = [
cells[index].textContent = currentPlayer === 1 ? 'X' : 'O'; [[0, 0], [0, 1], [0, 2]],
currentPlayer = 3 - currentPlayer; [[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[0, 2], [1, 2], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]],
];
return win_combinations.some(combination =>
combination.every(([row, col]) => this.cases[row][col] === player)
);
}
} }
function isRunning() { class AI {
return !checkWinner(board) && board.some(cell => cell === 0); constructor(level = 5, player = 2) {
this.level = level;
this.player = player;
} }
function checkWinner(tempBoard) { random_move(matrix) {
const winningCombinations = [ const available_moves = [];
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (const combination of winningCombinations) { matrix.cases.forEach((row, i) => {
const [a, b, c] = combination; row.forEach((cell, j) => {
if (tempBoard[a] !== 0 && tempBoard[a] === tempBoard[b] && tempBoard[a] === tempBoard[c]) { if (cell === 0) {
return tempBoard[a]; available_moves.push([i, j]);
}
} }
});
});
return 0; if (available_moves.length === 0) {
return null;
} }
function checkGameOver() { const random_index = Math.floor(Math.random() * available_moves.length);
const winner = checkWinner(board); return available_moves[random_index];
if (winner !== 0) {
alert(`Le joueur ${winner} a gagné !`);
} else if (!isRunning()) {
alert('Match nul !');
}
} }
function resetBoard() {
board = Array.from({ length: 9 }, () => 0);
currentPlayer = 1; move(matrix) {
running = true; return this.random_move(matrix);
cells.forEach(cell => (cell.textContent = '')); }
} }
function toggleAi() { class Game {
aiEnabled = !aiEnabled; constructor() {
gameMode = aiEnabled ? 'ai' : '1v1'; this.matrix = new Matrix();
this.ai = new AI();
this.player = 1;
this.gamemode = 'ai';
this.running = true;
this.ai_starts = false;
} }
function minimax(tempBoard, maximizing) { updateBoard() {
const winner = checkWinner(tempBoard); const cells = document.querySelectorAll('.cell');
if (winner !== 0) { this.matrix.cases.forEach((row, rowIndex) => {
return { eval: winner === 1 ? 1 : -1, move: null }; row.forEach((cell, cellIndex) => {
} else if (!tempBoard.some(cell => cell === 0)) { const cellElement = cells[rowIndex * 3 + cellIndex];
return { eval: 0, move: null }; cellElement.classList.remove('x', 'o');
if (cell === 1) {
cellElement.classList.add('x');
} else if (cell === 2) {
cellElement.classList.add('o');
}
});
});
} }
let bestEval, bestMove;
if (maximizing) {
bestEval = -Infinity;
for (let i = 0; i < tempBoard.length; i++) {
if (tempBoard[i] === 0) {
const newBoard = [...tempBoard];
newBoard[i] = 1;
const eval = minimax(newBoard, false).eval; handleCellClick(row, col) {
if (eval > bestEval) { if (this.running && this.matrix.mark(row, col, this.player)) {
bestEval = eval; if (this.matrix.check_win(this.player)) {
bestMove = i; alert(`Le joueur ${this.player} a gagné !`);
this.running = false;
} else if (this.matrix.is_full()) {
alert("Match nul !");
this.running = false;
} else {
this.player = 3 - this.player;
if (this.gamemode === 'ai' && this.player === this.ai.player) {
const [ai_row, ai_col] = this.ai.move(this.matrix);
if (ai_row !== null && ai_col !== null) {
this.matrix.mark(ai_row, ai_col, this.ai.player);
if (this.matrix.check_win(this.ai.player)) {
alert(`L'IA a gagné !`);
this.running = false;
} else if (this.matrix.is_full()) {
alert("Match nul !");
this.running = false;
} }
} }
this.player = 3 - this.player;
} }
} else {
bestEval = Infinity;
for (let i = 0; i < tempBoard.length; i++) {
if (tempBoard[i] === 0) {
const newBoard = [...tempBoard];
newBoard[i] = 2;
const eval = minimax(newBoard, true).eval;
if (eval < bestEval) {
bestEval = eval;
bestMove = i;
} }
this.updateBoard();
} }
} }
setupEventListeners() {
const cells = document.querySelectorAll('[data-cell]');
cells.forEach((cell, index) => {
const row = Math.floor(index / 3);
const col = index % 3;
cell.addEventListener('click', () => this.handleCellClick(row, col));
});
} }
return { eval: bestEval, move: bestMove };
start() {
this.running = true;
this.matrix = new Matrix();
if (this.gamemode === 'ai' && this.ai_starts) {
const [ai_row, ai_col] = this.ai.move(this.matrix);
this.matrix.mark(ai_row, ai_col, this.ai.player);
this.player = 3 - this.player;
} }
this.updateBoard();
this.setupEventListeners();
}
}
const game = new Game();
game.start();
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', () => game.start());
cells.forEach(cell => cell.addEventListener('click', handleClick)); const aiButton = document.getElementById('ai');
resetButton.addEventListener('click', resetBoard); aiButton.addEventListener('click', () => {
aiButton.addEventListener('click', toggleAi); game.gamemode = game.gamemode === 'ai' ? 'player' : 'ai';
game.start();
});

@ -48,3 +48,10 @@ button {
button:hover { button:hover {
background-color: #2C3036; background-color: #2C3036;
} }
.cell.x::after {
content: "X";
}
.cell.o::after {
content: "O";
}

@ -22,6 +22,6 @@
<button id="reset">Recommencer</button> <button id="reset">Recommencer</button>
<button id="ai">Activer/désactiver IA</button> <button id="ai">Activer/désactiver IA</button>
</div> </div>
<script src="script.js"></script> <script src="script1.js"></script>
</body> </body>
</html> </html>

Loading…
Cancel
Save