Advertisement
gosuodin

BAI TAP LON

Sep 16th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. /*
  2. * =========================================================================================
  3. * Name : plants_vs_zombies.cpp
  4. * Author : Nguyen Hoang Minh
  5. * Email : nghoangminh1505@gmail.com
  6. * Copyright : Faculty of Computer Science and Engineering - HCMC University of Technology
  7. * Description : Initial code for Assignment 1 - Data structures and Algorithms - Fall 2016
  8. * =========================================================================================
  9. */
  10.  
  11. //REMEMBER: Do not include any other library. Otherwise you will get 0 mark. :P
  12.  
  13. #ifndef _DEFS_H_
  14. #include "defs.h"
  15. #define _DEFS_H_
  16. #endif}
  17. struct plantfood_t
  18. {
  19. int chiso;
  20. plantfood_t*pNext = NULL;
  21. };
  22. struct list_plantfood
  23. {
  24. plantfood_t*pHead=NULL;
  25. };
  26.  
  27. // add vao cuoi plist
  28. void addTail(list_of_plants*&pList, plant *_plantNew) {
  29. plant *_pRun = pList->head;
  30. if (pList->head == NULL) {
  31. pList->head = _plantNew;
  32. }
  33. else {
  34. while (_pRun->next != NULL) {
  35. _pRun = _pRun->next;
  36. }
  37. _plantNew->next = NULL;
  38. _pRun->next = _plantNew;
  39. _pRun = _plantNew;
  40. }
  41.  
  42. }
  43.  
  44. // đảo ngược danh sách
  45. void ReversePlantList(list_of_plants*&pList) {
  46. plant*_pPre = NULL;
  47.  
  48. plant*_pCur = NULL;
  49.  
  50. if (pList == NULL) {
  51. return;
  52. }
  53. while (pList->head != NULL) {
  54. _pCur = pList->head;
  55. pList->head = pList->head->next;
  56. _pCur->next = _pPre;
  57. _pPre = _pCur;
  58. }
  59. pList->head = _pCur;
  60.  
  61.  
  62. }
  63. // tìm 1 plant cuoi danh sach
  64. plant* findPlantTail(list_of_plants* pList) {
  65. plant *_pRun = pList->head;
  66. plant *_pTail = new plant;
  67. if (pList->head == NULL) {
  68. pList->lose = true;
  69. }
  70. else {
  71. while (_pRun->next) {
  72. _pRun = _pRun->next;
  73. }
  74. }
  75. _pTail = _pRun;
  76.  
  77. return _pTail;
  78. }
  79. // tim plant o vi tri thu x
  80. plant*findPlant_x(list_of_plants*pList, int x) {
  81. plant *_pRun = pList->head;
  82. plant *_pPlantx = new plant();
  83. int i = 1;
  84. if (x == 1) {
  85. return pList->head;
  86. }
  87. else {
  88. while (_pRun->next != NULL&&i != x) {
  89. _pRun = _pRun->next;
  90. i++;
  91. }
  92. return _pRun;
  93. }
  94. }
  95. // xoa plant o vi tri thu x
  96. void delete_plant_x(list_of_plants*&pList, int x) {
  97. if (x == 1) {
  98. pList->lose = true;
  99. }
  100. else {
  101. int i = 1;
  102. plant* pDel;
  103. plant *_pRun;
  104. pDel = pList->head;
  105.  
  106. while (pDel != NULL && i != x - 1) {
  107. pDel = pDel->next;
  108. i++;
  109. }
  110. _pRun = pDel->next;
  111. pDel->next = pDel->next->next;
  112. delete _pRun;
  113. }
  114. }
  115.  
  116. // tong so damge cay thu 4 trong plist
  117. int SumAttack_plantEvent4(list_of_plants *pList) {
  118. plant*_pRun = pList->head;
  119. int Sumplant4 = 0;
  120. while (_pRun) {
  121. if (_pRun->number == 4) {
  122. Sumplant4 += _pRun->attack;
  123. }
  124. _pRun = _pRun->next;
  125. }
  126. return Sumplant4;
  127.  
  128. }
  129.  
  130. // luu chi so cua cay co plant food
  131. void add_plantfood(list_plantfood*pList,int chiso) {
  132. plantfood_t*pNew = new plantfood_t;
  133. pNew->chiso = chiso;
  134. plantfood_t*pRun = pList->pHead;
  135. if (pList->pHead == NULL) {
  136. pList->pHead = pNew;
  137. }
  138. else {
  139. while (pRun->pNext) {
  140. pRun = pRun->pNext;
  141. }
  142. pRun->pNext = pNew;
  143. pNew->pNext = NULL;
  144. pRun = pNew;
  145. }
  146. }
  147. bool isplant4_plantfood(list_of_plants*pList, list_plantfood*plist, int k) {
  148. plantfood_t*pRun = plist->pHead;
  149. int attack = 0;
  150. int i = 0;
  151. int n = 0;
  152. plant*pRun1 = pList->head;
  153. if (pList->head != NULL&&plist->pHead != NULL) {
  154. while (pRun)
  155. {
  156. while (pRun1)
  157. {
  158. if (pRun->chiso <= k&&pRun->chiso>0) {
  159. if (pRun1 == findPlant_x(pList, pRun->chiso)) {
  160. if (pRun1->number == 4) {
  161. n++;
  162. }
  163. pRun1 = pList->head;
  164. break;
  165. }
  166. pRun1 = pRun1->next;
  167. }
  168. else {
  169. break;
  170. }
  171. }
  172. pRun = pRun->pNext;
  173. }
  174. }
  175. if (n > 0) {
  176.  
  177. return true;
  178. }
  179. else {
  180. return false;
  181. }
  182. }
  183.  
  184. int Sumattack(list_of_plants*pList, list_plantfood*plist,int k) {
  185. plantfood_t*pRun = plist->pHead;
  186. int attack = 0;
  187. int i = 0;
  188. plant*pRun1 = pList->head;
  189. if (pList->head!=NULL&&plist->pHead!=NULL) {
  190. while (pRun)
  191. {
  192. while (pRun1)
  193. {
  194. if (pRun->chiso <= k&&pRun->chiso>0) {
  195. if (pRun1 == findPlant_x(pList, pRun->chiso)) {
  196. switch(pRun1->number)
  197. {
  198. case 1:attack += pRun1->attack * 5; break;
  199. case 2:attack += pRun1->attack * 2; break;
  200. case 4:attack += 1000; break;
  201. }
  202. pRun1 = pList->head;
  203. pRun->chiso = -1;
  204. break;
  205. }
  206. pRun1 = pRun1->next;
  207. }
  208. else {
  209. break;
  210. }
  211. }
  212. pRun = pRun->pNext;
  213. }
  214. }
  215. while (pRun1&&i!= k)
  216. {
  217. i++;
  218. attack += pRun1->attack;
  219. pRun1 = pRun1->next;
  220. }
  221. return attack;
  222. }
  223.  
  224. // tăng damge cho cây trong struct plant food
  225. void combat(eventList* pEvent, list_of_plants *& pList) {
  226. list_plantfood*plantx = new list_plantfood;
  227. plant *pPeaPodNow = new plant;
  228. int _temp, _attack_plant, _hp_plant, _hp_zombie, _attack_zombie;
  229. int i = 0;
  230. int _nPeaPod = 0;
  231. int _n_Event = 0;
  232. while (pEvent != NULL&&i < 1000 && pList->lose != true) {
  233. _temp = pEvent->nEventCode / 1000;
  234. _attack_plant = (pEvent->nEventCode % 1000) / 100;
  235. _hp_plant = pEvent->nEventCode % 100;
  236. _attack_zombie = -(pEvent->nEventCode % 1000) / 100;
  237. _hp_zombie = -pEvent->nEventCode % 100;
  238. switch (_temp)
  239. {
  240. //Pea shooter
  241. case 1: if (_attack_plant < 10 && _attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
  242. _n_Event++;
  243. plant*_plant = new plant();
  244. addTail(pList, _plant);
  245. _plant->attack = _attack_plant;
  246. _plant->hp = _hp_plant;
  247. _plant->number = 1;
  248. i++;
  249. break;
  250. }
  251. //Pea pod
  252. case 2:if (_attack_plant < 10 && _attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
  253. _n_Event++;
  254. if (_nPeaPod == 0) {
  255. plant*_plantN = new plant();
  256. _plantN->attack = _attack_plant;
  257. _plantN->hp = _hp_plant;
  258. _plantN->number = 2;
  259. addTail(pList, _plantN);
  260. pPeaPodNow = _plantN;
  261. i++;
  262. _nPeaPod++;
  263. }
  264. else {
  265. if (_nPeaPod == 1 || _nPeaPod == 2 || _nPeaPod == 3) {
  266. if (pPeaPodNow->hp < _hp_plant) {
  267. pPeaPodNow->hp = _hp_plant;
  268. }
  269. pPeaPodNow->attack += _attack_plant;
  270. _nPeaPod++;
  271. }
  272. else {
  273. if (_nPeaPod == 4) {
  274.  
  275. if (pPeaPodNow->hp < _hp_plant) {
  276. pPeaPodNow->hp = _hp_plant;
  277. }
  278. pPeaPodNow->attack += _attack_plant;
  279. _nPeaPod = 0;
  280. }
  281. }
  282. }
  283. break;
  284. }
  285. //Laser bean
  286. case 4: if (_attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
  287. _n_Event++;
  288. plant*_plant = new plant();
  289. addTail(pList, _plant);
  290. _plant->attack = _attack_plant;
  291. _plant->hp = _hp_plant;
  292. _plant->number = 4;
  293. i++;
  294. break;
  295. }
  296. case 0:
  297. switch (pEvent->nEventCode) {
  298. //Wall-nut
  299. case 3: {
  300. _n_Event++;
  301. plant*_plant = new plant();
  302. addTail(pList, _plant);
  303. _plant->attack = 0;
  304. _plant->hp = 10;
  305. _plant->number = 3;
  306. i++;
  307. break;
  308. }
  309. //Prospector zombie
  310. case -5:
  311. _n_Event++;
  312. ReversePlantList(pList);
  313. break;
  314. }
  315. //plant food
  316. if (pEvent->nEventCode > 49 && pEvent->nEventCode < 60) {
  317. _n_Event++;
  318. if (pList->head == NULL) {
  319. break;
  320. }
  321. else {
  322.  
  323. int I = pEvent->nEventCode % 10;
  324. int k = (_n_Event*I) % i + 1;
  325. plant*x = findPlant_x(pList, k);
  326. if (x->number == 3) {
  327. x->hp = 30;
  328. }
  329. else {
  330. add_plantfood(plantx, k);
  331. }
  332. }
  333.  
  334. break;
  335. }
  336. //Explorer zombie
  337. if (-pEvent->nEventCode > 319 && -pEvent->nEventCode < 400) {
  338. _n_Event++;
  339. int dem = 0;
  340. int _hp_zb = _hp_zb = -pEvent->nEventCode % 100;
  341. if (pList->head == NULL) {
  342. pList->lose == true;
  343. }
  344. else {
  345. while (_hp_zb > 0 && (pList->lose != true)) {
  346. plant *_pTail = findPlantTail(pList);
  347. if (_pTail->number == 2) {
  348. {
  349. _nPeaPod = 0;
  350. delete_plant_x(pList, i);
  351. i--;
  352. }
  353. }
  354. else {
  355. delete_plant_x(pList, i);
  356. i--;
  357. }
  358. _hp_zb = _hp_zb - Sumattack(pList, plantx, i);
  359. }
  360. }
  361. }
  362. break;
  363.  
  364. //Basic zombie
  365. case -1: if (_attack_zombie > 0 && _attack_zombie < 10 && _hp_zombie>19 && _hp_zombie < 100) {
  366. _n_Event++;
  367. if (pList->head == NULL) {
  368. pList->lose == true;
  369.  
  370. }
  371. else {
  372. while (_hp_zombie > 0 && (pList->lose != true))
  373. {
  374. plant *_pTail = findPlantTail(pList);
  375.  
  376. if (_attack_zombie >= _pTail->hp) {
  377. if (_pTail->number == 2) {
  378. {
  379. _nPeaPod = 0;
  380. delete_plant_x(pList, i);
  381. i--;
  382. }
  383.  
  384. }
  385. else {
  386. delete_plant_x(pList, i);
  387. i--;
  388. }
  389. }
  390. else {
  391. _pTail->hp = _pTail->hp - _attack_zombie;
  392. }
  393. _hp_zombie = _hp_zombie - Sumattack(pList, plantx, i);
  394.  
  395. }
  396. }
  397. break;
  398. }
  399. //Camel zombie
  400. case -22:
  401. case -23:
  402. case -24:
  403. {
  404. int _n_zombie1 = -((pEvent->nEventCode) / 1000) % 10;
  405. int _attack_zombie1 = -(pEvent->nEventCode % 1000) / 100;
  406. int _hp_zombie1 = -(pEvent->nEventCode % 100);
  407. int _hp1_zombie1 = -(pEvent->nEventCode % 100);
  408. if (_attack_zombie1 > 0 && _attack_zombie1 < 10 && _hp_zombie1>19 && _hp_zombie1 < 100) {
  409. _n_Event++;
  410. int k = 1;
  411. if (pList->head == NULL) {
  412. pList->lose == true;
  413.  
  414. }
  415. else {
  416. while (_n_zombie1 > 0 && pList->lose != true) {
  417. plant*_pTail1 = findPlantTail(pList);
  418. if (_attack_zombie1 >= _pTail1->hp) {
  419. if (_pTail1->number == 2) {
  420. _nPeaPod = 0;
  421. delete_plant_x(pList, i);
  422. i--;
  423. }
  424. else {
  425. delete_plant_x(pList, i);
  426. i--;
  427. }
  428. }
  429. else {
  430. _pTail1->hp = _pTail1->hp - _attack_zombie1;
  431. }
  432.  
  433. if (isplant4_plantfood(pList, plantx, i) == true) {
  434. _n_zombie1 = 0;
  435. }
  436. else {
  437.  
  438. if (Sumattack(pList, plantx, i) >= _hp_zombie1) {
  439. _n_zombie1--;
  440. _hp_zombie1 = -(pEvent->nEventCode % 100) - SumAttack_plantEvent4(pList)*k;
  441. }
  442. else {
  443. _hp_zombie1 = _hp_zombie1 - Sumattack(pList, plantx, i);
  444. }
  445. k++;
  446. }
  447. }
  448. }
  449. }
  450.  
  451.  
  452. break;
  453. }
  454. //Lost pilot zombie
  455.  
  456. case -40:
  457. case -41:
  458. case -42:
  459. case -43:
  460. case -44:
  461. case -45:
  462. case -46:
  463. case -47:
  464. case -48:
  465. case -49:
  466. {
  467. int _index_zombie2 = -((pEvent->nEventCode) / 1000) % 10;
  468. int _attack_zombie2 = -(pEvent->nEventCode % 1000) / 100;
  469. int _hp_zombie2 = -(pEvent->nEventCode % 100);
  470. if (_attack_zombie2 > 0 && _attack_zombie2 < 10 && _hp_zombie2>19 && _hp_zombie2 < 100) {
  471. _n_Event++;
  472.  
  473. if (pList->head == NULL) {
  474. pList->lose = true;
  475. }
  476. else {
  477. int k = (_index_zombie2*_n_Event) % i + 1;
  478. while (_hp_zombie2 > 0 && pList->lose != true) {
  479. plant*_plant_x = findPlant_x(pList, k);
  480. if (_plant_x->hp <= _attack_zombie2) {
  481. if (_plant_x->number == 2) {
  482. i--;
  483. delete_plant_x(pList, k);
  484. k--;
  485. _nPeaPod = 0;
  486. }
  487. else {
  488. i--;
  489. delete_plant_x(pList, k);
  490. k--;
  491. }
  492. }
  493. else {
  494. _plant_x->hp = _plant_x->hp - _attack_zombie2;
  495.  
  496. }
  497.  
  498. _hp_zombie2 = _hp_zombie2 - Sumattack(pList, plantx, k);
  499. }
  500. }
  501. }
  502.  
  503.  
  504. break;
  505. }
  506. }
  507. pEvent = pEvent->next;
  508. }
  509.  
  510.  
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement