aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-28 09:43:30 +0200
committerAdam <adammegarules1@gmail.com>2026-07-28 09:43:30 +0200
commitca5e947269608757a1af5295f5c77958de341fc3 (patch)
treed2e0fd2d5937a7ed3144289d6f85b86b1a0d1967 /src
parentbb42f7d438c088b18e953e38cb07d0cdf25f749a (diff)
making bot do check and add knight table
Diffstat (limited to 'src')
-rw-r--r--src/bot.cpp18
-rw-r--r--src/moves.cpp30
-rw-r--r--src/moves.hpp2
3 files changed, 48 insertions, 2 deletions
diff --git a/src/bot.cpp b/src/bot.cpp
index 8b9273e..3054b0d 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -10,8 +10,16 @@ int KNIGHT_VALUE = 200;
int BISHOP_VALUE = 300;
int ROOK_VALUE = 400;
int QUEEN_VALUE = 900;
+int CHECK = 300;
int MATE = 10000;
+int KNIGHT_TABLE[64] = {-50, -40, -30, -30, -30, -30, -40, -50, -40, -20, 0,
+ 0, 0, 0, -20, -40, -30, 0, 10, 15, 15, 10,
+ 0, -30, -30, 5, 15, 20, 20, 15, 5, -30, -30,
+ 0, 15, 20, 20, 15, 0, -30, -30, 5, 10, 15,
+ 15, 10, 5, -30, -40, -20, 0, 5, 5, 0, -20,
+ -40, -50, -40, -30, -30, -30, -30, -40, -50};
+
const int SEARCH_DEPTH = 3;
Move EngineGetBestMove(Game *b) {
@@ -92,8 +100,8 @@ float EvaluateBoardForWhite(Game *b) {
float score = 0;
bool isStaleMate = false;
-
GetLegalMoves(b);
+
if (b->state == WHITE_WON) {
return MATE;
}
@@ -104,7 +112,8 @@ float EvaluateBoardForWhite(Game *b) {
isStaleMate = true;
}
- for (Piece piece : b->pieces) {
+ for (int i = 0; i < 64; i++) {
+ Piece piece = b->pieces[i];
if (piece.type == NONE)
continue;
@@ -116,6 +125,7 @@ float EvaluateBoardForWhite(Game *b) {
break;
case KNIGHT:
value = KNIGHT_VALUE;
+ value += KNIGHT_TABLE[i];
break;
case BISHOP:
value = BISHOP_VALUE;
@@ -135,6 +145,10 @@ float EvaluateBoardForWhite(Game *b) {
else
score -= value;
}
+ bool isCheck = IsPieceTypeAttacked(b, KING, 1);
+ if (isCheck) {
+ score += CHECK;
+ }
if (isStaleMate) {
return 0;
}
diff --git a/src/moves.cpp b/src/moves.cpp
index 5401e8b..d7df595 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -123,6 +123,36 @@ std::vector<Move> GetPseudoLegalMoves(Game *b) {
return moves;
}
+bool IsSquareAttacked(Game *board, Position square, bool white) {
+ Game temp = *board;
+
+ // Generate moves for the attacking side
+ temp.turn = white;
+
+ auto moves = GetPseudoLegalMoves(&temp);
+
+ for (const Move &move : moves) {
+ if (move.To == square)
+ return true;
+ }
+
+ return false;
+}
+bool IsPieceTypeAttacked(Game *board, PieceType type, bool white) {
+ Game temp = *board;
+
+ // Generate moves for the attacking side
+ temp.turn = white;
+
+ auto moves = GetPseudoLegalMoves(&temp);
+
+ for (const Move &move : moves) {
+ if (temp.pieces[PositionToIndex(move.To)].type == type)
+ return true;
+ }
+
+ return false;
+}
std::vector<Move> GetLegalMoves(Game *b) {
std::vector<Move> moves = GetPseudoLegalMoves(b);
std::vector<Move> legalMove;
diff --git a/src/moves.hpp b/src/moves.hpp
index 4c6716e..592da3f 100644
--- a/src/moves.hpp
+++ b/src/moves.hpp
@@ -13,5 +13,7 @@ void GenerateSlidingMoves(Game *b, int from,
void GenerateKingMoves(Game *b, int from, std::vector<Move> &moves);
void GenerateKningtMoves(Game *b, int from, std::vector<Move> &moves);
void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves);
+bool IsSquareAttacked(Game *board, Position square, bool white);
+bool IsPieceTypeAttacked(Game *board, PieceType type, bool white);
#endif /* SRC_MOVES_H_ */