aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/board.cpp168
-rw-r--r--src/board.hpp17
-rw-r--r--src/fen.cpp138
-rw-r--r--src/fen.hpp7
-rw-r--r--src/main.cpp9
-rw-r--r--src/moves.hpp22
6 files changed, 193 insertions, 168 deletions
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 <print>
#include <string>
-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<unsigned char>(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<unsigned char>(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<unsigned char>(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 <cctype>
+#include <cstdio>
+#include <print>
+#include <string>
+
+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 <string>
+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 <algorithm>
#include <cctype>
#include <cstdint>
@@ -7,9 +9,6 @@
#include <string>
#include <vector>
-#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 <cstdint>
#include <vector>
-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<Move> GetLegalMoves(board *b);
Position IndexToPosition(int i);
void GenerateSlidingMoves(board *b, int from,