Advertisement
swistusmen

Logic

May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. //biblioteka logic HPP
  2. #ifndef LOGIC_HPP_INCLUDED
  3. #define LOGIC_HPP_INCLUDED
  4.  
  5.  
  6.  
  7. #endif // LOGIC_HPP_INCLUDED
  8. #pragma once
  9. #include <iostream>
  10. #include <string>
  11. #include <SFML/Graphics.hpp>
  12. #include <memory>
  13. #include <vector>
  14. #include <map>
  15. #define RAMKA 100
  16. #define SKALAR 25
  17. #define SRODKOWANIE 5
  18. //rozmiar planszy do gry to 60* 60 pól
  19.  
  20.  
  21. class Object{
  22. public:
  23. Object(int a,int b):x(a),y(b){};
  24. int ret_x(){return x;};
  25. int ret_y(){return y;};
  26. private:
  27. int x,y;
  28. };
  29.  
  30. class DoubleTab{
  31. public:
  32. bool IsZero(int ,int);
  33. void create(int);
  34. void Zero();
  35. void Set_value(int,int, bool);
  36. DoubleTab();
  37. int Get_size(){return SizeOfPlayground;};
  38. bool Sniff(int, int);
  39. private:
  40. int SizeOfPlayground;
  41. bool**wsk;
  42. };
  43.  
  44. class Playground{
  45. public:
  46. Playground();
  47. void LoadFromFileAndInitiate();//tak naprawde jest to konstruktor
  48. void update(sf::RenderWindow &);
  49. void Hunt();//sciaga elementy z wektora i przeszukuje otoczenie tego obiektu odsylajac delegacje dla kazdego
  50. //z obiektow
  51. //void FullFillTheValues(std::string& ,int );//zalazek dla inicjowanie planszy z menu
  52. void regenerate();///NOWE
  53. void draw(sf::RenderWindow &);///NOWE
  54.  
  55. void Hunting();
  56. private:
  57. DoubleTab plansza;
  58. sf::Sprite Cell;
  59. sf::Texture AliveCell;
  60.  
  61. void HuntAndKill(int ,int);//Dostaje pole z punktem na œrodku, sprawdza czy elementy pola s¹ ¿ywe
  62. void EraseTheVector();//czysci wektor
  63. void RegenerateFromStack();//regeneruje tablice, nirpotrzebne- relikt gameoflife8?
  64. int FindInt(std::string& );
  65. void Decompress(int,int,int,std::string &);//odczyt zaszyfrowanej inforamcji i ustawienie tablicy i wektora1 na wartosci
  66.  
  67. std::vector<Object> wektor1;
  68. std::vector<Object> wektor2;
  69. bool VectorSwitcher=0;//jesli zero tzn ze wetkro 1, zaczynam od pierwzego
  70. };
  71.  
  72. //////////////////////////////////////////////////////////////////LOGIC CPP
  73.  
  74. #include <iostream>
  75. #include <string>
  76. #include <SFML/Graphics.hpp>
  77. #include <vector>
  78. #include <map>
  79. #include <memory>
  80. #include "Logic.hpp"
  81. #include <boost/filesystem.hpp>
  82. #include <fstream>
  83. #include <sstream>
  84.  
  85. void Playground::Hunting()
  86. {
  87. for (; wektor1.empty()==0; wektor1.pop_back())
  88. {
  89. int x=wektor1.back().ret_x();
  90. int y=wektor1.back().ret_y();
  91. HuntAndKill(x,y);//przeszukuje w tablicy otoczenie
  92. }
  93. return;
  94. }
  95.  
  96. void Playground::regenerate()
  97. {
  98. plansza.Zero();
  99. for (; wektor2.empty()==0;) //stworzyc obiekt i umiescic w wektorze, przedstawic w tablicy
  100. {
  101. int x=wektor2.back().ret_x();
  102. int y=wektor2.back().ret_y();
  103. wektor2.erase(wektor2.begin()+wektor2.size());//odpowiednik wektor2.end()
  104. if(plansza.IsZero(x,y)==1)
  105. {
  106. plansza.Set_value(x,y,1);
  107. Object a{x,y};
  108. wektor1.push_back(a);
  109. }
  110. }
  111. if(wektor1.size()==0)
  112. {
  113. std::cout<<"Smierc populacji\n";
  114. exit(0);
  115. }
  116. return;
  117. }
  118.  
  119.  
  120.  
  121.  
  122. void Playground::update(sf::RenderWindow& appwindow)
  123. {
  124. std::vector<Object>::iterator it;
  125. if(VectorSwitcher==0){
  126. for(it=wektor1.begin();it!=wektor1.end();it++){
  127. Cell.setPosition(RAMKA+(it->ret_x()+SRODKOWANIE)*SKALAR,(it->ret_y()+SRODKOWANIE)*SKALAR);
  128. appwindow.draw(Cell);
  129. }}
  130. else{
  131. for(it=wektor2.begin();it!=wektor2.end();it++){
  132. Cell.setPosition(RAMKA+(it->ret_x()+SRODKOWANIE)*SKALAR,RAMKA+(it->ret_y()+SRODKOWANIE)*SKALAR);
  133. appwindow.draw(Cell);
  134. }}
  135. }
  136.  
  137. void Playground::Decompress(int w,int h,int rozm,std::string &napis)
  138. {
  139. int indeks_w=10,indeks_h=10;// indeksy komorek, ustawiamy na 10 bo tak, to ustawienie na planszy, 10,10 to 0,0
  140. bool number=0;
  141. std::vector<int> liczba;
  142. for(int i=0; i<static_cast<int>(napis.length()),napis[i]!=33; i++)//przesuwaj sie po elementach lancucha
  143. {
  144. if((napis[i]==48)||(napis[i]==49)||(napis[i]==50)||(napis[i]==51)||(napis[i]==52)||(napis[i]==53)||(napis[i]==54)||
  145. (napis[i]==55)||(napis[i]==56)||(napis[i]==57))//jesli jest cyfra, wlacz flage liczby, wrzuc na wektor
  146. {
  147. number=1;
  148. liczba.push_back(napis[i]-48);
  149. }
  150. else{//jesli nie jest liczba to ustaw odpowiednia iloϾ komorek w wektorze
  151. int suma=0;
  152. if(number==1)
  153. {
  154. for(int i=1; liczba.empty()==0; i=i*10,liczba.erase(liczba.begin()+liczba.size()-1))
  155. suma+=liczba.back()*i;
  156. }//ustal sume, ilu komorek dotyczy operacja
  157. else suma=1;
  158. for(int g=0;g<suma;indeks_h++,g++)//ustaw na w wetkorze te liczby
  159. {
  160. if(napis[i]==111)
  161. {
  162. Object obj{indeks_w,indeks_h};
  163. wektor1.push_back(obj);
  164. plansza.Set_value(indeks_w,indeks_h,true);//TRZEBA ZMIENIC
  165. }
  166. }
  167.  
  168. if(indeks_h==w+11)
  169. {
  170. indeks_h=10;
  171. indeks_w++;
  172. }
  173. number=0;
  174. }
  175. }
  176. }
  177.  
  178. int Playground::FindInt(std::string & napis)
  179. {
  180. std::vector<int> liczby;
  181. for(int i=0;i<static_cast<int>(napis.length());i++)
  182. if((napis[i]==48)||(napis[i]==49)||(napis[i]==50)||(napis[i]==51)||(napis[i]==52)||(napis[i]==53)||(napis[i]==54)||
  183. (napis[i]==55)||(napis[i]==56)||(napis[i]==57))
  184. liczby.push_back(napis[i]-48);
  185. //ponizej wziête z funkcji gimme z gameflife 08
  186. int suma=0;
  187. for(int i=1; liczby.empty()==0; i=i*10,liczby.erase(liczby.begin()+liczby.size()-1))
  188. suma+=liczby.back()*i;
  189. return suma;
  190. }
  191.  
  192. void Playground::LoadFromFileAndInitiate()
  193. {
  194. std::fstream plik;
  195. namespace fs=boost::filesystem;
  196. for(auto& p: fs::recursive_directory_iterator("RLE"))
  197. std::cout << p << '\n';
  198. std::cout<<"Podaj nazwe pliku ktory chcesz otworzyc: ";
  199. std::string name;
  200. std::cin>>name;
  201. plik.open(name,std::ios::in);
  202. if(!plik.is_open())
  203. {
  204. std::cerr<<"Nie udalo sie otworzyc pliku do zapisu\n";
  205. exit(0);
  206. }
  207. int w,h,rozm=0;
  208. std::string widd,heigg,mainstream;
  209. plik>>widd;
  210. plik>>heigg;
  211. plik>>mainstream;
  212. plik.close();
  213. w=FindInt(widd);
  214. h=FindInt(heigg);
  215. //if(w>h) rozm=w+20; else rozm=h+20;
  216. rozm=60;
  217. plansza.create(rozm);
  218. Decompress(w,h,rozm, mainstream);
  219. }
  220.  
  221. void Playground::RegenerateFromStack()
  222. {
  223. plansza.Zero();
  224. std::vector<Object>::iterator it;
  225. if(VectorSwitcher==1)
  226. for(it=wektor2.begin();it!=wektor2.end();it++)
  227. plansza.Set_value(it->ret_x(),it->ret_y(),true);
  228. else
  229. for(it=wektor1.begin();it!=wektor1.end();it++)
  230. plansza.Set_value(it->ret_x(),it->ret_y(),true);
  231.  
  232.  
  233. }
  234.  
  235. void Playground::EraseTheVector()
  236. {
  237. if(VectorSwitcher==0)
  238. wektor1.erase(wektor1.begin(),wektor1.end());
  239. else
  240. wektor2.erase(wektor2.begin(),wektor2.end());
  241.  
  242. }
  243.  
  244. void Playground::HuntAndKill(int x,int y)
  245. {
  246. int h,w;
  247. if(y>0)
  248. h=y-1;
  249. else if(y==0)
  250. h=plansza.Get_size()-1;
  251. if(x>0)
  252. w=x-1;
  253. else
  254. w=x;
  255. int maxw=x+2;
  256.  
  257. for(; ((w<maxw)&&(w<plansza.Get_size())); w++)
  258. {
  259. for(int i=0; i<3 ; i++)
  260. {
  261. if(1==plansza.Sniff(w,h))
  262. {
  263. Object a (w,h);//zmiana ze zwykłego obiektu a
  264. //if(VectorSwitcher==0){
  265. wektor2.push_back(a);
  266. //}
  267. //else{
  268. //wektor1.push_back(*a);
  269. //}
  270. }
  271. if (h==plansza.Get_size()-1)
  272. h=0;
  273. else
  274. h++;
  275. }
  276.  
  277. if(y>0)
  278. h=y-1;
  279. else if(y==0)
  280. h=plansza.Get_size()-1;
  281. }
  282. return;
  283. }
  284.  
  285. void Playground::Hunt()
  286. {
  287. if (VectorSwitcher==0)
  288. {
  289. for (;wektor1.empty()==0;wektor1.erase(wektor1.begin()+wektor1.size()) )
  290. {
  291. int x=wektor1.back().ret_x();
  292. int y=wektor1.back().ret_y();
  293. HuntAndKill(x,y);//przeszukuje w tablicy otoczenie
  294. }
  295. }
  296. else
  297. for (;wektor2.empty()==0;wektor2.erase(wektor2.begin()+wektor2.size()))//;wektor2.empty()==0;wektor2.pop_back()
  298. {
  299. int x=wektor2.back().ret_x();
  300. int y=wektor2.back().ret_y();
  301. HuntAndKill(x,y);//przeszukuje w tablicy otoczenie
  302. }
  303. if(VectorSwitcher==0)VectorSwitcher=1;
  304. else VectorSwitcher=0;
  305.  
  306. RegenerateFromStack();
  307. }
  308.  
  309. Playground::Playground():plansza()
  310. {
  311. AliveCell.loadFromFile("AliveCell.jpg");
  312. Cell.setTexture(AliveCell);
  313. VectorSwitcher=0;
  314. }
  315.  
  316. DoubleTab::DoubleTab()
  317. {
  318. SizeOfPlayground=0;
  319. wsk=nullptr;
  320. }
  321.  
  322. void DoubleTab::Zero()
  323. {
  324. for (int i=0;i<SizeOfPlayground;i++)
  325. for(int j=0;j<SizeOfPlayground;j++)
  326. Set_value(i,j,0);
  327. }
  328.  
  329. void DoubleTab::create(int rozmiar)
  330. {
  331. SizeOfPlayground=rozmiar;
  332. wsk=new bool*[rozmiar];
  333. for(int j=0;j<rozmiar;j++)
  334. wsk[j]=new bool[rozmiar];
  335. Zero();
  336. }
  337.  
  338. bool DoubleTab::IsZero(int x,int y)
  339. {
  340. if (wsk[x][y]==1)
  341. return false;
  342. return true;
  343. }
  344.  
  345. void DoubleTab::Set_value(int x,int y, bool val)
  346. {
  347. wsk[x][y]=val;
  348. }
  349.  
  350. bool DoubleTab::Sniff(int x,int y)
  351. {
  352. int xx,yy;
  353. if(y-1>=0)
  354. yy=y-1;
  355. else
  356. yy=Get_size()-1;
  357. if(x-1>=0)
  358. xx=x-1;
  359. else
  360. xx=x;
  361. int licznik=0,bufy=yy;
  362. int maxx=x+2;
  363. for(; ((xx<maxx)&&(xx<Get_size())); xx++)
  364. {
  365. for(int i=0; i<3; i++)
  366. {
  367. if(0==IsZero(xx,yy))
  368. {
  369. licznik++;
  370. }
  371. if(yy==Get_size()-1)
  372. yy=0;
  373. else
  374. yy++;
  375. }
  376. yy=bufy;
  377. }
  378. if((0==IsZero(x,y))&&((licznik==3)||(licznik==4)))
  379. return 1;
  380. else if((1==IsZero(x,y))&&(licznik==3))
  381. return 1;
  382. else
  383. return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement