From a3855da7d5c179f2ad05efd650072c9d96cd1f7a Mon Sep 17 00:00:00 2001 From: Didictateur Date: Mon, 13 Oct 2025 13:40:48 +0200 Subject: [PATCH] init board with pieces --- engine/core/board.hpp | 3 +++ engine/core/cell.hpp | 6 +++--- engine/core/game_state.cpp | 36 ++++++++++++++++++++++++++++++++++ engine/core/game_state.hpp | 24 ++++------------------- engine/core/move.hpp | 21 ++++++++++++++------ engine/core/move_generator.hpp | 4 ---- engine/core/move_result.hpp | 8 +++++++- engine/core/piece.hpp | 20 +++++++++++++++++++ 8 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 engine/core/game_state.cpp diff --git a/engine/core/board.hpp b/engine/core/board.hpp index 41caedb..e4e7994 100644 --- a/engine/core/board.hpp +++ b/engine/core/board.hpp @@ -17,6 +17,9 @@ public: int getWidth() const { return width; } int getHeight() const { return height; } Cell& getCell(int x, int y) { return grid[y][x]; } + void setPiece(int x, int y, const Piece& piece) { + grid[y][x].pieceId = std::make_shared(piece); + } }; } // namespace engine diff --git a/engine/core/cell.hpp b/engine/core/cell.hpp index 23b8d1b..cbfcc3f 100644 --- a/engine/core/cell.hpp +++ b/engine/core/cell.hpp @@ -1,14 +1,14 @@ #ifndef CELL_HPP #define CELL_HPP +#include "piece.hpp" #include +#include namespace engine { -struct PieceId; - struct Cell { - std::optional pieceId; + std::optional> pieceId; }; } // namespace engine diff --git a/engine/core/game_state.cpp b/engine/core/game_state.cpp new file mode 100644 index 0000000..e226fab --- /dev/null +++ b/engine/core/game_state.cpp @@ -0,0 +1,36 @@ +# include "game_state.hpp" + +namespace engine { + +GameState::GameState() : board(8, 8) { + + // white pieces + board.setPiece(0, 0, Piece(PieceColor::WHITE, PieceType::ROOK)); + board.setPiece(1, 0, Piece(PieceColor::WHITE, PieceType::KNIGHT)); + board.setPiece(2, 0, Piece(PieceColor::WHITE, PieceType::BISHOP)); + board.setPiece(3, 0, Piece(PieceColor::WHITE, PieceType::QUEEN)); + board.setPiece(4, 0, Piece(PieceColor::WHITE, PieceType::KING)); + board.setPiece(5, 0, Piece(PieceColor::WHITE, PieceType::BISHOP)); + board.setPiece(6, 0, Piece(PieceColor::WHITE, PieceType::KNIGHT)); + board.setPiece(7, 0, Piece(PieceColor::WHITE, PieceType::ROOK)); + + for (int x = 0; x < 8; ++x) { + board.setPiece(x, 1, Piece(PieceColor::WHITE, PieceType::PAWN)); + } + + // black pieces + board.setPiece(0, 7, Piece(PieceColor::BLACK, PieceType::ROOK)); + board.setPiece(1, 7, Piece(PieceColor::BLACK, PieceType::KNIGHT)); + board.setPiece(2, 7, Piece(PieceColor::BLACK, PieceType::BISHOP)); + board.setPiece(3, 7, Piece(PieceColor::BLACK, PieceType::QUEEN)); + board.setPiece(4, 7, Piece(PieceColor::BLACK, PieceType::KING)); + board.setPiece(5, 7, Piece(PieceColor::BLACK, PieceType::BISHOP)); + board.setPiece(6, 7, Piece(PieceColor::BLACK, PieceType::KNIGHT)); + board.setPiece(7, 7, Piece(PieceColor::BLACK, PieceType::ROOK)); + + for (int x = 0; x < 8; ++x) { + board.setPiece(x, 6, Piece(PieceColor::BLACK, PieceType::PAWN)); + } +} + +} // namespace engine \ No newline at end of file diff --git a/engine/core/game_state.hpp b/engine/core/game_state.hpp index 12922ea..e06859f 100644 --- a/engine/core/game_state.hpp +++ b/engine/core/game_state.hpp @@ -4,32 +4,16 @@ #include #include "piece.hpp" #include "move.hpp" +#include "board.hpp" namespace engine { -struct Cell { - std::optional pieceId; - // additional flags: trap, fog, etc. -}; - class GameState { +private: + Board board; + public: GameState(); - - int width() const; - int height() const; - bool isValid(const Coord& c) const; - - const Cell& cellAt(const Coord& c) const; - Cell& cellAt(const Coord& c); - - // snapshot / clone - GameState clone() const; - -private: - int w_ = 8; - int h_ = 8; - std::vector cells_; }; } // namespace engine diff --git a/engine/core/move.hpp b/engine/core/move.hpp index 191c0c9..a20beef 100644 --- a/engine/core/move.hpp +++ b/engine/core/move.hpp @@ -10,22 +10,31 @@ namespace engine { struct Coord { int x; int y; }; -using PieceId = int; - -enum class MoveActionKind { Move, Capture, Create, Remove, SetFlag }; +enum class MoveActionKind { + Move, + Capture, + Create, + Remove, + SetFlag +}; struct MoveAction { MoveActionKind kind; - PieceId piece; // 0 for create Coord from; Coord to; std::optional extra; }; -enum class MoveType { Normal, Capture, Castling, Promotion, Multi, CardEffect }; +enum class MoveType { + Normal, + Capture, + Castling, + Promotion, + Multi, + CardEffect +}; struct Move { - PieceId actor = 0; std::vector actions; MoveType type = MoveType::Normal; int playerId = -1; diff --git a/engine/core/move_generator.hpp b/engine/core/move_generator.hpp index 7806062..a431b58 100644 --- a/engine/core/move_generator.hpp +++ b/engine/core/move_generator.hpp @@ -9,10 +9,6 @@ namespace engine { class MoveGenerator { public: MoveGenerator(); - - // generate moves for a specific piece; pieceId == 0 -> all pieces for player - std::vector generateForPlayer(const GameState& state, int playerId) const; - std::vector generateForPiece(const GameState& state, PieceId pieceId) const; }; } // namespace engine diff --git a/engine/core/move_result.hpp b/engine/core/move_result.hpp index e2959c7..5abe684 100644 --- a/engine/core/move_result.hpp +++ b/engine/core/move_result.hpp @@ -7,7 +7,13 @@ namespace engine { -enum class MoveStatus { Ok, Illegal, BlockedByCard, OutOfBounds, RequiresConfirmation }; +enum class MoveStatus { + Ok, + Illegal, + BlockedByCard, + OutOfBounds, + RequiresConfirmation +}; struct MoveResult { MoveStatus status = MoveStatus::Ok; diff --git a/engine/core/piece.hpp b/engine/core/piece.hpp index b2cf9e4..89b20b3 100644 --- a/engine/core/piece.hpp +++ b/engine/core/piece.hpp @@ -3,10 +3,30 @@ namespace engine { +enum class PieceColor { + WHITE, + BLACK +}; + +enum class PieceType { + PAWN, + KNIGHT, + BISHOP, + ROOK, + QUEEN, + KING +}; + class Piece { private: + PieceColor color; + PieceType type; public: + Piece(PieceColor c, PieceType t) : color(c), type(t) {} + + PieceColor getColor() const { return color; } + PieceType getType() const { return type; } }; } // namespace engine