diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bot.cpp | 30 | ||||
| -rw-r--r-- | src/bot.hpp | 1 | ||||
| -rw-r--r-- | src/moves.cpp | 15 | ||||
| -rw-r--r-- | src/uci.cpp | 2 |
4 files changed, 35 insertions, 13 deletions
diff --git a/src/bot.cpp b/src/bot.cpp index 3054b0d..e976d11 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -10,7 +10,7 @@ int KNIGHT_VALUE = 200; int BISHOP_VALUE = 300; int ROOK_VALUE = 400; int QUEEN_VALUE = 900; -int CHECK = 300; +int CHECK = 20; int MATE = 10000; int KNIGHT_TABLE[64] = {-50, -40, -30, -30, -30, -30, -40, -50, -40, -20, 0, @@ -22,6 +22,21 @@ int KNIGHT_TABLE[64] = {-50, -40, -30, -30, -30, -30, -40, -50, -40, -20, 0, const int SEARCH_DEPTH = 3; +#include <fstream> + +void LogMove(const Move &move) { + std::ofstream file("moves.txt", std::ios::app); + + if (!file.is_open()) + return; + + file << static_cast<char>('a' + move.From.file) + << static_cast<char>('8' - move.From.rank) + << static_cast<char>('a' + move.To.file) + << static_cast<char>('8' - move.To.rank); + + file << '\n'; +} Move EngineGetBestMove(Game *b) { auto moves = GetLegalMoves(b); if (moves.size() == 0) { @@ -30,6 +45,7 @@ Move EngineGetBestMove(Game *b) { Move bestMove = moves[0]; float BestEval = (b->turn ? -INFINITY : INFINITY); for (Move move : moves) { + LogMove(move); Game TestBoard = *b; PlayMove(move, &TestBoard); float alpha = -INFINITY; @@ -95,6 +111,7 @@ float minimax(int depth, Game *b, float alpha, float beta) { return BestEval; } } +int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); } float EvaluateBoardForWhite(Game *b) { float score = 0; @@ -125,7 +142,7 @@ float EvaluateBoardForWhite(Game *b) { break; case KNIGHT: value = KNIGHT_VALUE; - value += KNIGHT_TABLE[i]; + value += KNIGHT_TABLE[PSTIndex(i, piece.color)]; break; case BISHOP: value = BISHOP_VALUE; @@ -145,10 +162,11 @@ float EvaluateBoardForWhite(Game *b) { else score -= value; } - bool isCheck = IsPieceTypeAttacked(b, KING, 1); - if (isCheck) { - score += CHECK; - } + if (IsPieceTypeAttacked(b, KING, true)) + score -= CHECK; // white king attacked + + if (IsPieceTypeAttacked(b, KING, false)) + score += CHECK; // black king attacked if (isStaleMate) { return 0; } diff --git a/src/bot.hpp b/src/bot.hpp index 242fdfe..74daefa 100644 --- a/src/bot.hpp +++ b/src/bot.hpp @@ -5,5 +5,6 @@ Move EngineGetBestMove(Game *b); float EvaluateBoardForWhite(Game *b); float minimax(int depth, Game *b, float alpha, float beta); +void LogMove(const Move &move); #endif /* SRC_BOT_H_ */ diff --git a/src/moves.cpp b/src/moves.cpp index d7df595..00cd437 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -1,6 +1,7 @@ #include <array> #include <cassert> #include <cstdint> +#include <iostream> #include <sys/types.h> #include <vector> @@ -61,20 +62,22 @@ void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) { moves.push_back({position, IndexToPosition(twoSteps)}); } } - - // check for (int fileOffset : {-1, 1}) { int targetFile = position.file + fileOffset; - if (targetFile < 0 || targetFile >= 8) { + int targetRank = position.rank + (pawn.color ? -1 : 1); + + if (targetFile < 0 || targetFile >= 8) continue; - } - int target = (position.rank + (pawn.color ? -1 : 1)) * 8 + targetFile; + if (targetRank < 0 || targetRank >= 8) + continue; + + int target = targetRank * 8 + targetFile; if (IndexToPosition(target) == b->enPassant && b->canEnpassant) { - // enpasstant!!!!!!! moves.push_back({position, IndexToPosition(target)}); } + if (b->pieces[target].type != NONE && b->pieces[target].color != pawn.color) { moves.push_back({position, IndexToPosition(target)}); diff --git a/src/uci.cpp b/src/uci.cpp index 4825ece..a2c3672 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -59,7 +59,7 @@ Move UciToMove(string uci) { return {from, to}; } static Game initBoard(string startingFEN) { - Game b; + Game b{}; b.enPassant = IndexToPosition(0); // default value b.canEnpassant = false; b.state = TURN; |
