extracted game logic into separate library

This commit is contained in:
Lauchmelder 2022-01-15 16:45:52 +01:00
parent c6a262a57f
commit 690b577e87
27 changed files with 388 additions and 175 deletions

21
engine/Board.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "Board.hpp"
#include "Game.hpp"
Board::Board(Game* parent) :
parent(parent)
{
}
void Board::RevealCard()
{
openCards.push_back(parent->GetStack().DrawCard());
if (callback)
callback(openCards.back());
}
void Board::SetRevealCallback(RevealCallbackFunc callbackFunc)
{
callback = callbackFunc;
}

27
engine/Board.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <functional>
#include <memory>
#include <vector>
#include "Card.hpp"
typedef std::function<void(std::shared_ptr<Card>)> RevealCallbackFunc;
class Game;
class Board
{
public:
Board(Game* parent);
void RevealCard();
inline const std::vector<std::shared_ptr<Card>>& GetOpenCards() { return openCards; }
void SetRevealCallback(RevealCallbackFunc callbackFunc);
private:
std::vector<std::shared_ptr<Card>> openCards;
RevealCallbackFunc callback;
Game* parent;
};

10
engine/CMakeLists.txt Normal file
View file

@ -0,0 +1,10 @@
add_library(koikoiengine STATIC
"Game.cpp"
"Card.cpp"
"CardStack.cpp"
"Board.cpp"
)
target_include_directories(koikoiengine PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)

7
engine/Card.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "Card.hpp"
Card::Card(Month month, int type) :
suit(month), type(type)
{
}

19
engine/Card.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
enum class Month
{
January, February, March, April, May, June, July, August, September, October, November, December
};
class Card
{
public:
Card(Month month, int type);
inline Month GetSuit() { return suit; }
inline int GetType() { return type; }
private:
Month suit;
int type;
};

32
engine/CardStack.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "CardStack.hpp"
#include <algorithm>
#include <chrono>
#include <random>
CardStack::CardStack()
{
// Create a full stack of cards
for (int month = 0; month < 11; month++)
{
for (int type = 0; type < 4; type++)
{
stack.push_back(std::make_shared<Card>(static_cast<Month>(month), type));
}
}
// Shuffle stack
std::default_random_engine engine(time(0));
std::shuffle(stack.begin(), stack.end(), engine);
}
CardStack::~CardStack()
{
}
std::shared_ptr<Card> CardStack::DrawCard()
{
std::shared_ptr<Card> drawnCard = stack.back();
stack.pop_back();
return drawnCard;
}

19
engine/CardStack.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <memory>
#include <vector>
#include "Card.hpp"
class CardStack
{
public:
CardStack();
~CardStack();
std::shared_ptr<Card> DrawCard();
inline bool Empty() const { return stack.empty(); }
private:
std::vector<std::shared_ptr<Card>> stack;
};

14
engine/Game.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "Game.hpp"
Game::Game() :
board(this)
{
}
void Game::Setup()
{
// Put 8 cards on the table
for (int i = 0; i < 8; i++)
board.RevealCard();
}

19
engine/Game.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include "CardStack.hpp"
#include "Board.hpp"
class Game
{
public:
Game();
~Game() {}
void Setup();
inline CardStack& GetStack() { return stack; }
inline Board& GetBoard() { return board; }
private:
CardStack stack;
Board board;
};