Skip to content

Commit

Permalink
Merge pull request #1 from ramazancetinkaya/ramazancetinkaya-patch-1
Browse files Browse the repository at this point in the history
Update script.js
  • Loading branch information
ramazancetinkaya authored Nov 24, 2023
2 parents 494cd9f + 0eabdef commit ca7f81a
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,31 @@ document.addEventListener('DOMContentLoaded', () => {
};

const makeAIMove = () => {
const bestMove = getBestMove();
let bestMove;

// Rastgele bir hamle yapılacak ilk tur
if (board.filter(cell => cell !== '').length === 1) {
bestMove = getRandomMove();
} else {
// İlerleyen turlarda stratejik hamle yapılacak
bestMove = getBestMove();
}

handleCellClick(bestMove);
};

const getRandomMove = () => {
const emptyCells = board.reduce((acc, cell, index) => {
if (cell === '') {
acc.push(index);
}
return acc;
}, []);

const randomIndex = Math.floor(Math.random() * emptyCells.length);
return emptyCells[randomIndex];
};

const getBestMove = () => {
let bestScore = -Infinity;
let bestMove;
Expand Down Expand Up @@ -137,7 +158,11 @@ document.addEventListener('DOMContentLoaded', () => {

const winner = checkWinner();
if (winner !== null) {
return scores[winner];
return scores[winner] / depth;
}

if (isTerminal()) {
return 0;
}

if (isMaximizing) {
Expand Down Expand Up @@ -172,6 +197,10 @@ document.addEventListener('DOMContentLoaded', () => {
}
};

const isTerminal = () => {
return checkWinner() !== null || board.every(cell => cell !== '');
};

modeSelector.addEventListener('change', () => {
const previousMode = playerMode;
playerMode = modeSelector.value;
Expand Down

0 comments on commit ca7f81a

Please sign in to comment.