diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-27 15:14:01 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-27 15:14:01 +0200 |
| commit | 35b21c6d970857dd945c5dfa476a27c8c2a8b1f6 (patch) | |
| tree | 68547059dfd9c402cfe535cf5b89f84c47a77687 /src/bot.cpp | |
| parent | 03437563f91a6f760fa8b0283e2e74586a9c7dac (diff) | |
adding bot
Diffstat (limited to 'src/bot.cpp')
| -rw-r--r-- | src/bot.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/bot.cpp b/src/bot.cpp new file mode 100644 index 0000000..8889156 --- /dev/null +++ b/src/bot.cpp @@ -0,0 +1,105 @@ +#include "bot.hpp" +#include "board.hpp" +#include "moves.hpp" +#include <cmath> +#include <ctime> + +int PAWN_VALUE = 100; +int KNIGHT_VALUE = 200; +int BISHOP_VALUE = 300; +int ROOK_VALUE = 400; +int QUEEN_VALUE = 900; + +Move EngineGetBestMove(Board *b) { + auto moves = GetLegalMoves(b); + std::srand(std::time(0)); // use current time as seed for random generator + int random_pos = std::rand() % moves.size(); + Move move = moves[random_pos]; + return move; +} + +float EvaluateBoard(Board *b) { + float score = 0; + Board BoardCopy = *b; + + bool isStaleMate = false; + + auto legalMoves = GetLegalMoves(&BoardCopy); + if (b->state == WHITE_WON) { + score = INFINITY; + } + if (b->state == BLACK_WON) { + score = INFINITY * -1; + } + if (b->state == STALEMATE || b->state == DRAW) { + isStaleMate = true; + } + int white_pawn_count = 0; + int white_knight_count = 0; + int white_bishop_count = 0; + int white_rook_count = 0; + int white_queen_count = 0; + + int black_pawn_count = 0; + int black_knight_count = 0; + int black_bishop_count = 0; + int black_rook_count = 0; + int black_queen_count = 0; + for (Piece piece : b->pieces) { + if (piece.type == NONE) { + continue; + } + if (piece.color) { + // white + if (piece.type == PAWN) { + white_pawn_count++; + } + if (piece.type == KNIGHT) { + white_knight_count++; + } + if (piece.type == BISHOP) { + white_bishop_count++; + } + if (piece.type == ROOK) { + white_rook_count++; + } + if (piece.type == QUEEN) { + white_queen_count++; + } + continue; + } + + // black + if (piece.type == PAWN) { + black_pawn_count++; + } + if (piece.type == KNIGHT) { + black_knight_count++; + } + if (piece.type == BISHOP) { + black_bishop_count++; + } + if (piece.type == ROOK) { + black_rook_count++; + } + if (piece.type == QUEEN) { + black_queen_count++; + } + } + + score += white_pawn_count * PAWN_VALUE; + score += white_knight_count * KNIGHT_VALUE; + score += white_bishop_count * BISHOP_VALUE; + score += white_rook_count * ROOK; + score += white_queen_count * QUEEN_VALUE; + + score -= black_pawn_count * PAWN_VALUE; + score -= black_knight_count * KNIGHT_VALUE; + score -= black_bishop_count * BISHOP_VALUE; + score -= black_rook_count * ROOK; + score -= black_queen_count * QUEEN_VALUE; + if (isStaleMate) { + return 0; + } + return score; +} |
