Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <vector>
- #include <algorithm>
- #include <random>
- #include <queue>
- #include <string>
- class CCardGame {
- public:
- // Defined types
- enum class GameStatus {game_ready, game_playing, first_win, second_win};
- enum class Player {first, second};
- typedef std::string String;
- typedef std::vector<String> StringArray;
- struct Card {
- const String suit;
- const String number;
- Card(String _suit, String _number)
- : suit(std::move(_suit))
- , number(std::move(_number))
- { }
- };
- typedef std::vector<Card> CardArray;
- /**
- * Builds ready-to-play game object
- * @param _starts_first is the player who'll
- * do the first step in the game
- */
- explicit CCardGame(Player _starts_first);
- /**
- * Builds ready-to-play game object
- * First step will be done by the first player
- */
- CCardGame();
- /**
- * Restarts the game
- */
- void restartGame();
- /** Use this method to change the player who'll do
- * the first step in next game
- * @param _starts_first
- */
- void choosePlayerStartFirst(Player _starts_first);
- /**
- * @return player who'll do the first step in the next game
- */
- Player getPlayerStartsFirst() const;
- /**
- * @return who'll do the next step
- */
- Player getPlayerPlaysNext() const;
- /**
- * @return game status
- */
- GameStatus getGameStatus() const;
- /**
- * @return 1st player's / 2nd player's / table cards
- */
- CardArray getFirstPlayerCards() const;
- CardArray getSecondPlayerCards() const;
- CardArray getTableCards() const;
- /** Starts the game
- * - changes status_ to GameStatus::game_playing
- */
- void start();
- /** This method moves game on one step
- * (if it was started by start() and haven't been finished yet)
- * Call getStep() getAllSteps() to see description of the step
- * @return true if game status is 'playing', otherwise false
- */
- void playOneStep();
- /**
- * @return true if there're some step info to read
- */
- bool isStepInfoReady();
- /**
- * @return string with description of the last game step
- * Removes this step from game memory
- */
- String getStepInfo();
- /**
- * @return StringArray of strings with all completed game steps
- * Clears all the steps inside the game
- */
- StringArray getAllStepsInfo();
- private:
- // Compile-time constants
- static constexpr int CARD_NUMBERS_COUNT = 13;
- static constexpr int CARD_SUITS_COUNT = 4;
- static constexpr int CARDS_IN_DECK = CARD_NUMBERS_COUNT * CARD_SUITS_COUNT;
- const StringArray CARD_NUMBERS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
- const StringArray CARD_SUITS = {"Spade", "Heart", "Club", "Diamond"};
- // Random generator and random_device as generator seed
- std::random_device rd_device_;
- std::mt19937 rd_gen_;
- // Game data
- std::deque<int> first_deck_;
- std::deque<int> second_deck_;
- std::deque<int> table_;
- Player starts_first_;
- Player plays_next_;
- GameStatus status_;
- std::queue<std::string> steps_;
- /**
- * Fills card decks, shuffles the cards inside
- * Filling data is integer numbers (card_index) associated with cards' suits and numbers by the formula:
- * Card suit: CARD_SUITS[card_index / CARD_NUMBERS_COUNT]
- * Card number: CARD_NUMBERS[card_index % CARD_NUMBERS_COUNT]
- *
- * Related convert methods:
- * getCardSuit(card_index)
- * getCardNumber(card_index)
- * convertDeckToString(deck)
- */
- void clearDecks();
- /**
- * @param _card_index - index of card in not-sorted deck
- * @return card suit description string
- */
- String getCardSuit(int _card_index) const;
- /**
- * @param _card_index - index of card in not-sorted deck
- * @return card number description string
- */
- String getCardNumber(int _card_index) const;
- /**
- * Converts deck of card indexes to array of card descriptions
- * Ex.: [0, 11, 36] -> ["2 Spade", "K Spade", "Q Club"]
- * @param _deck of int card indexes
- * @return deck of strings with card descriptions
- */
- CardArray deckToCardArray(const std::deque<int> &_deck) const;
- /**
- * Does one step of the game
- * Saves game steps info into steps_ queue
- *
- */
- void doOneStep();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement