dev: started to write Board constructor, added all derived Pieces

This commit is contained in:
A.M. Rowsell 2025-08-04 02:54:55 -04:00
commit ea62d03680
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
3 changed files with 85 additions and 18 deletions

View file

@ -7,33 +7,71 @@
#include "Board.hpp" #include "Board.hpp"
/*
* This is how the chessboard is organized in memory
* This handy dandy chart should help me from getting totally
* confused!
*
* FILE 0 1 2 3 4 5 6 7
* 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'
* +-----+-----+-----+-----+-----+-----+-----+-----+
* RANK 0 | A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1 | boardGrid[0][0] to boardGrid[0][7]
* RANK 1 | A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2 | boardGrid[1][0] to boardGrid[1][7]
* RANK 2 | A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3 | ...
* RANK 3 | A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4 |
* RANK 4 | A5 | B5 | C5 | D5 | E5 | F5 | G5 | H5 |
* RANK 5 | A6 | B6 | C6 | D6 | E6 | F6 | G6 | H6 |
* RANK 6 | A7 | B7 | C7 | D7 | E7 | F7 | G7 | H7 |
* RANK 7 | A8 | B8 | C8 | D8 | E8 | F8 | G8 | H8 | boardGrid[7][0] to boardGrid[7][7]
* +-----+-----+-----+-----+-----+-----+-----+-----+
*
*/
Board::Board() { Board::Board() {
// set up the board grid with smart pointers // set up the board grid with smart pointers
// initialize each square with a Piece, set to PIECE_EMPTY // initialize each square with a Piece, set to PIECE_EMPTY
boardGrid.resize(8); boardGrid.resize(8);
for(int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
boardGrid[i].resize(8); boardGrid[i].resize(8);
switch(i) { switch (i) {
case 0: case 0:
// white back rank // white back rank
boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE);
boardGrid[i][1] = std::make_unique<Knight>(PIECE_WHITE);
boardGrid[i][2] = std::make_unique<Bishop>(PIECE_WHITE);
boardGrid[i][3] = std::make_unique<Queen>(PIECE_WHITE);
boardGrid[i][4] = std::make_unique<King>(PIECE_WHITE);
boardGrid[i][5] = std::make_unique<Bishop>(PIECE_WHITE);
boardGrid[i][6] = std::make_unique<Knight>(PIECE_WHITE);
boardGrid[i][7] = std::make_unique<Rook>(PIECE_WHITE);
break; break;
case 1: case 1:
// white pawn rank // white pawn rank
for (int j = 0; j <= 8; j++) {
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE);
}
break; break;
case 2: case 2:
case 3: case 3:
case 4: case 4:
case 5: case 5:
//empty squares continue;
for(int j = 0; j <= 8; j++) {
boardGrid[i][j] = std::make_unique<Piece>(PIECE_EMPTY);
}
break;
case 6: case 6:
// black pawn rank // black pawn rank
for (int j = 0; j <= 8; j++) {
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK);
}
break; break;
case 7: case 7:
// black back rank // black back rank
boardGrid[i][0] = std::make_unique<Rook>(PIECE_BLACK);
boardGrid[i][1] = std::make_unique<Knight>(PIECE_BLACK);
boardGrid[i][2] = std::make_unique<Bishop>(PIECE_BLACK);
boardGrid[i][3] = std::make_unique<Queen>(PIECE_BLACK);
boardGrid[i][4] = std::make_unique<King>(PIECE_BLACK);
boardGrid[i][5] = std::make_unique<Bishop>(PIECE_BLACK);
boardGrid[i][6] = std::make_unique<Knight>(PIECE_BLACK);
boardGrid[i][7] = std::make_unique<Rook>(PIECE_BLACK);
default: default:
break; break;
} }
@ -47,6 +85,7 @@ void Board::setupInitialPosition() {
return; return;
} }
void Board::movePiece(Square from, Square to) { void Board::movePiece(Square from, Square to) {
return; return;
} }

View file

@ -24,12 +24,22 @@ std::vector<Move> Piece::getLegalMoves(const Square &from, const Board &board) c
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[8][2] = {
{-1, 0}, // Up
{-1, -1}, // up-left
{-1, 1}, // up-right
{1, 0}, // Down
{1, -1}, // down-left
{1, 1}, // down-right
{0, -1}, // Left
{0, 1} // Right
};
} }
std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[8][2] = { const int directions[4][2] = {
{-1, 0}, // Up {-1, 0}, // Up
{1, 0}, // Down {1, 0}, // Down
{0, -1}, // Left {0, -1}, // Left
@ -58,6 +68,5 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
} }
} }
// calculate legal moves somehow
return moveList; return moveList;
} }

View file

@ -44,14 +44,33 @@ public:
}; };
class King : public Piece { class King : public Piece {
public: King(PieceColour colour) : Piece(colour) {}
King(PieceColour colour) : Piece(colour) {} virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Rook : Public Piece { class Rook : public Piece {
Rook(PieceColour colour) : Piece(colour) {} Rook(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override; virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
}; };
class Queen : public Piece {
Queen(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
};
class Knight: public Piece {
Knight(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
};
class Bishop : public Piece {
Bishop(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
};
class Pawn : public Piece {
Pawn(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
};
#endif // PIECE_HPP #endif // PIECE_HPP