-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
236 lines (200 loc) · 6.79 KB
/
script.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
document.addEventListener('DOMContentLoaded', () => {
const boardElement = document.getElementById('board');
const modeSelector = document.getElementById('mode');
const turnElement = document.getElementById('turn');
const statusElement = document.getElementById('status');
const playerXScoreElement = document.getElementById('playerXScore');
const playerOScoreElement = document.getElementById('playerOScore');
const modalElement = document.getElementById('modal');
const modalMessageElement = document.getElementById('modal-message');
const playAgainButton = document.getElementById('play-again');
let board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let gameActive = true;
let playerMode = modeSelector.value; // Default: Player vs Player
let playerXScore = 0;
let playerOScore = 0;
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
const renderBoard = () => {
boardElement.innerHTML = '';
board.forEach((value, index) => {
const cell = document.createElement('div');
cell.classList.add('cell');
if (value === 'X' || value === 'O') {
cell.textContent = value;
}
cell.addEventListener('click', () => handleCellClick(index));
boardElement.appendChild(cell);
});
updateBoardColors();
};
const handleCellClick = (index) => {
if (gameActive && board[index] === '') {
board[index] = currentPlayer;
renderBoard();
if (checkWinner()) {
endGame(`${currentPlayer} Wins!`);
} else if (board.every(cell => cell !== '')) {
endGame('Draw!');
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
updateTurn();
if (playerMode === 'pva' && currentPlayer === 'O') {
makeAIMove();
}
}
}
};
const updateTurn = () => {
turnElement.textContent = `Turn: Player ${currentPlayer}`;
};
const checkWinner = () => {
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
highlightWinningCells(pattern);
return board[a];
}
}
return null;
};
const highlightWinningCells = (pattern) => {
pattern.forEach((index) => {
const cellElement = boardElement.children[index];
cellElement.classList.add('winner');
});
};
const endGame = (message) => {
gameActive = false;
statusElement.textContent = message;
if (message.includes('Wins')) {
currentPlayer === 'X' ? playerXScore++ : playerOScore++;
updateScore();
}
showModal(message);
};
const updateScore = () => {
playerXScoreElement.textContent = `Player X: ${playerXScore}`;
playerOScoreElement.textContent = `Player O: ${playerOScore}`;
};
const showModal = (message) => {
modalMessageElement.textContent = message;
modalElement.style.display = 'flex';
};
const hideModal = () => {
modalElement.style.display = 'none';
};
const makeAIMove = () => {
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;
for (let i = 0; i < 9; i++) {
if (board[i] === '') {
board[i] = 'O';
let score = minimax(board, 0, false);
board[i] = '';
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
return bestMove;
};
const minimax = (board, depth, isMaximizing) => {
const scores = {
X: -1,
O: 1,
tie: 0
};
const winner = checkWinner();
if (winner !== null) {
return scores[winner] / depth;
}
if (isTerminal()) {
return 0;
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 9; i++) {
if (board[i] === '') {
board[i] = 'O';
let score = minimax(board, depth + 1, false);
board[i] = '';
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 9; i++) {
if (board[i] === '') {
board[i] = 'X';
let score = minimax(board, depth + 1, true);
board[i] = '';
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
};
const updateBoardColors = () => {
for (let i = 0; i < 9; i++) {
const cellElement = boardElement.children[i];
cellElement.classList.remove('winner');
}
};
const isTerminal = () => {
return checkWinner() !== null || board.every(cell => cell !== '');
};
modeSelector.addEventListener('change', () => {
const previousMode = playerMode;
playerMode = modeSelector.value;
if (previousMode !== playerMode) {
// Oyun modu değiştiğinde skorları sıfırla
playerXScore = 0;
playerOScore = 0;
updateScore();
}
resetGame();
});
playAgainButton.addEventListener('click', () => {
hideModal();
resetGame();
});
const resetGame = () => {
board = ['', '', '', '', '', '', '', '', ''];
currentPlayer = 'X';
gameActive = true;
statusElement.textContent = '';
renderBoard();
if (playerMode === 'pva' && currentPlayer === 'O') {
makeAIMove();
}
};
resetGame();
});