Advertisement
Egor_Vakar

Core.cpp(coursework)

Dec 26th, 2022
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.83 KB | None | 0 0
  1. #include "Core.hpp"
  2.  
  3. TableHandle::TableHandle(sf::Texture &p_texture, float &dt) {
  4.  
  5.     this->cardsSprite = p_texture;
  6.     this->dt = dt;
  7.  
  8.     this->wins = 0;
  9.     this->lose = 0;
  10.     this->tie = 0;
  11.  
  12.     this->tableWinTex.loadFromFile("E:/C labs/MyBlackJack/resource/tableWin.png");
  13.     this->playerWinTex.loadFromFile("E:/C labs/MyBlackJack/resource/playerWin.png");
  14.     this->tieTex.loadFromFile("E:/C labs/MyBlackJack/resource/Tie.png");
  15.  
  16.     this->tableWinSpr.setTexture(tableWinTex);
  17.     this->tableWinSpr.setPosition(sf::Vector2f(150, 20));
  18.  
  19.     this->playerWinSpr.setTexture(playerWinTex);
  20.     this->playerWinSpr.setPosition(sf::Vector2f(150, 20));
  21.  
  22.     this->tieSpr.setTexture(tieTex);
  23.     this->tieSpr.setPosition(sf::Vector2f(150, 20));
  24.  
  25.     this->font.loadFromFile("E:/C labs/MyBlackJack/font/SuperMario256.ttf");
  26.  
  27.     this->playerDisplay.setFont(this->font);
  28.     this->playerDisplay.setString("Player:");
  29.     this->playerDisplay.setFillColor(sf::Color::White);
  30.     this->playerDisplay.setPosition(sf::Vector2f(430, 450));
  31.  
  32.     this->tableDisplay.setFont(this->font);
  33.     this->tableDisplay.setFillColor(sf::Color::White);
  34.     this->tableDisplay.setString("Dealer:");
  35.     this->tableDisplay.setPosition(sf::Vector2f(430, 77));
  36.  
  37.     this->tableStats.setFont(this->font);
  38.     this->tableStats.setFillColor(sf::Color::Red);
  39.     this->tableStats.setPosition(sf::Vector2f(620, 720));
  40.     this->tableStats.setCharacterSize(24);
  41.  
  42.     this->playerStats.setFont(this->font);
  43.     this->playerStats.setFillColor(sf::Color::Green);
  44.     this->playerStats.setPosition(sf::Vector2f(150, 720));
  45.     this->playerStats.setCharacterSize(24);
  46.  
  47.     this->tieStats.setFont(this->font);
  48.     this->tieStats.setFillColor(sf::Color::Yellow);
  49.     this->tieStats.setPosition(sf::Vector2f(450, 720));
  50.     this->tieStats.setCharacterSize(24);
  51.  
  52.     this->playersChips.setFont(this->font);
  53.     this->playersChips.setFillColor(sf::Color::White);
  54.     this->playersChips.setPosition(sf::Vector2f(130, 450));
  55.     this->playersChips.setCharacterSize(24);
  56.  
  57.     this->totalBet.setFont(this->font);
  58.     this->totalBet.setFillColor(sf::Color::White);
  59.     this->totalBet.setPosition(sf::Vector2f(420, 280));
  60.     this->totalBet.setCharacterSize(28);
  61.  
  62.     this->playersSumTxt.setFont(this->font);
  63.     this->playersSumTxt.setFillColor(sf::Color::White);
  64.     this->playersSumTxt.setPosition(sf::Vector2f(558, 450));
  65.  
  66.     this->dealersSumTxt.setFont(this->font);
  67.     this->dealersSumTxt.setFillColor(sf::Color::White);
  68.     this->dealersSumTxt.setPosition(sf::Vector2f(554, 77));
  69. }
  70.  
  71. TableHandle::~TableHandle() {
  72.  
  73.     playerCards.clear();
  74.     tableCards.clear();
  75.  
  76. }
  77.  
  78. void TableHandle::GameRun(float &dt) {
  79.     if (this->gamerun) {
  80.  
  81.         // # Display Text
  82.         if (this->playerturn) {
  83.             this->playerDisplay.setFillColor(sf::Color::Yellow);
  84.             this->tableDisplay.setFillColor(sf::Color::White);
  85.         } else {
  86.             this->playerDisplay.setFillColor(sf::Color::White);
  87.             this->tableDisplay.setFillColor(sf::Color::Yellow);
  88.         }
  89.  
  90.         if (!this->updGame) {
  91.             dealersum = 0;
  92.             playersum = 0;
  93.             for (auto &pc: this->playerCards) {
  94.                 playersum += pc.card_value;
  95.             }
  96.             for (auto &tc: this->tableCards) {
  97.                 dealersum += tc.card_value;
  98.             }
  99.  
  100.             if (firstCards) {
  101.                 // # player init cards given
  102.                 for (int i = 0; i < 2; i++) {
  103.                     this->EventAddCards();
  104.                 }
  105.                 // # Table init cards given
  106.                 playerturn = false;
  107.                 for (int i = 0; i < 2; i++) {
  108.                     this->EventAddCards();
  109.                 }
  110.  
  111.                 this->playerturn = true;
  112.                 this->firstCards = false;
  113.                 this->buttonLocked = false;
  114.             } else {
  115.  
  116.                 // # AI Table
  117.                 if (!playerturn) {
  118.                     if (this->delayAI >= 1.f) {
  119.                         this->lockedAI = false;
  120.                         this->delayAI = 0;
  121.                     }
  122.  
  123.                     if (!lockedAI) {
  124.                         if (dealersum > playersum || playersum > 21 || dealersum > 21) {
  125.                             switch (this->CheckWins()) {
  126.                                 case 0:
  127.                                     if (!doOnce) {
  128.                                         this->wins++;
  129.                                         chips += bet * 2;
  130.                                         bet = 0;
  131.                                         this->doOnce = true;
  132.                                     }
  133.                                     this->showPlayerWinner = true;
  134.                                     this->lockedShowWinner = true;
  135.                                     break;
  136.                                 case 1:
  137.                                     if (!doOnce) {
  138.                                         this->lose++;
  139.                                         bet = 0;
  140.                                         this->doOnce = true;
  141.                                     }
  142.  
  143.                                     this->showTableWinner = true;
  144.                                     this->lockedShowWinner = true;
  145.                                     break;
  146.                                 case 2:
  147.                                     if (!doOnce) {
  148.                                         this->tie++;
  149.                                         chips += bet;
  150.                                         bet = 0;
  151.                                         this->doOnce = true;
  152.                                     }
  153.  
  154.                                     this->showTie = true;
  155.                                     this->lockedShowWinner = true;
  156.                                     break;
  157.                             }
  158.                         } else {
  159.                             this->EventAddCards();
  160.                             this->lockedAI = true;
  161.                             this->delayAI = 0;
  162.                         }
  163.                     } else {
  164.                         this->delayAI += dt;
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.  
  170.         this->playerStats.setString("Player Wins: " + std::to_string(this->wins));
  171.         this->tableStats.setString("Dealer Wins: " + std::to_string(this->lose));
  172.         this->tieStats.setString("Tie: " + std::to_string(this->tie));
  173.         this->totalBet.setString("Bet: " + std::to_string(bet));
  174.         this->playersChips.setString("Chips: " + std::to_string(chips));
  175.         this->playersSumTxt.setString(std::to_string(this->playersum));
  176.         this->dealersSumTxt.setString(std::to_string(this->dealersum));
  177.     }
  178. }
  179.  
  180. bool TableHandle::EventAddCards() {
  181.  
  182.     if (playerturn) {
  183.  
  184.         playerCards.push_back(*new Cards(this->cardsSprite, playerCards.size(), getTurn()));
  185.  
  186.         int playersum = 0;
  187.  
  188.         for (auto &pc: this->playerCards) {
  189.  
  190.             playersum += pc.card_value;
  191.         }
  192.  
  193.         if (playersum > 21) {
  194.  
  195.             return true;
  196.         }
  197.  
  198.         return false;
  199.  
  200.     } else {
  201.  
  202.         int tablesum = 0;
  203.         tableCards.push_back(*new Cards(this->cardsSprite, tableCards.size(), getTurn()));
  204.  
  205.         for (auto &tc: this->tableCards) {
  206.  
  207.             tablesum += tc.card_value;
  208.         }
  209.  
  210.         if (tablesum > 21) {
  211.  
  212.             return true;
  213.         }
  214.  
  215.         return false;
  216.  
  217.     }
  218. }
  219.  
  220. inline void TableHandle::EventStop() {
  221.  
  222.     if (playerturn) {
  223.         playerturn = false;
  224.         buttonLocked = false;
  225.     } else {
  226.         playerturn = true;
  227.         buttonLocked = true;
  228.     }
  229. }
  230.  
  231. inline void TableHandle::EventQuit() {
  232.  
  233.     this->gamerun = false;
  234. }
  235.  
  236. uint16_t TableHandle::CheckWins() {
  237.  
  238.     int playersum = 0;
  239.     int tablesum = 0;
  240.  
  241.     for (auto &pc: this->playerCards) {
  242.  
  243.         playersum += pc.card_value;
  244.     }
  245.  
  246.     for (auto &tc: this->tableCards) {
  247.  
  248.         tablesum += tc.card_value;
  249.     }
  250.  
  251.     // # 0 = Player Win
  252.     // # 1 = Table Win
  253.     // # 2 = Tie
  254.  
  255.     if (playersum == 21 && tablesum == 21) {
  256.         return 2;
  257.     } else if (playersum == 21 && tablesum != 21) {
  258.         return 0;
  259.     } else if (tablesum == 21 && playersum != 21) {
  260.         return 1;
  261.     } else if (tablesum > playersum && tablesum <= 21) {
  262.         return 1;
  263.     } else if (playersum < tablesum && tablesum > 21) {
  264.         return 0;
  265.     } else if (playersum > 21 && tablesum <= 21) {
  266.         return 1;
  267.     } else {
  268.         return 2;
  269.     }
  270. }
  271.  
  272. void TableHandle::RestartMatch() {
  273.  
  274.     playerCards.clear();
  275.     tableCards.clear();
  276.     this->playerturn = true;
  277.     this->firstCards = true;
  278.     this->buttonLocked = true;
  279.  
  280.     this->showTableWinner = false;
  281.     this->showPlayerWinner = false;
  282.     this->showTie = false;
  283.  
  284.     this->delayShowWinner = 0;
  285.     this->lockedShowWinner = false;
  286.     this->doOnce = false;
  287.     this->doOnceWinner = false;
  288. }
  289.  
  290. inline int TableHandle::getTurn() {
  291.  
  292.     return (playerturn) ? 1 : 2;
  293. }
  294.  
  295. void TableHandle::EventHandle(enum Actions event) {
  296.  
  297.     switch (event) {
  298.  
  299.         case 1:
  300.  
  301.             if (this->EventAddCards()) {
  302.  
  303.                 this->playerturn = false;
  304.                 this->buttonLocked = true;
  305.             }
  306.  
  307.             break;
  308.         case 2:
  309.             this->EventStop();
  310.             break;
  311.         case 3:
  312.             this->EventQuit();
  313.             break;
  314.         case 4:
  315.             this->EventBet();
  316.             break;
  317.         case 5:
  318.             this->newGame = true;
  319.             break;
  320.         case 6:
  321.             this->showDevInfo = true;
  322.             break;
  323.         case 7:
  324.             this->showGuide = true;
  325.             break;
  326.         case 8:
  327.             if (this->guidePage > 1) {
  328.                 this->guidePage--;
  329.             }
  330.             break;
  331.         case 9:
  332.             if (this->guidePage < 4) {
  333.                 this->guidePage++;
  334.             }
  335.             break;
  336.         case 10: {
  337.             this->saveGame = true;
  338.             break;
  339.         }
  340.         case 11:
  341.  
  342.             this->updGame = true;
  343.             this->newGame = true;
  344.             break;
  345.  
  346.     }
  347. }
  348.  
  349. void EventStartNew() {
  350.  
  351. }
  352.  
  353. void TableHandle::DrawTable(sf::RenderWindow &app, float &dt) {
  354.  
  355.     if (!this->updGame) {
  356.         if (this->gamerun) {
  357.             for (auto &pc: this->playerCards) {
  358.  
  359.                 app.draw(pc.Draw());
  360.             }
  361.  
  362.             for (auto &tc: this->tableCards) {
  363.  
  364.                 app.draw(tc.Draw());
  365.             }
  366.  
  367.             app.draw(playerStats);
  368.             app.draw(tableStats);
  369.             app.draw(tieStats);
  370.             app.draw(playersChips);
  371.             app.draw(totalBet);
  372.  
  373.             app.draw(playerDisplay);
  374.             app.draw(playersSumTxt);
  375.             app.draw(dealersSumTxt);
  376.             app.draw(tableDisplay);
  377.  
  378.  
  379.             if (this->showPlayerWinner) {
  380.  
  381.                 if (!this->doOnceWinner) {
  382.                     this->doOnceWinner = true;
  383.                 }
  384.  
  385.                 app.draw(this->playerWinSpr);
  386.  
  387.                 if (this->delayShowWinner >= 3.f) {
  388.  
  389.                     this->lockedShowWinner = false;
  390.                     this->delayShowWinner = 0;
  391.                 }
  392.  
  393.                 if (!lockedShowWinner) {
  394.                     this->RestartMatch();
  395.                 } else {
  396.  
  397.                     this->delayShowWinner += dt;
  398.                 }
  399.             }
  400.             if (this->showTableWinner) {
  401.  
  402.                 if (!this->doOnceWinner) {
  403.  
  404.                     if (rand() % 2 == 1) {
  405.                     } else {
  406.                     }
  407.                     this->doOnceWinner = true;
  408.                 }
  409.  
  410.                 app.draw(this->tableWinSpr);
  411.  
  412.                 if (this->delayShowWinner >= 3.f) {
  413.                     this->lockedShowWinner = false;
  414.                     this->delayShowWinner = 0;
  415.                 }
  416.  
  417.                 if (!lockedShowWinner) {
  418.  
  419.                     this->RestartMatch();
  420.                 } else {
  421.  
  422.                     this->delayShowWinner += dt;
  423.                 }
  424.             }
  425.             if (this->showTie) {
  426.  
  427.                 if (!this->doOnceWinner) {
  428.                     this->doOnceWinner = true;
  429.                 }
  430.  
  431.                 app.draw(this->tieSpr);
  432.  
  433.                 if (this->delayShowWinner >= 3.f) {
  434.                     this->lockedShowWinner = false;
  435.                     this->delayShowWinner = 0;
  436.                 }
  437.  
  438.                 if (!lockedShowWinner) {
  439.  
  440.                     this->RestartMatch();
  441.                 } else {
  442.  
  443.                     this->delayShowWinner += dt;
  444.                 }
  445.             }
  446.             if (this->bet == 0 && this->chips == 0) {
  447.                 app.close();
  448.             }
  449.         }
  450.     } else {
  451.         this->updGame = false;
  452.     }
  453. }
  454.  
  455. void TableHandle::EventBet() {
  456.     if (chips > 0) {
  457.         bet += 20;
  458.         chips -= 20;
  459.     }
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement