diff --git a/source/main.cpp b/source/main.cpp index 609c828..f8fa160 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,6 +4,7 @@ #include "movegen.h" #include "perft.h" #include "evaluate.h" +#include "search.h" int main() { initLeaperAttacks(); @@ -11,5 +12,7 @@ int main() { Board board; + SearchPosition(board, 3); + return 0; } \ No newline at end of file diff --git a/source/search.cpp b/source/search.cpp new file mode 100644 index 0000000..7240dda --- /dev/null +++ b/source/search.cpp @@ -0,0 +1,51 @@ +#include "search.h" +#include "movegen.h" +#include "evaluate.h" + +int NegaMax(Board &board, int depth) { + if (depth == 0) return Evaluate(board); + + GenerateMoves(board, board.sideToMove); + + int max = -50000000; + + for (Move move : board.moveList) { + Board copy = board; + board.MakeMove(move); + int score = -NegaMax(board, depth - 1); + board = copy; + + if (score > max) { + max = score; + } + } + + return max; +} + +Move NegaMaxHandler(Board &board, int depth) { + Move bestMove = Move(); + + GenerateMoves(board, board.sideToMove); + + int max = -50000000; + + for (Move move : board.moveList) { + Board copy = board; + board.MakeMove(move); + int score = -NegaMax(board, depth - 1); + board = copy; + + if (score > max) { + max = score; + bestMove = move; + } + } + + return bestMove; +} + +void SearchPosition(Board &board, int depth) { + std::cout << "best move: "; + NegaMaxHandler(board, depth).PrintMove(); +} \ No newline at end of file diff --git a/source/search.h b/source/search.h new file mode 100644 index 0000000..bd9cafe --- /dev/null +++ b/source/search.h @@ -0,0 +1,6 @@ +#pragma once +#include "board.h" + +int NegaMax(Board &board, int depth); +Move NegaMaxHandler(Board &board, int depth); +void SearchPosition(Board &board, int depth); \ No newline at end of file