first version of stickfosh, simply evaluates a
Some checks failed
tagged-release / Tagged Release (push) Has been cancelled

position based on the material (no AB-pruning)
This commit is contained in:
Karma Riuk 2025-02-05 17:11:09 +01:00
parent 982f933698
commit 97b3eeb984
3 changed files with 108 additions and 4 deletions

View File

@ -1,12 +1,11 @@
#include "board.hpp"
#include "perft.hpp"
#include "stickfosh.hpp"
#include <iostream>
int main(int argc, char* argv[]) {
std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
// Board b = Board::setup_fen_position(pos);
perft();
std::string move = search(pos, 4);
std::cout << move << std::endl;
return 0;
}

96
cpp/src/stickfosh.cpp Normal file
View File

@ -0,0 +1,96 @@
#include "stickfosh.hpp"
#include "pieces/piece.hpp"
#include "threadpool.hpp"
#include <future>
#include <map>
static int INFINITY = std::numeric_limits<int>::max();
std::string search(std::string pos, int depth) {
Board b = Board::setup_fen_position(pos);
ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves();
std::map<std::string, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert(
{move.to_string(), pool.enqueue(minimax, tmp_board, depth - 1)}
);
}
std::string best_move;
int best_eval = -INFINITY;
for (auto& [move, future] : futures) {
int eval = future.get();
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
return best_move;
}
int minimax(const Board& b, int depth) {
if (b.is_checkmate_for(b.white_to_play ? White : Black))
return -INFINITY;
if (depth == 0)
return eval(b);
std::vector<Move> moves = b.all_legal_moves();
int best_evaluation = 0;
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -minimax(tmp_board, depth - 1);
best_evaluation = std::max(best_evaluation, tmp_eval);
}
return best_evaluation;
}
static int PawnValue = 100;
static int KnightValue = 300;
static int BishopValue = 320;
static int RookValue = 500;
static int QueenValue = 900;
int count_material(const Board& b, int8_t colour) {
int ret = 0;
for (int i = 0; i < 64; i++) {
if (b.colour_at(i) == colour)
switch (b.squares[i] & 0b111) {
case Piece::Pawn:
ret += PawnValue;
break;
case Piece::Knigt:
ret += KnightValue;
break;
case Piece::Bishop:
ret += BishopValue;
break;
case Piece::Rook:
ret += RookValue;
break;
case Piece::Queen:
ret += QueenValue;
break;
}
}
return ret;
}
int eval(const Board& b) {
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);
int evaluation = white_eval - black_eval;
int perspective = b.white_to_play ? 1 : -1;
return perspective * evaluation;
}

9
cpp/src/stickfosh.hpp Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "board.hpp"
#include <string>
std::string search(std::string, int);
int minimax(const Board&, int);
int eval(const Board&);