For a team course project at the University of Lethbridge, four of us built a blackjack game in C++ over two sprints. Sprint 1 was the game itself with a model–view–controller structure. Sprint 2 turned it into a REST API, so any client (a browser page, curl, a test suite) could play.
I want to cover two pieces I wrote, the deck and the JSON data model, plus what working on a team taught me about splitting up code.
The architecture
model/ Card, Deck, Hand, Player, CPU (game rules and state)
view/ UI (console output, sprint 1)
controller/ Game, GameController, Server (game flow + HTTP endpoints)
dto/ CardDTO, HandDTO, GameStateDTO (what goes over the wire)
test/ unit tests + API tests
The server runs on port 8600 with four endpoints:
| Endpoint | What it does |
|---|---|
GET /game/start |
New game, deal two cards each |
GET /game/hit |
Player draws a card |
GET /game/stand |
Dealer plays out their hand, game is decided |
GET /game |
Current state |
My part 1: the Deck
The deck is small, but everything else depends on it being right: 52 unique cards, properly shuffled, dealt one at a time.
void Deck::reset() {
for (int i = 0; i < cards.size(); i++) {
delete cards[i];
}
cards.clear();
for (int suit = 0; suit < 4; ++suit) {
for (int rank = 0; rank < 13; ++rank) {
cards.push_back(new Card(static_cast<Card::Suit>(suit),
static_cast<Card::Rank>(rank)));
}
}
shuffle();
}
void Deck::shuffle() {
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(cards.begin(), cards.end(), g);
}
Two things I'd point out:
-
Shuffling uses
std::shufflewith a Mersenne Twister, notrand().std::shuffleimplements a proper Fisher–Yates shuffle, so every ordering is equally likely. -
Dealing takes from the back of the vector with
pop_back(), which is O(1). Taking from the front would shift every remaining card.
What I'd change now
The deck stores raw Card* pointers and deletes them by hand. Once a card is dealt, it leaves the deck, and it's no longer obvious who is responsible for deleting it. If I rewrote it, I'd store cards by value (std::vector<Card>), since a card is just two small enums. No new, no delete, no ownership questions.
My part 2: the JSON data model
For Sprint 2, the game state had to become JSON. Oat++ uses DTOs (data transfer objects): classes declared with macros that the framework can serialise automatically. I co-wrote these with a teammate:
class CardDTO : public oatpp::DTO {
DTO_INIT(CardDTO, DTO)
DTO_FIELD(String, suit);
DTO_FIELD(String, rank);
};
class HandDTO : public oatpp::DTO {
DTO_INIT(HandDTO, DTO)
DTO_FIELD(List<Object<CardDTO>>, cards);
};
class GameStateDTO : public oatpp::DTO {
DTO_INIT(GameStateDTO, DTO)
DTO_FIELD(Object<HandDTO>, playerHand);
DTO_FIELD(Object<HandDTO>, dealerHand);
DTO_FIELD(Int32, playerScore);
DTO_FIELD(Int32, dealerScore);
DTO_FIELD(String, status);
};
A response after /game/start looks like this:
{
"playerHand": { "cards": [ {"suit": "CLUB", "rank": "SEVEN"},
{"suit": "SPADE", "rank": "TWO"} ] },
"dealerHand": { "cards": [ {"suit": "DIAMOND", "rank": "QUEEN"},
{"suit": "HIDDEN", "rank": "HIDDEN"} ] },
"playerScore": 9,
"dealerScore": 0,
"status": "in_progress"
}
The design decision I like most here is that the server hides the dealer's second card. The DTO for the dealer's hand replaces it with HIDDEN and reports a dealer score of 0 until the player stands. If the server sent the real card and trusted the client not to show it, anyone could open the browser's network tab and see it. The rule "don't send data the client shouldn't have" applies to every API, not just games.
Testing and Docker
The project has tests for the game state and for each endpoint (start, hit, stand). Our Dockerfile builds and runs the tests before building the server:
RUN make test_cardGame
RUN ./test_cardGame
EXPOSE 8600
RUN make cardGame
ENTRYPOINT ["./cardGame"]
If any test fails, the image doesn't build. It's a simple way to make sure nothing broken ever gets packaged.
What working on a team taught me
-
Agree on interfaces first. Once we'd agreed on the header files for
Card,DeckandHand, we could each build our class in parallel and it all fit together. - Write down who owns what. Our README listed every class and its author. That made code reviews and questions much faster.
- Know the limits of your design. The server keeps one game in memory, so two people playing at once would share a game. That was fine for a course project, but a real version would need a game ID per session.
Team
My group and I built this together. I wrote Deck, co-wrote the DTOs, and worked on the Makefile with the team. The code is on GitHub.
Top comments (0)