SHARE
TWEET

Untitled

a guest Apr 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include"Deck.h"
  3. #include<cstring>
  4. #include"YuGiOhCard.h"
  5. #include<iostream>
  6. #include<fstream>
  7.  
  8. using namespace std;
  9.  
  10. Deck::Deck()
  11. {
  12.     for (int i = 0; i < 40; i++)
  13.     {
  14.         YuGiOhCardList[i] = YuGiOhCard();
  15.     }
  16. }
  17. int Deck::numberOfSpellCards() const
  18. {
  19.     int counter=40;
  20.     for (int i = 0; i < 40; i++)
  21.     {
  22.         if (this->YuGiOhCardList[i].getIsMonster() == 1)
  23.         {
  24.             counter--;
  25.         }
  26.     }
  27.     return counter;
  28. }
  29. int Deck::numberOfMonsterCards() const
  30. {
  31.     int counter = 0;
  32.     for (int i = 0; i < 40; i++)
  33.     {
  34.         if (this->YuGiOhCardList[i].getIsMonster() == 1)
  35.         {
  36.             counter++;
  37.         }
  38.     }
  39.     return counter;
  40. }
  41. void Deck::DeckSetTwoCards(int firstNumber, int secondNumber)
  42. {
  43.     char*name = new char[58];
  44.     strcpy(name, "Wild Dragon");
  45.     YuGiOhCardList[firstNumber] = YuGiOhCard(2500, 3000, name, true);// zashto ako tuka e "Neeshto si " ne stava
  46.     strcpy(name, "Strong stench");
  47.     YuGiOhCardList[secondNumber] = YuGiOhCard(0, 0, name, false);
  48. }
  49. bool Deck::getIsMonster(int i) const
  50. {
  51.     return this->YuGiOhCardList[i].getIsMonster();
  52. }
  53. char* Deck::getYuGiOhCardListName(int i) const
  54. {
  55.     return this->YuGiOhCardList[i].getName();
  56. }
  57. void Deck::setYuGiOhCardListName(char*name, int i)
  58. {
  59.     YuGiOhCardList[i].SetName(name);
  60. }
  61. void Deck::printDeck()
  62. {
  63.     for (int i = 0; i < 40; i++)
  64.     {
  65.         YuGiOhCardList[i].print();
  66.     }
  67. }
  68. //void Deck::ChangeCard(char*name, Deck deck)
  69. //{
  70. //  for (int i = 0; i < 40; i++)
  71. //  {
  72. //      if (strcmp(YuGiOhCardList[i].getName(), name) == 0)
  73. //      {
  74. //          char newName[58];
  75. //          std::cout << "Input new name";
  76. //          std::cin.getline(newName, 58);
  77. //          YuGiOhCardList[i] = YuGiOhCard(0, 0, newName, 0);
  78. //      }
  79. //  }
  80. //}
  81. void Deck::ChangeCard(char*name, Deck deck)
  82. {
  83.     for (int i = 0; i < 40; i++)
  84.     {
  85.         if (strcmp(YuGiOhCardList[i].getName(), name) == 0)
  86.         {
  87.             if (deck.getIsMonster(i) == true)
  88.             {
  89.                 int newAtPoints;
  90.                 std::cout << "Input new Attack Points: ";
  91.                 std::cin >> newAtPoints;
  92.                 int newDefPoints;
  93.                 std::cout << "Input new Defence Points ";
  94.                 std::cin >> newDefPoints;
  95.                 char newName[58];
  96.                 std::cin.ignore();
  97.                 std::cout << "Enter new name: ";
  98.                 std::cin.getline(newName, 57);
  99.                 YuGiOhCardList[i] = YuGiOhCard(newAtPoints, newDefPoints, newName, true); break;
  100.             }
  101.             else
  102.             {
  103.                 char newName[58];
  104.                 std::cin.getline(newName, 58);
  105.                 YuGiOhCardList[i] = YuGiOhCard(0, 0, newName, 0);
  106.             }
  107.         }
  108.     }
  109. }
  110. char* serializeCard(YuGiOhCard yugiohCard) {
  111.     // 58 bytes - name, 4 bytes - attackPoints, 4 bytes - defencePoints, 1 byte - isMonster
  112.     char* data = new char[67];
  113.     for (int i = 0; i < 58; i++)
  114.     {
  115.         data[i] = yugiohCard.getName()[i];
  116.     }
  117.     int attackPoints = yugiohCard.getAttP();
  118.     int deffencePoints = yugiohCard.getDefP();
  119.     data[61] = attackPoints;
  120.     data[60] = attackPoints >> 8;
  121.     data[59] = attackPoints >> 16;
  122.     data[58] = attackPoints >> 24;
  123.     data[65] = deffencePoints;
  124.     data[64] = deffencePoints >> 8;
  125.     data[63] = deffencePoints >> 16;
  126.     data[62] = deffencePoints >> 24;
  127.     data[66] = yugiohCard.getIsMonster();
  128.     return data;
  129. }
  130. void Deck::saveIntoFile(const char* fileName) {
  131.     ofstream deckFile(fileName, ios::out | ios::binary);
  132.     for (int i = 0; i < 40; i++)
  133.     {
  134.         char* data = serializeCard(YuGiOhCardList[i]);
  135.         deckFile.write(data, 67);
  136.     }
  137.     deckFile.close();
  138. }
  139.  
  140. YuGiOhCard deserializeCard(char* data) {
  141.     YuGiOhCard yugiohcard;
  142.     char name[58];
  143.     for (int i = 0; i < 58; i++)
  144.     {
  145.         name[i] = data[i];
  146.     }
  147.     yugiohcard.SetName(name);
  148.     int attackPoints = data[58] | ((int)data[59] << 8) | ((int)data[60] << 16) | ((int)data[61] << 24);
  149.     int deffencePoints = data[62] | ((int)data[63] << 8) | ((int)data[64] << 16) | ((int)data[65] << 24);
  150.     bool isMonster = data[66];
  151.     yugiohcard.setAttP(attackPoints);
  152.     yugiohcard.setDefP(deffencePoints);
  153.     yugiohcard.setIsMonster(isMonster);
  154.     return yugiohcard;
  155. }
  156. Deck::Deck(const char* fileName) {
  157.     char data[67];
  158.     ifstream deckFile(fileName, ios::in | ios::binary);
  159.     int i = 0;
  160.     if (deckFile.good()) {
  161.         while (!deckFile.read(data, 67)) {
  162.             YuGiOhCardList[i] = deserializeCard(data);
  163.             i++;
  164.         }
  165.     }
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top