mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Add Piece and Board class
This commit is contained in:
@@ -2,5 +2,28 @@
|
||||
|
||||
Board::Board(int rows, int cols) :
|
||||
m_rows(rows),
|
||||
m_cols(cols),
|
||||
m_grid(rows, )
|
||||
m_cols(cols)
|
||||
{
|
||||
m_grid.resize(m_rows * m_cols);
|
||||
m_component = std::make_unique<ComponentManager>(m_rows * m_cols);
|
||||
}
|
||||
|
||||
Board::~Board() {
|
||||
|
||||
}
|
||||
|
||||
const int Board::getIndex(int row, int col) {
|
||||
return row * m_cols + col;
|
||||
}
|
||||
|
||||
bool Board::initialize() {
|
||||
m_grid[getIndex(0, 0)] = std::make_unique<Piece>(PlayerID::P1);
|
||||
m_grid[getIndex(0, 2)] = std::make_unique<Piece>(PlayerID::P1);
|
||||
m_grid[getIndex(6, 6)] = std::make_unique<Piece>(PlayerID::P2);
|
||||
m_grid[getIndex(5, 5)] = std::make_unique<Piece>(PlayerID::P2);
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Piece>& Board::at(int row, int col) {
|
||||
return m_grid[row * m_cols + col];
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "Piece.h"
|
||||
#include "ComponentManager.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
// 用 (row, col) 表示坐标,0-based
|
||||
@@ -14,11 +15,15 @@ struct Position {
|
||||
|
||||
class Board {
|
||||
private:
|
||||
std::vector<std::vector<std::unique_ptr<Piece>>> m_grid;
|
||||
//使用一维数组表示棋盘
|
||||
std::vector<std::unique_ptr<Piece>> m_grid;
|
||||
std::unique_ptr<ComponentManager> m_component;
|
||||
const int m_rows;
|
||||
const int m_cols;
|
||||
public:
|
||||
Board(int rows, int cols);
|
||||
~Board();
|
||||
|
||||
const int getIndex(int row, int col);
|
||||
std::unique_ptr<Piece>& at(int row, int col);
|
||||
bool initialize();
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "ConnectedComponentManager.h"
|
||||
#include "ComponentManager.h"
|
||||
|
||||
ConnectedComponentManager::ConnectedComponentManager(int numPieces)
|
||||
ComponentManager::ComponentManager(int numPieces)
|
||||
: m_totalPieces(numPieces) {
|
||||
//初始化数组大小 ROWS * COLS
|
||||
m_parent.resize(numPieces);
|
||||
@@ -14,14 +14,14 @@ ConnectedComponentManager::ConnectedComponentManager(int numPieces)
|
||||
}
|
||||
}
|
||||
|
||||
int ConnectedComponentManager::find(int pieceId) {
|
||||
int ComponentManager::find(int pieceId) {
|
||||
if (m_parent[pieceId] != pieceId) {
|
||||
m_parent[pieceId] = find(m_parent[pieceId]);
|
||||
}
|
||||
return m_parent[pieceId];
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::unite(int pieceId1, int pieceId2) {
|
||||
void ComponentManager::unite(int pieceId1, int pieceId2) {
|
||||
int root1 = find(pieceId1);
|
||||
int root2 = find(pieceId2);
|
||||
|
||||
@@ -49,14 +49,14 @@ void ConnectedComponentManager::unite(int pieceId1, int pieceId2) {
|
||||
addConnection(pieceId1, pieceId2);
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::addConnection(int pieceId1, int pieceId2) {
|
||||
void ComponentManager::addConnection(int pieceId1, int pieceId2) {
|
||||
//将元素放入邻接表
|
||||
m_adjacentList[pieceId1].insert(pieceId2);
|
||||
m_adjacentList[pieceId2].insert(pieceId1);
|
||||
|
||||
}
|
||||
|
||||
bool ConnectedComponentManager::disconnectFromNeighbor(int pieceId, int neighborId){
|
||||
bool ComponentManager::disconnectFromNeighbor(int pieceId, int neighborId){
|
||||
// 检查是否真的相连
|
||||
if (!areDirectlyConnected(pieceId, neighborId)) {
|
||||
return false;
|
||||
@@ -70,7 +70,7 @@ bool ConnectedComponentManager::disconnectFromNeighbor(int pieceId, int neighbor
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConnectedComponentManager::disconnectFromComponent(int pieceId) {
|
||||
bool ComponentManager::disconnectFromComponent(int pieceId) {
|
||||
int oldComponentId = find(pieceId);
|
||||
if (oldComponentId == -1) return false;
|
||||
|
||||
@@ -96,7 +96,7 @@ bool ConnectedComponentManager::disconnectFromComponent(int pieceId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::recomputeComponentsAfterDisconnection(int disconnectedPiece) {
|
||||
void ComponentManager::recomputeComponentsAfterDisconnection(int disconnectedPiece) {
|
||||
int oldComponentId = find(disconnectedPiece);
|
||||
if (oldComponentId == -1 || m_componentPieces[disconnectedPiece].size() <= 1) {
|
||||
return;
|
||||
@@ -110,7 +110,7 @@ void ConnectedComponentManager::recomputeComponentsAfterDisconnection(int discon
|
||||
handleComponentSplit(oldComponentId, remainingPieces);
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::handleComponentSplit(int oldComponentId, const std:: unordered_set<int>& remainingPieces ) {
|
||||
void ComponentManager::handleComponentSplit(int oldComponentId, const std:: unordered_set<int>& remainingPieces ) {
|
||||
std::unordered_set<int> visited;
|
||||
std::vector<std::unordered_set<int>> newComponents;
|
||||
|
||||
@@ -148,7 +148,7 @@ void ConnectedComponentManager::handleComponentSplit(int oldComponentId, const s
|
||||
}
|
||||
|
||||
|
||||
std::unordered_set<int> ConnectedComponentManager::bfsConnectedRegion(int startPiece, const std::unordered_set<int>& availablepieces) {
|
||||
std::unordered_set<int> ComponentManager::bfsConnectedRegion(int startPiece, const std::unordered_set<int>& availablepieces) {
|
||||
std::unordered_set<int> connectedRegion;
|
||||
std::queue<int> queue;
|
||||
|
||||
@@ -173,7 +173,7 @@ std::unordered_set<int> ConnectedComponentManager::bfsConnectedRegion(int startP
|
||||
return connectedRegion;
|
||||
}
|
||||
|
||||
int ConnectedComponentManager::createNewComponent(int rootPiece) {
|
||||
int ComponentManager::createNewComponent(int rootPiece) {
|
||||
m_parent[rootPiece] = rootPiece;
|
||||
m_rank[rootPiece] = 0;
|
||||
m_componentPieces[rootPiece] = {rootPiece};
|
||||
@@ -181,11 +181,11 @@ int ConnectedComponentManager::createNewComponent(int rootPiece) {
|
||||
return rootPiece;
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::selectComponentByPiece(int pieceId) {
|
||||
void ComponentManager::selectComponentByPiece(int pieceId) {
|
||||
m_selectedComponentId = find(pieceId);
|
||||
}
|
||||
|
||||
const std::unordered_set<int>& ConnectedComponentManager::getSelectedComponent() const {
|
||||
const std::unordered_set<int>& ComponentManager::getSelectedComponent() const {
|
||||
static std::unordered_set<int> emptySet;
|
||||
if (m_selectedComponentId == -1 ||
|
||||
m_componentPieces.find(m_selectedComponentId) == m_componentPieces.end()) {
|
||||
@@ -195,29 +195,29 @@ const std::unordered_set<int>& ConnectedComponentManager::getSelectedComponent()
|
||||
return m_componentPieces.at(m_selectedComponentId);
|
||||
}
|
||||
|
||||
int ConnectedComponentManager::getComponentId(int pieceId) const {
|
||||
int ComponentManager::getComponentId(int pieceId) const {
|
||||
auto it = m_pieceToComponent.find(pieceId);
|
||||
return (it != m_pieceToComponent.end()) ? it->second : -1;
|
||||
}
|
||||
|
||||
const std::unordered_set<int>& ConnectedComponentManager::getPiecesInComponent(int componentId) const {
|
||||
const std::unordered_set<int>& ComponentManager::getPiecesInComponent(int componentId) const {
|
||||
static std::unordered_set<int> emptySet;
|
||||
auto it = m_componentPieces.find(componentId);
|
||||
|
||||
return (it != m_componentPieces.end()) ? it->second : emptySet;
|
||||
}
|
||||
|
||||
bool ConnectedComponentManager::areConnected(int pieceId1, int pieceId2) {
|
||||
bool ComponentManager::areConnected(int pieceId1, int pieceId2) {
|
||||
return find(pieceId1) == find(pieceId2);
|
||||
}
|
||||
|
||||
const std::unordered_set<int>& ConnectedComponentManager::getPieceConnections(int pieceId) const {
|
||||
const std::unordered_set<int>& ComponentManager::getPieceConnections(int pieceId) const {
|
||||
static std::unordered_set<int> emptySet;
|
||||
if (pieceId < 0 || pieceId >= m_totalPieces) return emptySet;
|
||||
return m_adjacentList[pieceId];
|
||||
}
|
||||
|
||||
bool ConnectedComponentManager::areDirectlyConnected(int pieceId1, int pieceId2) const {
|
||||
bool ComponentManager::areDirectlyConnected(int pieceId1, int pieceId2) const {
|
||||
if (pieceId1 < 0 || pieceId1 >= m_totalPieces ||
|
||||
pieceId2 < 0 || pieceId2 >= m_totalPieces) {
|
||||
return false;
|
||||
@@ -225,11 +225,11 @@ bool ConnectedComponentManager::areDirectlyConnected(int pieceId1, int pieceId2)
|
||||
return m_adjacentList[pieceId1].find(pieceId2) != m_adjacentList[pieceId1].end();
|
||||
}
|
||||
|
||||
void ConnectedComponentManager::clearSelection() {
|
||||
void ComponentManager::clearSelection() {
|
||||
m_selectedComponentId = -1;
|
||||
}
|
||||
|
||||
std:: unordered_map<int, std::unordered_set<int>> ConnectedComponentManager::getAllComponents() const {
|
||||
std:: unordered_map<int, std::unordered_set<int>> ComponentManager::getAllComponents() const {
|
||||
return m_componentPieces;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
|
||||
class ConnectedComponentManager {
|
||||
class ComponentManager {
|
||||
private:
|
||||
// 并查集的父节点数组,使用连续内存存储
|
||||
std::vector<int> m_parent;
|
||||
@@ -35,7 +35,7 @@ private:
|
||||
|
||||
public:
|
||||
// 构造函数:初始化指定数量的棋子
|
||||
explicit ConnectedComponentManager(int numPieces);
|
||||
explicit ComponentManager(int numPieces);
|
||||
// 查找操作(带路径压缩)
|
||||
int find(int pieceId);
|
||||
// 合并两个棋子所在的组件(按秩合并)
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
Game::Game()
|
||||
{
|
||||
|
||||
m_board = std::make_unique<Board>();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,12 +19,15 @@ void Game::cleanup() {
|
||||
|
||||
|
||||
bool Game::initialize() {
|
||||
|
||||
// 初始化游戏特定资源(棋盘、棋子等)
|
||||
if (!m_board->initialize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 初始化游戏特定资源(棋盘、棋子等)
|
||||
|
||||
// ...
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
//#include "Board.h"
|
||||
#include "Board.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class Game {
|
||||
private:
|
||||
|
||||
std::unique_ptr<Board> m_board;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Piece.h"
|
||||
|
||||
Piece::Piece(PlayerColer color) : m_color(color) {
|
||||
Piece::Piece(PlayerID ID) : m_owner(ID) {
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ Piece::~Piece() {
|
||||
|
||||
}
|
||||
|
||||
PlayerColer Piece::getPlayerColor() const {
|
||||
return m_color;
|
||||
PlayerID Piece::getPieceOwner() const {
|
||||
return m_owner;
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
enum class PlayerColer {
|
||||
WHITE,
|
||||
BLACK
|
||||
enum class PlayerID {
|
||||
P1,
|
||||
P2
|
||||
};
|
||||
|
||||
|
||||
class Piece {
|
||||
private:
|
||||
PlayerColer m_color;
|
||||
PlayerID m_owner;
|
||||
public:
|
||||
Piece(PlayerColer color);
|
||||
Piece(PlayerID ID);
|
||||
~Piece();
|
||||
PlayerColer getPlayerColor() const;
|
||||
PlayerID getPieceOwner() const;
|
||||
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user