-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.js
146 lines (133 loc) · 5.21 KB
/
ai.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
/** Artifical Intelligence.
* @author Brian Kim
*/
class Ai {
constructor() {
this.winningval = 280;
this.losingval = -100000;
this._lastfrom = null;
this._lastto = null;
}
findmove(turn, board) {
if (turn == "W") {
this.gametree(board, 2, true, 1, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
let fromx = parseInt(this._lastfrom.charAt(0), 10);
let fromy = parseInt(this._lastfrom.charAt(1), 10);
let tox = parseInt(this._lastto.charAt(0), 10);
let toy = parseInt(this._lastto.charAt(1), 10);
let hfromx = fromx + 1;
let hfromy = fromy + 1;
let htox = tox + 1;
let htoy = toy + 1;
board.makemove(hfromx.toString() + hfromy.toString(), htox.toString() + htoy.toString());
} else if (turn == "B") {
this.gametree(board, 2, true, -1, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
let fromx = parseInt(this._lastfrom.charAt(0), 10);
let fromy = parseInt(this._lastfrom.charAt(1), 10);
let tox = parseInt(this._lastto.charAt(0), 10);
let toy = parseInt(this._lastto.charAt(1), 10);
let hfromx = fromx + 1;
let hfromy = fromy + 1;
let htox = tox + 1;
let htoy = toy + 1;
board.makemove(hfromx.toString() + hfromy.toString(), htox.toString() + htoy.toString());
}
}
gametree(board, depth, savemode, sense, alpha, beta) {
let triedto = null;
let triedfrom = null;
if (depth == 0) {
return this.staticScore(board);
}
if (sense == 1) {
let bestval = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < board.ftwhitemoves().length; i++) {
let from = board.ftwhitemoves()[i][0];
let to = board.ftwhitemoves()[i][1];
board.passivemove(from, to);
let val = this.gametree(board, depth - 1, false, -1, alpha, beta);
board.undo();
if (val > bestval) {
triedfrom = from;
triedto = to;
if (savemode) {
this._lastfrom = triedfrom;
this._lastto = triedto;
}
}
bestval = Math.max(bestval, val);
alpha = Math.max(alpha, bestval);
if (beta <= alpha) {
break;
}
}
return bestval;
} else if (sense == -1) {
let bestval = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < board.ftblackmoves().length; i++) {
let from = board.ftblackmoves()[i][0];
let to = board.ftblackmoves()[i][1];
board.passivemove(from, to);
let val = this.gametree(board, depth - 1, false, 1, alpha, beta);
board.undo();
if (val < bestval) {
triedfrom = from;
triedto = to;
if (savemode) {
this._lastfrom = triedfrom;
this._lastto = triedto;
}
}
bestval = Math.min(bestval, val);
beta = Math.min(beta, bestval);
if (beta <= alpha) {
break;
}
}
return bestval;
}
}
staticScore(board) {
let score = 0;
if (board._winner == "W") {
score += this.winningval;
}
if (board._winner == "B") {
score -= this.winningval;
}
for (let i = 0; i < board._piecelocations.length; i++) {
let x = parseInt(board._piecelocations[i].charAt(0), 10);
let y = parseInt(board._piecelocations[i].charAt(1), 10);
if (board._board[y][x].piece == "♔") {
score += 250;
} else if (board._board[y][x].piece == "♚") {
score -= 250;
} else if (board._board[y][x].piece == "♕") {
score += 130;
} else if (board._board[y][x].piece == "♛") {
score -= 130;
} else if (board._board[y][x].piece == "♖") {
score += 90;
} else if (board._board[y][x].piece == "♜") {
score -= 90;
} else if (board._board[y][x].piece == "♗") {
score += 65;
} else if (board._board[y][x].piece == "♝") {
score -= 65;
} else if (board._board[y][x].piece == "♘") {
score += 65;
} else if (board._board[y][x].piece == "♞") {
score -= 65;
} else if (board._board[y][x].piece == "♙") {
score += 30
score += Math.abs(y - 0) * 5;
} else if (board._board[y][x].piece == "♟") {
score -= 30
score -= Math.abs(y - 8) * 5;
}
}
score -= 6 * board._blackmoves.length;
score += 6 * board._whitemoves.length;
return score;
}
}