Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =========================================================================================
- * Name : plants_vs_zombies.cpp
- * Author : Nguyen Hoang Minh
- * Email : [email protected]
- * 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
- using namespace std;
- struct list_plant_food {
- int number;
- int add_attack;
- list_plant_food *next;
- };
- void insert_food (list_plant_food *&food, int k, int power) {
- list_plant_food *temp = new list_plant_food;
- list_plant_food *ptemp;
- if (food == NULL) {
- temp->number = k;
- temp->add_attack = power;
- temp->next = NULL;
- food = temp;
- }
- else {
- ptemp = food;
- while (ptemp->next != NULL) {
- ptemp = ptemp->next;
- }
- temp->number = k;
- temp->add_attack = power;
- temp->next = NULL;
- ptemp->next = temp;
- }
- }
- int total_add_att(list_plant_food *food, list_of_plants *pList,int vt_zombie) {
- int m = 0;
- list_plant_food *foo1 = new list_plant_food;
- foo1 = food;
- while (foo1 != NULL) {
- if (foo1->number>0 && foo1->number < vt_zombie) {
- foo1->number = -1;
- m += foo1->add_attack;
- }
- foo1 = foo1->next;
- }
- return m;
- }
- bool check_plant(list_of_plants *pList) {
- bool k = false;
- plant *check = pList->head;
- while (check!=NULL)
- {
- if (check->attack > 0) k = true;
- check = check->next;
- }
- return k;
- }
- int count_2(list_of_plants * pList) {
- plant*count = new plant;
- count = pList->head;
- int i = 0;
- int vt = 0;// vi tri bui cuoi cung
- while (count != NULL) {
- i++;
- if (count->number == 2) vt = i;
- count = count->next;
- }
- return vt;
- }
- void insert(list_of_plants *& pList, plant * zombie_pilot, int k_pilot) {
- plant *p, *q;
- int i = 0;
- p = zombie_pilot;
- q = pList->head;
- while (q != NULL && i != k_pilot - 1) //duyet den vi tri k-1
- {
- i++;
- q = q->next;
- }
- p->next = q->next;
- q->next = p;
- }
- int count_zombie(list_of_plants * pList) {
- plant*count1 = new plant;
- count1 = pList->head;
- int i = 1;
- while (count1->number >= 0) {
- i++;
- count1 = count1->next;
- if (count1 == NULL) break;
- }
- return i;
- }
- int count_attack4(list_of_plants * pList) {
- plant*count4 = new plant;
- count4 = pList->head;
- int i = 0;
- while (count4 != NULL) {
- if (count4->number == 4) i += count4->attack;
- count4 = count4->next;
- }
- return i;
- }
- int del(list_of_plants * pList, int k, int &tree_plant2, bool &plant_number2) {
- int i = 1;
- plant* pDel = new plant;
- pDel = pList->head;
- if (k == 1) {
- if (plant_number2==true && pList->head->number == 2) tree_plant2 = 0;
- pList->head = pList->head->next;
- delete pDel;
- }
- else {
- while (pDel != NULL && i != k - 1)
- {
- pDel = pDel->next;
- i++;
- }
- plant *temp = pDel->next;
- if (plant_number2==true && pDel->next->number == 2) tree_plant2 =0;
- pDel->next = pDel->next->next;
- delete temp;
- }
- return i;
- }
- int total_attack(list_of_plants *pList, int &add_attack) {
- int i = 0;
- plant* total = new plant;
- total = pList->head;
- while (total!=NULL && total->number>0)
- {
- i += total->attack;
- total = total->next;
- }
- i = i + add_attack;
- add_attack = 0;
- return i;
- }
- int total_attack2(list_of_plants *pList, int &add_attack) {
- int i = 0;
- plant* total = new plant;
- total = pList->head;
- while (total!=NULL )
- {
- if (total->number > 0 && total->number < 4)i += total->attack;
- if (total->number < 0) break;
- total = total->next;
- }
- i += add_attack;
- add_attack = 0;
- return i;
- }
- int total_plant(list_of_plants * pList) {
- int i = 0;
- plant* total = new plant;
- total = pList->head;
- while (total != NULL) {
- i++;
- total = total->next;
- }
- return i;
- }
- void combat(eventList* pEvent, list_of_plants *& pList) {
- list_plant_food * food = new list_plant_food;
- food = NULL;
- plant* tre = new plant;
- tre->next = NULL;
- int tree_number2 = 0;
- int number_event = 0;
- bool check_food_in_plant4 = false;
- bool plant_number2 = true;
- while (pEvent != NULL) {
- // them cay so 3
- if (pEvent->nEventCode == 3) {
- number_event++;
- plant* tree = new plant;
- tree->number = 3;
- tree->attack = 0;
- tree->hp = 10;
- tree->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = tree;
- tre = tree;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = tree;
- tre = tree;
- }
- }
- // them plant food
- if (pEvent->nEventCode > 49 && pEvent->nEventCode < 60) {
- number_event++;
- /*if (pEvent->nEventCode == 50) {
- cout << pEvent->nEventCode;
- }*/
- plant* a = new plant;
- a = pList->head;
- list_plant_food *temp1 = new list_plant_food;
- temp1 = food;
- bool kiemtra = true;
- if (a != NULL) {
- int I = pEvent->nEventCode % 10;
- int s = total_plant(pList);
- if (s > 0) {
- int k_food = (number_event*I) % s + 1;
- // kiem tra tron list moi
- while (temp1 != NULL) {
- if (temp1->number == k_food) {
- kiemtra = false;
- break;
- }
- temp1 = temp1->next;
- }
- ////// kiem tra cai cay number==3
- plant* tree = new plant;
- tree = pList->head;
- for (int i = 1; i < k_food; i++) {
- tree = tree->next;
- }
- if (tree->number == 3) tree->hp = 30;
- else if (tree->number == 4) check_food_in_plant4 = true;
- else if (tree->number == 1 && kiemtra == true) {
- int add_attack = tree->attack * 5;
- insert_food(food, k_food, add_attack);
- }
- else if (tree->number == 2 && kiemtra == true) {
- int add_attack = tree->attack * 2;
- insert_food(food, k_food, add_attack);
- }
- }
- }
- }
- // them cay so 1
- if (pEvent->nEventCode > 1100 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode < 1911 && pEvent->nEventCode % 100>0) {
- number_event++;
- /*if (pEvent->nEventCode == 1203) {
- cout << pEvent->nEventCode;*/
- plant* tree_1 = new plant;
- tree_1->number = 1; pEvent->nEventCode = pEvent->nEventCode - 1000;
- tree_1->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
- tree_1->hp = pEvent->nEventCode;
- tree_1->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = tree_1;
- tre = tree_1;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = tree_1;
- tre = tree_1;
- }
- }
- // them cay so 4
- if (pEvent->nEventCode > 4100 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode < 4911 && pEvent->nEventCode % 100>0) {
- number_event++;
- plant* tree = new plant;
- tree->number = 4; pEvent->nEventCode = pEvent->nEventCode - 4000;
- tree->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
- tree->hp = pEvent->nEventCode;
- tree->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = tree;
- tre = tree;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = tree;
- tre = tree;
- }
- }
- // them cay so 2
- if (pEvent->nEventCode > 2100 && pEvent->nEventCode < 2911 && pEvent->nEventCode % 100 < 11 && pEvent->nEventCode % 100 > 0) {
- number_event++;
- plant* tree = new plant;
- tree->number = 2; pEvent->nEventCode = pEvent->nEventCode - 2000;
- tree->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
- tree->hp = pEvent->nEventCode;
- tree->next = NULL;
- // Danh so thu tu cho cay
- tree_number2++;
- // them cay so 2 dau tien lam bui cay
- if (tree_number2 == 1) {
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = tree;
- tre = tree;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = tree;
- tre = tree;
- }
- }
- if (tree_number2 > 1 && tree_number2 < 6) {
- // Tim kiem bui cay
- if (plant_number2 == true) {
- plant* add_tree2 = pList->head;
- int k;
- k = count_2(pList);
- for (int x1 = 1; x1 < k; x1++) {
- add_tree2 = add_tree2->next;
- }
- add_tree2->attack += tree->attack;
- if (tree->hp > add_tree2->hp) add_tree2->hp = tree->hp;
- if (tree_number2 == 5) tree_number2 = 0;
- }
- else {
- plant* add_tree2 = pList->head;
- while (add_tree2->number > 0) {
- if (add_tree2->number == 2) break;
- add_tree2 = add_tree2->next;
- }
- add_tree2->attack += tree->attack;
- if (tree->hp > add_tree2->hp) add_tree2->hp = tree->hp;
- if (tree_number2 == 5) {
- plant_number2 = true;
- tree_number2 = 0;
- }
- }
- }
- }
- // them zombie_basic
- if (pEvent->nEventCode > -2000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-1121 && pEvent->nEventCode % 100>-100) {
- number_event++;
- plant* zombie_basic = new plant;
- zombie_basic->number = -1; pEvent->nEventCode = abs(pEvent->nEventCode) - 1000;
- zombie_basic->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
- zombie_basic->hp = pEvent->nEventCode;
- zombie_basic->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = zombie_basic;
- tre = zombie_basic;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = zombie_basic;
- tre = zombie_basic;
- }
- // Danh nhau di nao !
- plant* a = new plant;
- a = pList->head;
- int number_attack = 1;
- while (zombie_basic->hp > 0 && a->number > 0)
- {
- int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
- plant* att_zombie = pList->head;
- for (int x2 = 1; x2 < k_zombie - 1; x2++) {
- att_zombie = att_zombie->next;
- }
- if (number_attack % 2 == 1) {// Zombie tan cong
- number_attack++;
- if (att_zombie->hp <= zombie_basic->attack) {
- del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
- }
- else {
- att_zombie->hp = att_zombie->hp - zombie_basic->attack; // tru mau cua Plant
- }
- }
- else {// Tong cay trong tan cong
- number_attack++;
- int add_attack = total_add_att(food, pList, k_zombie);
- zombie_basic->hp = zombie_basic->hp - total_attack(pList, add_attack);
- if (zombie_basic->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
- }
- }
- }
- // Them Camel zombie
- if (pEvent->nEventCode > -25000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-22119 && pEvent->nEventCode % 100>-100) {
- number_event++;
- plant* zombie_camel = new plant;
- zombie_camel->number = -2; pEvent->nEventCode = abs(pEvent->nEventCode) - 20000;
- zombie_camel->attack = pEvent->nEventCode / 100; pEvent->nEventCode = pEvent->nEventCode % 100;
- zombie_camel->hp = pEvent->nEventCode;
- zombie_camel->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = zombie_camel;
- tre = zombie_camel;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = zombie_camel;
- tre = zombie_camel;
- }
- // Danh nhau di nao !
- plant* a = new plant;
- a = pList->head;
- int number_attack = 1;
- while (zombie_camel->hp > 0 && a->number > 0)
- {
- int k_zombie = count_zombie(pList);
- plant* att_zombie = pList->head;
- for (int x2 = 1; x2 < k_zombie - 1; x2++) {
- att_zombie = att_zombie->next;// tro den vi tri cay truoc mat zombie
- }
- if (number_attack % 2 == 1) {// Zombie tan cong
- number_attack++;
- if (att_zombie->hp <= (zombie_camel->attack) % 10) {
- del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
- }
- else {
- att_zombie->hp -= (zombie_camel->attack) % 10; // tru HP cua Plant
- }
- }
- else {// Tong cay trong tan cong
- number_attack++;
- if (check_food_in_plant4 == true) {
- del(pList, k_zombie, tree_number2, plant_number2);
- check_food_in_plant4 = false;
- break;
- }
- else {
- int add_attack = total_add_att(food, pList, k_zombie);
- pEvent->nEventCode = pEvent->nEventCode - count_attack4(pList);
- zombie_camel->hp = zombie_camel->hp - count_attack4(pList);
- zombie_camel->hp = zombie_camel->hp - total_attack2(pList, add_attack);
- if (zombie_camel->hp < 1 && zombie_camel->attack>20) {
- zombie_camel->attack -= 10;
- zombie_camel->hp = pEvent->nEventCode;
- }
- else if (zombie_camel->hp < 1 && zombie_camel->attack < 20)
- {
- del(pList, k_zombie, tree_number2, plant_number2);
- }
- }
- }
- }
- /*int k_zombie2 = count_zombie(pList);
- plant *zombie_camel2 = pList->head;
- while (zombie_camel2->next != NULL) {
- zombie_camel2 = zombie_camel2->next;
- }
- if (zombie_camel2->hp < 1 && zombie_camel2->attack < 20) del(pList, k_zombie2, tree_number2, plant_number2);*/
- }
- // them pilot zombie ;
- if (pEvent->nEventCode > -50000 && pEvent->nEventCode % 100 < -19 && pEvent->nEventCode<-40119 && pEvent->nEventCode % 100>-100)
- {
- /*if (pEvent->nEventCode == -44698) {
- cout << pEvent->nEventCode;*/
- number_event++;
- plant* zombie_pilot = new plant;
- zombie_pilot->number = -4; pEvent->nEventCode = abs(pEvent->nEventCode) - 40000;
- int I = pEvent->nEventCode / 1000;
- pEvent->nEventCode = pEvent->nEventCode % 1000;
- zombie_pilot->attack = pEvent->nEventCode / 100;
- pEvent->nEventCode = pEvent->nEventCode % 100;
- zombie_pilot->hp = pEvent->nEventCode;
- zombie_pilot->next = NULL;
- int s = total_plant(pList);
- int k_pilot = (number_event*I) % s + 1;
- insert(pList, zombie_pilot, k_pilot);
- // Danh nhau di nao !
- plant* a = new plant;
- a = pList->head;
- int number_attack = 1;
- while (zombie_pilot->hp > 0 && a->number > 0)
- {
- int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
- plant* att_zombie = pList->head;
- for (int x2 = 1; x2 < k_zombie - 1; x2++) {
- att_zombie = att_zombie->next;
- }
- if (number_attack % 2 == 1) {// Zombie tan cong
- number_attack++;
- if (att_zombie->hp <= zombie_pilot->attack) {
- del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
- }
- else {
- att_zombie->hp = att_zombie->hp - zombie_pilot->attack; // tru HP cua Plant
- }
- }
- else {// Tong cay trong tan cong
- number_attack++;
- int add_attack = total_add_att(food, pList, k_zombie + 1);
- zombie_pilot->hp = zombie_pilot->hp - total_attack(pList, add_attack);
- if (zombie_pilot->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
- }
- }
- }
- if (pEvent->nEventCode > -400 && pEvent->nEventCode < -319) {
- number_event++;
- plant* zombie_explorer = new plant;
- zombie_explorer->number = -3; pEvent->nEventCode = abs(pEvent->nEventCode) - 300;
- zombie_explorer->attack = 300; pEvent->nEventCode = pEvent->nEventCode % 100;
- zombie_explorer->hp = pEvent->nEventCode;
- zombie_explorer->next = NULL;
- if (pList->head == NULL)
- {
- pList->lose = false;
- pList->head = zombie_explorer;
- tre = zombie_explorer;
- }
- else {
- tre = pList->head;
- while (tre->next != NULL) {
- tre = tre->next;
- }
- tre->next = zombie_explorer;
- tre = zombie_explorer;
- }
- // Danh nhau di nao !
- plant* a = new plant;
- a = pList->head;
- int number_attack = 1;
- while (zombie_explorer->hp > 0 && a->number > 0)
- {
- int k_zombie = count_zombie(pList); // Lay vi tri cay truoc mat zombie
- plant* att_zombie = pList->head;
- for (int x2 = 1; x2 < k_zombie - 1; x2++) {
- att_zombie = att_zombie->next;
- }
- if (number_attack % 2 == 1) {// Zombie tan cong
- number_attack++;
- del(pList, k_zombie - 1, tree_number2, plant_number2);// xoa cay
- }
- else {// Tong cay trong tan cong
- number_attack++;
- int add_attack = total_add_att(food, pList, k_zombie);
- zombie_explorer->hp = zombie_explorer->hp - total_attack(pList, add_attack);
- if (zombie_explorer->hp < 1) del(pList, k_zombie, tree_number2, plant_number2);
- }
- }
- }
- if (pEvent->nEventCode == -5) {
- number_event++;
- if (check_plant(pList) == true) {
- plant_number2 = false;
- plant *k_1 = NULL;
- plant *pre = NULL;
- while (pList->head != NULL) {
- k_1 = pList->head;
- pList->head = pList->head->next;
- k_1->next = pre;
- pre = k_1;
- }
- pList->head = k_1;
- }
- }
- pEvent = pEvent->next;
- }
- /*if (pList->head->number <0) pList->lose = true;*/
- }
Advertisement
Add Comment
Please, Sign In to add comment