Advertisement
shchuko

CardGame.h

Jun 15th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. #include <vector>
  5. #include <algorithm>
  6. #include <random>
  7. #include <queue>
  8. #include <string>
  9.  
  10.  
  11. class CCardGame {
  12. public:
  13.     // Defined types
  14.     enum class GameStatus {game_ready, game_playing, first_win, second_win};
  15.     enum class Player {first, second};
  16.  
  17.     typedef std::string String;
  18.     typedef std::vector<String> StringArray;
  19.  
  20.     struct Card {
  21.         const String suit;
  22.         const String number;
  23.  
  24.         Card(String _suit, String _number)
  25.             : suit(std::move(_suit))
  26.             , number(std::move(_number))
  27.         { }
  28.     };
  29.  
  30.     typedef std::vector<Card> CardArray;
  31.  
  32.     /**
  33.      * Builds ready-to-play game object
  34.      * @param _starts_first is the player who'll
  35.      * do the first step in the game
  36.      */
  37.     explicit CCardGame(Player _starts_first);
  38.  
  39.     /**
  40.      * Builds ready-to-play game object
  41.      * First step will be done by the first player
  42.      */
  43.     CCardGame();
  44.  
  45.     /**
  46.      * Restarts the game
  47.      */
  48.     void restartGame();
  49.  
  50.     /** Use this method to change the player who'll do
  51.      *  the first step in next game
  52.      * @param _starts_first
  53.      */
  54.     void choosePlayerStartFirst(Player _starts_first);
  55.  
  56.     /**
  57.      * @return player who'll do the first step in the next game
  58.      */
  59.     Player getPlayerStartsFirst() const;
  60.  
  61.     /**
  62.      * @return who'll do the next step
  63.      */
  64.     Player getPlayerPlaysNext() const;
  65.  
  66.     /**
  67.      * @return game status
  68.      */
  69.     GameStatus getGameStatus() const;
  70.  
  71.     /**
  72.      * @return 1st player's / 2nd player's / table cards
  73.      */
  74.     CardArray getFirstPlayerCards() const;
  75.     CardArray getSecondPlayerCards() const;
  76.     CardArray getTableCards() const;
  77.  
  78.     /** Starts the game
  79.      * - changes status_ to GameStatus::game_playing
  80.      */
  81.     void start();
  82.  
  83.     /** This method moves game on one step
  84.      * (if it was started by start() and haven't been finished yet)
  85.      * Call getStep() getAllSteps() to see description of the step
  86.      * @return true if game status is 'playing', otherwise false
  87.      */
  88.     void playOneStep();
  89.  
  90.     /**
  91.      * @return true if there're some step info to read
  92.      */
  93.     bool isStepInfoReady();
  94.  
  95.     /**
  96.      * @return string with description of the last game step
  97.      * Removes this step from game memory
  98.      */
  99.     String getStepInfo();
  100.  
  101.     /**
  102.      * @return StringArray of strings with all completed game steps
  103.      * Clears all the steps inside the game
  104.      */
  105.     StringArray getAllStepsInfo();
  106.  
  107.  
  108. private:
  109.     // Compile-time constants
  110.     static constexpr int CARD_NUMBERS_COUNT = 13;
  111.     static constexpr int CARD_SUITS_COUNT = 4;
  112.     static constexpr int CARDS_IN_DECK = CARD_NUMBERS_COUNT * CARD_SUITS_COUNT;
  113.  
  114.     const StringArray CARD_NUMBERS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  115.     const StringArray CARD_SUITS = {"Spade", "Heart", "Club", "Diamond"};
  116.  
  117.     // Random generator and random_device as generator seed
  118.     std::random_device rd_device_;
  119.     std::mt19937 rd_gen_;
  120.  
  121.     // Game data
  122.     std::deque<int> first_deck_;
  123.     std::deque<int> second_deck_;
  124.     std::deque<int> table_;
  125.  
  126.     Player starts_first_;
  127.     Player plays_next_;
  128.  
  129.     GameStatus status_;
  130.     std::queue<std::string> steps_;
  131.  
  132.     /**
  133.      * Fills card decks, shuffles the cards inside
  134.      * Filling data is integer numbers (card_index) associated with cards' suits and numbers by the formula:
  135.      *     Card suit: CARD_SUITS[card_index / CARD_NUMBERS_COUNT]
  136.      *     Card number: CARD_NUMBERS[card_index % CARD_NUMBERS_COUNT]
  137.      *
  138.      * Related convert methods:
  139.      *     getCardSuit(card_index)
  140.      *     getCardNumber(card_index)
  141.      *     convertDeckToString(deck)
  142.      */
  143.     void clearDecks();
  144.  
  145.     /**
  146.      * @param _card_index - index of card in not-sorted deck
  147.      * @return card suit description string
  148.      */
  149.     String getCardSuit(int _card_index) const;
  150.  
  151.     /**
  152.      * @param _card_index - index of card in not-sorted deck
  153.      * @return card number description string
  154.      */
  155.     String getCardNumber(int _card_index) const;
  156.  
  157.     /**
  158.      * Converts deck of card indexes to array of card descriptions
  159.      * Ex.: [0, 11, 36] -> ["2 Spade", "K Spade", "Q Club"]
  160.      * @param _deck of int card indexes
  161.      * @return deck of strings with card descriptions
  162.      */
  163.     CardArray deckToCardArray(const std::deque<int> &_deck) const;
  164.  
  165.     /**
  166.      * Does one step of the game
  167.      * Saves game steps info into steps_ queue
  168.      *
  169.      */
  170.     void doOneStep();
  171.  
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement