diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-27 20:14:54 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-27 20:14:54 +0200 |
| commit | 51d27e1ab9058c9e0fafb399e22edf7b38470510 (patch) | |
| tree | f0528298123fcb33eb426a5aa099afd2a8d00a90 /src | |
| parent | f1ded0290393709d437c6e149861ceb91d241c64 (diff) | |
adding uci
Diffstat (limited to 'src')
| -rw-r--r-- | src/board.cpp | 4 | ||||
| -rw-r--r-- | src/board.hpp | 6 | ||||
| -rw-r--r-- | src/bot.cpp | 12 | ||||
| -rw-r--r-- | src/bot.hpp | 6 | ||||
| -rw-r--r-- | src/fen.cpp | 2 | ||||
| -rw-r--r-- | src/fen.hpp | 2 | ||||
| -rw-r--r-- | src/main.cpp | 31 | ||||
| -rw-r--r-- | src/moves.cpp | 16 | ||||
| -rw-r--r-- | src/moves.hpp | 12 | ||||
| -rw-r--r-- | src/uci.cpp (renamed from src/repl.cpp) | 55 | ||||
| -rw-r--r-- | src/uci.hpp (renamed from src/repl.hpp) | 9 |
11 files changed, 96 insertions, 59 deletions
diff --git a/src/board.cpp b/src/board.cpp index 3c5f5ec..0698571 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -35,7 +35,7 @@ Piece createPiece(PieceType type, bool color, bool moved) { piece.moved = moved; return piece; } -void printBoard(Board *b) { +void printBoard(Game *b) { std::string line = " +------------------------+\n"; std::string whiteLetters = " a b c d e f g h \n"; std::string blackLetters = " h g f e d c b a \n"; @@ -100,7 +100,7 @@ void printBoard(Board *b) { std::printf("Board Eval: %f\n", EvaluateBoardForWhite(b)); } -void PlayMove(Move move, Board *b) { +void PlayMove(Move move, Game *b) { // TODO: add all fide behavior Piece piece = b->pieces[PositionToIndex(move.From)]; Piece piece2 = b->pieces[PositionToIndex(move.To)]; diff --git a/src/board.hpp b/src/board.hpp index e782be5..5e4e777 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -35,7 +35,7 @@ struct Position { bool operator==(const Position &) const = default; }; -struct Board { +struct Game { Piece pieces[64]; bool turn; // 1 white; 0 black std::string castle; @@ -52,9 +52,9 @@ struct Move { bool operator==(const Move &) const = default; }; -void printBoard(Board *b); +void printBoard(Game *b); Piece createPiece(PieceType type, bool color, bool moved = false); -void PlayMove(Move move, Board *b); +void PlayMove(Move move, Game *b); int PositionToIndex(Position i); #endif /* SRC_BOARD_H_ */ diff --git a/src/bot.cpp b/src/bot.cpp index e3a39f2..d0730d8 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -17,7 +17,7 @@ const int SEARCH_DEPTH = 3; int positions_consider = 0; -Move EngineGetBestMove(Board *b) { +Move EngineGetBestMove(Game *b) { auto moves = GetLegalMoves(b); if (moves.size() == 0) { assert(false && "Unhanled error zero legal moves for bot"); @@ -25,7 +25,7 @@ Move EngineGetBestMove(Board *b) { Move bestMove = moves[0]; float BestEval = (b->turn ? -INFINITY : INFINITY); for (Move move : moves) { - Board TestBoard = *b; + Game TestBoard = *b; PlayMove(move, &TestBoard); float alpha = -INFINITY; float beta = INFINITY; @@ -46,7 +46,7 @@ Move EngineGetBestMove(Board *b) { std::cout << "\nConsider: " << positions_consider << "\n"; return bestMove; } -float minimax(int depth, Board *b, float alpha, float beta) { +float minimax(int depth, Game *b, float alpha, float beta) { if (depth == 0) { return EvaluateBoardForWhite(b); } @@ -58,7 +58,7 @@ float minimax(int depth, Board *b, float alpha, float beta) { float bestEval = -INFINITY; for (Move move : moves) { - Board next = *b; + Game next = *b; PlayMove(move, &next); float eval = minimax(depth - 1, &next, alpha, beta); @@ -76,7 +76,7 @@ float minimax(int depth, Board *b, float alpha, float beta) { float BestEval = INFINITY; for (Move move : moves) { - Board next = *b; + Game next = *b; PlayMove(move, &next); float eval = minimax(depth - 1, &next, alpha, beta); @@ -92,7 +92,7 @@ float minimax(int depth, Board *b, float alpha, float beta) { } } -float EvaluateBoardForWhite(Board *b) { +float EvaluateBoardForWhite(Game *b) { positions_consider++; float score = 0; diff --git a/src/bot.hpp b/src/bot.hpp index 1403bc4..242fdfe 100644 --- a/src/bot.hpp +++ b/src/bot.hpp @@ -2,8 +2,8 @@ #define SRC_BOT_H_ #include "board.hpp" -Move EngineGetBestMove(Board *b); -float EvaluateBoardForWhite(Board *b); -float minimax(int depth, Board *b, float alpha, float beta); +Move EngineGetBestMove(Game *b); +float EvaluateBoardForWhite(Game *b); +float minimax(int depth, Game *b, float alpha, float beta); #endif /* SRC_BOT_H_ */ diff --git a/src/fen.cpp b/src/fen.cpp index fa8f289..21ea778 100644 --- a/src/fen.cpp +++ b/src/fen.cpp @@ -9,7 +9,7 @@ bool WHITE = true; bool BLACK = false; -void setBoardFen(std::string fen, Board *b) { +void setBoardFen(std::string fen, Game *b) { // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 int file = 0; int rank = 0; diff --git a/src/fen.hpp b/src/fen.hpp index 1bd7f80..406f5fe 100644 --- a/src/fen.hpp +++ b/src/fen.hpp @@ -5,5 +5,5 @@ #include "board.hpp" -void setBoardFen(std::string fen, Board *b); +void setBoardFen(std::string fen, Game *b); #endif /* SRC_FEN_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 555e728..7b7e6fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include "board.hpp" #include "fen.hpp" #include "moves.hpp" -#include "repl.hpp" +#include "uci.hpp" #include <cstdio> #include <cstdlib> #include <ctime> @@ -14,7 +14,7 @@ enum Mode { REPL, }; -void setAllPiecesToEmpty(Board *b) { +void setAllPiecesToEmpty(Game *b) { Piece EmptyPiece = { false, NONE, @@ -24,8 +24,8 @@ void setAllPiecesToEmpty(Board *b) { b->pieces[i] = EmptyPiece; }; }; -Board initBoard(string startingFEN) { - Board b; +Game initBoard(string startingFEN) { + Game b; b.enPassant = IndexToPosition(0); // default value b.canEnpassant = false; b.state = TURN; @@ -36,31 +36,16 @@ Board initBoard(string startingFEN) { setBoardFen(startingFEN, &b); return b; }; -#define askMode = false int main(int argc, char **argv) { srand(time(0)); - std::string startingFen = - "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; if (argc < 1 || argc > 2) { - printf("wrong arg count, use: ./chess [optinal starting fen]\n"); + printf("wrong arg count, use: ./chess [--repl for debugging]\n"); return 1; } - printf("arg count: %d\n", argc); - if (argc == 2) { - startingFen = argv[1]; - } - - Mode mode = REPL; - // ADD support for other modes here + if (argc > 1 && std::string(argv[1]) == "--repl") + return startREPL(); - Board b = initBoard(startingFen); - if (mode == REPL) { - int replExitCode = startREPL(&b); - return replExitCode; - } - if (mode == EXIT) { - return 0; - } + UciInit(); return 0; } diff --git a/src/moves.cpp b/src/moves.cpp index 9c7991a..5401e8b 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -6,7 +6,7 @@ #include "board.hpp" #include "moves.hpp" -void GenerateKnightMoves(Board *b, int from, std::vector<Move> &moves) { +void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves) { Piece knight = b->pieces[from]; if (knight.type != KNIGHT) { @@ -34,7 +34,7 @@ void GenerateKnightMoves(Board *b, int from, std::vector<Move> &moves) { moves.push_back({IndexToPosition(from), IndexToPosition(next)}); } }; -void GeneratePawnMoves(Board *b, int from, std::vector<Move> &moves) { +void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) { Piece pawn = b->pieces[from]; if (pawn.type != PAWN) { assert(false && "Calling generate pawn moves on non pawn"); @@ -83,7 +83,7 @@ void GeneratePawnMoves(Board *b, int from, std::vector<Move> &moves) { // the end } -std::vector<Move> GetPseudoLegalMoves(Board *b) { +std::vector<Move> GetPseudoLegalMoves(Game *b) { std::vector<Move> moves; moves.reserve(50); // almost all position dont have that many moves @@ -123,12 +123,12 @@ std::vector<Move> GetPseudoLegalMoves(Board *b) { return moves; } -std::vector<Move> GetLegalMoves(Board *b) { +std::vector<Move> GetLegalMoves(Game *b) { std::vector<Move> moves = GetPseudoLegalMoves(b); std::vector<Move> legalMove; for (Move move : moves) { bool isLegal = true; - Board testBoard = *b; + Game testBoard = *b; PlayMove(move, &testBoard); auto opponentResponse = GetPseudoLegalMoves(&testBoard); for (Move move : opponentResponse) { @@ -143,7 +143,7 @@ std::vector<Move> GetLegalMoves(Board *b) { if (legalMove.size() == 0) { bool isCheck = false; - Board testBoard = *b; + Game testBoard = *b; auto opponentResponse = GetPseudoLegalMoves(&testBoard); for (Move move : opponentResponse) { if (testBoard.pieces[PositionToIndex(move.To)].type == KING) { @@ -170,7 +170,7 @@ Position IndexToPosition(int i) { return {rank, file}; } -void GenerateSlidingMoves(Board *b, int from, +void GenerateSlidingMoves(Game *b, int from, const std::array<int, 4> &directions, std::vector<Move> &moves) { for (uint i = 0; i < directions.size(); i++) { @@ -205,7 +205,7 @@ void GenerateSlidingMoves(Board *b, int from, }; }; -void GenerateKingMoves(Board *b, int from, std::vector<Move> &moves) { +void GenerateKingMoves(Game *b, int from, std::vector<Move> &moves) { if (b->pieces[from].type != KING) { assert(false && "calling generate king moves on non king"); return; diff --git a/src/moves.hpp b/src/moves.hpp index a957d3a..4c6716e 100644 --- a/src/moves.hpp +++ b/src/moves.hpp @@ -4,14 +4,14 @@ #include "board.hpp" #include <vector> -std::vector<Move> GetLegalMoves(Board *b); -std::vector<Move> GetPseudoLegalMoves(Board *b); +std::vector<Move> GetLegalMoves(Game *b); +std::vector<Move> GetPseudoLegalMoves(Game *b); Position IndexToPosition(int i); -void GenerateSlidingMoves(Board *b, int from, +void GenerateSlidingMoves(Game *b, int from, const std::array<int, 4> &directions, std::vector<Move> &moves); -void GenerateKingMoves(Board *b, int from, std::vector<Move> &moves); -void GenerateKningtMoves(Board *b, int from, std::vector<Move> &moves); -void GeneratePawnMoves(Board *b, int from, std::vector<Move> &moves); +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); #endif /* SRC_MOVES_H_ */ diff --git a/src/repl.cpp b/src/uci.cpp index b936af0..debf886 100644 --- a/src/repl.cpp +++ b/src/uci.cpp @@ -1,6 +1,7 @@ -#include "repl.hpp" +#include "uci.hpp" #include "board.hpp" #include "bot.hpp" +#include "fen.hpp" #include "moves.hpp" #include <algorithm> #include <cctype> @@ -10,6 +11,7 @@ #include <print> #include <string> +using namespace std; bool isValidChessRank(char rank) { if (rank >= '1' && rank <= '8') { return true; @@ -24,7 +26,56 @@ bool isValidChessFile(char rank) { return false; } } -int startREPL(Board *b) { +static Game initBoard(string startingFEN) { + Game b; + b.enPassant = IndexToPosition(0); // default value + b.canEnpassant = false; + b.state = TURN; + b.turn = true; + b.castle = ""; + b.halfMoveClock = 0; + b.MoveClock = 0; + setBoardFen(startingFEN, &b); + return b; +}; +void UciInit() { + Game game = + initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + + string line; + + while (getline(cin, line)) { + string cmd = line; + + transform(cmd.begin(), cmd.end(), cmd.begin(), ::toupper); + + if (cmd == "QUIT") { + break; + } + if (cmd == "UCI") { + cout << "uciok\n"; + cout.flush(); + } else if (cmd == "ISREADY") { + cout << "readyok\n"; + cout.flush(); + } else if (cmd.starts_with("GO")) { + Move best = EngineGetBestMove(&game); + + cout << "bestmove "; + cout << (char)(best.From.file + 'a') + << 8 - best.From.rank + << (char)(best.To.file + 'a') << 8 - best.To.rank; + println(); + + cout.flush(); + } + } +} +int startREPL() { + + Game g = + initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + Game *b = &g; printBoard(b); while (true) { GetLegalMoves(b); diff --git a/src/repl.hpp b/src/uci.hpp index 5f457c4..9ef939d 100644 --- a/src/repl.hpp +++ b/src/uci.hpp @@ -1,12 +1,13 @@ -#ifndef SRC_REPL_H_ -#define SRC_REPL_H_ +#ifndef SRC_UCI_H_ +#define SRC_UCI_H_ #include "board.hpp" #include <vector> -int startREPL(Board *b); +int startREPL(); void PrintMoves(const std::vector<Move> &moves); bool isValidChessRank(char rank); bool isValidChessFile(char rank); -#endif /* SRC_REPL_H_ */ +void UciInit(); +#endif /* SRC_UCI_H_ */ |
