chessmcu/Piece.hpp
2025-08-25 16:04:25 -04:00

172 lines
3.7 KiB
C++

// © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0.
#ifndef PIECE_HPP
#define PIECE_HPP
#pragma once
#include <cstdint>
#include <utility>
#include <string>
#include <memory>
#include <vector>
#include "Board.hpp"
class Board;
enum PieceType {
PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY
};
enum PieceColour {
PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY
};
enum Rank {
R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7
};
enum File {
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7
};
struct Square {
Rank rank;
File file;
bool isValid() const {
return rank >= 0 && rank < 8 && file >= 0 && file < 8;
}
};
struct Move {
Square from;
Square to;
};
class Piece {
private:
friend class Board;
protected:
PieceColour colour;
PieceType pieceType;
std::string pieceName;
char pieceSymbol;
bool hasMoved = false;
public:
Piece(PieceColour pColour) : colour(pColour) {
}
virtual ~Piece();
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const;
PieceColour getColour() const {
return colour;
}
std::string getPieceName() const {
return pieceName;
}
char getPieceSymbol() const {
return pieceSymbol;
}
PieceType getPieceType() const {
return pieceType;
}
bool checkIfMoved() const {
return hasMoved;
}
};
class King : public Piece {
friend class Board;
public:
King(PieceColour colour) : Piece(colour) {
pieceName = "King";
pieceSymbol = 'K';
pieceType = KING;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
bool checkForCheck() const {
return inCheck;
}
bool checkForCastle() const {
return canCastle;
}
protected:
bool canCastle = true;
bool inCheck = false;
};
class Rook : public Piece {
friend class Board;
public:
Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook";
pieceSymbol = 'R';
pieceType = ROOK;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Queen : public Piece {
friend class Board;
public:
Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen";
pieceSymbol = 'Q';
pieceType = QUEEN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Knight : public Piece {
friend class Board;
public:
Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight";
pieceSymbol = 'N';
pieceType = KNIGHT;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Bishop : public Piece {
friend class Board;
public:
Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop";
pieceSymbol = 'B';
pieceType = BISHOP;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Pawn : public Piece {
friend class Board;
public:
Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn";
pieceSymbol = 'P';
pieceType = PAWN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
#endif // PIECE_HPP