hungkt1997

ass_23/09

Sep 23rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.78 KB | None | 0 0
  1. /*
  2. * =========================================================================================
  3. * Name : plants_vs_zombies.cpp
  4. * Author : Nguyen Hoang Minh
  5. * Copyright : Faculty of Computer Science and Engineering - HCMC University of Technology
  6. * Description : Initial code for Assignment 1 - Data structures and Algorithms - Fall 2016
  7. * =========================================================================================
  8. */
  9.  
  10. //REMEMBER: Do not include any other library. Otherwise you will get 0 mark. :P
  11.  
  12. #ifndef _DEFS_H_
  13. #include "defs.h"
  14. #define _DEFS_H_
  15. #endif
  16. using namespace std;
  17. struct list_plant_food {
  18. int number;
  19. int add_attack;
  20. list_plant_food *next;
  21. };
  22. void insert_food (list_plant_food *&food, int k, int power) {
  23. list_plant_food *temp = new list_plant_food;
  24. list_plant_food *ptemp;
  25. if (food == NULL) {
  26. temp->number = k;
  27. temp->add_attack = power;
  28. temp->next = NULL;
  29. food = temp;
  30. }
  31. else {
  32. ptemp = food;
  33. while (ptemp->next != NULL) {
  34. ptemp = ptemp->next;
  35. }
  36. temp->number = k;
  37. temp->add_attack = power;
  38. temp->next = NULL;
  39. ptemp->next = temp;
  40. }
  41. }
  42. int total_add_att(list_plant_food *food, list_of_plants *pList,int vt_zombie) {
  43. int m = 0;
  44. list_plant_food *foo1 = new list_plant_food;
  45. foo1 = food;
  46. while (foo1 != NULL) {
  47. if (foo1->number>0 && foo1->number < vt_zombie) {
  48. foo1->number = -1;
  49. m += foo1->add_attack;
  50. }
  51. foo1 = foo1->next;
  52. }
  53. return m;
  54. }
  55. bool check_plant(list_of_plants *pList) {
  56. bool k = false;
  57. plant *check = pList->head;
  58. while (check!=NULL)
  59. {
  60. if (check->attack > 0) k = true;
  61. check = check->next;
  62. }
  63. return k;
  64. }
  65. int count_2(list_of_plants * pList) {
  66. plant*count = new plant;
  67. count = pList->head;
  68. int i = 0;
  69. int vt = 0;// vi tri bui cuoi cung
  70. while (count != NULL) {
  71. i++;
  72. if (count->number == 2) vt = i;
  73. count = count->next;
  74. }
  75. return vt;
  76. }
  77. void insert(list_of_plants *& pList, plant * zombie_pilot, int k_pilot) {
  78. plant *p, *q;
  79. int i = 0;
  80. p = zombie_pilot;
  81. q = pList->head;
  82. while (q != NULL && i != k_pilot - 1) //duyet den vi tri k-1
  83. {
  84. i++;
  85. q = q->next;
  86. }
  87. p->next = q->next;
  88. q->next = p;
  89. }
  90. int count_zombie(list_of_plants * pList) {
  91. plant*count1 = new plant;
  92. count1 = pList->head;
  93. int i = 1;
  94. while (count1->number >= 0) {
  95. i++;
  96. count1 = count1->next;
  97. if (count1 == NULL) break;
  98. }
  99. return i;
  100. }
  101.  
  102. int count_attack4(list_of_plants * pList) {
  103. plant*count4 = new plant;
  104. count4 = pList->head;
  105. int i = 0;
  106. while (count4 != NULL) {
  107. if (count4->number == 4) i += count4->attack;
  108. count4 = count4->next;
  109. }
  110. return i;
  111. }
  112. int del(list_of_plants * pList, int k, int &tree_plant2, bool &plant_number2) {
  113. int i = 1;
  114. plant* pDel = new plant;
  115. pDel = pList->head;
  116. if (k == 1) {
  117. if (plant_number2==true && pList->head->number == 2) tree_plant2 = 0;
  118. pList->head = pList->head->next;
  119. delete pDel;
  120. }
  121. else {
  122. while (pDel != NULL && i != k - 1)
  123. {
  124. pDel = pDel->next;
  125. i++;
  126. }
  127. plant *temp = pDel->next;
  128. if (plant_number2==true && pDel->next->number == 2) tree_plant2 =0;
  129. pDel->next = pDel->next->next;
  130. delete temp;
  131. }
  132. return i;
  133. }
  134. int total_attack(list_of_plants *pList, int &add_attack) {
  135. int i = 0;
  136. plant* total = new plant;
  137. total = pList->head;
  138. while (total!=NULL && total->number>0)
  139. {
  140. i += total->attack;
  141. total = total->next;
  142. }
  143. i = i + add_attack;
  144. add_attack = 0;
  145. return i;
  146. }
  147. int total_attack2(list_of_plants *pList, int &add_attack) {
  148. int i = 0;
  149. plant* total = new plant;
  150. total = pList->head;
  151. while (total!=NULL )
  152. {
  153. if (total->number > 0 && total->number < 4)i += total->attack;
  154. if (total->number < 0) break;
  155. total = total->next;
  156. }
  157. i += add_attack;
  158. add_attack = 0;
  159. return i;
  160. }
  161. int total_plant(list_of_plants * pList) {
  162. int i = 0;
  163. plant* total = new plant;
  164. total = pList->head;
  165. while (total != NULL) {
  166. i++;
  167. total = total->next;
  168. }
  169. return i;
  170. }
  171. void combat(eventList* pEvent, list_of_plants *& pList) {
  172. list_plant_food * food = new list_plant_food;
  173. food = NULL;
  174. plant* tre = new plant;
  175. tre->next = NULL;
  176. int tree_number2 = 0;
  177. int number_event = 0;
  178.  
  179. bool check_food_in_plant4 = false;
  180. bool plant_number2 = true;
  181. while (pEvent != NULL) {
  182. // them cay so 3
  183. if (pEvent->nEventCode == 3) {
  184. number_event++;
  185. plant* tree = new plant;
  186. tree->number = 3;
  187. tree->attack = 0;
  188. tree->hp = 10;
  189. tree->next = NULL;
  190. if (pList->head == NULL)
  191. {
  192. pList->lose = false;
  193. pList->head = tree;
  194. tre = tree;
  195. }
  196. else {
  197. tre = pList->head;
  198. while (tre->next != NULL) {
  199. tre = tre->next;
  200. }
  201. tre->next = tree;
  202. tre = tree;
  203. }
  204. }
  205. // them plant food
  206. if (pEvent->nEventCode > 49 && pEvent->nEventCode < 60) {
  207. number_event++;
  208. /*if (pEvent->nEventCode == 50) {
  209. cout << pEvent->nEventCode;
  210. }*/
  211. plant* a = new plant;
  212. a = pList->head;
  213. list_plant_food *temp1 = new list_plant_food;
  214. temp1 = food;
  215. bool kiemtra = true;
  216. if (a != NULL) {
  217. int I = pEvent->nEventCode % 10;
  218. int s = total_plant(pList);
  219. if (s > 0) {
  220. int k_food = (number_event*I) % s + 1;
  221. // kiem tra tron list moi
  222. while (temp1 != NULL) {
  223. if (temp1->number == k_food) {
  224. kiemtra = false;
  225. break;
  226. }
  227. temp1 = temp1->next;
  228. }
  229. ////// kiem tra cai cay number==3
  230. plant* tree = new plant;
  231. tree = pList->head;
  232. for (int i = 1; i < k_food; i++) {
  233. tree = tree->next;
  234. }
  235.  
  236. if (tree->number == 3) tree->hp = 30;
  237.  
  238. else if (tree->number == 4) check_food_in_plant4 = true;
  239.  
  240. else if (tree->number == 1 && kiemtra == true) {
  241. int add_attack = tree->attack * 5;
  242. insert_food(food, k_food, add_attack);
  243. }
  244. else if (tree->number == 2 && kiemtra == true) {
  245. int add_attack = tree->attack * 2;
  246. insert_food(food, k_food, add_attack);
  247. }
  248. }
  249. }
  250. }
  251. // them cay so 1
  252. if (pEvent->nEventCode > 1100 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode < 1911 && pEvent->nEventCode % 100>0) {
  253. number_event++;
  254. /*if (pEvent->nEventCode == 1203) {
  255. cout << pEvent->nEventCode;*/
  256. plant* tree_1 = new plant;
  257. tree_1->number = 1; pEvent->nEventCode = pEvent->nEventCode - 1000;
  258. tree_1->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
  259. tree_1->hp = pEvent->nEventCode;
  260. tree_1->next = NULL;
  261. if (pList->head == NULL)
  262. {
  263. pList->lose = false;
  264. pList->head = tree_1;
  265. tre = tree_1;
  266. }
  267. else {
  268. tre = pList->head;
  269. while (tre->next != NULL) {
  270. tre = tre->next;
  271. }
  272. tre->next = tree_1;
  273. tre = tree_1;
  274. }
  275. }
  276. // them cay so 4
  277. if (pEvent->nEventCode > 4100 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode < 4911 && pEvent->nEventCode % 100>0) {
  278. number_event++;
  279. plant* tree = new plant;
  280. tree->number = 4; pEvent->nEventCode = pEvent->nEventCode - 4000;
  281. tree->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
  282. tree->hp = pEvent->nEventCode;
  283. tree->next = NULL;
  284. if (pList->head == NULL)
  285. {
  286. pList->lose = false;
  287. pList->head = tree;
  288. tre = tree;
  289. }
  290. else {
  291. tre = pList->head;
  292. while (tre->next != NULL) {
  293. tre = tre->next;
  294. }
  295. tre->next = tree;
  296. tre = tree;
  297. }
  298. }
  299. // them cay so 2
  300. if (pEvent->nEventCode > 2100 && pEvent->nEventCode < 2911 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode % 100 > 0) {
  301. number_event++;
  302. plant* tree = new plant;
  303. tree->number = 2; pEvent->nEventCode = pEvent->nEventCode - 2000;
  304. tree->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
  305. tree->hp = pEvent->nEventCode;
  306. tree->next = NULL;
  307. // Danh so thu tu cho cay
  308. tree_number2++;
  309. // them cay so 2 dau tien lam bui cay
  310. if (tree_number2 == 1) {
  311. if (pList->head == NULL)
  312. {
  313. pList->lose = false;
  314. pList->head = tree;
  315. tre = tree;
  316. }
  317. else {
  318. tre = pList->head;
  319. while (tre->next != NULL) {
  320. tre = tre->next;
  321. }
  322. tre->next = tree;
  323. tre = tree;
  324. }
  325. }
  326.  
  327. if (tree_number2 > 1 && tree_number2 < 6) {
  328. // Tim kiem bui cay
  329. if (plant_number2 == true) {
  330. plant* add_tree2 = pList->head;
  331. int k;
  332. k = count_2(pList);
  333. for (int x1 = 1; x1 < k; x1++) {
  334. add_tree2 = add_tree2->next;
  335. }
  336. add_tree2->attack += tree->attack;
  337. if (tree->hp > add_tree2->hp) add_tree2->hp = tree->hp;
  338. if (tree_number2 == 5) tree_number2 = 0;
  339. }
  340. else {
  341. plant* add_tree2 = pList->head;
  342. while (add_tree2->number > 0) {
  343. if (add_tree2->number == 2) break;
  344. add_tree2 = add_tree2->next;
  345. }
  346. add_tree2->attack += tree->attack;
  347. if (tree->hp > add_tree2->hp) add_tree2->hp = tree->hp;
  348. if (tree_number2 == 5) {
  349. plant_number2 = true;
  350. tree_number2 = 0;
  351. }
  352. }
  353. }
  354. }
  355. // them zombie_basic
  356. if (pEvent->nEventCode > -2000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-1121 && pEvent->nEventCode % 100>-100) {
  357. number_event++;
  358. plant* zombie_basic = new plant;
  359. zombie_basic->number = -1; pEvent->nEventCode = abs(pEvent->nEventCode) - 1000;
  360. zombie_basic->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
  361. zombie_basic->hp = pEvent->nEventCode;
  362. zombie_basic->next = NULL;
  363. if (pList->head == NULL)
  364. {
  365. pList->lose = false;
  366. pList->head = zombie_basic;
  367. tre = zombie_basic;
  368. }
  369. else {
  370. tre = pList->head;
  371. while (tre->next != NULL) {
  372. tre = tre->next;
  373. }
  374. tre->next = zombie_basic;
  375. tre = zombie_basic;
  376. }
  377. // Danh nhau di nao !
  378. plant* a = new plant;
  379. a = pList->head;
  380. int number_attack = 1;
  381. while (zombie_basic->hp > 0 && a->number > 0)
  382. {
  383. int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
  384. plant* att_zombie = pList->head;
  385. for (int x2 = 1; x2 < k_zombie - 1; x2++) {
  386. att_zombie = att_zombie->next;
  387. }
  388.  
  389. if (number_attack % 2 == 1) {// Zombie tan cong
  390. number_attack++;
  391.  
  392. if (att_zombie->hp <= zombie_basic->attack) {
  393. del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
  394. }
  395. else {
  396. att_zombie->hp = att_zombie->hp - zombie_basic->attack; // tru mau cua Plant
  397. }
  398. }
  399. else {// Tong cay trong tan cong
  400. number_attack++;
  401. int add_attack = total_add_att(food, pList, k_zombie);
  402. zombie_basic->hp = zombie_basic->hp - total_attack(pList, add_attack);
  403. if (zombie_basic->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
  404. }
  405. }
  406. }
  407. // Them Camel zombie
  408. if (pEvent->nEventCode > -25000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-22119 && pEvent->nEventCode % 100>-100) {
  409. number_event++;
  410. plant* zombie_camel = new plant;
  411. zombie_camel->number = -2; pEvent->nEventCode = abs(pEvent->nEventCode) - 20000;
  412. zombie_camel->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
  413. zombie_camel->hp = pEvent->nEventCode;
  414. zombie_camel->next = NULL;
  415. if (pList->head == NULL)
  416. {
  417. pList->lose = false;
  418. pList->head = zombie_camel;
  419. tre = zombie_camel;
  420. }
  421. else {
  422. tre = pList->head;
  423. while (tre->next != NULL) {
  424. tre = tre->next;
  425. }
  426. tre->next = zombie_camel;
  427. tre = zombie_camel;
  428. }
  429. // Danh nhau di nao !
  430. plant* a = new plant;
  431. a = pList->head;
  432. int number_attack = 1;
  433. while (zombie_camel->hp > 0 && a->number > 0)
  434. {
  435. int k_zombie = count_zombie(pList);
  436. plant* att_zombie = pList->head;
  437. for (int x2 = 1; x2 < k_zombie - 1; x2++) {
  438. att_zombie = att_zombie->next;// tro den vi tri cay truoc mat zombie
  439. }
  440.  
  441. if (number_attack % 2 == 1) {// Zombie tan cong
  442. number_attack++;
  443.  
  444. if (att_zombie->hp <= (zombie_camel->attack) % 10) {
  445. del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
  446. }
  447. else {
  448. att_zombie->hp -= (zombie_camel->attack) % 10; // tru HP cua Plant
  449. }
  450. }
  451. else {// Tong cay trong tan cong
  452. number_attack++;
  453. if (check_food_in_plant4 == true) {
  454. del(pList, k_zombie, tree_number2, plant_number2);
  455. check_food_in_plant4 = false;
  456. break;
  457. }
  458. else {
  459. int add_attack = total_add_att(food, pList, k_zombie);
  460. pEvent->nEventCode = pEvent->nEventCode - count_attack4(pList);
  461. zombie_camel->hp = zombie_camel->hp - count_attack4(pList);
  462. zombie_camel->hp = zombie_camel->hp - total_attack2(pList, add_attack);
  463.  
  464. if (zombie_camel->hp < 1 && zombie_camel->attack>20) {
  465. zombie_camel->attack -= 10;
  466. zombie_camel->hp = pEvent->nEventCode;
  467. }
  468. else if (zombie_camel->hp < 1 && zombie_camel->attack < 20)
  469. {
  470. del(pList, k_zombie, tree_number2, plant_number2);
  471. }
  472. }
  473. }
  474. }
  475. /*int k_zombie2 = count_zombie(pList);
  476. plant *zombie_camel2 = pList->head;
  477. while (zombie_camel2->next != NULL) {
  478. zombie_camel2 = zombie_camel2->next;
  479. }
  480. if (zombie_camel2->hp < 1 && zombie_camel2->attack < 20) del(pList, k_zombie2, tree_number2, plant_number2);*/
  481.  
  482. }
  483.  
  484. // them pilot zombie ;
  485. if (pEvent->nEventCode > -50000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-40119 && pEvent->nEventCode % 100>-100)
  486. {
  487. /*if (pEvent->nEventCode == -44698) {
  488. cout << pEvent->nEventCode;*/
  489. number_event++;
  490. plant* zombie_pilot = new plant;
  491. zombie_pilot->number = -4; pEvent->nEventCode = abs(pEvent->nEventCode) - 40000;
  492. int I = pEvent->nEventCode / 1000;
  493. pEvent->nEventCode = pEvent->nEventCode % 1000;
  494. zombie_pilot->attack = pEvent->nEventCode / 100;
  495. pEvent->nEventCode = pEvent->nEventCode % 100;
  496. zombie_pilot->hp = pEvent->nEventCode;
  497. zombie_pilot->next = NULL;
  498. int s = total_plant(pList);
  499. int k_pilot = (number_event*I) % s + 1;
  500.  
  501. insert(pList, zombie_pilot, k_pilot);
  502. // Danh nhau di nao !
  503. plant* a = new plant;
  504. a = pList->head;
  505. int number_attack = 1;
  506. while (zombie_pilot->hp > 0 && a->number > 0)
  507. {
  508. int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
  509. plant* att_zombie = pList->head;
  510. for (int x2 = 1; x2 < k_zombie - 1; x2++) {
  511. att_zombie = att_zombie->next;
  512. }
  513.  
  514. if (number_attack % 2 == 1) {// Zombie tan cong
  515. number_attack++;
  516.  
  517. if (att_zombie->hp <= zombie_pilot->attack) {
  518. del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
  519. }
  520. else {
  521. att_zombie->hp = att_zombie->hp - zombie_pilot->attack; // tru HP cua Plant
  522. }
  523. }
  524. else {// Tong cay trong tan cong
  525. number_attack++;
  526. int add_attack = total_add_att(food, pList, k_zombie + 1);
  527. zombie_pilot->hp = zombie_pilot->hp - total_attack(pList, add_attack);
  528. if (zombie_pilot->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
  529. }
  530. }
  531.  
  532. }
  533. if (pEvent->nEventCode > -400 && pEvent->nEventCode < -319) {
  534. number_event++;
  535. plant* zombie_explorer = new plant;
  536. zombie_explorer->number = -3; pEvent->nEventCode = abs(pEvent->nEventCode) - 300;
  537. zombie_explorer->attack = 300; pEvent->nEventCode = pEvent->nEventCode % 100;
  538. zombie_explorer->hp = pEvent->nEventCode;
  539. zombie_explorer->next = NULL;
  540. if (pList->head == NULL)
  541. {
  542. pList->lose = false;
  543. pList->head = zombie_explorer;
  544. tre = zombie_explorer;
  545. }
  546. else {
  547. tre = pList->head;
  548. while (tre->next != NULL) {
  549. tre = tre->next;
  550. }
  551. tre->next = zombie_explorer;
  552. tre = zombie_explorer;
  553. }
  554. // Danh nhau di nao !
  555. plant* a = new plant;
  556. a = pList->head;
  557. int number_attack = 1;
  558. while (zombie_explorer->hp > 0 && a->number > 0)
  559. {
  560. int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
  561. plant* att_zombie = pList->head;
  562. for (int x2 = 1; x2 < k_zombie - 1; x2++) {
  563. att_zombie = att_zombie->next;
  564. }
  565.  
  566. if (number_attack % 2 == 1) {// Zombie tan cong
  567. number_attack++;
  568. del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
  569. }
  570. else {// Tong cay trong tan cong
  571. number_attack++;
  572. int add_attack = total_add_att(food, pList, k_zombie);
  573. zombie_explorer->hp = zombie_explorer->hp - total_attack(pList, add_attack);
  574. if (zombie_explorer->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
  575. }
  576. }
  577. }
  578. if (pEvent->nEventCode == -5) {
  579. number_event++;
  580. if (check_plant(pList) == true) {
  581. plant_number2 = false;
  582. plant *k_1 = NULL;
  583. plant *pre = NULL;
  584. while (pList->head != NULL) {
  585. k_1 = pList->head;
  586. pList->head = pList->head->next;
  587. k_1->next = pre;
  588. pre = k_1;
  589. }
  590. pList->head = k_1;
  591. }
  592. }
  593.  
  594. pEvent = pEvent->next;
  595. }
  596. /*if (pList->head->number <0) pList->lose = true;*/
  597. }
Advertisement
Add Comment
Please, Sign In to add comment