-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1player.html
126 lines (111 loc) · 3.94 KB
/
1player.html
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
<!DOCTYPE html>
<html>
<head>
<title>Tic-Tac-Toe - You play against an AI Opponent</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="index.js"></script>
</head>
<body>
<h1>Tic-Tac-Toe : You play against an AI Opponent</h1>
<div class="board" id="board">
<div class="cell" onclick="makeMove(0, 0)"></div>
<div class="cell" onclick="makeMove(0, 1)"></div>
<div class="cell" onclick="makeMove(0, 2)"></div>
<div class="cell" onclick="makeMove(1, 0)"></div>
<div class="cell" onclick="makeMove(1, 1)"></div>
<div class="cell" onclick="makeMove(1, 2)"></div>
<div class="cell" onclick="makeMove(2, 0)"></div>
<div class="cell" onclick="makeMove(2, 1)"></div>
<div class="cell" onclick="makeMove(2, 2)"></div>
</div>
<p id="message"></p>
<button class="refresh-button" onclick="resetGame()">Restart Game</button>
<script>
let board = [["", "", ""], ["", "", ""], ["", "", ""]];
let currentPlayer = "X";
let gameOver = false;
function makeMove(row, col) {
if (!gameOver && board[row][col] === "") {
board[row][col] = currentPlayer;
document.getElementById("board").children[row * 3 + col].innerHTML = currentPlayer;
if (checkWinner(currentPlayer)) {
document.getElementById("message").innerHTML = currentPlayer + " wins!";
markWinningCells(currentPlayer);
gameOver = true;
} else if (isBoardFull()) {
document.getElementById("message").innerHTML = "It's a draw!";
gameOver = true;
} else {
currentPlayer = (currentPlayer === "X") ? "O" : "X";
if (!gameOver && currentPlayer === "O") {
setTimeout(makeComputerMove, 500);
}
}
}
}
function makeComputerMove() {
if (!gameOver) {
const bestMove = minimax(board, "O");
const row = bestMove.index[0];
const col = bestMove.index[1];
makeMove(row, col);
}
}
function minimax(board, player) {
const availableMoves = getAvailableMoves(board);
if (checkWinner("X")) {
return { score: -1 };
} else if (checkWinner("O")) {
return { score: 1 };
} else if (availableMoves.length === 0) {
return { score: 0 };
}
const moves = [];
for (let i = 0; i < availableMoves.length; i++) {
const move = {};
move.index = availableMoves[i];
board[availableMoves[i][0]][availableMoves[i][1]] = player;
if (player === "O") {
const result = minimax(board, "X");
move.score = result.score;
} else {
const result = minimax(board, "O");
move.score = result.score;
}
board[availableMoves[i][0]][availableMoves[i][1]] = "";
moves.push(move);
}
let bestMove;
if (player === "O") {
let bestScore = -Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
let bestScore = Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
function getAvailableMoves(board) {
const moves = [];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === "") {
moves.push([i, j]);
}
}
}
return moves;
}
</script>
</body>
</html>