dev: Continue to write the chess logic, working through it bit by bit
This commit is contained in:
parent
d6eb6b69c2
commit
ba64e2bcf3
4 changed files with 96 additions and 15 deletions
25
Board.cpp
25
Board.cpp
|
|
@ -13,8 +13,29 @@ Board::Board() {
|
||||||
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);
|
||||||
for(int j = 0; j < 8; j++) {
|
switch(i) {
|
||||||
boardGrid[i][j] = std::make_unique<Piece>(PIECE_EMPTY);
|
case 0:
|
||||||
|
// white back rank
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// white pawn rank
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
case 4:
|
||||||
|
case 5:
|
||||||
|
//empty squares
|
||||||
|
for(int j = 0; j <= 8; j++) {
|
||||||
|
boardGrid[i][j] = std::make_unique<Piece>(PIECE_EMPTY);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
// black pawn rank
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
// black back rank
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,12 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Board();
|
Board();
|
||||||
~Board();
|
virtual ~Board();
|
||||||
void setupInitialPosition();
|
void setupInitialPosition();
|
||||||
Piece *getPieceAt(int x, int y) const;
|
Piece *getPieceAt(Square square) const;
|
||||||
void movePiece(Square from, Square to);
|
void movePiece(Square from, Square to);
|
||||||
bool isInBounds(int x, int y) const;
|
bool isInBounds(Square square) const;
|
||||||
|
bool isEmpty(Square square) const;
|
||||||
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
||||||
void deserializeBoard(uint64_t incomingBoard);
|
void deserializeBoard(uint64_t incomingBoard);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
52
Piece.cpp
52
Piece.cpp
|
|
@ -7,8 +7,56 @@
|
||||||
|
|
||||||
#include "Piece.hpp"
|
#include "Piece.hpp"
|
||||||
|
|
||||||
std::vector<std::pair<int, int>> King::getLegalMoves(int x, int y, const Board& board) const {
|
Piece::~Piece() {
|
||||||
std::vector<std::pair<int, int>> moveList;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// because the Piece class is instantiated in another class using
|
||||||
|
// unique_ptr, non-pure virtual functions apparently *must*
|
||||||
|
// have definitions, or the linker freaks out
|
||||||
|
// so this is just a stub that does nothing. it doesn't matter
|
||||||
|
// because only the derived class versions should be called.
|
||||||
|
|
||||||
|
std::vector<Move> Piece::getLegalMoves(const Square &from, const Board &board) const {
|
||||||
|
std::vector<Move> moveList;
|
||||||
|
return moveList;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
|
||||||
|
std::vector<Move> moveList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const {
|
||||||
|
std::vector<Move> moveList;
|
||||||
|
const int directions[8][2] = {
|
||||||
|
{-1, 0}, // Up
|
||||||
|
{1, 0}, // Down
|
||||||
|
{0, -1}, // Left
|
||||||
|
{0, 1} // Right
|
||||||
|
};
|
||||||
|
for (auto& dir : directions) {
|
||||||
|
int r = from.rank + dir[0];
|
||||||
|
int f = from.file + dir[1];
|
||||||
|
|
||||||
|
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||||
|
const auto& target = board[r][f];
|
||||||
|
|
||||||
|
if (!target) {
|
||||||
|
moves.push_back({from,
|
||||||
|
{r, f}});
|
||||||
|
} else if (target->color != this->color) {
|
||||||
|
moves.push_back({from,
|
||||||
|
{r, f}});
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
r += dir[0];
|
||||||
|
f += dir[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// calculate legal moves somehow
|
// calculate legal moves somehow
|
||||||
return moveList;
|
return moveList;
|
||||||
|
|
|
||||||
25
Piece.hpp
25
Piece.hpp
|
|
@ -9,7 +9,7 @@
|
||||||
#define PIECE_HPP
|
#define PIECE_HPP
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -18,13 +18,19 @@ class Board;
|
||||||
|
|
||||||
enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY };
|
enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY };
|
||||||
enum PieceColour { PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY };
|
enum PieceColour { PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY };
|
||||||
enum Rank { R1 = 1, R2 = 2, R3 = 3, R4 = 4, R5 = 5, R6 = 6, R7 = 7, R8 = 8 };
|
enum Rank { R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7 };
|
||||||
enum File { A, B, C, D, E, F, G, H };
|
enum File { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7 };
|
||||||
struct Square {
|
struct Square {
|
||||||
Rank rank;
|
Rank rank;
|
||||||
File file;
|
File file;
|
||||||
|
bool isValid() const {
|
||||||
|
return rank >= 0 && rank < 8 && file >= 0 && file < 8;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
struct Move {
|
||||||
|
Square from;
|
||||||
|
Square to;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Piece {
|
class Piece {
|
||||||
protected:
|
protected:
|
||||||
PieceColour colour;
|
PieceColour colour;
|
||||||
|
|
@ -32,15 +38,20 @@ protected:
|
||||||
public:
|
public:
|
||||||
// methods
|
// methods
|
||||||
Piece(PieceColour pColour) : colour(pColour) {}
|
Piece(PieceColour pColour) : colour(pColour) {}
|
||||||
virtual ~Piece() {}
|
virtual ~Piece();
|
||||||
virtual std::vector<std::pair<int, int>> getLegalMoves(int x, int y, const Board &board) const;
|
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const;
|
||||||
PieceColour getColour() const { return colour; }
|
PieceColour getColour() const { return colour; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class King : public Piece {
|
class King : public Piece {
|
||||||
public:
|
public:
|
||||||
King(PieceColour colour) : Piece(colour) {}
|
King(PieceColour colour) : Piece(colour) {}
|
||||||
std::vector<std::pair<int, int>> getLegalMoves(int x, int y, const Board &board) const override;
|
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Rook : Public Piece {
|
||||||
|
Rook(PieceColour colour) : Piece(colour) {}
|
||||||
|
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
|
||||||
};
|
};
|
||||||
#endif // PIECE_HPP
|
#endif // PIECE_HPP
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue