Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include "card.hh"
  2. #include "turn.hh"
  3. #include "deck.hh"
  4. #include "context.hh"
  5. #include "player.hh"
  6. #include "cardstore.hh"
  7.  
  8. #include <algorithm>
  9.  
  10. TurnContext::TurnContext(GameContext& context, Player* player, Deck& deck) : gameContext(context), self(*player), deck(deck), shuffled(false), hand(deck.draw(shuffled, 5)), actions(1), buys(1), coin(0), phase(Interface::PHASE_PRE_TURN) {}
  11.  
  12. TurnContext::~TurnContext()
  13. {
  14.     deck.discard(played);
  15.     deck.discard(hand);
  16. }
  17.  
  18. bool TurnContext::selectCard(const Interface::CardSelection& options, bool optional)
  19. {
  20.     if(!optional && options.empty())
  21.         throw Exception::LogicError("options cannot be empty if optional is false");
  22.     if(options.empty())
  23.         return false;
  24.     if(options.size() == 1 && !optional){
  25.         options.toggle(0);//
  26.         return true;
  27.     }
  28.     self.selectCard(*this, options, optional);//optional madafaka
  29.     if(!optional && options.countSelected() != 1)
  30.         throw Exception::RuntimeError("!optional && options.countSelected() != 1");
  31.     if(options.countSelected() == 1)
  32.         return true;
  33.     else
  34.         return false;
  35. }
  36.  
  37. size_t TurnContext::selectCards(const Interface::CardSelection& options, size_t min, size_t max)
  38. {
  39.     if(!options.validate())
  40.         throw Exception::LogicError("!options.validate()");
  41.     if(max > options.size())
  42.         max = options.size();
  43.     if(min > max)
  44.         min = max;
  45.     if(options.empty()){
  46.         return 0;
  47.     }
  48.     self.selectCards(*this, options, min, max);
  49.     size_t i = options.countSelected();
  50.     if(i < min || i > max)
  51.         throw Exception::RuntimeError("the player selection is invalid");
  52.     return i;
  53. }
  54.  
  55. bool TurnContext::decide(enum Interface::Decision decision, const Card* card, const Player* owner)
  56. {
  57.     return self.decide(*this, decision, card, owner);
  58. }
  59.  
  60. void TurnContext::gainTrashed(const Card* card)
  61. {
  62.   gainCard(card, TurnContext::GAIN_DISCARD, false);
  63. }
  64.  
  65. void TurnContext::trashRevealed(const Card* card)
  66. {
  67.   trashCard(card);
  68. }
  69.  
  70. void TurnContext::trashLastPlayed()
  71. {
  72.   trashCard(played.back());
  73.   played.pop_back();
  74. }
  75.  
  76. const Card* TurnContext::revealCard()
  77. {
  78.   if (!deck.canDraw(shuffled))
  79.     return NULL;
  80.   else
  81.     return deck.draw(shuffled, 1).front();
  82. }
  83.  
  84. std::vector<const Card*> TurnContext::revealCards(size_t n)
  85. {
  86.   std::vector<const Card*> cards = deck.draw(shuffled, n);
  87.   return cards;
  88. }
  89.  
  90. void TurnContext::toHand(const std::vector<const Card*>& cards)
  91. {
  92.   std::copy(cards.begin(), cards.end(), std::back_inserter(hand));
  93. }
  94.  
  95. void TurnContext::drawCards(size_t n)
  96. {
  97.     std::vector<const Card*> vec = deck.draw(shuffled, n);
  98.     copy(vec.begin(), vec.end(), hand.end());
  99. }
  100.  
  101. void TurnContext::playSelected(const Interface::CardSelection& selection)
  102. {
  103.     for(auto it = selection.rbegin(); it != selection.rend(); it++){
  104.         if(it->selected){//1
  105.             if(!it->card->validForPhase(getPhase()))//1.1
  106.                 throw Exception::LogicError("card not valid for phase");
  107.  
  108.             if(getPhase() == Interface::PHASE_ACTION){//1.2
  109.                 if(actions == 0){//1.2.1
  110.                     throw Exception::LogicError("no action left");
  111.                 }
  112.                 else{//1.2.1
  113.                     actions--;
  114.                 }
  115.             }
  116.             hand.erase(hand.begin() + it->idx);//1.3
  117.             played.push_back(it->card);//1.4
  118.             playCard(it->card);//1.5
  119.         }
  120.     }
  121. }
  122.  
  123. void TurnContext::playSelectedTwice(const Interface::CardSelection& selection)
  124. {
  125.   const Card* card = selection.getSelectedCard();
  126.   if (!card->validForPhase(getPhase()))
  127.     throw Exception::LogicError("TurnContext::playSelectedTwice Incorrect phase / play");
  128.   hand.erase(hand.begin() + selection.getSelectedIdx());
  129.   played.push_back(card);
  130.   playCard(card);
  131.   playCard(card);
  132. }
  133.  
  134. void TurnContext::discardSelected(const Interface::CardSelection& selection)
  135. {
  136.     for(auto it = selection.rbegin(); it != selection.rend(); it++){
  137.         if(it->selected){
  138.             deck.discard(it->card);
  139.             hand.erase(hand.begin() + it->idx);
  140.         }
  141.     }
  142. }
  143.  
  144. void TurnContext::trashSelected(const Interface::CardSelection& selection)
  145. {
  146.   for (auto iter = selection.rbegin();iter != selection.rend();iter++)
  147.     if (iter->selected)
  148.     {
  149.       trashCard(iter->card);
  150.       hand.erase(hand.begin() + iter->idx);
  151.     }
  152. }
  153.  
  154. void TurnContext::gainSelected(const Interface::CardSelection& selection, GainType gainType)
  155. {
  156.     const Card* card = selection.getSelectedCard();
  157.     gainCard(card->name, gainType);//card->name not card
  158. }
  159.  
  160. void TurnContext::returnSelectedToDeck(const Interface::CardSelection& selection)
  161. {
  162.   deck.returnToDeck(selection.getSelectedCard());
  163.   for (auto iter = selection.rbegin();iter != selection.rend();iter++)
  164.     if (iter->selected)
  165.       hand.erase(hand.begin() + iter->idx);
  166. }
  167.  
  168. bool TurnContext::canDraw() const
  169. {
  170.   return deck.canDraw(shuffled);
  171. }
  172.  
  173. void TurnContext::discard(const std::vector<const Card*>& cards)
  174. {
  175.   deck.discard(cards);
  176. }
  177.  
  178. void TurnContext::discard(const Card* card)
  179. {
  180.   deck.discard(card);
  181. }
  182.  
  183. void TurnContext::discardEntireDeck()
  184. {
  185.   deck.discardEntireDeck();
  186. }
  187.  
  188. void TurnContext::returnToDeck(const Card* card)
  189. {
  190.   deck.returnToDeck(card);
  191. }
  192.  
  193. bool TurnContext::attackProtection()
  194. {
  195.   throw Exception::Unimplemented("exercise 4");
  196. }
  197.  
  198. void TurnContext::buySelected(const Interface::CardSelection& selection)
  199. {
  200.     if(buys == 0)
  201.         throw Exception::LogicError("buys = 0");
  202.     const Card* card = selection.getSelectedCard();
  203.     if(coin < card->cost)
  204.         throw Exception::LogicError("insufficient funds");
  205.     buys--;
  206.     coin -= card->cost;
  207.     gainCard(card);
  208. }
  209.  
  210. void TurnContext::gainCard(const std::string& name, GainType gainType)
  211. {
  212.   gainCard(CardStore::get(name), gainType);
  213. }
  214.  
  215. void TurnContext::gainCard(const Card* card, TurnContext::GainType gainType, bool fromStorage)
  216. {
  217.   // Do nothing if the card is not found in the storage
  218.   if (fromStorage && !gameContext.storage.removeCard(card)) // Storage access
  219.     return;
  220.   if (gainType == GAIN_DISCARD)
  221.     discard(card);
  222.   else if (gainType == GAIN_HAND)
  223.     hand.push_back(card);
  224.   else if (gainType == GAIN_TOP_OF_DECK)
  225.     deck.returnToDeck(card);
  226.   gameContext.invokeEvent(card, self, &Player::onGain);
  227. }
  228.  
  229. void TurnContext::trashCard(const Card* card)
  230. {
  231.   gameContext.invokeEvent(card, self, &Player::onTrash);
  232.   gameContext.storage.trashCard(card); // Storage access
  233. }
  234.  
  235. void TurnContext::playCard(const Card* card)
  236. {
  237.   gameContext.invokeEvent(card, self, &Player::onPlay);
  238.   card->play(*this);
  239.   gameContext.setActiveCard(NULL);
  240. }
  241.  
  242. bool TurnContext::playReaction(const Cards::ReactionCard* card)
  243. {
  244.   gameContext.invokeEvent(card, self, &Player::onReact);
  245.   return card->react(*this);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement