-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (174 loc) · 4.87 KB
/
script.js
File metadata and controls
196 lines (174 loc) · 4.87 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
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
//变量声明
//testing
let originBoard;
let huPlayer;
let aiPlayer;
const circlePng = './circle.png';
const crossPng = './cross.png';
const restartButton = document.getElementById('restartButton');
const xButton = document.getElementById('xButton');
const oButton = document.getElementById('oButton');
const winCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[2, 4, 6],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
];
//单元格绑定
const cells = document.querySelectorAll('.cell');
document.querySelector('.endgame').style.display = 'none';
restartButton.addEventListener('click', reStartGame);
xButton.addEventListener('click', chooseSign);
oButton.addEventListener('click', chooseSign);
function reStartGame() {
document.querySelector('.startgame').style.display = 'flex';
startGame();
}
//确认人机标识
function chooseSign(e) {
if (e.target.id === 'xButton') {
huPlayer = crossPng;
aiPlayer = circlePng;
} else if (e.target.id === 'oButton') {
huPlayer = circlePng;
aiPlayer = crossPng;
}
//关闭提示框
document.querySelector('.startgame').style.display = 'none';
startGame();
}
function startGame() {
//控制提示框弹出和关闭
document.querySelector('.endgame').style.display = 'none';
//创建一个九宫格棋盘数组,元素为0-9
originBoard = Array.from(Array(9).keys());
//给HTML上的单元格绑定事件监听器
for (let cell of cells) {
cell.addEventListener('click', turnClick);
cell.style.removeProperty('background-color');
cell.innerHTML = '';
}
//给提示框选标识绑定事件监听器
xButton.addEventListener('click', chooseSign);
oButton.addEventListener('click', chooseSign);
}
//确认人点击的九宫格
function turnClick(square) {
if (typeof originBoard[square.target.id] === 'number') {
turn(square.target.id, huPlayer);
if (!checkTie()) {
turn(bestSpot(), aiPlayer);
}
}
}
//给点击的九宫格画上标记
function turn(squareId, player) {
const newChild = document.createElement('img');
newChild.src = player;
document.getElementById([squareId]).appendChild(newChild);
originBoard[squareId] = player;
gameWon = checkWin(originBoard, player);
if (gameWon) gameOver(gameWon);
}
function checkWin(board, player) {
let checkedSquare = board.reduce(
(accum, curr, i) => (curr === player ? accum.concat(i) : accum),
[]
);
let gameWon = null;
for (let [index, win] of winCombos.entries()) {
if (win.every((element) => checkedSquare.indexOf(element) > -1)) {
gameWon = { index, player };
break;
}
}
return gameWon;
}
function gameOver(gameWon) {
for (let index of winCombos[gameWon.index]) {
const selectedCells = document.getElementById(index);
selectedCells.style.backgroundColor = '#f2a84e';
selectedCells.style.animation = 'highlight 1s 4';
}
for (let cell of cells) {
cell.removeEventListener('click', turnClick);
}
declareWinner(gameWon.player == huPlayer ? 'You Win!' : 'You lose.');
}
function declareWinner(who) {
document.querySelector('.endgame').style.display = 'flex';
document.querySelector('.text').innerText = who;
}
function emptySquares() {
return originBoard.filter((s) => typeof s == 'number');
}
function bestSpot() {
return minimax(originBoard, aiPlayer).index;
}
function checkTie() {
if (emptySquares().length == 0) {
for (let cell of cells) {
cell.style.backgroundColor = '#d5dedb';
cell.removeEventListener('click', turnClick);
}
declareWinner('Tie Game!');
return true;
}
return false;
}
function minimax(newBoard, player) {
//检查空格并存储到数组
let availSpots = emptySquares();
//确定static value
if (checkWin(newBoard, huPlayer)) {
return { score: -10 };
}
if (checkWin(newBoard, aiPlayer)) {
return { score: 10 };
} else if (availSpots.length === 0) {
return { score: 0 };
}
let moves = [];
//通过递归调用minmax函数来绘制预测落子步骤并记录
for (let spot of availSpots) {
let move = {};
move.index = newBoard[spot];
newBoard[spot] = player;
//recursion starts here
if (player == aiPlayer) {
let result = minimax(newBoard, huPlayer);
move.score = result.score;
} else {
let result = minimax(newBoard, aiPlayer);
move.score = result.score;
}
//将空位重置为空
newBoard[spot] = move.index;
moves.push(move);
}
//根据static value来得出最优解
let bestMove;
if (player === aiPlayer) {
let bestScore = -1000;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
let bestScore = 1000;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
//返回最优解
return moves[bestMove];
}