-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLBotFelipe02.js
More file actions
224 lines (197 loc) · 8.16 KB
/
LLBotFelipe02.js
File metadata and controls
224 lines (197 loc) · 8.16 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
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
var extend = require('./extend.js').extend,
Board = require('./chessboard.js').Board,
LiacBot = require('./base_client.js').LiacBot,
// Constants
MINIMAX_DEPTH = 4,
DRAW_SCORE = 100000000,
PAWN_SCORE = [100, 120, 140, 170, 200, 230, Infinity],
KNIGHT_SCORE = 200,
QUEEN_SCORE = 500,
estimationCount = 0,
Team = {
WHITE : 1,
BLACK : -1
},
// class LLBot extends LiacBot // (base_client)
LLBot = extend(
// Classe pai
LiacBot,
// Construtor
function () {
this.lastMove = null;
},
// Propriedades default e métodos
{
name: "LLBotFelipe02",
onMove: function (state) {
console.log(state.board);
console.log("Generating a move...");
estimationCount = 0;
var board = new Board(state),
moves,
move;
if (state.bad_move) {
console.log(state);
}
//moves = board.generate();
//move = moves[Math.floor(Math.random() * moves.length)];
var result = this.chooseMove(board);
move = result.move;
this.last_move = move;
console.log("Number of estimations: " + estimationCount);
console.log("Move from [" + move.from.x + "][" + move.from.y + "] "+
"to [" + move.to.x + "][" + move.to.y + "]");
console.log("Path: " + result.path);
this.sendMove(move.from, move.to);
},
onGameOver: function (state) {
console.log('Game Over');
console.log(state);
console.log('---------');
},
estimateScore: function (board) {
var score = 0;
// Counts number of white pieces minus black pieces
var x, y, p, pawnDistance;
for (x = 0; x < 8; x += 1) {
for (y = 0; y < 8; y += 1) {
p = board.cells[x][y];
if (p) {
if (p.type == 'p') { // Pawn
if (p.team === Team.WHITE) {
pawnDistance = y - 1;
} else {
pawnDistance = 6 - y;
}
score += p.team * PAWN_SCORE[pawnDistance];
} else if (p.type == 'n') {
score += p.team * KNIGHT_SCORE;
} else if (p.type == 'q') {
score += p.team * QUEEN_SCORE;
}
}
}
}
estimationCount += 1;
return score;
},
// returns a move
chooseMove: function (board) {
var result = this.minimax(board, MINIMAX_DEPTH, -Infinity, Infinity);
return { move: result.move, path: result.path };
},
moveToString: function (move) {
letter = ["a", "b", "c", "d", "e", "f", "g", "h"];
return "(" + letter[move.from.x] + (move.from.y + 1) + " -> " +
letter[move.to.x] + (move.to.y + 1) + "). ";
},
minimax : function (board, depth, alpha, beta) {
if (depth == 0 || board.isTerminal()) {
if (depth == 0) {
return { value: this.estimateScore(board), move: null, path: "" };
} else {
return { value: this.estimateScore(board), move: null, path: "TERMINAL. " };
}
}
var bestValue, val;
var bestMove = null;
var pathDeeper = "";
if (board.whoMoves === Team.WHITE) {
// White moves, maximize value
bestValue = -Infinity;
// Generate all possible moves
var possibleMoves = board.generate();
// No possible moves, draw.
if (possibleMoves.length === 0) {
//console.log("no moves for white");
return { value: board.whoMoves * DRAW_SCORE, move: null };
}
var that = this;
possibleMoves.every(function (move) {
var result = that.minimax(board.afterMove(move), depth-1, alpha, beta);
val = result.value;
pathDeeper = result.path;
if (val > alpha) {
alpha = val;
}
if (bestMove == null) {
bestMove = move;
}
if (val > bestValue) {
bestValue = val;
if (move)
bestMove = move;
}
if (beta <= alpha)
return false; // Break 'every' loop
return true;
});
}
else {
// Black moves, minimize value
bestValue = Infinity;
// Generate all possible moves
var possibleMoves = board.generate();
// No possible moves, draw.
if (possibleMoves.length === 0) {
//console.log("no moves for black");
return { value: board.whoMoves * DRAW_SCORE, move: null };
}
var that = this;
possibleMoves.every(function (move) {
var result = that.minimax(board.afterMove(move), depth-1, alpha, beta);
val = result.value;
pathDeeper = result.path;
if (val < beta) {
beta = val;
}
if (bestMove == null) {
bestMove = move;
}
if (val < bestValue) {
bestValue = val;
if (move)
bestMove = move;
}
if (beta <= alpha)
return false; // Break 'every' loop
return true;
});
}
//console.log("a minimax chosen step successful");
var pathCurrent = this.moveToString(bestMove) + pathDeeper;
return { value: bestValue, move: bestMove, path: pathCurrent};
}
}
);
exports.LLBot = LLBot;
//////////////////////////////////////////////////
// Main (para quando é executado diretamente)
//////////////////////////////////////////////////
function main() {
var bot = new LLBot(),
portIdx = process.argv.indexOf("-p"),
hostIdx = process.argv.indexOf("-h"),
port,
host;
if (process.argv.indexOf("--help") !== -1) {
//console.log("Usage: node LLBot.js [-p PORT] [-h HOST]");
process.exit();
}
if (portIdx > 0) {
port = parseInt(process.argv[portIdx + 1], 10);
}
if (hostIdx > 0) {
host = process.argv[hostIdx + 1];
}
if (port) {
bot.port = port;
}
if (host) {
bot.ip = host;
}
bot.start();
}
if (require.main === module) {
main();
}