-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsoleGame.cpp
79 lines (72 loc) · 1.89 KB
/
consoleGame.cpp
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
#include "consoleGame.h"
ConsoleGame::ConsoleGame() : ai(AI("*", black))
{
mapToCode = map<string,userCode>{
{ "newgame", userCode::newgame },
{ "setposition", userCode::setposition },
{ "help", userCode::help}
};
ai.printAscii();
}
void ConsoleGame::ioLoop(string initialCommand)
{
vector<string> inputList;
boost::trim(initialCommand);
boost::escaped_list_separator<char> sep("", " ", "");
boost::tokenizer<boost::escaped_list_separator<char>> tokenize(initialCommand, sep);
for (auto token : tokenize) {
inputList.push_back(token);
}
interpretInput(inputList);
while (true) {
waitForInput(inputList);
interpretInput(inputList);
}
}
void ConsoleGame::waitForInput(vector<string>& inputList)
{
cout << ">>> ";
string userInput;
inputList.clear();
getline(cin, userInput);
// Tokenize input
boost::trim(userInput);
boost::escaped_list_separator<char> sep("", " ", "");
boost::tokenizer<boost::escaped_list_separator<char>> tokenize(userInput, sep);
for (auto token : tokenize) {
inputList.push_back(token);
}
}
void ConsoleGame::interpretInput(const vector<string>& inputList)
{
if (inputList.empty()) return;
switch (mapToCode[inputList[0]]) {
case userCode::newgame:
// Resets hash tables and sets position to standard starting position
ai.reset();
ai.resetHash();
ai.currentAge = 0;
break;
case userCode::setposition: break;
case userCode::help: cout << inGameHelp << endl; break;
default:
// Move was entered
play(inputList[0]);
break;
}
}
void ConsoleGame::play(const string& move)
{
if (ai.isUserMoveValid(move, white)) {
ai.playStringMoves({ move }, white);
ai.printAscii();
cout << "Thinking...\n";
Move bestMove = ai.getBestMove(black, 5, false).first;
ai.playStringMoves({ shortNotation(bestMove) }, black);
ai.printAscii();
cout << "Computer: " << bestMove << endl;
}
else {
cout << "\"" << move << "\" is not a valid move\n";
}
}