87 lines
2.3 KiB
Dart
87 lines
2.3 KiB
Dart
import 'dart:io';
|
|
import 'package:dartsunfish/index.dart';
|
|
|
|
class GameBot {
|
|
GameBot();
|
|
|
|
void gameMain() {
|
|
var hist = <Position>[
|
|
Position(initial, 0, [true, true], [true, true], 0, 0),
|
|
];
|
|
var searcher = Searcher();
|
|
while (true) {
|
|
UI.printBoard(hist.last);
|
|
|
|
if (hist.last.score <= -MATE_LOWER) {
|
|
print('You lost');
|
|
break;
|
|
}
|
|
|
|
// We query the user until she enters a (pseudo) legal move.
|
|
var move = <int>[];
|
|
var score = 0;
|
|
final possibleMoves = hist.last.generateMoves().toList();
|
|
while (!possibleMoves.doContains(move)) {
|
|
var pattern = RegExp(
|
|
r'([a-h][1-8])' * 2,
|
|
caseSensitive: false,
|
|
multiLine: false,
|
|
);
|
|
print('Your move:');
|
|
var input = stdin.readLineSync();
|
|
var match = pattern.firstMatch(input.toString());
|
|
if (match != null) {
|
|
move = [UI.parse(match.group(1)), UI.parse(match.group(2))];
|
|
if (!possibleMoves.doContains(move)) {
|
|
print('Not valid move typed in. Try again');
|
|
}
|
|
} else {
|
|
// Inform the user when invalid input (e.g. "help") is entered
|
|
print('Please enter a move like g8f6');
|
|
}
|
|
}
|
|
try {
|
|
hist.add(hist.last.movePiece(move));
|
|
} catch (e) {
|
|
// print(e.toString());
|
|
}
|
|
|
|
// After our move we rotate the board and print it again.
|
|
// This allows us to see the effect of our move.
|
|
UI.printBoard(hist.last.rotate());
|
|
// print('${hist.last.score}');
|
|
|
|
if (hist.last.score <= -MATE_LOWER) {
|
|
print('You won');
|
|
break;
|
|
}
|
|
/*
|
|
*/
|
|
// Fire up the engine to look for a move.
|
|
// var _depth = 0;
|
|
var start = getSeconds();
|
|
for (var s in searcher.search(hist.last, hist.toSet())) {
|
|
// _depth = s[0];
|
|
move = s[0];
|
|
score = s[1] ?? 0;
|
|
if (getSeconds() - start > 1) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (score == MATE_UPPER) {
|
|
print('Checkmate!');
|
|
}
|
|
// The black player moves from a rotated position, so we have to
|
|
// 'back rotate' the move before printing it.
|
|
print('My move: ${UI.render(119 - move[0]) + UI.render(119 - move[1])}');
|
|
hist.add(hist.last.movePiece(move));
|
|
}
|
|
}
|
|
|
|
void playGame() {
|
|
setPst();
|
|
gameMain();
|
|
}
|
|
}
|