forked from ccccourse/wp111b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
期末
81 lines (77 loc) · 2.08 KB
/
期末
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
<!DOCTYPE html>
<html>
<head>
<title>圈圈叉叉</title>
<style>
.board {
display: flex;
flex-wrap: wrap;
width: 310px;
}
.cell {
width: 100px;
height: 100px;
border: 1px solid black;
font-size: 48px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
</style>
</head>
<body>
<div class="board">
<div class="cell" onclick="makeMove(0)"></div>
<div class="cell" onclick="makeMove(1)"></div>
<div class="cell" onclick="makeMove(2)"></div>
<div class="cell" onclick="makeMove(3)"></div>
<div class="cell" onclick="makeMove(4)"></div>
<div class="cell" onclick="makeMove(5)"></div>
<div class="cell" onclick="makeMove(6)"></div>
<div class="cell" onclick="makeMove(7)"></div>
<div class="cell" onclick="makeMove(8)"></div>
</div>
<script>
let currentPlayer = 'O';
let board = ['', '', '', '', '', '', '', '', ''];
const winningCombinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
function makeMove(index) {
if (board[index] === '') {
board[index] = currentPlayer;
document.getElementsByClassName('cell')[index].innerText = currentPlayer;
if (checkWin(currentPlayer)) {
alert(currentPlayer + '獲勝!');
resetGame();
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
}
function checkWin(player) {
for (let combination of winningCombinations) {
if (
board[combination[0]] === player &&
board[combination[1]] === player &&
board[combination[2]] === player
) {
return true;
}
}
return false;
}
function resetGame() {
board = ['', '', '', '', '', '', '', '', ''];
currentPlayer = 'X';
const cells = document.getElementsByClassName('cell');
for (let i = 0; i < cells.length; i++) {
cells[i].innerText = '';
}
}
</script>
</body>
</html>