-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplain.js
112 lines (98 loc) · 3.54 KB
/
plain.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
var rl = require('readline').createInterface(process.stdin, process.stdout)
, DEF = require('./lib/define')
, lp = require('./lib')
, ai = require('./lib/ai')
var wordList = lp.importDir('./words')
, results = []
, board
console.log('System Ready!');
rl.setPrompt('% ');
rl.prompt();
rl.on('line', function(line) {
line = line.trim().toLowerCase();
var op = line.substr(0,1);
if (op === '=') {
console.log('Board status')
console.log('--------------------');
console.log(board.boardWithColor());
} else if (op === '[') {
var move = eval(line);
if (typeof move == 'object') {
board.applyMove(move, false);
console.log('Board status updated like below:');
console.log('--------------------');
console.log(board.boardWithColor());
var val = board.evaluate();
if (val == DEF.MINUS_INFINITE) {
ai.printSummary(false);
} else if (val == DEF.PLUS_INFINITE) {
ai.printSummary(true);
}
}
} else if (op === '<') {
line = line.substring(1);
var status = eval(line);
if (typeof status == 'object') {
board.board = status;
console.log('Board status updated like below:');
console.log(board.board);
}
} else if (op === '1' || op === '2') {
if (op === '2') {
board.board = board.board.multiply([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]);
}
var startTime = new Date();
var val = board.solve();
var endTime = new Date();
var time = (endTime - startTime) / 1000;
if (val == DEF.MINUS_INFINITE) {
ai.printSummary(false);
} else {
if (val == DEF.PLUS_INFINITE) {
ai.printSummary(true);
}
board.applyMove(board.bestMove[1], true);
console.log('Best Move: ' + board.bestMove[2]);
console.log(board.bestMove[1]);
console.log('--------------------');
console.log('Found best move at step ' + board.bestMoveIndex + '(' + parseInt(board.bestMoveIndex / board.words.length * 100) + '%), time spent: ' + time + 's');
}
if (op === '2') {
board.board = board.board.multiply([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]);
}
console.log(board.boardWithColor());
} else if (op === '/') {
line = line.substring(1);
var filtered = lp.filterResults(line, usableWords);
lp.printResults(filtered);
console.log('--------------------');
console.log('Filtered ' + filtered.length + ' results');
} else if (line.length == 25) {
var startTime = new Date();
usableWords = lp.solveBoard(line, wordList);
usableWords = usableWords.sort(function(a, b) {
return b[0].length - a[0].length;
});
var time = (new Date() - startTime) / 1000;
startTime = new Date();
console.log('--------------------');
console.log('Found ' + usableWords.length + ' results, time spent: ' + time + 's');
// remove subsets words
usableWords = lp.removeSubsets(usableWords);
var time = (new Date() - startTime) / 1000;
startTime = new Date();
var words = [];
for (var i = 0; i < usableWords.length; i++) {
var word = usableWords[i][0];
words.push(word);
}
board = new ai.Board(line, words);
time = (new Date() - startTime) / 1000;
console.log('Using ' + words.length + ' results, with ' + board.words.length + ' variations, time spent: ' + time + 's');
} else {
console.log('Unknown input. Enter a 25-characters-long board, or use \'/\' to filter the results.');
}
rl.prompt();
}).on('close', function() {
process.exit(0);
});