-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessAI.html
280 lines (263 loc) · 8.81 KB
/
chessAI.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<html>
<body background="Background.png" onload="start()"></body>
<a href="Passage00.html">返回主页</a>
<link rel="stylesheet" href="chessboard-1.0.0.css">
<link rel="stylesheet" href="style.css">
<script src="jquery-3.6.0.min.js"></script>
<script src="chess.js"></script>
<script src="chessboard-1.0.0.js"></script>
<div id="board" style="width: 750px"></div>
<button id="solveBtn" onclick="makeMove()">计算</button>
<button id="undoBtn" onclick="Undo()">悔棋</button>
<input type="text" name="settext" id="settext">
<button id="setBtn" onclick="Set()">设定</button>
<br>深度=<span id="d"></span>
<br>节点数=<span id="jiedian"></span>
<br>计算速度(节点/秒)=<span id="jssd"></span>
<br>时间(秒)=<span id="time"></span>
<br>分数=<span id="val"></span>
</html>
<script>
document.body.addEventListener('touchmove', function(e){e.preventDefault();}, { passive: false })
var game = new Chess()
var board = null
var pos=['a8','b8','c8','d8','e8','f8','g8','h8','a7','b7','c7','d7','e7','f7','g7','h7','a6','b6','c6','d6','e6','f6','g6','h6','a5','b5','c5','d5','e5','f5','g5','h5','a4','b4','c4','d4','e4','f4','g4','h4','a3','b3','c3','d3','e3','f3','g3','h3','a2','b2','c2','d2','e2','f2','g2','h2','a1','b1','c1','d1','e1','f1','g1','h1']
function Undo(){
game.undo()
var config = {
draggable: true,
position:game.fen(),
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd,
}
board = Chessboard('board', config)
}
function Set(){
var fen=document.getElementById('settext').value
game.load(fen)
var config = {
draggable: true,
position:game.fen(),
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd,
}
board = Chessboard('board', config)
}
var reverseArray = function(array) {
return array.slice().reverse();
};
var Pawn =
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
[1.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0],
[0.5, 0.5, 1.0, 2.5, 2.5, 1.0, 0.5, 0.5],
[0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0],
[0.5, -0.5, -1.0, 0.0, 0.0, -1.0, -0.5, 0.5],
[0.5, 1.0, 1.0, -2.0, -2.0, 1.0, 1.0, 0.5],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
];
var pawn = reverseArray(Pawn);
var Knight =
[
[-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0],
[-4.0, -2.0, 0.0, 0.0, 0.0, 0.0, -2.0, -4.0],
[-3.0, 0.0, 1.0, 1.5, 1.5, 1.0, 0.0, -3.0],
[-3.0, 0.5, 1.5, 2.0, 2.0, 1.5, 0.5, -3.0],
[-3.0, 0.0, 1.5, 2.0, 2.0, 1.5, 0.0, -3.0],
[-3.0, 0.5, 1.0, 1.5, 1.5, 1.0, 0.5, -3.0],
[-4.0, -2.0, 0.0, 0.5, 0.5, 0.0, -2.0, -4.0],
[-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0]
];
var knight=reverseArray(Knight)
var Bishop = [
[ -2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0],
[ -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[ -1.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, -1.0],
[ -1.0, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, -1.0],
[ -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, -1.0],
[ -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[ -1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, -1.0],
[ -2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0]
];
var bishop = reverseArray(Bishop);
var Rook = [
[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[ 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5],
[ -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0]
];
var rook = reverseArray(Rook);
var Queen = [
[ -2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0],
[ -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[ -1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[ -0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[ 0.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[ -1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[ -1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0],
[ -2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0]
];
var queen=reverseArray(Queen)
var King = [
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -2.0, -3.0, -3.0, -4.0, -4.0, -3.0, -3.0, -2.0],
[ -1.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -1.0],
[ 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0 ],
[ 2.0, 3.0, 1.0, 0.0, 0.0, 1.0, 3.0, 2.0 ]
];
var king = reverseArray(King);
function fenscore(){
var wscore=0;
var bscore=0;
var K=0;
var Q=0;
var R=0;
var B=0;
var N=0;
var P=0;
var k=0;
var q=0;
var r=0;
var b1=0;
var n=0;
var p=0;
for(var i=0;i<64;i++){
var t=game.get(pos[i])
if(t!=null){
var t1=t.type
var t2=t.color
var a=Math.floor(i/8)
var b=i%8
if(t1=='k'&&t2=='w'){wscore+=9000+King[a][b]}
else if(t1=='q'&&t2=='w'){wscore+=90+Queen[a][b]}
else if(t1=='r'&&t2=='w'){wscore+=50+Rook[a][b]}
else if(t1=='b'&&t2=='w'){wscore+=30+Bishop[a][b]}
else if(t1=='n'&&t2=='w'){wscore+=30+knight[a][b]}
else if(t1=='p'&&t2=='w'){wscore+=10+Pawn[a][b]}
else if(t1=='k'&&t2=='b'){bscore+=9000+king[a][b]}
else if(t1=='q'&&t2=='b'){bscore+=90+queen[a][b]}
else if(t1=='r'&&t2=='b'){bscore+=50+rook[a][b]}
else if(t1=='b'&&t2=='b'){bscore+=30+bishop[a][b]}
else if(t1=='n'&&t2=='b'){bscore+=30+knight[a][b]}
else{bscore+=10+pawn[a][b]}
}
}
if(game.turn()=='w'){
wscore+=1.5
return wscore-bscore
}
else{
bscore+=1.5
return bscore-wscore
}
}
var minimax =function(depth, game,alpha,beta) {
var ffoundpv=false;
if (depth <= 0) {
return fenscore();
}
var newGameMoves = game.ugly_moves();
if(newGameMoves.length==0){
if(game.in_check()){
return -8999.5 + D/2-depth/2
}
else{
return 0
}
}
if(game.in_threefold_repetition()){
return 0
}
for (var i = 0; i < newGameMoves.length; i++) {
game.ugly_move(newGameMoves[i]);
positionCount++;
if(ffoundpv){
bestMove=-minimax(depth - 1, game, -alpha-0.5, -alpha);
if(bestMove>alpha&&bestMove<beta){
bestMove = -minimax(depth - 1, game, -beta, -alpha);
}
}
else{
bestMove = -minimax(depth - 1, game, -beta, -alpha);
}
game.undo();
if (bestMove>=beta) {
return beta;
}
if (bestMove>alpha) {
alpha=bestMove
bestMoveFound=newGameMoves[i]
ffoundpv=true;
}
}
if(depth==D){return [bestMoveFound,alpha]}
return alpha
};
function onDragStart (source, piece, position, orientation) {
// do not pick up pieces if the game is over
if (game.game_over()) return false
return true
}
var makeMove = function () {
var bestMove = getMove();
game.ugly_move(bestMove);
board.position(game.fen());
renderMoveHistory(game.history());
};
function getMove () {
positionCount = 0;
D = 0;
var value=0
var alpha=-Infinity
var beta=Infinity
var d=new Date().getTime()
while(D<4&&Math.abs(value)<20000){
D+=2
var bestMoveandvalue = minimax(D, game,alpha,beta);
var d2 = new Date().getTime();
var bestMove=bestMoveandvalue[0]
var value=bestMoveandvalue[1]
var moveTime = (d2 - d)/1000;
var positionsPerS = ( positionCount/ moveTime);
}
document.getElementById("d").innerHTML=D
document.getElementById("jiedian").innerHTML=positionCount
document.getElementById("jssd").innerHTML=positionsPerS
document.getElementById("time").innerHTML=moveTime
document.getElementById("val").innerHTML=value
return bestMove;
}
function onDrop (source, target) {
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: 'q' // NOTE: always promote to a queen for example simplicity
})
// illegal move
if (move === null) return 'snapback'
}
// update the board position after the piece snap
// for castling, en passant, pawn promotion
function onSnapEnd () {
board.position(game.fen())
}
var config = {
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd,
}
board = Chessboard('board', config)
</script>