From 7609c9d8e614d02c7ad532171c361f012857e3d8 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 11:08:43 +0200 Subject: adding good readme --- src/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 5be1194..57b7077 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "board.hpp" @@ -44,5 +45,24 @@ int main(int argc, char **argv) { printBoard(&b); auto moves = GetLegalMoves(&b); PrintMoves(moves); + while (true) { + std::cout << "> "; + std::string command; + std::cin >> command; + + transform(command.begin(), command.end(), command.begin(), ::toupper); + if (command.length() != 4) { + std::cout << "Unknown Command, use EXIT to exit"; + std::println(); + continue; + } + if (command == "EXIT") { + std::println(); + std::cout << "Exiting...\n"; + return 0; + } + std::cout << command; + std::println(); + } return EXIT_SUCCESS; } -- cgit v1.2.3 From 78ea1d6464483de6d9e0148da77a4c1c705a48ea Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 12:46:35 +0200 Subject: adding move repl --- src/main.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 57b7077..ccec7a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -13,6 +15,20 @@ Piece NonePiece = { }; #include +bool isValidChessRank(char rank) { + if (rank >= '1' && rank <= '8') { + return true; + } else { + return false; + } +} +bool isValidChessFile(char rank) { + if (rank >= 'A' && rank <= 'H') { + return true; + } else { + return false; + } +} void PrintMoves(const std::vector &moves) { std::cout << "Moves (" << moves.size() << "):\n"; @@ -61,7 +77,40 @@ int main(int argc, char **argv) { std::cout << "Exiting...\n"; return 0; } - std::cout << command; + if (!isValidChessFile(command[0])) { + std::cout << "ERROR: Expected valid file at first place"; + std::println(); + continue; + } + + if (!isValidChessRank(command[1])) { + std::cout << "ERROR: Expected valid rank at second place"; + std::println(); + continue; + } + + if (!isValidChessFile(command[2])) { + std::cout << "ERROR: Expected valid file at third place"; + std::println(); + continue; + } + + if (!isValidChessRank(command[3])) { + std::cout << "ERROR: Expected valid rank at four place"; + std::println(); + continue; + } + + // we know its a valid chess pos + uint8_t fromFile = command[0] - 'A'; + uint8_t fromRank = command[1] - '0'; + uint8_t toFile = command[2] - 'A'; + uint8_t toRank = command[3] - '0'; + std::cout << fromFile << "\n"; + std::cout << fromRank << "\n"; + std::cout << toFile << "\n"; + std::cout << toRank << "\n"; + Move move = {fromRank, fromFile, toRank, toFile}; std::println(); } return EXIT_SUCCESS; -- cgit v1.2.3 From 3b231aa281e16319f8bed37da49e53dd6b950650 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 13:35:36 +0200 Subject: fixing --- src/main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index ccec7a1..a508042 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,6 +65,7 @@ int main(int argc, char **argv) { std::cout << "> "; std::string command; std::cin >> command; + std::cout << command << "\n"; transform(command.begin(), command.end(), command.begin(), ::toupper); if (command.length() != 4) { @@ -106,10 +107,10 @@ int main(int argc, char **argv) { uint8_t fromRank = command[1] - '0'; uint8_t toFile = command[2] - 'A'; uint8_t toRank = command[3] - '0'; - std::cout << fromFile << "\n"; - std::cout << fromRank << "\n"; - std::cout << toFile << "\n"; - std::cout << toRank << "\n"; + std::cout << static_cast(fromFile) << "\n"; + std::cout << static_cast(fromRank) << "\n"; + std::cout << static_cast(toFile) << "\n"; + std::cout << static_cast(toRank) << "\n"; Move move = {fromRank, fromFile, toRank, toFile}; std::println(); } -- cgit v1.2.3 From 1db141c6c0d87cde4270d50b661162273ef6a9f5 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 13:54:49 +0200 Subject: adding move chenging to repl --- src/main.cpp | 36 +++++++++++++++++++++++------------- src/moves.hpp | 4 ++++ 2 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index a508042..bea30b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ +#include #include #include #include #include #include #include +#include #include "board.hpp" #include "moves.hpp" @@ -39,8 +41,7 @@ void PrintMoves(const std::vector &moves) { } } int main(int argc, char **argv) { - std::string startingFen = - "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/8/RNBQKBNR w KQkq - 0 1"; if (argc < 1 || argc > 2) { printf("wrong arg count, use: ./chess [optinal starting fen]\n"); return 1; @@ -59,8 +60,6 @@ int main(int argc, char **argv) { }; setBoardFen(startingFen, &b); printBoard(&b); - auto moves = GetLegalMoves(&b); - PrintMoves(moves); while (true) { std::cout << "> "; std::string command; @@ -103,15 +102,26 @@ int main(int argc, char **argv) { } // we know its a valid chess pos - uint8_t fromFile = command[0] - 'A'; - uint8_t fromRank = command[1] - '0'; - uint8_t toFile = command[2] - 'A'; - uint8_t toRank = command[3] - '0'; - std::cout << static_cast(fromFile) << "\n"; - std::cout << static_cast(fromRank) << "\n"; - std::cout << static_cast(toFile) << "\n"; - std::cout << static_cast(toRank) << "\n"; - Move move = {fromRank, fromFile, toRank, toFile}; + int fromFile = command[0] - 'A'; + int fromRank = '8' - command[1]; + int toFile = command[2] - 'A'; + int toRank = '8' - command[3]; + std::cout << fromFile << "\n"; + std::cout << fromRank << "\n"; + std::cout << toFile << "\n"; + std::cout << toRank << "\n"; + Move move = { + {static_cast(fromRank), static_cast(fromFile)}, + {static_cast(toRank), static_cast(toFile)}}; + + auto moves = GetLegalMoves(&b); + if (std::ranges::find(moves, move) != moves.end()) { + std::cout << "Move is legal!\n"; + } else { + std::cout << "Move is not legal!\n"; + PrintMoves(moves); + continue; + } std::println(); } return EXIT_SUCCESS; diff --git a/src/moves.hpp b/src/moves.hpp index 7e95910..5f893ca 100644 --- a/src/moves.hpp +++ b/src/moves.hpp @@ -8,11 +8,15 @@ struct Position { uint8_t rank; uint8_t file; + + bool operator==(const Position &) const = default; }; struct Move { Position From; Position To; + + bool operator==(const Move &) const = default; }; enum MoveResult { -- cgit v1.2.3 From c04f179b9902532a8d5db0c2ebd98460a70fc652 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 14:10:48 +0200 Subject: making repl work --- src/board.cpp | 168 +++++++++------------------------------------------------- src/board.hpp | 17 +++++- src/fen.cpp | 138 +++++++++++++++++++++++++++++++++++++++++++++++ src/fen.hpp | 7 +++ src/main.cpp | 9 ++-- src/moves.hpp | 22 -------- 6 files changed, 193 insertions(+), 168 deletions(-) create mode 100644 src/fen.cpp create mode 100644 src/fen.hpp (limited to 'src/main.cpp') diff --git a/src/board.cpp b/src/board.cpp index 5c30d75..e8fa787 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -5,9 +5,6 @@ #include #include -bool WHITE = true; -bool BLACK = false; - const char toChar(pieceType type) { switch (type) { case NONE: @@ -43,153 +40,40 @@ void printBoard(board *b) { std::cout << letter; std::cout << line; - for (int rank = 0; rank < 8; rank++) { - std::printf("%d |", 8 - rank); - for (int file = 0; file < 8; file++) { - Piece piece = b->pieces[rank * 8 + file]; - char symbol = toChar(piece.type); - if (piece.color == BLACK) { - symbol = std::tolower(static_cast(symbol)); + if (b->turn) { + for (int rank = 0; rank < 8; rank++) { + std::printf("%d |", 8 - rank); + for (int file = 0; file < 8; file++) { + Piece piece = b->pieces[rank * 8 + file]; + char symbol = toChar(piece.type); + if (piece.color == false) { + symbol = std::tolower(static_cast(symbol)); + } + std::printf(" %c", symbol); + } + std::printf(" | %d\n", 8 - rank); + } + } else { + for (int rank = 7; rank >= 0; rank--) { + std::printf("%d |", 8 - rank); + for (int file = 7; file >= 0; file--) { + Piece piece = b->pieces[rank * 8 + file]; + char symbol = toChar(piece.type); + if (piece.color == false) { + symbol = std::tolower(static_cast(symbol)); + } + std::printf(" %c", symbol); } - std::printf(" %c", symbol); + std::printf(" | %d\n", 8 - rank); } - std::printf(" | %d\n", 8 - rank); } std::cout << line; std::cout << letter; std::println(); - std::println("Turn: {}", b->turn == WHITE ? "White" : "Black"); + std::println("Turn: {}", b->turn ? "White" : "Black"); std::println("Castling: {}", b->castle); } -void setBoardFen(std::string fen, board *b) { - // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 - int file = 0; - int rank = 0; - - std::string castle = ""; - - enum parserState { - POSITION, - TURN, - CASTLE, - ENPASSANT, - CLOCK1, - CLOCK2, - }; - parserState state = POSITION; - - for (int i = 0; i < fen.length(); i++) { - std::printf("state: %d\n", state); - std::printf("doing: %c\n", fen[i]); - if (fen[i] == ' ') { - if (state == POSITION) { - state = TURN; - continue; - } - if (state == TURN) { - state = CASTLE; - continue; - } - if (state == CASTLE) { - state = ENPASSANT; - continue; - } - if (state == ENPASSANT) { - state = CLOCK1; - continue; - } - if (state == CLOCK1) { - state = CLOCK2; - continue; - } - } - if (state == ENPASSANT) { - continue; - } - if (state == CASTLE) { - if (fen[i] == '-') { - castle = "-"; - continue; - } - if (toupper(fen[i]) == 'K') { - castle += fen[i]; - continue; - } - if (toupper(fen[i]) == 'Q') { - castle += fen[i]; - continue; - } - } - if (state == TURN) { - if (toupper(fen[i]) == 'W') { - b->turn = WHITE; - continue; - } - if (toupper(fen[i]) == 'B') { - b->turn = BLACK; - continue; - } - } - if (state == POSITION) { - if (isdigit(fen[i])) { - int move = fen[i] - '0'; - file += move; - if (file > 8) { - std::println("Fatal: file was to big"); - return; // malformed rank - } - continue; - } - if (fen[i] == '/') { - file = 0; - rank++; - if (rank > 8) { - std::println("Fatal: rank was to big"); - return; // malformed rank - } - continue; - } - if (toupper(fen[i]) == 'N') { - Piece piece = createPiece(KNIGHT, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'R') { - Piece piece = createPiece(ROOK, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'Q') { - Piece piece = createPiece(QUEEN, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'B') { - Piece piece = createPiece(BISHOP, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'P') { - Piece piece = createPiece(PAWN, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'K') { - Piece piece = createPiece(KING, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - } - break; - } - b->castle = castle.empty() ? "-" : castle; -} +void PlayMove(Move move, board *b) { return; } diff --git a/src/board.hpp b/src/board.hpp index 3bf581a..f085326 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -29,7 +29,22 @@ struct board { uint16_t MoveClock; }; +struct Position { + uint8_t rank; + uint8_t file; + + bool operator==(const Position &) const = default; +}; + +struct Move { + Position From; + Position To; + + bool operator==(const Move &) const = default; +}; void printBoard(board *b); void setBoardFen(std::string fen, board *b); Piece createPiece(pieceType type, bool color, bool moved = false); -#endif /* SRC_BOARD_H_ */ + +void PlayMove(Move move, board *b); // TODO implement +#endif /* SRC_BOARD_H_ */ diff --git a/src/fen.cpp b/src/fen.cpp new file mode 100644 index 0000000..8244425 --- /dev/null +++ b/src/fen.cpp @@ -0,0 +1,138 @@ +#include "board.hpp" +#include +#include +#include +#include + +bool WHITE = true; +bool BLACK = false; + +void setBoardFen(std::string fen, board *b) { + // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 + int file = 0; + int rank = 0; + + std::string castle = ""; + + enum parserState { + POSITION, + TURN, + CASTLE, + ENPASSANT, + CLOCK1, + CLOCK2, + }; + parserState state = POSITION; + + for (int i = 0; i < fen.length(); i++) { + std::printf("state: %d\n", state); + std::printf("doing: %c\n", fen[i]); + if (fen[i] == ' ') { + if (state == POSITION) { + state = TURN; + continue; + } + if (state == TURN) { + state = CASTLE; + continue; + } + if (state == CASTLE) { + state = ENPASSANT; + continue; + } + if (state == ENPASSANT) { + state = CLOCK1; + continue; + } + if (state == CLOCK1) { + state = CLOCK2; + continue; + } + } + if (state == ENPASSANT) { + continue; + } + if (state == CASTLE) { + if (fen[i] == '-') { + castle = "-"; + continue; + } + if (toupper(fen[i]) == 'K') { + castle += fen[i]; + continue; + } + if (toupper(fen[i]) == 'Q') { + castle += fen[i]; + continue; + } + } + if (state == TURN) { + if (toupper(fen[i]) == 'W') { + b->turn = WHITE; + continue; + } + if (toupper(fen[i]) == 'B') { + b->turn = BLACK; + continue; + } + } + if (state == POSITION) { + if (isdigit(fen[i])) { + int move = fen[i] - '0'; + file += move; + if (file > 8) { + std::println("Fatal: file was to big"); + return; // malformed rank + } + continue; + } + if (fen[i] == '/') { + file = 0; + rank++; + if (rank > 8) { + std::println("Fatal: rank was to big"); + return; // malformed rank + } + continue; + } + if (toupper(fen[i]) == 'N') { + Piece piece = createPiece(KNIGHT, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + if (toupper(fen[i]) == 'R') { + Piece piece = createPiece(ROOK, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + if (toupper(fen[i]) == 'Q') { + Piece piece = createPiece(QUEEN, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + if (toupper(fen[i]) == 'B') { + Piece piece = createPiece(BISHOP, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + if (toupper(fen[i]) == 'P') { + Piece piece = createPiece(PAWN, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + if (toupper(fen[i]) == 'K') { + Piece piece = createPiece(KING, isupper(fen[i])); + b->pieces[rank * 8 + file] = piece; + file++; + continue; + } + } + break; + } + b->castle = castle.empty() ? "-" : castle; +} diff --git a/src/fen.hpp b/src/fen.hpp new file mode 100644 index 0000000..c1a723e --- /dev/null +++ b/src/fen.hpp @@ -0,0 +1,7 @@ +#ifndef SRC_BOARD_H_ +#define SRC_BOARD_H_ + +#include "board.hpp" +#include +void setBoardFen(std::string fen, board *b); +#endif /* SRC_BOARD_H_ */ diff --git a/src/main.cpp b/src/main.cpp index bea30b0..d59195d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include "board.hpp" +#include "moves.hpp" #include #include #include @@ -7,9 +9,6 @@ #include #include -#include "board.hpp" -#include "moves.hpp" - Piece NonePiece = { false, NONE, @@ -123,6 +122,10 @@ int main(int argc, char **argv) { continue; } std::println(); + PlayMove(move, &b); + b.MoveClock++; + b.turn = !b.turn; + printBoard(&b); } return EXIT_SUCCESS; } diff --git a/src/moves.hpp b/src/moves.hpp index 5f893ca..d99fc68 100644 --- a/src/moves.hpp +++ b/src/moves.hpp @@ -2,30 +2,8 @@ #define SRC_MOVES_H_ #include "board.hpp" -#include #include -struct Position { - uint8_t rank; - uint8_t file; - - bool operator==(const Position &) const = default; -}; - -struct Move { - Position From; - Position To; - - bool operator==(const Move &) const = default; -}; - -enum MoveResult { - SUCCES, - FRIENDLY_PIECE, - PIECE_IN_WAY, -}; - -MoveResult PlayMove(Move move, board *b); // TODO implement std::vector GetLegalMoves(board *b); Position IndexToPosition(int i); void GenerateSlidingMoves(board *b, int from, -- cgit v1.2.3 From 54704ebd1b03b3f33bcd7d2905a8538f5a842f0f Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 15:45:51 +0200 Subject: i am stupid --- src/board.cpp | 8 +++++++- src/board.hpp | 1 - src/fen.cpp | 5 +++-- src/fen.hpp | 10 ++++------ src/main.cpp | 12 +++++++----- 5 files changed, 21 insertions(+), 15 deletions(-) (limited to 'src/main.cpp') diff --git a/src/board.cpp b/src/board.cpp index cbafcc6..201bd3a 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -1,11 +1,14 @@ #include "board.hpp" +#include #include #include #include #include #include +#include const char toChar(pieceType type) { + switch (type) { case NONE: return '.'; @@ -22,7 +25,7 @@ const char toChar(pieceType type) { case KING: return 'K'; default: - return '?'; + std::unreachable(); } } @@ -80,6 +83,9 @@ void printBoard(board *b) { std::println(); std::println("Turn: {}", b->turn ? "White" : "Black"); + + assert(b->castle != ""); + std::println("Castling: {}", b->castle); } diff --git a/src/board.hpp b/src/board.hpp index f085326..23f89ad 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -43,7 +43,6 @@ struct Move { bool operator==(const Move &) const = default; }; void printBoard(board *b); -void setBoardFen(std::string fen, board *b); Piece createPiece(pieceType type, bool color, bool moved = false); void PlayMove(Move move, board *b); // TODO implement diff --git a/src/fen.cpp b/src/fen.cpp index 8244425..a6773c2 100644 --- a/src/fen.cpp +++ b/src/fen.cpp @@ -1,3 +1,4 @@ +#include "fen.hpp" #include "board.hpp" #include #include @@ -94,13 +95,13 @@ void setBoardFen(std::string fen, board *b) { return; // malformed rank } continue; - } + }; if (toupper(fen[i]) == 'N') { Piece piece = createPiece(KNIGHT, isupper(fen[i])); b->pieces[rank * 8 + file] = piece; file++; continue; - } + }; if (toupper(fen[i]) == 'R') { Piece piece = createPiece(ROOK, isupper(fen[i])); b->pieces[rank * 8 + file] = piece; diff --git a/src/fen.hpp b/src/fen.hpp index 55d47ae..19f88e2 100644 --- a/src/fen.hpp +++ b/src/fen.hpp @@ -1,11 +1,9 @@ -#ifndef SRC_BOARD_H_ -#define SRC_BOARD_H_ +#ifndef SRC_FEN_H_ +#define SRC_FEN_H_ #include #include "board.hpp" -using namespace std; - -void setBoardFen(string fen, board *b); -#endif /* SRC_BOARD_H_ */ +void setBoardFen(std::string fen, board *b); +#endif /* SRC_FEN_H_ */ diff --git a/src/main.cpp b/src/main.cpp index d59195d..ba5e57c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "board.hpp" +#include "fen.hpp" #include "moves.hpp" #include #include @@ -66,16 +67,17 @@ int main(int argc, char **argv) { std::cout << command << "\n"; transform(command.begin(), command.end(), command.begin(), ::toupper); - if (command.length() != 4) { - std::cout << "Unknown Command, use EXIT to exit"; - std::println(); - continue; - } if (command == "EXIT") { std::println(); std::cout << "Exiting...\n"; return 0; } + + if (command.length() != 4) { + std::cout << "Unknown Command, use EXIT to exit"; + std::println(); + continue; + } if (!isValidChessFile(command[0])) { std::cout << "ERROR: Expected valid file at first place"; std::println(); -- cgit v1.2.3 From 809063825a9b0cc1edd01816ec78de55b0d7683e Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 16:08:46 +0200 Subject: improing code --- src/board.cpp | 5 +++-- src/main.cpp | 23 +++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src/main.cpp') diff --git a/src/board.cpp b/src/board.cpp index 201bd3a..081cc7d 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -11,7 +11,7 @@ const char toChar(pieceType type) { switch (type) { case NONE: - return '.'; + return '.'; case PAWN: return 'P'; case KNIGHT: @@ -25,7 +25,8 @@ const char toChar(pieceType type) { case KING: return 'K'; default: - std::unreachable(); + assert(false && "Unknown piece"); + return '?'; } } diff --git a/src/main.cpp b/src/main.cpp index ba5e57c..7a6e7f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include "board.hpp" -#include "fen.hpp" +#include "fen.hpp" #include "moves.hpp" #include #include @@ -10,11 +10,6 @@ #include #include -Piece NonePiece = { - false, - NONE, - false, -}; #include bool isValidChessRank(char rank) { @@ -31,6 +26,16 @@ bool isValidChessFile(char rank) { return false; } } +void setAllPiecesToEmpty(board *b) { + Piece NonePiece = { + false, + NONE, + false, + }; + for (int i = 0; i < 64; i++) { + b->pieces[i] = NonePiece; + }; +} void PrintMoves(const std::vector &moves) { std::cout << "Moves (" << moves.size() << "):\n"; @@ -41,7 +46,8 @@ void PrintMoves(const std::vector &moves) { } } int main(int argc, char **argv) { - std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/8/RNBQKBNR w KQkq - 0 1"; + 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"); return 1; @@ -55,9 +61,6 @@ int main(int argc, char **argv) { b.castle = ""; b.halfMoveClock = 0; b.MoveClock = 0; - for (int i = 0; i < 64; i++) { - b.pieces[i] = NonePiece; - }; setBoardFen(startingFen, &b); printBoard(&b); while (true) { -- cgit v1.2.3 From a46558e0aab30eb88ac3cafac8d490174bd4785f Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Jul 2026 16:33:03 +0200 Subject: making everything more strict + fixing the warnings/error --- .github/workflows/cmake-single-platform.yml | 5 ++++- CMakeLists.txt | 19 +++++++++++++++++++ src/board.cpp | 11 +++++------ src/board.hpp | 2 +- src/fen.cpp | 2 +- src/main.cpp | 2 +- src/moves.cpp | 5 +++-- 7 files changed, 34 insertions(+), 12 deletions(-) (limited to 'src/main.cpp') diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index c02bed4..844556e 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -31,10 +31,13 @@ jobs: env: CC: gcc-14 CXX: g++-14 - run: cmake -B build -S . + run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Build env: CC: gcc-14 CXX: g++-14 run: cmake --build build + + - name: Run clang-tidy + run: clang-tidy src/*.cpp -p build diff --git a/CMakeLists.txt b/CMakeLists.txt index 3782845..3f5a42f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,3 +14,22 @@ file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.cpp src/*.h src/*.hpp) add_executable(${PROJECT_NAME} ${SOURCE_FILES}) target_include_directories(${PROJECT_NAME} PRIVATE src) + +if (MSVC) + target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX) +else() + target_compile_options(${PROJECT_NAME} PRIVATE + -Wall + -Wextra + -Wpedantic + -Werror + ) +endif() + +target_compile_options(${PROJECT_NAME} PRIVATE + -fsanitize=address,undefined +) + +target_link_options(${PROJECT_NAME} PRIVATE + -fsanitize=address,undefined +) diff --git a/src/board.cpp b/src/board.cpp index 081cc7d..172a9be 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -5,13 +5,11 @@ #include #include #include -#include - -const char toChar(pieceType type) { +char toChar(pieceType type) { switch (type) { case NONE: - return '.'; + return '.'; case PAWN: return 'P'; case KNIGHT: @@ -25,7 +23,8 @@ const char toChar(pieceType type) { case KING: return 'K'; default: - assert(false && "Unknown piece"); + std::cout << "\n" << type << "\n"; + assert(false && "Unknown piece" && type); return '?'; } } @@ -90,4 +89,4 @@ void printBoard(board *b) { std::println("Castling: {}", b->castle); } -void PlayMove(Move move, board *b) { return; } +// void PlayMove(Move move, board *b) { return; } diff --git a/src/board.hpp b/src/board.hpp index 23f89ad..afdb938 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -13,7 +13,7 @@ enum pieceType { KING, }; -const char toChar(pieceType type); +char toChar(pieceType type); struct Piece { bool color; diff --git a/src/fen.cpp b/src/fen.cpp index a6773c2..9c3154c 100644 --- a/src/fen.cpp +++ b/src/fen.cpp @@ -25,7 +25,7 @@ void setBoardFen(std::string fen, board *b) { }; parserState state = POSITION; - for (int i = 0; i < fen.length(); i++) { + for (uint i = 0; i < fen.length(); i++) { std::printf("state: %d\n", state); std::printf("doing: %c\n", fen[i]); if (fen[i] == ' ') { diff --git a/src/main.cpp b/src/main.cpp index 7a6e7f4..261c19d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,7 +127,7 @@ int main(int argc, char **argv) { continue; } std::println(); - PlayMove(move, &b); + // PlayMove(move, &b); // todo implement b.MoveClock++; b.turn = !b.turn; printBoard(&b); diff --git a/src/moves.cpp b/src/moves.cpp index 2ff8ed6..f02dfbe 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "board.hpp" #include "moves.hpp" @@ -11,7 +12,7 @@ std::vector GetLegalMoves(board *b) { constexpr std::array rook_Moves{-1, 1, 8, -8}; constexpr std::array bishop_Moves{-9, 9, -7, 7}; - for (int i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++) { + for (uint i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++) { Piece piece = b->pieces[i]; if (piece.type == NONE) { continue; @@ -42,7 +43,7 @@ Position IndexToPosition(int i) { void GenerateSlidingMoves(board *b, int from, const std::array &directions, std::vector &moves) { - for (int i = 0; i < directions.size(); i++) { + for (uint i = 0; i < directions.size(); i++) { int direction = directions[i]; int i2 = from; -- cgit v1.2.3