Guest User

Untitled

a guest
Jul 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class CreatureCaretaker
  8. {
  9. private:
  10.     int creaturePow;
  11.     int creatureLife;
  12.     string creatureName;
  13.  
  14.     int creatureNum;
  15.     int nodeNum;
  16.  
  17.     int creatureList[11];
  18.     string listSortName[11];
  19.     int listSortPow[11];
  20.     int listSortLife[11];
  21.  
  22.     int arrayAccess;
  23.  
  24.     void swapName(string *element1, string *element2)
  25.     {
  26.         string temp;
  27.  
  28.         temp = *element1;
  29.         *element1 = *element2;
  30.         *element2 = temp;
  31.     }
  32.  
  33.     void swap(int *element1, int *element2)
  34.     {
  35.         int temp = 0;
  36.  
  37.         temp = *element1;
  38.         *element1 = *element2;
  39.         *element2 = temp;
  40.     }
  41.  
  42.     CreatureCaretaker *head, *tail, *right, *left, *newNode;
  43. public:
  44.     CreatureCaretaker()
  45.     {
  46.         creaturePow = 0;
  47.         creatureLife = 0;
  48.         creatureNum = 0;
  49.         creatureName = "Default Slot";
  50.         arrayAccess = 0;
  51.  
  52.     }
  53.  
  54.     CreatureCaretaker(int givenPow, int givenLife, string givenName)
  55.     {
  56.         this->creaturePow = givenPow;
  57.         this->creatureLife = givenLife;
  58.         this->creatureName = givenName;
  59.     }
  60.  
  61.     void addCreature(int creaturePow, int creatureLife, string creatureName);
  62.     void showCreature();
  63.     void removeCreature();
  64.     void exchangeSortPow(int givenPow[], int givenLife[], string givenName[], int size);
  65.     void exchangeSortLife(int givenPow[], int givenLife[], string givenName[], int size);
  66.     void showPow();
  67.     void showLife();
  68.  
  69. }; CreatureCaretaker ooo;
  70.  
  71. void CreatureCaretaker::addCreature(int creaturePow, int creatureLife, string creatureName)
  72. {
  73.     if(head == NULL)
  74.     {
  75.         head = new CreatureCaretaker(creaturePow, creatureLife, creatureName);
  76.         tail = head;
  77.         head->left = NULL;
  78.         head->right = newNode;
  79.         listSortPow[arrayAccess] = creaturePow;
  80.         listSortLife[arrayAccess] = creatureLife;
  81.         listSortName[arrayAccess] = creatureName;
  82.     }
  83.     else
  84.     {
  85.         arrayAccess += 1;
  86.         if(arrayAccess > 0)
  87.         {
  88.             newNode = new CreatureCaretaker(creaturePow, creatureLife, creatureName);
  89.             tail->right = newNode;
  90.             newNode->left = tail;
  91.             tail = tail->right;
  92.             tail->right = NULL;
  93.  
  94.             listSortPow[arrayAccess] = creaturePow;
  95.             listSortLife[arrayAccess] = creatureLife;
  96.             listSortName[arrayAccess] = creatureName;
  97.         }
  98.     }
  99. }
  100.  
  101. void CreatureCaretaker::showCreature()
  102. {
  103.     CreatureCaretaker *tmp;
  104.     tmp = head;
  105.  
  106.     if(tmp == NULL)
  107.         cout << "head is NULL";
  108.  
  109.     for(creatureNum = 1; tmp != NULL; creatureNum++)
  110.     {
  111.         if(creatureNum < 11)
  112.         {
  113.             cout << "Creature Number - " << creatureNum << endl;
  114.             cout << "Name - " << tmp->creatureName << endl;
  115.             cout << "Power - " << tmp->creaturePow << endl;
  116.             cout << "Life Points - " << tmp->creatureLife << endl << endl;
  117.             tmp = tmp->right;
  118.             this->creatureNum = creatureNum;
  119.             creatureList[creatureNum];
  120.         }
  121.     }
  122. }
  123.  
  124. void CreatureCaretaker::removeCreature()
  125. {
  126.     cout << "Remove which creature number?" << endl;
  127.     cin >> nodeNum;
  128.  
  129.     CreatureCaretaker *tmp, *oldTmp, *frontTmp;
  130.     tmp = head;
  131.  
  132.     if(nodeNum > tmp->creatureNum)
  133.         return;
  134.  
  135.     if(nodeNum == 1)
  136.     {
  137.         frontTmp = head;
  138.         head = frontTmp->right;
  139.         free(frontTmp);
  140.     }
  141.     else
  142.     {
  143.         oldTmp = tmp;
  144.         for(int counter = 2; counter <= nodeNum; counter++)
  145.         {
  146.             oldTmp = tmp;
  147.             tmp = tmp->right;
  148.         }
  149.  
  150.         oldTmp->right = tmp->right;
  151.         free(tmp);
  152.     }
  153. }
  154.  
  155. void CreatureCaretaker::exchangeSortPow(int givenPow[], int givenLife[], string givenName[], int size)
  156. {
  157.     int counter1, counter2;
  158.  
  159.     for(counter1 = 0; counter1 < size - 1; counter1++)
  160.     {
  161.         for(counter2 = counter1 + 1; counter2 < size; counter2++)
  162.         {
  163.             if(givenPow[counter1] > givenPow[counter2])
  164.             {
  165.                 swap(&givenPow[counter1], &givenPow[counter2]);
  166.                 swap(&givenLife[counter1], &givenLife[counter2]);
  167.                 swapName(&givenName[counter1], &givenName[counter2]);
  168.             }
  169.         }
  170.     }
  171. }
  172.  
  173. void CreatureCaretaker::exchangeSortLife(int givenPow[], int givenLife[], string givenName[], int size)
  174. {
  175.     int counter1, counter2;
  176.  
  177.     for(counter1 = 0; counter1 < size - 1; counter1++)
  178.     {
  179.         for(counter2 = counter1 + 1; counter2 < size; counter2++)
  180.         {
  181.             if(givenLife[counter1] > givenLife[counter2])
  182.             {
  183.                 swap(&givenPow[counter1], &givenPow[counter2]);
  184.                 swap(&givenLife[counter1], &givenLife[counter2]);
  185.                 swapName(&givenName[counter1], &givenName[counter2]);
  186.             }
  187.         }
  188.     }
  189. }
  190.  
  191. void CreatureCaretaker::showPow()
  192. {
  193.     exchangeSortPow(listSortPow, listSortLife, listSortName, creatureNum);
  194.  
  195.     int counter;
  196.     counter = 1;
  197.  
  198.     while(counter < creatureNum)
  199.     {
  200.         cout << "Creature Number - " << counter << endl;
  201.         cout << "Name - " << listSortName[counter] << endl;
  202.         cout << "Power - " << listSortPow[counter] << endl;
  203.         cout << "Life Points - " << listSortLife[counter] << endl << endl;
  204.         counter++;
  205.     }
  206. }
  207.  
  208. void CreatureCaretaker::showLife()
  209. {
  210.     exchangeSortLife(listSortPow, listSortLife, listSortName, creatureNum);
  211.    
  212.     int count;
  213.     int counter = creatureNum - 1;
  214.     for(count = 1; counter > 0; count++)
  215.     {
  216.         cout << "Creature Number - " << count << endl;
  217.         cout << "Name - " << listSortName[counter] << endl;
  218.         cout << "Power - " << listSortPow[counter] << endl;
  219.         cout << "Life Points - " << listSortLife[counter] << endl << endl;
  220.         counter--;
  221.     }
  222. }
  223.  
  224. int main()
  225. {
  226.             CreatureCaretaker *head;
  227.  
  228.             int selectNumber;
  229.             int creaturePow = 10;
  230.             int creatureLife = 11;
  231.             int creatureSlot = 10;
  232.             string creatureName = "asd asd";
  233.  
  234.             cout << "Welcome to the Creature Caretaker!" << endl;
  235.             cout << "What would you like to do?" << endl << endl;
  236.  
  237.             cout << "1 - Add a creature" << endl;
  238.             cout << "2 - Remove a creature" << endl;
  239.             cout << "3 - Show game creatures" << endl;
  240.             cout << "4 - Sort creatures by power" << endl;
  241.             cout << "5 - Sort creatures by life points" << endl;
  242.             cout << "6 - Exit the Program" << endl;
  243.  
  244.             cout << "\nEnter chosen option: ";
  245.             cin >> selectNumber;
  246.  
  247.             if (selectNumber == '1')
  248.             {
  249.                
  250.                         cout << "\nAdding a creature in now." << endl;
  251.  
  252.                         cout << "\nEnter creature name as text." << endl;
  253.                         cin >> creatureName;
  254.  
  255.                         cout << "\nEnter card power as number." << endl;
  256.                         cin >> creaturePow;
  257.  
  258.                         cout << "\nEnter card life points as number." << endl;
  259.                         cin >> creatureLife;
  260.  
  261.                         if((creaturePow >= 1 && creaturePow <= 20) && (creatureLife >= 1 && creatureLife <= 20))
  262.                         {
  263.                                 ooo.addCreature(creaturePow, creatureLife, creatureName);
  264.                         }
  265.                         else
  266.                             {
  267.                                 cout << endl << "Invalid Input!" << endl;
  268.                             }
  269.                
  270.             }
  271.  
  272.  
  273.  
  274.    
  275.  
  276.     /*ooo.addCreature(1, 12, "a");
  277.     /*ooo.addCreature(2, 32, "b");
  278.     ooo.addCreature(3, 67, "c");
  279.     ooo.addCreature(4, 43, "d");
  280.     ooo.addCreature(5, 14, "e");
  281.     ooo.addCreature(6, 11, "f");
  282.     ooo.addCreature(7, 12, "g");
  283.     ooo.addCreature(8, 13, "h");
  284.     ooo.addCreature(9, 14, "i");
  285.     ooo.addCreature(10, 11, "j");*/
  286.     ooo.showCreature();
  287.  
  288.     cout << "Ascending Order (Power)" << endl;
  289.     ooo.showPow();
  290.  
  291.     cout << "Descending Order (Life)" << endl;
  292.     ooo.showLife();
  293.  
  294.    
  295.     getch();
  296. }
Add Comment
Please, Sign In to add comment