-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomBot.js
More file actions
74 lines (69 loc) · 2.25 KB
/
RandomBot.js
File metadata and controls
74 lines (69 loc) · 2.25 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
(function (exports) {
'use strict';
var extend = require('./extend.js').extend,
Board = require('./chessboard.js').Board,
LiacBot = require('./base_client.js').LiacBot,
// class RandomBot extends LiacBot // (base_client)
RandomBot = extend(
// Classe pai
LiacBot,
// Construtor
function () {
this.lastMove = null;
},
// Propriedades default e métodos
{
name: "Random Bot",
onMove: function (state) {
console.log(state.board);
console.log("Generating a move...");
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)];
this.last_move = move;
this.sendMove(move.from, move.to);
},
onGameOver: function (state) {
console.log('Game Over');
console.log(state);
console.log('---------');
}
}
);
exports.RandomBot = RandomBot;
//////////////////////////////////////////////////
// Main (para quando é executado diretamente)
//////////////////////////////////////////////////
function main() {
var bot = new RandomBot(),
portIdx = process.argv.indexOf("-p"),
hostIdx = process.argv.indexOf("-h"),
port,
host;
if (process.argv.indexOf("--help") !== -1) {
console.log("Usage: node RandomBot.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();
}
}(exports));