Facebook
From Mungo Pheasant, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 288
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8.  
  9. class Character
  10. {
  11. private:
  12.         int _hp;
  13.         string _nazwa;
  14.  
  15. public:
  16.  
  17.         void setHp(int hp)
  18.         {
  19.                 _hp = hp;
  20.         }
  21.  
  22.         int getHp()
  23.         {
  24.                 return _hp;
  25.         }
  26.  
  27.         void setName(string nazwa)
  28.         {
  29.                 _nazwa = nazwa;
  30.         }
  31.  
  32.  
  33.         string getName()
  34.         {
  35.                 return _nazwa;
  36.         }
  37.  
  38.         string to_String()
  39.         {
  40.                 stringstream s;
  41.                 s << "Postac nazywa sie " << _nazwa << " i ma " << _hp << " punktow zycia" << endl;
  42.                 return s.str();
  43.         }
  44. };
  45.  
  46.  
  47. class CharacterEditor
  48. {
  49. private:
  50.         vector<Character> characters;
  51.  
  52. public:
  53.  
  54.         Character createCharacter()                                                  // tworzenie postaci
  55.         {
  56.                 Character temp;
  57.  
  58.                 cout << "Podaj nazwe: ";
  59.                 string nazwa;
  60.                 cin >> nazwa;
  61.                 cout << "Podaj ilosc zycia: ";
  62.                 int hp;
  63.                 cin >> hp;
  64.                 temp.setName(nazwa);
  65.                 temp.setHp(hp);
  66.                 characters.push_back(temp);
  67.  
  68.                 return temp;
  69.         }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.         string to_String()                                                          // tworzy jeden spojny string
  76.         {
  77.                 cout << "Nasze postacie:" << endl;
  78.  
  79.                 stringstream s;
  80.                 int j = 0;
  81.                 for (unsigned int i = 0; i < characters.size(); i++)
  82.                 {
  83.                         s << "Nr:" << j << "  " << characters[i].to_String();             // numer j, żeby móc edytować
  84.                         j++;
  85.                 }
  86.                 return s.str();
  87.         }
  88.  
  89.  
  90.  
  91.         void saveToFile(string zapis)                                              // zapisywanie postaci
  92.         {
  93.                 fstream plik;
  94.                 plik.open(zapis, ios::out);
  95.  
  96.                 for (unsigned int i = 0; i < characters.size(); i++)
  97.                 {
  98.  
  99.                         plik << characters[i].getName();
  100.                         plik << ":";
  101.                         plik << characters[i].getHp() << ";" << endl;                      // rozdzielenie elementów obiektu dwukropek, koniec obiektu średnik;
  102.  
  103.                 }
  104.  
  105.                 plik.close();
  106.         }
  107.  
  108.  
  109.         void loadFromFile(string wczytaj)                                         // wczytywanie postaci
  110.         {
  111.                 characters.clear();
  112.  
  113.                 int y = 0;
  114.                 string linia, tekst, tekst2;
  115.  
  116.                 fstream plik2;
  117.                 plik2.open(wczytaj, ios::in);
  118.  
  119.                 if (plik2.good() == false)
  120.                 {
  121.                         cout << "Plik nie istnieje!" << endl;
  122.                 }
  123.  
  124.                 while (getline(plik2, linia))
  125.                 {
  126.                         Character temp;
  127.  
  128.                         tekst = "";
  129.                         tekst2 = "";
  130.                         size_t dwukropek_pozycja = linia.find(':');
  131.                         size_t srednik_pozycja = linia.find(';');
  132.                         tekst.insert(0, linia, 0, dwukropek_pozycja);                                                      // kopiowanie nazwy
  133.                         tekst2.insert(0, linia, dwukropek_pozycja + 1, srednik_pozycja - dwukropek_pozycja - 1);          // okreslenie dlugosci znakow ile ma hp
  134.                         y = atoi(tekst2.c_str());
  135.  
  136.                         temp.setName(tekst);
  137.                         temp.setHp(y);
  138.                         characters.push_back(temp);
  139.                 }
  140.  
  141.  
  142.                 plik2.close();
  143.         }
  144.  
  145.  
  146.         void editCharacter()                                                      // edycja postaci
  147.         {
  148.                 int n = 0;
  149.                 char c = '\0';
  150.                 int zdrowie = 0;
  151.                 string nazwa;
  152.                 cout << "Postac pod ktorym numerem chcesz edytowac? " << endl;
  153.                 cin >> n;
  154.                 cout << "Wybrales edycje postaci nr: " << n << " o imieniu " + characters[n].getName() + " ktora ma " << characters[n].getHp() << " punktow zdrowia" << endl;
  155.                 cout << "Chcesz edytowac imie(n) czy zdrowie(z)?" << endl;
  156.                 cin >> c;
  157.                 if (c == 'n') {
  158.                         cout << "Podaj nowa nazwe" << endl;
  159.                         cin >> nazwa;
  160.                         characters[n].setName(nazwa);
  161.                 }
  162.                 else if (c == 'z') {
  163.                         cout << "Podaj nowa liczbe punktow zdrowie" << endl;
  164.                         cin >> zdrowie;
  165.                         characters[n].setHp(zdrowie);
  166.                 }
  167.                 else {
  168.                         cout << "Wpisales zla litere!" << endl;
  169.                 }
  170.  
  171.         }
  172.  
  173.  
  174. };
  175.  
  176.  
  177. int main()
  178. {
  179.         CharacterEditor postacie;
  180.         int wybor = 9;
  181.  
  182.  
  183.         for (;;)
  184.         {
  185.  
  186.                 cout << endl;
  187.                 cout << "MENU GLOWNE" << endl;
  188.                 cout << "-----------------" << endl;
  189.                 cout << "1. Dodaj postac" << endl;
  190.                 cout << "2. Zapisz" << endl;
  191.                 cout << "3. Wczytaj" << endl;
  192.                 cout << "4. Wyswietl postacie" << endl;
  193.                 cout << "5. Edycja postaci" << endl;
  194.                 cout << "6. Koniec programu" << endl;
  195.  
  196.  
  197.                 cout << endl;
  198.                 wybor = getchar();
  199.  
  200.                 switch (wybor)
  201.                 {
  202.                 case '1':
  203.                         postacie.createCharacter();
  204.                         break;
  205.  
  206.                 case '2':
  207.                         postacie.saveToFile("postacie.dat");
  208.                         break;
  209.  
  210.                 case '3':
  211.                         postacie.loadFromFile("postacie.dat");
  212.                         break;
  213.  
  214.                 case '4':
  215.                         cout << postacie.to_String();
  216.                         cout << "Aby kontynuowac wcisnij enter";
  217.                         cin.get();
  218.                         break;
  219.  
  220.                 case '5':
  221.                         cout << postacie.to_String() << endl;
  222.                         postacie.editCharacter();
  223.                         break;
  224.  
  225.                 case '6':
  226.                         exit(0);
  227.                         break;
  228.  
  229.                 default: cout << "Nie ma takiej opcji w menu!";
  230.                 }
  231.  
  232.                 cin.get();
  233.                 system("cls");
  234.         }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.         cin.get();
  241.         cin.get();
  242.  
  243. }