Skip to content

Commit

Permalink
Implemented check to see if a position is legal/valid, added setPiece…
Browse files Browse the repository at this point in the history
… and removePiece function to the Board class
  • Loading branch information
Dannyj1 committed Oct 8, 2024
1 parent dec48d5 commit 2db260c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#pragma once

#include <utility>

#include <cstdint>
#include <utility>

#include "bitwise.h"
#include "constants.h"
#include "macros.h"
#include "types.h"
Expand Down Expand Up @@ -220,4 +220,8 @@ inline uint64_t calculateKingAttacks(uint64_t bb) {
inline uint64_t kingAttacks(const uint8_t square) {
return kingAttacksTable[square];
}

inline uint64_t squareToBitboard(const uint8_t square) { return 1ULL << square; }

inline uint8_t bitboardToSquare(const uint64_t bb) { return bitscanForward(bb); }
} // namespace Zagreus
17 changes: 15 additions & 2 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@

#include "board.h"

#include "bitwise.h"

namespace Zagreus {
bool Board::isMoveLegal(Move move) const {
// TODO: Implement
template <PieceColor movedColor>
bool Board::isPositionLegal() const {
constexpr PieceColor opponentColor = !movedColor;
constexpr Piece king = movedColor == PieceColor::WHITE ? Piece::WHITE_KING : Piece::BLACK_KING;
const uint64_t kingBB = getBitboard<king>();
const uint8_t kingSquare = bitscanForward(kingBB);

return !getSquareAttackersByColor<opponentColor>(kingSquare);
}

template bool Board::isPositionLegal<PieceColor::WHITE>() const;
template bool Board::isPositionLegal<PieceColor::BLACK>() const;

uint64_t Board::getSquareAttackers(const uint8_t square) const {
using enum Piece;

Expand All @@ -42,4 +53,6 @@ uint64_t Board::getSquareAttackers(const uint8_t square) const {
| (bishopAttacks(square, occupied) & bishopsQueens)
| (rookAttacks(square, occupied) & rooksQueens);
}

void Board::makeMove(const Move& move) {}
} // namespace Zagreus
35 changes: 34 additions & 1 deletion src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,40 @@ class Board {
return ~occupied;
}

[[nodiscard]] bool isMoveLegal(Move move) const;
template <Piece piece>
void setPiece(uint8_t square) {
const uint64_t squareBB = squareToBitboard(square);

board[square] = piece;
bitboards[IDX(piece)] |= squareBB;
occupied |= squareBB;
colorBoards[IDX(pieceColor(piece))] |= squareBB;
}

void removePiece(uint8_t square) {
const uint64_t squareBB = squareToBitboard(square);
const Piece piece = board[square];

board[square] = Piece::EMPTY;
bitboards[IDX(piece)] &= ~squareBB;
occupied &= ~squareBB;
colorBoards[IDX(pieceColor(piece))] &= ~squareBB;
}

template <Piece piece>
void removePiece(uint8_t square) {
const uint64_t squareBB = squareToBitboard(square);

board[square] = Piece::EMPTY;
bitboards[IDX(piece)] &= ~squareBB;
occupied &= ~squareBB;
colorBoards[IDX(pieceColor(piece))] &= ~squareBB;
}

void makeMove(const Move& move);

template <PieceColor movedColor>
[[nodiscard]] bool isPositionLegal() const;

[[nodiscard]] uint64_t getSquareAttackers(uint8_t square) const;

Expand Down
2 changes: 1 addition & 1 deletion src/move_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void generatePawnMoves(const Board& board, MoveList& moves, const uint64_t genMa
// TODO: Handle promotions
// TODO: Move attacks to table lookup
constexpr Piece pawn = color == PieceColor::WHITE ? Piece::WHITE_PAWN : Piece::BLACK_PAWN;
constexpr PieceColor opponentColor = color == PieceColor::WHITE ? PieceColor::BLACK : PieceColor::WHITE;
constexpr PieceColor opponentColor = !color;

const uint64_t pawnBB = board.getBitboard<pawn>();
const uint64_t emptyBB = board.getEmptyBitboard();
Expand Down
22 changes: 17 additions & 5 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,28 @@ enum class PieceColor : uint8_t { WHITE, BLACK, EMPTY = 255 };

enum class Piece : uint8_t {
WHITE_PAWN,
WHITE_KNIGHT,
WHITE_BISHOP,
WHITE_ROOK,
WHITE_QUEEN,
WHITE_KING,
BLACK_PAWN,
WHITE_KNIGHT,
BLACK_KNIGHT,
WHITE_BISHOP,
BLACK_BISHOP,
WHITE_ROOK,
BLACK_ROOK,
WHITE_QUEEN,
BLACK_QUEEN,
WHITE_KING,
BLACK_KING,
EMPTY = 255
};

inline constexpr PieceColor operator!(const PieceColor color) {
return color == PieceColor::WHITE ? PieceColor::BLACK : PieceColor::WHITE;
}

inline constexpr PieceColor pieceColor(const Piece piece) {
return static_cast<PieceColor>(static_cast<uint8_t>(piece) / 2);
}

inline constexpr PieceType pieceType(const Piece piece) {
return static_cast<PieceType>(static_cast<uint8_t>(piece) % 2);
}

0 comments on commit 2db260c

Please sign in to comment.