Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =========================================================================================
- * Name : plants_vs_zombies.cpp
- * Author : Nguyen Hoang Minh
- * Email : nghoangminh1505@gmail.com
- * Copyright : Faculty of Computer Science and Engineering - HCMC University of Technology
- * Description : Initial code for Assignment 1 - Data structures and Algorithms - Fall 2016
- * =========================================================================================
- */
- //REMEMBER: Do not include any other library. Otherwise you will get 0 mark. :P
- #ifndef _DEFS_H_
- #include "defs.h"
- #define _DEFS_H_
- #endif}
- struct plantfood_t
- {
- int chiso;
- plantfood_t*pNext = NULL;
- };
- struct list_plantfood
- {
- plantfood_t*pHead=NULL;
- };
- // add vao cuoi plist
- void addTail(list_of_plants*&pList, plant *_plantNew) {
- plant *_pRun = pList->head;
- if (pList->head == NULL) {
- pList->head = _plantNew;
- }
- else {
- while (_pRun->next != NULL) {
- _pRun = _pRun->next;
- }
- _plantNew->next = NULL;
- _pRun->next = _plantNew;
- _pRun = _plantNew;
- }
- }
- // đảo ngược danh sách
- void ReversePlantList(list_of_plants*&pList) {
- plant*_pPre = NULL;
- plant*_pCur = NULL;
- if (pList == NULL) {
- return;
- }
- while (pList->head != NULL) {
- _pCur = pList->head;
- pList->head = pList->head->next;
- _pCur->next = _pPre;
- _pPre = _pCur;
- }
- pList->head = _pCur;
- }
- // tìm 1 plant cuoi danh sach
- plant* findPlantTail(list_of_plants* pList) {
- plant *_pRun = pList->head;
- plant *_pTail = new plant;
- if (pList->head == NULL) {
- pList->lose = true;
- }
- else {
- while (_pRun->next) {
- _pRun = _pRun->next;
- }
- }
- _pTail = _pRun;
- return _pTail;
- }
- // tim plant o vi tri thu x
- plant*findPlant_x(list_of_plants*pList, int x) {
- plant *_pRun = pList->head;
- plant *_pPlantx = new plant();
- int i = 1;
- if (x == 1) {
- return pList->head;
- }
- else {
- while (_pRun->next != NULL&&i != x) {
- _pRun = _pRun->next;
- i++;
- }
- return _pRun;
- }
- }
- // xoa plant o vi tri thu x
- void delete_plant_x(list_of_plants*&pList, int x) {
- if (x == 1) {
- pList->lose = true;
- }
- else {
- int i = 1;
- plant* pDel;
- plant *_pRun;
- pDel = pList->head;
- while (pDel != NULL && i != x - 1) {
- pDel = pDel->next;
- i++;
- }
- _pRun = pDel->next;
- pDel->next = pDel->next->next;
- delete _pRun;
- }
- }
- // tong so damge cay thu 4 trong plist
- int SumAttack_plantEvent4(list_of_plants *pList) {
- plant*_pRun = pList->head;
- int Sumplant4 = 0;
- while (_pRun) {
- if (_pRun->number == 4) {
- Sumplant4 += _pRun->attack;
- }
- _pRun = _pRun->next;
- }
- return Sumplant4;
- }
- // luu chi so cua cay co plant food
- void add_plantfood(list_plantfood*pList,int chiso) {
- plantfood_t*pNew = new plantfood_t;
- pNew->chiso = chiso;
- plantfood_t*pRun = pList->pHead;
- if (pList->pHead == NULL) {
- pList->pHead = pNew;
- }
- else {
- while (pRun->pNext) {
- pRun = pRun->pNext;
- }
- pRun->pNext = pNew;
- pNew->pNext = NULL;
- pRun = pNew;
- }
- }
- bool isplant4_plantfood(list_of_plants*pList, list_plantfood*plist, int k) {
- plantfood_t*pRun = plist->pHead;
- int attack = 0;
- int i = 0;
- int n = 0;
- plant*pRun1 = pList->head;
- if (pList->head != NULL&&plist->pHead != NULL) {
- while (pRun)
- {
- while (pRun1)
- {
- if (pRun->chiso <= k&&pRun->chiso>0) {
- if (pRun1 == findPlant_x(pList, pRun->chiso)) {
- if (pRun1->number == 4) {
- n++;
- }
- pRun1 = pList->head;
- break;
- }
- pRun1 = pRun1->next;
- }
- else {
- break;
- }
- }
- pRun = pRun->pNext;
- }
- }
- if (n > 0) {
- return true;
- }
- else {
- return false;
- }
- }
- int Sumattack(list_of_plants*pList, list_plantfood*plist,int k) {
- plantfood_t*pRun = plist->pHead;
- int attack = 0;
- int i = 0;
- plant*pRun1 = pList->head;
- if (pList->head!=NULL&&plist->pHead!=NULL) {
- while (pRun)
- {
- while (pRun1)
- {
- if (pRun->chiso <= k&&pRun->chiso>0) {
- if (pRun1 == findPlant_x(pList, pRun->chiso)) {
- switch(pRun1->number)
- {
- case 1:attack += pRun1->attack * 5; break;
- case 2:attack += pRun1->attack * 2; break;
- case 4:attack += 1000; break;
- }
- pRun1 = pList->head;
- pRun->chiso = -1;
- break;
- }
- pRun1 = pRun1->next;
- }
- else {
- break;
- }
- }
- pRun = pRun->pNext;
- }
- }
- while (pRun1&&i!= k)
- {
- i++;
- attack += pRun1->attack;
- pRun1 = pRun1->next;
- }
- return attack;
- }
- // tăng damge cho cây trong struct plant food
- void combat(eventList* pEvent, list_of_plants *& pList) {
- list_plantfood*plantx = new list_plantfood;
- plant *pPeaPodNow = new plant;
- int _temp, _attack_plant, _hp_plant, _hp_zombie, _attack_zombie;
- int i = 0;
- int _nPeaPod = 0;
- int _n_Event = 0;
- while (pEvent != NULL&&i < 1000 && pList->lose != true) {
- _temp = pEvent->nEventCode / 1000;
- _attack_plant = (pEvent->nEventCode % 1000) / 100;
- _hp_plant = pEvent->nEventCode % 100;
- _attack_zombie = -(pEvent->nEventCode % 1000) / 100;
- _hp_zombie = -pEvent->nEventCode % 100;
- switch (_temp)
- {
- //Pea shooter
- case 1: if (_attack_plant < 10 && _attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
- _n_Event++;
- plant*_plant = new plant();
- addTail(pList, _plant);
- _plant->attack = _attack_plant;
- _plant->hp = _hp_plant;
- _plant->number = 1;
- i++;
- break;
- }
- //Pea pod
- case 2:if (_attack_plant < 10 && _attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
- _n_Event++;
- if (_nPeaPod == 0) {
- plant*_plantN = new plant();
- _plantN->attack = _attack_plant;
- _plantN->hp = _hp_plant;
- _plantN->number = 2;
- addTail(pList, _plantN);
- pPeaPodNow = _plantN;
- i++;
- _nPeaPod++;
- }
- else {
- if (_nPeaPod == 1 || _nPeaPod == 2 || _nPeaPod == 3) {
- if (pPeaPodNow->hp < _hp_plant) {
- pPeaPodNow->hp = _hp_plant;
- }
- pPeaPodNow->attack += _attack_plant;
- _nPeaPod++;
- }
- else {
- if (_nPeaPod == 4) {
- if (pPeaPodNow->hp < _hp_plant) {
- pPeaPodNow->hp = _hp_plant;
- }
- pPeaPodNow->attack += _attack_plant;
- _nPeaPod = 0;
- }
- }
- }
- break;
- }
- //Laser bean
- case 4: if (_attack_plant > 0 && _hp_plant > 0 && _hp_plant <= 10) {
- _n_Event++;
- plant*_plant = new plant();
- addTail(pList, _plant);
- _plant->attack = _attack_plant;
- _plant->hp = _hp_plant;
- _plant->number = 4;
- i++;
- break;
- }
- case 0:
- switch (pEvent->nEventCode) {
- //Wall-nut
- case 3: {
- _n_Event++;
- plant*_plant = new plant();
- addTail(pList, _plant);
- _plant->attack = 0;
- _plant->hp = 10;
- _plant->number = 3;
- i++;
- break;
- }
- //Prospector zombie
- case -5:
- _n_Event++;
- ReversePlantList(pList);
- break;
- }
- //plant food
- if (pEvent->nEventCode > 49 && pEvent->nEventCode < 60) {
- _n_Event++;
- if (pList->head == NULL) {
- break;
- }
- else {
- int I = pEvent->nEventCode % 10;
- int k = (_n_Event*I) % i + 1;
- plant*x = findPlant_x(pList, k);
- if (x->number == 3) {
- x->hp = 30;
- }
- else {
- add_plantfood(plantx, k);
- }
- }
- break;
- }
- //Explorer zombie
- if (-pEvent->nEventCode > 319 && -pEvent->nEventCode < 400) {
- _n_Event++;
- int dem = 0;
- int _hp_zb = _hp_zb = -pEvent->nEventCode % 100;
- if (pList->head == NULL) {
- pList->lose == true;
- }
- else {
- while (_hp_zb > 0 && (pList->lose != true)) {
- plant *_pTail = findPlantTail(pList);
- if (_pTail->number == 2) {
- {
- _nPeaPod = 0;
- delete_plant_x(pList, i);
- i--;
- }
- }
- else {
- delete_plant_x(pList, i);
- i--;
- }
- _hp_zb = _hp_zb - Sumattack(pList, plantx, i);
- }
- }
- }
- break;
- //Basic zombie
- case -1: if (_attack_zombie > 0 && _attack_zombie < 10 && _hp_zombie>19 && _hp_zombie < 100) {
- _n_Event++;
- if (pList->head == NULL) {
- pList->lose == true;
- }
- else {
- while (_hp_zombie > 0 && (pList->lose != true))
- {
- plant *_pTail = findPlantTail(pList);
- if (_attack_zombie >= _pTail->hp) {
- if (_pTail->number == 2) {
- {
- _nPeaPod = 0;
- delete_plant_x(pList, i);
- i--;
- }
- }
- else {
- delete_plant_x(pList, i);
- i--;
- }
- }
- else {
- _pTail->hp = _pTail->hp - _attack_zombie;
- }
- _hp_zombie = _hp_zombie - Sumattack(pList, plantx, i);
- }
- }
- break;
- }
- //Camel zombie
- case -22:
- case -23:
- case -24:
- {
- int _n_zombie1 = -((pEvent->nEventCode) / 1000) % 10;
- int _attack_zombie1 = -(pEvent->nEventCode % 1000) / 100;
- int _hp_zombie1 = -(pEvent->nEventCode % 100);
- int _hp1_zombie1 = -(pEvent->nEventCode % 100);
- if (_attack_zombie1 > 0 && _attack_zombie1 < 10 && _hp_zombie1>19 && _hp_zombie1 < 100) {
- _n_Event++;
- int k = 1;
- if (pList->head == NULL) {
- pList->lose == true;
- }
- else {
- while (_n_zombie1 > 0 && pList->lose != true) {
- plant*_pTail1 = findPlantTail(pList);
- if (_attack_zombie1 >= _pTail1->hp) {
- if (_pTail1->number == 2) {
- _nPeaPod = 0;
- delete_plant_x(pList, i);
- i--;
- }
- else {
- delete_plant_x(pList, i);
- i--;
- }
- }
- else {
- _pTail1->hp = _pTail1->hp - _attack_zombie1;
- }
- if (isplant4_plantfood(pList, plantx, i) == true) {
- _n_zombie1 = 0;
- }
- else {
- if (Sumattack(pList, plantx, i) >= _hp_zombie1) {
- _n_zombie1--;
- _hp_zombie1 = -(pEvent->nEventCode % 100) - SumAttack_plantEvent4(pList)*k;
- }
- else {
- _hp_zombie1 = _hp_zombie1 - Sumattack(pList, plantx, i);
- }
- k++;
- }
- }
- }
- }
- break;
- }
- //Lost pilot zombie
- case -40:
- case -41:
- case -42:
- case -43:
- case -44:
- case -45:
- case -46:
- case -47:
- case -48:
- case -49:
- {
- int _index_zombie2 = -((pEvent->nEventCode) / 1000) % 10;
- int _attack_zombie2 = -(pEvent->nEventCode % 1000) / 100;
- int _hp_zombie2 = -(pEvent->nEventCode % 100);
- if (_attack_zombie2 > 0 && _attack_zombie2 < 10 && _hp_zombie2>19 && _hp_zombie2 < 100) {
- _n_Event++;
- if (pList->head == NULL) {
- pList->lose = true;
- }
- else {
- int k = (_index_zombie2*_n_Event) % i + 1;
- while (_hp_zombie2 > 0 && pList->lose != true) {
- plant*_plant_x = findPlant_x(pList, k);
- if (_plant_x->hp <= _attack_zombie2) {
- if (_plant_x->number == 2) {
- i--;
- delete_plant_x(pList, k);
- k--;
- _nPeaPod = 0;
- }
- else {
- i--;
- delete_plant_x(pList, k);
- k--;
- }
- }
- else {
- _plant_x->hp = _plant_x->hp - _attack_zombie2;
- }
- _hp_zombie2 = _hp_zombie2 - Sumattack(pList, plantx, k);
- }
- }
- }
- break;
- }
- }
- pEvent = pEvent->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement