Commit a7d24092 authored by alaskalinuxuser's avatar alaskalinuxuser
Browse files

ply moves enabled.

parent ce79782b
......@@ -26,6 +26,7 @@ string inputString = "";
bool runProgram = true;
bool styleRandom = false;
Board board;
int chosenPly = 3;
Moves movePick;
string engineMove = "";
......@@ -73,7 +74,7 @@ void inputGo()
{
cout << "Going...." << endl;
engineMove = "";
engineMove = movePick.bestMove(board.moveBoard(), board.getTurn(), styleRandom, board.getIsPassent(), board.getEnPassent(), 1);
engineMove = movePick.bestMove(board.moveBoard(), board.getTurn(), styleRandom, board.getIsPassent(), board.getEnPassent(), chosenPly);
cout << "bestmove " + engineMove << endl;
}
void inputQuit()
......
......@@ -1633,20 +1633,12 @@ string Moves::available(string boardPositions, bool whoseTurn, bool isPass, char
*/
}
string Moves::bestMove(string boardPositions, bool whoseTurn, bool styleRandom, bool isPass, char enPass, int plyMoves){
string Moves::plyMove(string boardPositions, bool whoseTurn, bool isPass, char enPass, int plyMoves){
plyMoves--;
string candidateList = Moves::available(boardPositions, whoseTurn, isPass, enPass);
string chosenMove = "";
int currentEval = 0;
if (candidateList.size() > 0){
if (styleRandom) {
// Make a random move.
srand((int)time(0) + candidateList.size());
int r = (rand() % (candidateList.size()/5));
for (int a = 0; a < 4; a++) {
chosenMove += candidateList[(5*r)+a];
} // End random move.
} else {
// Not random, so make educated moves.
currentEval = evaluations.getEval(boardPositions);
cout << "Current eval: " << " time " << (int)time(0) << " eval "<< currentEval << endl;
int moveEval = 0;
......@@ -1667,7 +1659,6 @@ string Moves::bestMove(string boardPositions, bool whoseTurn, bool styleRandom,
boardPositions[first] = '-';
moveEval = evaluations.getEval(boardPositions);
cout << "Move eval: " << chosenMove << " eval " << moveEval << " places " << boardPositions << endl;
plyMoves--;
if (whoseTurn){
// White's turn, higher is better.
if ( moveEval > bestEval ) {
......@@ -1687,10 +1678,76 @@ string Moves::bestMove(string boardPositions, bool whoseTurn, bool styleRandom,
bestEval = moveEval;
}
}
try {
if (plyMoves > 0){
whoseTurn = !whoseTurn;
string nextMove = plyMove(boardPositions, whoseTurn, isPass, enPass, plyMoves);
whoseTurn = !whoseTurn;
int third =((nextMove.at(0) - 'a' + 1) + (((nextMove.at(1) - '1') * 8) - 1));
int fourth =((nextMove.at(2) - 'a' + 1) + (((nextMove.at(3) - '1') * 8) - 1));
char thirdPieceHolder = boardPositions[third];
char fourthPieceHolder = boardPositions[fourth];
boardPositions[fourth] = boardPositions[third];
boardPositions[third] = '-';
int newMoveEval = evaluations.getEval(boardPositions);
cout << "Move eval: " << chosenMove << " eval " << moveEval << " places " << boardPositions << endl;
if (whoseTurn){
// White's turn, higher is better.
if ( newMoveEval > bestEval ) {
chosenMove = proposedMove;
bestEval = newMoveEval;
} else if ( newMoveEval == bestEval && (int)time(0)%2 == 0) {
chosenMove = proposedMove;
bestEval = newMoveEval;
}
} else {
// Black's turn, lower is better.
if ( newMoveEval < bestEval ) {
chosenMove = proposedMove;
bestEval = newMoveEval;
} else if ( newMoveEval == bestEval && (int)time(0)%2 == 0) {
chosenMove = proposedMove;
bestEval = newMoveEval;
}
}
boardPositions[third] = thirdPieceHolder;
boardPositions[fourth] = fourthPieceHolder;
}
} catch (...) {
cout << " Exception " << endl;
} // End try/catch block
boardPositions[first] = firstPieceHolder;
boardPositions[second] = secondPieceHolder;
} // End candidate list moves.
} else {
// candidate list is 0.... game over.
}
return chosenMove;
}
string Moves::bestMove(string boardPositions, bool whoseTurn, bool styleRandom, bool isPass, char enPass, int plyMoves){
string candidateList = Moves::available(boardPositions, whoseTurn, isPass, enPass);
string chosenMove = "";
if (candidateList.size() > 0){
if (styleRandom) {
// Make a random move.
srand((int)time(0) + candidateList.size());
int r = (rand() % (candidateList.size()/5));
for (int a = 0; a < 4; a++) {
chosenMove += candidateList[(5*r)+a];
} // End random move.
} else {
// Not random, so make educated moves.
try {
chosenMove = plyMove(boardPositions, whoseTurn, isPass, enPass, plyMoves);
} catch (...) {
cout << " Exception " << endl;
} // End try/catch block
}// End not random.
if (chosenMove == "") {
// Chosen move is still blank, and list is not 0, that is wrong, so pick a random move.
chosenMove = bestMove(boardPositions, whoseTurn, true, isPass, enPass, plyMoves);}
}// End candidates list.
return chosenMove;
}
......@@ -28,9 +28,12 @@ public:
// All available moves.
string available(string boardPositions, bool whoseTurn, bool isPass, char enPass);
// Best move.
// Choose a move, random or weighted.
string bestMove(string boardPositions, bool whoseTurn, bool styleRandom, bool isPass, char enPass, int plyMoves);
// Weighted move, considering ply.
string plyMove(string boardPositions, bool whoseTurn, bool isPass, char enPass, int plyMoves);
// Is the king safe?
bool isKingSafe(string boardPositions, bool whoseTurn);
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment