Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libData/) # Library: Core data layer
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libCore/) # Library: Core game logic
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libNetwork/) # Library: Network layer networking
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libGameNet/) # Library: Application layer networking
Expand Down
8 changes: 4 additions & 4 deletions src/gui/GameWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ void GameWidget::setCurrentPlayerText() {
}

void GameWidget::setGameStateText() {
static const std::map<app::GameStatus, std::string> message{{app::GameStatus::Idle, "Idle"},
{app::GameStatus::Waiting, "Waiting for Player"},
{app::GameStatus::Active, "Active"},
{app::GameStatus::Done, "Game Finished"}};
static const std::map<GameStatus, std::string> message{{GameStatus::Idle, "Idle"},
{GameStatus::Ready, "Waiting for Player"},
{GameStatus::Active, "Active"},
{GameStatus::Done, "Game Finished"}};
assert(message.contains(m_game.status()));
m_statusLabel->setText(QString::fromStdString(message.at(m_game.status())));
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace go::gui {
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
// Setup Window
setWindowTitle("Go Game");
setWindowFlags(windowFlags() | Qt::Tool | Qt::WindowStaysOnTopHint);
buildLayout();
}

Expand Down
14 changes: 7 additions & 7 deletions src/gui/boardRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void BoardRenderer::drawBackground(QPainter& painter) const {
painter.restore();
}

void BoardRenderer::drawStone(QPainter& painter, Id x, Id y, const Board::Value player) const {
void BoardRenderer::drawStone(QPainter& painter, unsigned x, unsigned y, const Board::Stone player) const {
if (!isReady()) {
return;
}
Expand All @@ -113,22 +113,22 @@ void BoardRenderer::drawStone(QPainter& painter, Id x, Id y, const Board::Value
const int drawY = static_cast<int>((m_coordStart - m_drawStepPx) + y * m_stoneSize);
const QRect dest{drawX, drawY, static_cast<int>(m_stoneSize), static_cast<int>(m_stoneSize)};

const auto& texture = (player == Board::Value::Black) ? m_scaledBlack : m_scaledWhite;
const auto& texture = (player == Board::Stone::Black) ? m_scaledBlack : m_scaledWhite;
painter.drawImage(dest, texture);
}

void BoardRenderer::drawStones(QPainter& painter, const Board& board) const {
for (Id i = 0; i != board.size(); ++i) {
for (Id j = 0; j != board.size(); ++j) {
if (board.getAt({i, j}) != Board::Value::Empty) {
drawStone(painter, i, j, board.getAt({i, j}));
for (unsigned i = 0; i != board.size(); ++i) {
for (unsigned j = 0; j != board.size(); ++j) {
if (board.get({i, j}) != Board::Stone::Empty) {
drawStone(painter, i, j, board.get({i, j}));
}
}
}
}

bool BoardRenderer::pixelToCoord(int pX, int pY, Coord& coord) const {
Id x, y;
unsigned x, y;
if (pixelToCoord(pX, x) && pixelToCoord(pY, y)) {
coord = {x, y};
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/gui/boardRenderer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "core/board.hpp"
#include "core/types.hpp"
#include "data/board.hpp"
#include "data/player.hpp"

#include <QImage>
#include <QPainter>
Expand All @@ -28,7 +28,7 @@ class BoardRenderer {
//! Draw all stones given a board.
void drawStones(QPainter& painter, const Board& board) const;
//! Draw a single stone at a given index.
void drawStone(QPainter& painter, Id x, Id y, Board::Value player) const;
void drawStone(QPainter& painter, unsigned x, unsigned y, Board::Stone player) const;

//! Transforms pixel value to board coordinate.
bool pixelToCoord(int px, unsigned& coord) const;
Expand Down
14 changes: 2 additions & 12 deletions src/libApp/gameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ void GameServer::onNetworkEvent(gameNet::SessionId sessionId, const gameNet::Cli
}

void GameServer::onGameDelta(const GameDelta& delta) {
const auto toGameNetCoord = [](Coord c) -> gameNet::Coord { return gameNet::Coord{c.x, c.y}; };
const auto toGameNetCoords = [](const std::vector<Coord>& coords) -> std::vector<gameNet::Coord> {
std::vector<gameNet::Coord> res;
res.reserve(coords.size());
for (const auto& c: coords) {
res.emplace_back(gameNet::Coord{c.x, c.y});
}
return res;
};

gameNet::ServerAction action = gameNet::ServerAction::Pass;
switch (delta.action) {
case GameAction::Place:
Expand All @@ -123,8 +113,8 @@ void GameServer::onGameDelta(const GameDelta& delta) {
.turn = delta.moveId,
.seat = delta.player == Player::Black ? gameNet::Seat::Black : gameNet::Seat::White,
.action = action,
.coord = delta.coord.has_value() ? std::optional<gameNet::Coord>(toGameNetCoord(*delta.coord)) : std::nullopt,
.captures = toGameNetCoords(delta.captures),
.coord = delta.coord,
.captures = delta.captures,
.next = delta.nextPlayer == Player::Black ? gameNet::Seat::Black : gameNet::Seat::White,
.status = delta.gameActive ? gameNet::GameStatus::Active : gameNet::GameStatus::Draw,
};
Expand Down
12 changes: 3 additions & 9 deletions src/libApp/include/app/position.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#pragma once

#include "app/eventHub.hpp"
#include "core/board.hpp"
#include "core/types.hpp"
#include "data/board.hpp"
#include "data/gameStatus.hpp"
#include "data/player.hpp"
#include "gameNet/nwEvents.hpp"

namespace go::app {

enum class GameStatus {
Idle, //!< Nothing happening
Waiting, //!< Waiting for players to connect.
Active, //!< Game being played.
Done //!< Game over.
};

class Position {
public:
Position(EventHub& hub); //!< Hub allows to signal listeners on interesting updates.
Expand Down
4 changes: 2 additions & 2 deletions src/libApp/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void Position::updatePosition(const gameNet::ServerDelta& delta) {

if (delta.action == gameNet::ServerAction::Place) {
if (delta.coord) {
m_board.setAt(Coord{delta.coord->x, delta.coord->y}, delta.seat == gameNet::Seat::Black ? Board::Value::Black : Board::Value::White);
m_board.place(Coord{delta.coord->x, delta.coord->y}, delta.seat == gameNet::Seat::Black ? Board::Stone::Black : Board::Stone::White);
for (const auto c: delta.captures) {
m_board.setAt({c.x, c.y}, Board::Value::Empty);
m_board.place({c.x, c.y}, Board::Stone::Empty);
}
} else {
Logger().Log(Logging::LogLevel::Warning, "Game delta missing place coordinate; skipping board update.");
Expand Down
4 changes: 2 additions & 2 deletions src/libApp/sessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void SessionManager::connect(const std::string& hostIp) {
{
std::lock_guard<std::mutex> lock(m_stateMutex);
m_position.reset(9u);
m_position.setStatus(GameStatus::Waiting);
m_position.setStatus(GameStatus::Ready);
m_chatHistory.clear();
}
m_localServer.reset();
Expand All @@ -40,7 +40,7 @@ void SessionManager::host(unsigned boardSize) {
{
std::lock_guard<std::mutex> lock(m_stateMutex);
m_position.reset(boardSize);
m_position.setStatus(GameStatus::Waiting);
m_position.setStatus(GameStatus::Ready);
m_chatHistory.clear();
}

Expand Down
1 change: 0 additions & 1 deletion src/libCamera/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ set_output_directory(${targetName}) # Set the output directory of the library
target_compile_definitions(${targetName} PUBLIC VERSION_MAJOR=0)
target_compile_definitions(${targetName} PUBLIC VERSION_MINOR=0)
target_compile_definitions(${targetName} PUBLIC VERSION_PATCH=1)

7 changes: 4 additions & 3 deletions src/libCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ set(targetName libCore)

# Get files to build
set(headers
${CMAKE_CURRENT_LIST_DIR}/include/core/types.hpp
${CMAKE_CURRENT_LIST_DIR}/include/core/board.hpp
${CMAKE_CURRENT_LIST_DIR}/include/core/game.hpp
${CMAKE_CURRENT_LIST_DIR}/include/core/position.hpp
${CMAKE_CURRENT_LIST_DIR}/include/core/gameEvent.hpp
Expand All @@ -17,7 +15,6 @@ set(headers
${CMAKE_CURRENT_LIST_DIR}/zobristHash.hpp
)
set(sources
${CMAKE_CURRENT_LIST_DIR}/board.cpp
${CMAKE_CURRENT_LIST_DIR}/position.cpp
${CMAKE_CURRENT_LIST_DIR}/game.cpp
${CMAKE_CURRENT_LIST_DIR}/sgfHandler.cpp
Expand All @@ -36,6 +33,10 @@ target_include_directories(${targetName}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include
)
target_link_libraries(${targetName}
PUBLIC
go::data
)

# Setup project settings
set_project_warnings(${targetName}) # Which warnings to enable
Expand Down
31 changes: 0 additions & 31 deletions src/libCore/board.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/libCore/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Game::handleEvent(const PutStoneEvent& event) {
return;
}

Position next{m_position.board.size()};
GamePosition next{m_position.board.size()};
std::vector<Coord> captures{};
if (isNextPositionLegal(m_position, m_position.currentPlayer, event.c, *m_hasher, m_seenHashes, next, captures)) {
m_consecutivePasses = 0;
Expand Down Expand Up @@ -97,7 +97,7 @@ void Game::handleEvent(const PassEvent& event) {
return;
}

Position next = m_position;
GamePosition next = m_position;
next.pass(*m_hasher);

if (m_seenHashes.contains(next.hash)) {
Expand Down
4 changes: 3 additions & 1 deletion src/libCore/include/core/IZobristHash.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "core/types.hpp"
#include "data/board.hpp"
#include "data/player.hpp"

#include <cstdint>

namespace go {
Expand Down
34 changes: 0 additions & 34 deletions src/libCore/include/core/board.hpp

This file was deleted.

1 change: 0 additions & 1 deletion src/libCore/include/core/eventHub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "core/IGameSignalListener.hpp"
#include "core/IGameStateListener.hpp"
#include "core/types.hpp"

#include <mutex>
#include <vector>
Expand Down
4 changes: 2 additions & 2 deletions src/libCore/include/core/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "core/eventHub.hpp"
#include "core/gameEvent.hpp"
#include "core/position.hpp"
#include "core/types.hpp"
#include "data/player.hpp"

#include <unordered_set>

Expand Down Expand Up @@ -42,7 +42,7 @@ class Game {
bool m_gameActive;
unsigned m_consecutivePasses{0}; //!< Two consequtive passes ends game.

Position m_position;
GamePosition m_position;
EventQueue m_eventQueue; //!< Queue of internal game events we have to handle.
EventHub m_eventHub; //!< Hub to signal updates of the game state to external components.

Expand Down
7 changes: 5 additions & 2 deletions src/libCore/include/core/gameEvent.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "core/types.hpp"
#include "data/board.hpp"
#include "data/player.hpp"

#include <cstdint>
#include <optional>
#include <variant>
#include <vector>
Expand All @@ -21,7 +23,7 @@ using GameEvent = std::variant<PutStoneEvent, PassEvent, ResignEvent, ShutdownEv


//! Types of signals.
enum GameSignal : uint64_t {
enum GameSignal : std::uint64_t {
GS_None = 0,
GS_BoardChange = 1 << 0, //!< Board was modified.
GS_PlayerChange = 1 << 1, //!< Active player changed.
Expand All @@ -31,6 +33,7 @@ enum GameSignal : uint64_t {

//! Type of move.
enum class GameAction { Place, Pass, Resign };

//! Symbolises the game state change after one move.
struct GameDelta {
unsigned moveId; //!< Move number.
Expand Down
8 changes: 4 additions & 4 deletions src/libCore/include/core/moveChecker.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "core/IZobristHash.hpp"
#include "core/board.hpp"
#include "core/position.hpp"
#include "core/types.hpp"
#include "data/board.hpp"
#include "data/player.hpp"

#include <cstdint>
#include <optional>
Expand All @@ -23,7 +23,7 @@ bool isSuicide(const Board& board, Player player, Coord c);
bool isValidMove(const Board& board, Player player, Coord c);

//! Compute resulting position if the move is legal (including superko via history). Returns false when illegal.
bool isNextPositionLegal(const Position& current, Player player, Coord c, IZobristHash& hasher, const std::unordered_set<uint64_t>& history, Position& out,
std::vector<Coord>& outCaptures);
bool isNextPositionLegal(const GamePosition& current, Player player, Coord c, IZobristHash& hasher, const std::unordered_set<uint64_t>& history,
GamePosition& out, std::vector<Coord>& outCaptures);

} // namespace go
8 changes: 4 additions & 4 deletions src/libCore/include/core/position.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#pragma once

#include "core/IZobristHash.hpp"
#include "core/board.hpp"
#include "core/types.hpp"
#include "data/board.hpp"
#include "data/player.hpp"

namespace go {

//! The current game position.
struct Position {
struct GamePosition {
Board board; //!< Current board.
Player currentPlayer{Player::Black}; //!< Current Player.
uint64_t hash{0}; //!< Game state hash.
unsigned moveId{0}; //!< Move number of game.

public:
Position(std::size_t boardSize);
GamePosition(std::size_t boardSize);

void putStone(Coord c, IZobristHash& hasher); //!< Current player puts a stone (assumes legal move).
void pass(IZobristHash& hasher); //!< Current player passes his turn.
Expand Down
Loading
Loading