aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-27 15:14:01 +0200
committerAdam <adammegarules1@gmail.com>2026-07-27 15:14:01 +0200
commit35b21c6d970857dd945c5dfa476a27c8c2a8b1f6 (patch)
tree68547059dfd9c402cfe535cf5b89f84c47a77687 /src
parent03437563f91a6f760fa8b0283e2e74586a9c7dac (diff)
adding bot
Diffstat (limited to 'src')
-rw-r--r--src/board.cpp2
-rw-r--r--src/bot.cpp105
-rw-r--r--src/bot.hpp8
-rw-r--r--src/repl.cpp9
4 files changed, 117 insertions, 7 deletions
diff --git a/src/board.cpp b/src/board.cpp
index e8933eb..0d884a6 100644
--- a/src/board.cpp
+++ b/src/board.cpp
@@ -1,4 +1,5 @@
#include "board.hpp"
+#include "bot.hpp"
#include <cassert>
#include <cctype>
#include <cstdio>
@@ -92,6 +93,7 @@ void printBoard(Board *b) {
8 - (int)b->enPassant.rank);
std::printf("Move clock: %d\n", b->MoveClock);
std::printf("Half move clock: %d\n", b->halfMoveClock);
+ std::printf("Board Eval: %f\n", EvaluateBoard(b));
}
void PlayMove(Move move, Board *b) {
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;
+}
diff --git a/src/bot.hpp b/src/bot.hpp
new file mode 100644
index 0000000..1a2b951
--- /dev/null
+++ b/src/bot.hpp
@@ -0,0 +1,8 @@
+#ifndef SRC_BOT_H_
+#define SRC_BOT_H_
+
+#include "board.hpp"
+Move EngineGetBestMove(Board *b);
+float EvaluateBoard(Board *b);
+
+#endif /* SRC_BOT_H_ */
diff --git a/src/repl.cpp b/src/repl.cpp
index 8eae719..ce545d1 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -1,5 +1,6 @@
#include "repl.hpp"
#include "board.hpp"
+#include "bot.hpp"
#include "moves.hpp"
#include <algorithm>
#include <cctype>
@@ -53,13 +54,7 @@ int startREPL(Board *b) {
}
if (b->state == TURN) {
if (b->turn == false) {
- auto moves = GetLegalMoves(b);
- std::srand(
- std::time(0)); // use current time as seed for random generator
- int random_pos = std::rand() %
- moves.size(); // Modulo to restrict the number of
- // random values to be at most A.size()-1
- Move move = moves[random_pos];
+ Move move = EngineGetBestMove(b);
PlayMove(move, b);
b->turn = !b->turn;
printBoard(b);