Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Source.h"
- #include <conio.h>
- #include <iostream>
- #include <ctime>
- #include <string>
- #include <fstream>
- #include <istream>
- #include <sstream>
- #include <cstdlib>
- #include <windows.h>
- using namespace std;
- Virus** Global::virus = nullptr;
- User** Global::users = nullptr;
- int Global::virus_counter = 0;
- int Global::active_virus = 0;
- int Global::user_counter = 0;
- int Global::Userint = 0;
- // operatory
- ostream& operator<<(ostream &out, User **users)
- {
- for (int i = 0; i < Global::user_counter; i++)
- {
- cout << "Numer uzytkownika: " << i << endl;
- cout << "" << users[i]->getLogin() << endl;
- cout << endl;
- }
- return out;
- }
- istream& operator>>(istream& in, User* usr)
- {
- string l, p;
- cout<<"Wpisz login: "<<endl;
- cin >> l;
- cout<<"Wpisz haslo: "<<endl;
- cin>>p;
- usr->setLogin(l);
- usr->setPassword(p);
- return in;
- }
- // konstruktor kopiujący
- Virus::Virus(Virus *virusC)
- {
- this->type = virusC->getType();
- this->name = virusC->getName();
- this->size = virusC->getSize();
- this->range = virusC->getRange();
- this->power = new Power(virusC->power->spreadSpeed);
- }
- //
- //konstruktor z wartosciami domyslnymi
- Virus::Virus()
- {
- power = new Power;
- type = 1;
- name = "Default";
- size = 50;
- range = 0;
- }
- string& Virus::getName()
- {
- return name;
- }
- float Virus::getSize()
- {
- return size;
- }
- int Virus::getType()
- {
- return type;
- }
- float Virus::getRange()
- {
- return range;
- }
- void Virus::set(string n, float m)
- {
- }
- void Virus::set(string n, float m, float e)
- {
- }
- void Virus::set(string n, float m, float e, float u)
- {
- }
- void DNA::EpidemicRate()
- {
- range = size * (*power)->spreadSpeed; // przeciazona ->
- cout << "Wirus zadziala w promieniu: " << (*power)->spreadSpeed <<"km."<< endl;
- }
- void DNA::set(string n, float m)
- {
- name = n;
- size = m;
- type = 1;
- }
- float DNA::getMultiplier2()
- {
- return 0;
- }
- void DNA::view()
- {
- cout << "Nazwa wirusa: " << getName() << endl;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << getSize() << endl;
- cout << "Typ: wirus DNA" << endl;
- }
- void RNA::EpidemicRate()
- {
- range = size * (*power)->spreadSpeed * (multiplier*0.4);
- cout << "Ten wirus wywoluje epidemie w zasiegu: " << range << "km." << endl;
- }
- void RNA::set(string n, float m, float e)
- {
- this->name = n;
- this->size = m;
- this->multiplier = e;
- this->type = 2;
- }
- float RNA::getMultiplier()
- {
- return multiplier;
- }
- float RNA::getMultiplier2()
- {
- return 0;
- }
- void RNA::view()
- {
- cout << "Nazwa wirusa: " << getName() << endl;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << getSize() << endl;
- cout << "Podaj mnoznik: " << getMultiplier() << endl;
- cout << "Typ: wirus RNA" << endl;
- }
- float Hybrid::getZone()
- {
- return zone;
- }
- float Hybrid::getEnvirons()
- {
- return environs;
- }
- void Hybrid::EpidemicRate()
- {
- range = size * (*power)->spreadSpeed;
- zone = (multiplier * multiplier2) * (*power)->spreadSpeed;
- environs = size * (*power)->spreadSpeed * (multiplier*0.4);
- cout << "Wirus zadziała w promieniu: " << range << "km." << endl;
- cout << "Skazenie utrzyma sie w promieniu: " << zone << "km." << endl;
- cout << "Fauna i flora skazona w promienu: " << environs << "km."<< endl;
- }
- void Hybrid::set(string n, float m, float e, float u)
- {
- name = n;
- size = m;
- multiplier = e;
- multiplier2 = u;
- type = 3;
- }
- float Hybrid::getMultiplier()
- {
- return type;
- }
- float Hybrid::getMultiplier2()
- {
- return multiplier2;
- }
- void Hybrid::view()
- {
- cout << "Nazwa: " << getName() << endl;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << getSize() << endl;
- cout << "Mnoznik: " << getMultiplier() << endl;
- cout << "Podaj mnoznik mutagenu: " << getMultiplier2() << endl;
- cout << "Typ: Hybryda" << endl;
- }
- void Global::viewallViruses()
- {
- int x;
- for (int i = 0; i < virus_counter; i++)
- {
- cout << "Numer: " << i << endl;
- cout << "Nazwa: " << virus[i]->getName() << endl;
- x = virus[i]->getType();
- switch (x)
- {
- case 1:
- cout << "Typ: DNA." << endl;
- break;
- case 2:
- cout << "Typ: RNA." << endl;
- break;
- case 3:
- cout << "Typ: Hybryda." << endl;
- break;
- }
- cout << endl;
- }
- }
- //++GLOBAL==
- //dynamiczne zarządzanie pamięcią
- bool Global::insertViruses(int index, int type)
- {
- if (index != 0)
- {
- if (index < 0 || virus_counter <= index)
- {
- cout << "Nieprawidlowy indeks" << endl;
- _getch();
- return false;
- }
- }
- Virus **new_virus = new Virus*[virus_counter + 1];
- for (int i = 0; i < virus_counter + 1; i++)
- {
- if (i < index)
- new_virus[i] = virus[i];
- else if (i == index)
- new_virus[i] = VirusCreator::createVirus(type);
- else if (i > index)
- new_virus[i] = virus[i - 1];
- }
- delete[] virus;
- virus = new_virus;
- virus_counter++;
- return true;
- }
- Virus* Global::AddViruses(int type)
- {
- Virus **new_virus = new Virus*[virus_counter + 1];
- for (int i = 0; i < virus_counter; i++)
- {
- new_virus[i] = virus[i];
- }
- Virus *new1 = VirusCreator::createVirus(type);
- new_virus[virus_counter] = new1;
- delete[] virus;
- virus = new_virus;
- virus_counter++;
- return new1;
- }
- void Global::DeleteViruses(int index)
- {
- int x;
- Virus **new_virus = new Virus*[virus_counter - 1];
- int j = 0;
- for (int i = 0; i < virus_counter; i++)
- {
- if (i != index)
- {
- new_virus[j++] = virus[i];
- }
- else
- delete virus[i];
- }
- delete[] virus;
- virus = new_virus;
- virus_counter--;
- }
- void Global::viewUsers()
- {
- for (int i = 0; i < user_counter; i++)
- {
- cout << "Numer uzytkownika: " << i << endl;
- cout << "" << users[i]->getLogin() << endl;
- cout << endl;
- }
- }
- //dodawanie użytkowników
- void Global::AddUser()
- {
- User **new_user = new User*[user_counter + 1];
- for (int i = 0; i < user_counter; i++)
- {
- new_user[i] = users[i];
- }
- new_user[user_counter] = new User;
- delete[] virus;
- users = new_user;
- user_counter++;
- }
- void Global::DeleteUser(int index)
- {
- int x;
- User **new_user = new User*[user_counter - 1];
- int j = 0;
- for (int i = 0; i < user_counter; i++)
- {
- if (i != index)
- {
- new_user[j++] = users[i];
- }
- else
- delete users[i];
- }
- delete[] users;
- users = new_user;
- user_counter--;
- Global::Userint--;
- }
- //usuwanie tablic wskaźników
- void Global::destroyVirusesArr()
- {
- for (int i = 0; i < virus_counter; i++)
- {
- delete virus[i];
- }
- delete[] virus;
- }
- void Global::destoryUsers()
- {
- for (int i = 0; i < user_counter; i++)
- {
- delete users[i];
- }
- delete[] users;
- }
- //wczytywanie z pliku
- void Global::loadViruses()
- {
- string name, size, i, multiplier, type, mutagen;
- int Ii, Isize, Imultiplier, Itype, Imutagen;
- ifstream file("viruses.txt");
- getline(file, i);
- Ii = atoi(i.c_str());
- for (int i = 0; i < Ii; i++)
- {
- getline(file, type);
- Itype = atoi(type.c_str());
- switch (Itype)
- {
- case 1:
- {
- getline(file, name, ';') && getline(file, size);
- Isize = atoi(size.c_str());
- AddViruses(1);
- virus[i]->set(name, Isize);
- }
- break;
- case 2:
- {
- getline(file, name, ';') && getline(file, size, ';') && getline(file, multiplier);
- Isize = atoi(size.c_str());
- Imultiplier = atoi(multiplier.c_str());
- AddViruses(2);
- virus[i]->set(name, Isize, Imultiplier);
- }
- break;
- case 3:
- {
- getline(file, name, ';') && getline(file, size, ';') && getline(file, multiplier, ';') && getline(file, mutagen);
- Isize = atoi(size.c_str());
- Imultiplier = atoi(multiplier.c_str());
- Imutagen = atoi(mutagen.c_str());
- AddViruses(3);
- virus[i]->set(name, Isize, Imultiplier, Imutagen);
- }
- break;
- }
- }
- file.close();
- }
- //zapisywanie do pliku
- void Global::saveViruses()
- {
- int type;
- ofstream file("viruses.txt");
- file << virus_counter << endl;
- for (int i = 0; i < virus_counter; i++)
- {
- type=virus[i]->getType();
- file << type << endl;
- switch (type)
- {
- case 1:
- {
- file << virus[i]->getName() << ";" << virus[i]->getSize() << endl;
- }
- break;
- case 2:
- {
- file << virus[i]->getName() << ";" << virus[i]->getSize() << ";" << virus[i]->getMultiplier() << endl;
- }
- break;
- case 3:
- {
- file << virus[i]->getName() << ";" << virus[i]->getSize() << ";" << virus[i]->getMultiplier() << ";" << virus[i]->getMultiplier2() << endl;
- }
- break;
- }
- }
- file.close();
- }
- //zapisywanie do pliku
- void Global::saveUsers()
- {
- ofstream file("users.txt");
- file << user_counter << endl;
- for (int i = 0; i < user_counter; i++)
- {
- file << users[i]->getLogin() << ";" << users[i]->getPassword() << endl;
- }
- }
- //wczytywanie z pliku
- void Global::loadUsers()
- {
- string login, pass, i;
- int Ii;
- ifstream file("users.txt");
- getline(file, i);
- Ii = atoi(i.c_str());
- for (int i = 0; i < Ii; i++)
- {
- getline(file, login, ';') && getline(file, pass);
- AddUser();
- users[i]->setLogin(login);
- users[i]->setPassword(pass);
- }
- }
- void Global::log_in()
- {
- char x;
- int i;
- bool l;
- string log, pass;
- cout << endl << endl << endl;
- do
- {
- system("cls");
- cout << "Logowanie (Z) / Nowy Gracz (R)" << endl;
- x =_getch();
- switch(x)
- {
- case 'z':
- {
- system("cls");
- cout << "Podaj Login: " << endl;
- cout << "";
- cin>>log;
- i = findLog(log);
- Global::Userint = i;
- if (i != -1)
- {
- cout << "Podaj Haslo: " << endl;
- cout << "";
- cin >> pass;
- if (users[i]->checkPassword(pass) == true)
- return;
- else
- {
- cout << "Zle haslo!" << endl;
- cout << "Nacisnij dowolny klawisz, aby kontynuowac." << endl;
- _getch();
- }
- }
- else
- {
- cout << "Gracz o podanym loginie nie istnieje!" << endl;
- cout << "Nacisnij dowolny klawisz aby kontynuowac." << endl;
- _getch();
- }
- }
- break;
- case 'r':
- {
- registration(); // wewnatrz tej metody przeciazony >>
- Global::Userint = user_counter-1;
- saveUsers();
- cout << "Tworzenie gracza zakonczone. Mozna sie logowac." << endl;
- cout << "Nacisnij dowolny przycisk, aby kontynuowac." << endl;
- _getch();
- }
- break;
- }
- } while (pass != "e");
- }
- void Global::registration()
- {
- /*string login, pass;
- cout << "Podaj nowy login: " << endl;
- cout << "";
- cin >> login;
- cout << "Podaj nowe haslo: " << endl;
- cout << "";
- cin >> pass;*/
- AddUser();
- cin>>users[user_counter-1];
- //users[user_counter - 1]->setLogin(login);
- //users[user_counter - 1]->setPassword(pass);
- }
- //++MENU++
- void Global::menu()
- {
- char control;
- int type, x, id;
- //int idC = 0;
- string nazwa;
- Virus* oneVirus;
- float size, mnoznik, mutagen, factor, multiplier;
- for (;;)
- {
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl<<endl;
- cout << "1 : Obliczanie zasiegu epidemii" << endl;
- cout << "2 : Wirusy" << endl;
- cout << "3 : Baza graczy" << endl;
- cout << "4 : Wyjscie" << endl;
- control = _getch();
- switch (control)
- {
- case '1':
- {
- if (virus_counter == 0)
- {
- cout << "Nie ma jeszcze zadnych wirusow!" << endl;
- _getch();
- break;
- }
- do
- {
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl << endl;
- cout << "1 : wybierz wirus" << endl;
- cout << "2 : aby obliczyc zniszczenia" << endl;
- cout << "3 : aby wrocic" << endl;
- // wywalone wyswietlanie wspolczynnika
- cout << "Aktualnie wybrany wirus: " << endl << endl;
- virus[Global::active_virus]->view(); // wyciek pamięci1
- x = _getch();
- switch (x)
- {
- case '1':
- {
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl << endl;
- if (active_virus >= 0)
- Global::viewallViruses();
- else
- cout << "Brak dostepnych wirusow." << endl;
- cout << "=========================================" << endl;
- cout << "Podaj numer wirusa: " << endl;
- cin >> active_virus;
- if ((active_virus > virus_counter) || (active_virus < 0))
- {
- cout << "Nie ma takiego wirusa." << endl;
- active_virus = 0;
- Sleep(500);
- break;
- }
- }
- break;
- // w tym miejscu wykorzystujemy polimorfizm
- case '2':
- {
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl << endl;
- virus[active_virus]->EpidemicRate();
- cout << endl << "Nacisnij dowolny przycisk, aby kontynuowac." << endl;
- _getch();
- }
- break;
- case '3':
- {
- }
- break;
- }
- }while(x != '3');
- }
- break;
- case '2': // dynamiczne zarządzanie pamięcią
- {
- do{
- system("cls");
- cout << "Zalogowano jako: " << (*users[Userint])() << endl << endl; // przeciazony ()
- cout << "Lista istniejacych wirusow: " << endl;
- Global::viewallViruses();
- cout << "n - stworz nowy wirus" << endl;
- cout << "u - usun wirus" << endl;
- cout << "i - insert " << endl;
- cout << "b - powrot" << endl;
- cout << "l - statystyki wirusa" << endl;
- x =_getch();
- switch (x)
- {
- case 'n':
- {
- cout << endl;
- cout << "1: DNA" << endl;
- cout << "2: RNA" << endl;
- cout << "3: Hybryda" << endl;
- cout << "Podaj typ wirusa: " << endl;
- type =_getch();
- switch (type)
- {
- case '1':
- {
- oneVirus = Global::AddViruses(1);
- cout << "Podaj nazwe: " << endl;
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cin >> size;
- virus[virus_counter - 1]->set(nazwa, size);
- cout << "Parametry dodanego wirusa" << endl;
- // podsumowanie parametrow wirusa ()
- for (int i = 0; i < 3; i++)
- {
- cout << (*virus[virus_counter - 1])[i]; // przeciazony []
- cout << endl;
- }
- (*oneVirus) = (*oneVirus); // przeciazony =
- _getch();
- }
- break;
- case '2':
- {
- oneVirus = Global::AddViruses(2);
- cout << "Podaj nazwe: " << endl;
- cout << "";
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cout << "";
- cin >> size;
- cout << "Podaj mnoznik: " << endl;
- cout << "";
- cin >> multiplier;
- virus[virus_counter - 1]->set(nazwa, size, multiplier);
- if (virus_counter > 1)
- {
- // sprawdza element przed dodanym elementem i nowododany element
- if ((*Global::virus[virus_counter - 2]) - (*oneVirus)) // przeciazony -
- cout << "Wirus na poprzedniej pozycji ma mniejszy rozmiar";
- else
- cout << "Wirus na poprzedniej pozycji ma wiekszy rozmiar";
- _getch();
- }
- }
- break;
- case '3':
- {
- Global::AddViruses(3);
- cout << "Podaj nazwe: " << endl;
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cin >> size;
- cout << "Podaj mnoznik: " << endl;
- cin >> mnoznik;
- cout << "Podaj mnoznik mutagenu: " << endl;
- cin >> mutagen;
- virus[virus_counter - 1]->set(nazwa, size, mnoznik, mutagen);
- if (virus_counter > 1)
- {
- // sprawdza element przed dodanym elementem i nowododany element
- if ((*Global::virus[virus_counter - 2]) + (*oneVirus)) // przeciazony +
- cout << "Wirus na poprzedniej pozycji ma wiekszy zasieg";
- else
- cout << "Wirus na poprzedniej pozycji ma mniejszy zasieg";
- _getch();
- }
- }
- break;
- default:
- {
- cout << "brak opcji" << endl;
- Sleep(700);
- }
- } //type
- }
- break;
- case 'l':
- {
- cout << "Podaj numer wirusa: " << endl;
- cout << "";
- cin >> id;
- if ((virus_counter == 0) || (id >= virus_counter) || (id < 0))
- {
- cout << "Nie ma takiego wirusa." << endl;
- active_virus = 0;
- Sleep(500);
- break;
- }
- system("cls");
- //cout << "Zalogowano jako: " << users[Userint]->getLogin() << endl << endl;
- cout << "Zalogowano jako: " << (*users[Userint])() << endl << endl;
- virus[id]->view();
- cout << endl;
- cout << "Nacisnij dowolny przycisk, aby kontynuowac." << endl;
- _getch();
- }
- break;
- case 'u':
- {
- cout << "Podaj numer wirusa: " << endl;
- cin >> id;
- if ((virus_counter == 0) || (id >= virus_counter) || (id < 0))
- {
- cout << "Nie ma takiego wirusa." << endl;
- active_virus = 0;
- Sleep(500);
- break;
- }
- DeleteViruses(id);
- }
- break;
- case 'i':
- {
- cout << "Podaj numer wirusa: " << endl;
- cin >> id;
- cout << "1: DNA" << endl;
- cout << "2: RNA" << endl;
- cout << "3: Hybryda" << endl;
- cout << "Podaj typ wirusa: " << endl;
- type =_getch();
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl << endl;
- switch (type)
- {
- case '1':
- {
- if (Global::insertViruses(id, 1)) // obsluzona wewnatrz weryfikacja wprowadzonego id
- {
- cout << "Podaj nazwe wirusa: " << endl;
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cin >> size;
- virus[id]->set(nazwa, size);
- }
- }
- break;
- case '2':
- {
- if (Global::insertViruses(id, 2))
- {
- cout << "Podaj nazwe wirusa: " << endl;
- cout << "";
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cout << "";
- cin >> size;
- cout << "Podaj mnoznik: " << endl;
- cout << "";
- cin >> mnoznik;
- virus[id]->set(nazwa, size, mnoznik);
- }
- }
- break;
- case '3':
- {
- if (Global::insertViruses(id, 3))
- {
- cout << "Podaj nazwe wirusa: " << endl;
- cin >> nazwa;
- cout << "Podaj rozmiar komorki wirusa (1-10): " << endl;
- cin >> size;
- cout << "Podaj mnoznik: " << endl;
- cin >> multiplier;
- cout << "Podaj mnoznik mutagenu: " << endl;
- cin >> mutagen;
- virus[id]->set(nazwa, size, mnoznik, mutagen);
- }
- }
- break;
- default:
- {
- cout << "brak opcji" << endl;
- Sleep(700);
- }
- } //type
- }
- break;
- break;
- }
- } while (x != 'b');
- }
- break;
- case '4':
- {
- return;
- }
- break;
- case '3':
- {
- int deli;
- string pass;
- do
- {
- system("cls");
- cout << "Gracz: " << users[Userint]->getLogin() << endl << endl;
- cout << users; // przeciążenie ostream
- cout << "b - powrot." << endl;
- cout << "u - usun gracza" << endl;
- x =_getch();
- switch (x)
- {
- case 'u':
- {
- cout << "Ktorego uzytkownika chcesz usunac?" << endl;
- cout << "";
- cin >> deli;
- if (deli >= user_counter || deli < 0)
- {
- cout << "Nie istnieje taki uzytkownik" << endl;
- Sleep(800);
- break;
- }
- if (deli == Userint)
- {
- cout << "Nie mozesz usunac swojego konta." << endl;
- Sleep(800);
- break;
- }
- cout << "Aby usunac uzytkownika musisz podac jego haslo: " << endl;
- cout << "";
- cin >> pass;
- if (users[deli]->checkPassword(pass) == true)
- {
- DeleteUser(deli);
- cout << "Uzytkownik zostal usuniety!." << endl;
- Sleep(700);
- }
- else
- {
- cout << "Niewlasciwe haslo!" << endl;
- Sleep(700);
- }
- break;
- }
- break;
- case 'b':
- {
- }
- break;
- }
- } while (x != 'b');
- }
- break;
- case '5':
- {
- Global::saveViruses();
- Global::saveUsers();
- return;
- }
- break;
- }
- }
- }
- Virus* VirusCreator::createVirus(int type)
- {
- if (type == 1)
- return new DNA;
- else if (type == 2)
- return new RNA;
- else if (type == 3)
- return new Hybrid;
- else
- return nullptr;
- }
- User::User()
- {
- login = "Entry_level_user";
- password = "secret";
- }
- void User::setLogin(string l)
- {
- this->login = l;
- }
- void User::setPassword(string p)
- {
- this->password = p;
- }
- string User::getLogin()
- {
- return login;
- }
- string User::getPassword()
- {
- return password;
- }
- bool User::checkPassword(string pass)
- {
- if (pass == password)
- return true;
- else
- return false;
- }
- int Global::findLog(string log)
- {
- for (int i = 0; i < Global::user_counter; i++)
- {
- if (log == Global::users[i]->getLogin())
- return i;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement