init board with pieces

This commit is contained in:
Didictateur 2025-10-13 13:40:48 +02:00
parent edb429ce33
commit a3855da7d5
8 changed files with 88 additions and 34 deletions

View file

@ -17,6 +17,9 @@ public:
int getWidth() const { return width; } int getWidth() const { return width; }
int getHeight() const { return height; } int getHeight() const { return height; }
Cell& getCell(int x, int y) { return grid[y][x]; } 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>(piece);
}
}; };
} // namespace engine } // namespace engine

View file

@ -1,14 +1,14 @@
#ifndef CELL_HPP #ifndef CELL_HPP
#define CELL_HPP #define CELL_HPP
#include "piece.hpp"
#include <optional> #include <optional>
#include <memory>
namespace engine { namespace engine {
struct PieceId;
struct Cell { struct Cell {
std::optional<PieceId> pieceId; std::optional<std::shared_ptr<Piece>> pieceId;
}; };
} // namespace engine } // namespace engine

View file

@ -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

View file

@ -4,32 +4,16 @@
#include <optional> #include <optional>
#include "piece.hpp" #include "piece.hpp"
#include "move.hpp" #include "move.hpp"
#include "board.hpp"
namespace engine { namespace engine {
struct Cell {
std::optional<PieceId> pieceId;
// additional flags: trap, fog, etc.
};
class GameState { class GameState {
private:
Board board;
public: public:
GameState(); 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<Cell> cells_;
}; };
} // namespace engine } // namespace engine

View file

@ -10,22 +10,31 @@ namespace engine {
struct Coord { int x; int y; }; struct Coord { int x; int y; };
using PieceId = int; enum class MoveActionKind {
Move,
enum class MoveActionKind { Move, Capture, Create, Remove, SetFlag }; Capture,
Create,
Remove,
SetFlag
};
struct MoveAction { struct MoveAction {
MoveActionKind kind; MoveActionKind kind;
PieceId piece; // 0 for create
Coord from; Coord from;
Coord to; Coord to;
std::optional<int> extra; std::optional<int> extra;
}; };
enum class MoveType { Normal, Capture, Castling, Promotion, Multi, CardEffect }; enum class MoveType {
Normal,
Capture,
Castling,
Promotion,
Multi,
CardEffect
};
struct Move { struct Move {
PieceId actor = 0;
std::vector<MoveAction> actions; std::vector<MoveAction> actions;
MoveType type = MoveType::Normal; MoveType type = MoveType::Normal;
int playerId = -1; int playerId = -1;

View file

@ -9,10 +9,6 @@ namespace engine {
class MoveGenerator { class MoveGenerator {
public: public:
MoveGenerator(); MoveGenerator();
// generate moves for a specific piece; pieceId == 0 -> all pieces for player
std::vector<Move> generateForPlayer(const GameState& state, int playerId) const;
std::vector<Move> generateForPiece(const GameState& state, PieceId pieceId) const;
}; };
} // namespace engine } // namespace engine

View file

@ -7,7 +7,13 @@
namespace engine { namespace engine {
enum class MoveStatus { Ok, Illegal, BlockedByCard, OutOfBounds, RequiresConfirmation }; enum class MoveStatus {
Ok,
Illegal,
BlockedByCard,
OutOfBounds,
RequiresConfirmation
};
struct MoveResult { struct MoveResult {
MoveStatus status = MoveStatus::Ok; MoveStatus status = MoveStatus::Ok;

View file

@ -3,10 +3,30 @@
namespace engine { namespace engine {
enum class PieceColor {
WHITE,
BLACK
};
enum class PieceType {
PAWN,
KNIGHT,
BISHOP,
ROOK,
QUEEN,
KING
};
class Piece { class Piece {
private: private:
PieceColor color;
PieceType type;
public: public:
Piece(PieceColor c, PieceType t) : color(c), type(t) {}
PieceColor getColor() const { return color; }
PieceType getType() const { return type; }
}; };
} // namespace engine } // namespace engine