diff --git a/8382/Kuzina Anastasiya/Base.cpp b/8382/Kuzina Anastasiya/Base.cpp new file mode 100644 index 000000000..d697caa35 --- /dev/null +++ b/8382/Kuzina Anastasiya/Base.cpp @@ -0,0 +1,360 @@ +#include "Base.h" + +base::base(int num) { + number = num; + hp = feature(200); + max_units = 3; + tmp_unit = 0; + count_units = 0; + x = 0; + is_alive = true; + y = 0; + name = 30; + owner = NULL; + for (int i = 0; i < 3; i++) { + units[i] = create_unit(get_creature()); + is_unit[i] = true; + } + +} + +base::~base() { + for (int i = 0; i < 3; i++) { + if(is_unit[i]) + delete(units[i]); + } + +} + +void* base::get_owner() { return owner; } + +void base::set_owner(void* own) { owner = own; } + +void base::set_coords(int xx, int yy) { + if (xx < 0 || yy < 0) throw except("(base::set_coords) Trying to set base negative coords: ("+to_string(xx)+","+to_string(yy)+")\n"); + x = xx; + y = yy; +} + +int base::set_hp(int new_hp) { + if (new_hp <= 0) { + new_hp = 0; + is_alive = false; + name = 'X'; + } + hp.set_tmp(new_hp); + if (hp.get_tmp() <= 0) return 1; +} + +int base::sub_hp(int sub) { + if(sub > 0) hp.add( -1*sub); + if (hp.get_tmp() <= 0) { + set_hp(0); + return 1; + } +} + +bool base::is_base_alive(){ + return is_alive; +} + +int base::get_hp() { + return hp.get_tmp(); +} + +unit* base::get_unit(int i) { + int f = is_any_alive(); + if (f == -1) return NULL; + if (i < 0 || i > 2) return units[f]; + if (is_unit[i]) return units[i]; + else return NULL; +} + +bool base::is_unit_alive(int i) { + if (i < 0 || i > 2) return is_unit[0]; //ERROR + return is_unit[i]; + +} + +int base::get_number() { + return number; +} + +unit* base::get_tmp_unit() { + if(!is_unit[tmp_unit]) return NULL; + return units[tmp_unit]; +} + +bool base::set_tmp_unit(int i) { + if (i < 0 || i > 2) return false; + if (!is_unit[i]) return false; + tmp_unit = i; + return true; +} + +int base::is_any_alive() { + for (int i = 0; i < 3; i++) { + if (is_unit[i]) return i; + } + logger::add("Base number " + to_string(number) + " has no more units alive!\n"); + return -1; +} + +int base::how_many_alive() { + int i = 0; + for (int j = 0; j < 3; j++) { + if (is_unit[i])i++; + } + return i; + +} + +int base::get_x() { + return x; +} + +int base::get_y() { + return y; +} + +char base::get_name() { + return name; +} + +void base::set_unit(int x, int y, char name, int hdp, int hp_max, int armor, int attack, int xp, int xp_level, int level, int i) { + if (is_unit[i]) delete_unit(i); + if (x < 0 || y < 0 || hdp <= 0 || i < 0 || i > 2) throw except("(base::set_unit)Something goes wrong with creating unit!\n\tStats: (" + to_string(x)+"," +to_string(y) + ") hp: " + to_string(hdp) +" at " + to_string(i) +" position in base number "+ to_string(number) + "\n"); + + + + is_unit[i] = true; + switch (name) { + case 'c': { + crow* unit_crow = new crow; + unit_crow->set_base_nubmer(number); + unit_crow->set_base(this); + count_units++; + unit_crow->set_coords(x, y); + unit_crow->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_crow; + return; + } + case 'w': { + wolf* unit_wolf = new wolf; + unit_wolf->set_base_nubmer(number); + unit_wolf->set_base(this); + count_units++; + unit_wolf->set_coords(x, y); + unit_wolf->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_wolf; + + return; + } + case 'h': { + hyena* unit_hyena = new hyena; + unit_hyena->set_base_nubmer(number); + unit_hyena->set_base(this); + unit_hyena->set_coords(x, y); + unit_hyena->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_hyena; + count_units++; + + return; + } + case 'l': { + lizard* unit_lizard = new lizard; + unit_lizard->set_base_nubmer(number); + unit_lizard->set_base(this); + count_units++; + unit_lizard->set_coords(x, y); + unit_lizard->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_lizard; + return; + } + case 'd': { + duck* unit_duck = new duck; + unit_duck->set_base_nubmer(number); + unit_duck->set_base(this); + count_units++; + unit_duck->set_coords(x, y); + unit_duck->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_duck; + return; + } + case 'f': { + frog* unit_frog = new frog; + unit_frog->set_base_nubmer(number); + unit_frog->set_base(this); + count_units++; + unit_frog->set_coords(x, y); + unit_frog->set_all(hdp, hp_max, armor, attack, xp, xp_level, level); + + + units[i] = unit_frog; + + return; + } + + + } + is_unit[i] = false; + throw except("(base::set_unit) Can't create unit with this name: "+to_string(name)+"\n\tit's dosn't match any type-name\n"); +} + +creature base::get_creature() { + //srand(time(0)); + int c = rand() % 6; + switch (c) { + case 0: return Duck; + case 1: return Crow; + case 2: return Wolf; + case 3: return Hyena; + case 4: return Frog; + case 5: return Lizard; + } + +} + +unit* base::create_unit(creature type = Crow) { + if (count_units >= max_units) { + cout << "There is maximum count of units alredy!\n"; + return NULL; + } + if (type == None) { + logger::add("Can't create None unit\n"); + throw exception("(base::create_unit) Can't create None-type unit!\n"); + return NULL; + } // ERROR + if (!is_alive) return NULL; + + logger::add("Base number "+to_string(number) +" just make new unit-"); + switch (type) { + case Crow: { + crow* unit_crow = new crow; + unit_crow->set_base_nubmer(number); + unit_crow->set_base(this); + count_units++; + logger::add("crow\n"); + return unit_crow; + break; + } + case Wolf: { + wolf* unit_wolf = new wolf; + unit_wolf->set_base_nubmer(number); + unit_wolf->set_base(this); + count_units++; + logger::add("wolf\n"); + return unit_wolf; + break; + } + case Hyena: { + hyena* unit_hyena = new hyena; + unit_hyena->set_base_nubmer(number); + unit_hyena->set_base(this); + count_units++; + logger::add("hyena\n"); + return unit_hyena; + break; + } + case Lizard: { + lizard* unit_lizard = new lizard; + unit_lizard->set_base_nubmer(number); + unit_lizard->set_base(this); + count_units++; + logger::add("lizard\n"); + return unit_lizard; + break; + } + case Duck: { + duck* unit_duck = new duck; + unit_duck->set_base_nubmer(number); + unit_duck->set_base(this); + count_units++; + logger::add("duck\n"); + return unit_duck; + break; + } + case Frog: { + frog* unit_frog = new frog; + unit_frog->set_base_nubmer(number); + unit_frog->set_base(this); + logger::add("frog\n"); + count_units++; + return unit_frog; + + break; + } + + } + +} + +int base::create_unit() { + if (count_units >= max_units) { + cout << "There is maximum count of units alredy!\n"; + return -1; + } + if (!is_alive) return -1; + + for (int i = 0; i < 3; i++) { + if (!is_unit[i]) { + units[i] = create_unit(get_creature()); + is_unit[i] = true; + return i; + } + + } + return -1; + +} + +int base::delete_unit() { + for (int i = 0; i < max_units; i++) { + if (is_unit[i] && (units[i])->get_hp() <= 0 ) { + delete(units[i]); + logger::add("Base number " + to_string(number) + " just delete unit\n"); + is_unit[i] = false; + count_units--; + } + + } + return -1; +} + +int base::delete_unit(int i) { + if (is_unit[i]) { + delete units[i]; + is_unit[i] = false; + count_units--; + } + return i; +} + +void base::get_info() { + + std::cout << " \t Your base at (" << x << " ; " << y << ") \n"; + std::cout << " \t with " << hp.get_tmp() << "\\200 hp\n"; +} + +bool operator-(base& un, unit* it) { + if (un.is_alive) logger::add("\tBase number " + to_string(un.number) + " get hit; now it has " + to_string(un.get_hp()) + " hp\n"); + + un.sub_hp(it->get_attack() / 2); + if (un.get_hp() > 0) return false; + if (!un.is_alive) return true; + logger::add("Base number " + to_string(un.number) + " is dead!\n"); + un.set_hp(0); + un.is_alive = false; + un.name = 'X'; + return true; +} diff --git a/8382/Kuzina Anastasiya/Base.h b/8382/Kuzina Anastasiya/Base.h new file mode 100644 index 000000000..c2ab97745 --- /dev/null +++ b/8382/Kuzina Anastasiya/Base.h @@ -0,0 +1,54 @@ +#pragma once +#include +#include +#include "Unit.h" +#include "Logger.h" +#include "Exept.h" +//#include "Field.h" + +class base { + int x; + int y; + int tmp_unit; + feature hp; + char name; + int number; + bool is_alive; + int max_units; + int count_units; + unit* units[3]; + bool is_unit[3]; + void* owner; + unit* create_unit(creature type); + creature get_creature(); + + +public: + base(int num); + ~base(); + int how_many_alive(); + int create_unit(); + void set_unit(int x, int y, char name, int hdp, int hp_max, int armor, int attack, int xp, int xp_level, int level, int i); + + int get_hp(); + void* get_owner(); + void set_owner(void* own); + void get_info(); + unit* get_tmp_unit(); + bool set_tmp_unit(int i); + int get_number(); + bool is_base_alive(); + bool is_unit_alive(int i); + int is_any_alive(); + + char get_name(); + int get_x(); + int get_y(); + friend bool operator-(base& un, unit* it); + void set_coords(int xx, int yy); + int set_hp(int new_hp); + int sub_hp(int sub); + unit* get_unit(int i); + int delete_unit(); + int delete_unit(int i); +}; diff --git a/8382/Kuzina Anastasiya/Exept.cpp b/8382/Kuzina Anastasiya/Exept.cpp new file mode 100644 index 000000000..648e2c7e1 --- /dev/null +++ b/8382/Kuzina Anastasiya/Exept.cpp @@ -0,0 +1,5 @@ +#include "Exept.h" + + +except::except(std::string e): std::exception(e.c_str()){ +} diff --git a/8382/Kuzina Anastasiya/Exept.h b/8382/Kuzina Anastasiya/Exept.h new file mode 100644 index 000000000..344da4552 --- /dev/null +++ b/8382/Kuzina Anastasiya/Exept.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include + + + +class except : public std::exception { +public: + except(std::string e); + +}; diff --git a/8382/Kuzina Anastasiya/Feature.cpp b/8382/Kuzina Anastasiya/Feature.cpp new file mode 100644 index 000000000..3f4b4da32 --- /dev/null +++ b/8382/Kuzina Anastasiya/Feature.cpp @@ -0,0 +1,71 @@ +#include "Feature.h" + +feature::feature() { + max = 1; + tmp = 1; +} + +feature::feature(int n_max) { + max = n_max; + tmp = n_max; +} + +int feature::get_tmp() { + return tmp; +} + +int feature::get_max() { + return max; +} + +void feature::set_tmp(int n_tmp) { + if (n_tmp < 0) n_tmp = 0; + tmp = n_tmp; +} + +void feature::set_max(int n_max) { + if (n_max <= 0) n_max = 1; + max = n_max; +} + +bool feature::add(int num) { //num < 0 + tmp += num; + if (tmp > max) tmp = max; + if (tmp <= 0) return 1; // 1, + return 0; +} + + +ulevel::ulevel() { + tmp_xp = 0; + tmp_level = 1; + xp_for_next_level = 20; +} + +void ulevel::new_level() { + tmp_xp = 0; + tmp_level++; + xp_for_next_level = xp_for_next_level*2; + +} + +bool ulevel::plus_xp(int xp) { + tmp_xp += xp; + if (tmp_xp >= xp_for_next_level) { + new_level(); + return 1; + } + return 0; +} + +int ulevel::get_tmp_xp() { + return tmp_xp; +} + +int ulevel::get_tmp_level() { + return tmp_level; +} + +int ulevel::get_xp_for_next_level() { + return xp_for_next_level; +} \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Feature.h b/8382/Kuzina Anastasiya/Feature.h new file mode 100644 index 000000000..19b95b3c7 --- /dev/null +++ b/8382/Kuzina Anastasiya/Feature.h @@ -0,0 +1,27 @@ +#pragma once + +class feature { + int tmp; + int max; +public: + feature(); + feature(int n_max); + int get_tmp(); + int get_max(); + void set_tmp(int n_tmp); + void set_max(int n_max); + bool add(int num); +}; + +class ulevel { + int tmp_xp; + int tmp_level; + int xp_for_next_level; + void new_level(); +public: + ulevel(); + bool plus_xp(int xp); + int get_tmp_level(); + int get_xp_for_next_level(); + int get_tmp_xp(); +}; diff --git a/8382/Kuzina Anastasiya/Field.cpp b/8382/Kuzina Anastasiya/Field.cpp new file mode 100644 index 000000000..09c43eeca --- /dev/null +++ b/8382/Kuzina Anastasiya/Field.cpp @@ -0,0 +1,343 @@ +#pragma once +#include +using namespace std; +#include +#include +#include +#include "Base.h" +#include "Field.h" + + field_cell::field_cell(int new_y, int new_x, int new_land) { + is_bas = false; + tmp_base = NULL; + if (new_x < 0 || new_y < 0) throw except("(field_cell::fild_cell) Trying to create cell with negative coords: ("+to_string(new_x)+","+to_string(new_y)+")\n"); + x = new_x; + y = new_y; + type_unit = None; + int c = rand() % 50; + if (c <= 5 || c > 12) { + type_item = Non; + tmp_item = NULL; + } + else if (c > 5 && c < 8) { + type_item = Exp; + tmp_item = new exp_bottle(); + } + else if(c > 7 && c < 10){ + type_item = Pill; + tmp_item = new hp_pill(); + } + else if (c > 9 && c < 12) { + type_item = Zatochka; + tmp_item = new zatochka(); + } + else if (c > 11 && c < 13) { + type_item = Rand; + tmp_item = new rpill(); + } + switch (new_land) { + case 0: { + land = sunflower(); + break; + } + case 1:{ + land = lake(); + break; + } + case 2: { + land = mountain(); + break; + } + case 3: { + land = forest(); + break; + } + default: + throw except("(filed_cell::field_cell) Cell("+to_string(x)+","+to_string(y)+") Wrong landscape number! Expected for 0 to 3, get: "+to_string(new_land)+"\n"); + } + } + void field_cell::set_unit(unit* new_unit) { + tmp_unit = new_unit; + type_unit = new_unit->get_type(); + new_unit->set_coords(x, y); + } + int field_cell::set_item(int c) { + if (c == 1) { + type_item = Exp; + tmp_item = new exp_bottle(); + } + else if (c == 2) { + type_item = Pill; + tmp_item = new hp_pill(); + } + else if (c == 3) { + type_item = Zatochka; + tmp_item = new zatochka(); + } + else if (c == 4) { + type_item = Rand; + tmp_item = new rpill(); + } + else throw except("(field_cell::set_item) Cell(" + to_string(x) + "," + to_string(y) + ") Incorrect item number, expected from 1 to 4, get: "+to_string(c)+"\n"); + return c; + } + bool field_cell::is_empty() { + int tes = 1; + if (type_unit != None) tes = 0; + if (type_item != Non) tes = 0; + if (is_bas) tes = 0; + + return tes; + } + void field_cell::set_unit_type(creature new_type) { + type_unit = new_type; + } + int field_cell::get_x() { + return x; + } + int field_cell::get_y() { + return y; + } + bool field_cell::is_base() { return is_bas;} + Item field_cell::get_item_type() { + return type_item; + } + item* field_cell::get_item() { + if (type_item == Non) throw except("(field_cell::get_item) Cell(" + to_string(x) + "," + to_string(y) + ") Trying to get item, but item-type is None\n"); + return tmp_item; + } + void field_cell::delete_item() { + if (type_item != Non) { + delete(tmp_item); + type_item = Non; + } + } + void field_cell::set_base(base* b) { + if (is_bas) throw except("(field_cell::set_base) Cell(" + to_string(x) + "," + to_string(y) + ") There is alredy base! Can't rewrite\n"); + tmp_base = b; + is_bas = true; + //ERROR + } + base* field_cell::get_base() { return tmp_base; } + + char field_cell::get_land_icon() { + return land.get_icon(); + } + int field_cell::get_color() { + return land.get_color(); + } + int field_cell::get_land_number() { + if (get_land_icon() == 126) return 1; + if (get_land_icon() == 15) return 0; + if (get_land_icon() == 6) return 3; + if (get_land_icon() == 94)return 2; + } + biome field_cell::get_land() { + return land; + } + int field_cell::get_background() { + return land.get_background(); + } + + creature field_cell::get_unit_type() { + return type_unit; + } + unit* field_cell::get_unit() { + + return tmp_unit; + } + void field_cell::delete_unit() { + tmp_unit = NULL; + set_unit_type(None); + return; + } + + + + field::field(int x, int y) { + if (x <= 0 || y <= 0) throw except("(field::field)Field can't be created with this parameters: ("+to_string(x)+","+to_string(y)+")\n"); + logger::add("Field (" +to_string(x) + "," +to_string(y)+") was created\n"); + max_unit = y * x / 3; + count_units = 0; + x_size = x; + y_size = y; + map = (field_cell**)malloc(sizeof(field_cell*) * y_size); + + for (int i = 0; i < y_size; i++) { + map[i] = (field_cell*)malloc(sizeof(field_cell) * x_size); + + for (int j = 0; j < x_size; j++) + map[i][j] = field_cell(i, j, random_land(i, j)); + + } + return; + } + int field::random_land(int y, int x) { + if(x < 0 || y < 0) throw except("(field::random_land) Can't return random land with this coords (" + to_string(x) + "," + to_string(y) + ")\n"); + int r = 1; + if (x == 0) { + if (y == 0) { + return rand() % 4; + } + else { + r = rand() % 3; + if (r) return rand() % 4; + else return map[y - 1][x].get_land_number(); + } + } + else { + r = rand() % 2; + if (r) return rand() % 4; + else return map[y][x-1].get_land_number(); + } + return 0; + } + field_cell* field::get_cell(int x, int y) { + if (x < 0 || y < 0 || x>=x_size ||y >= y_size) throw except("(field::get_cell)Can't return field cell["+to_string(x)+","+to_string(y)+"]\n"); + else return &(map[y][x]); + } + + void field::set_base(int x, int y, base* b) { + if (x < 0 || y < 0) throw except("(field::set_base)Can't set base at cell[" + to_string(x) + "," + to_string(y) + "]\n"); + map[y][x].set_base(b); + } + field_cell* field::find_empty(int x, int y) { + int s = 50000; + int m = 0; + field_cell* tsmp; + int xx = -1, yy = -1; + for (int i = 0; i < y_size; i++) { + for (int j = 0; j < x_size; j++) { + if (map[i][j].is_empty()) { + m = (x - j)* (x - j) + (y- i)* (y - i); + if (m < s) { + s = m; + xx = j; + yy = i; + } + } + } + } + if (xx < 0 || yy < 0) throw except("(field::find_empty) Field has no empty cells!\n"); + return &(map[yy][xx]); + } + void field::move_unit(int x1, int y1, int x2, int y2) { + + if (y1 < 0 || y2 < 0 || x1 < 0 || x2 < 0) return; + if (y1 >= y_size || y2 >= y_size || x1 >= x_size || x2 >= x_size) return; + + if (map[y1][x1].get_unit_type() == None) { + logger::add("No unit at (" + to_string(x1) + "," + to_string(y1) + "), move denyed\n"); + cout << "There is no unit!\n"; + throw except("(field::move_unit) Can't move unit from (" + to_string(x1) + "," + to_string(y1) + ") There is no unit!\n"); + return; + } + base* bb; + logger::add("unit"+ map[y1][x1].get_unit()->get_u_info() +"trying to move to (" + to_string(x2) + "," + to_string(y2)+")\n"); + if (map[y2][x2].get_unit_type() != None) { + bb = (static_cast(map[y2][x2].get_unit()->get_base())); + logger::add(" but can't: there is unit already:" + map[y2][x2].get_unit()->get_u_info() + "\n"); + if (map[y1][x1].get_unit()->get_base_number() == map[y2][x2].get_unit()->get_base_number()) { + logger::add("\t and it's his bro!"); + } + else + *(map[y2][x2].get_unit()) - map[y1][x1].get_unit(); + + if (map[y2][x2].get_unit()->get_hp() <= 0) { + int i = (bb)->delete_unit(); + map[y2][x2].delete_unit(); + if (i != -1) { + find_empty(bb->get_x(), bb->get_y())->set_unit(bb->get_unit(i)); + } + + + } + } + if (map[y2][x2].is_base()) { + logger::add(" but can't: there base"); + if (map[y1][x1].get_unit()->get_base_number() == map[y2][x2].get_base()->get_number()) { + logger::add(" and it's unit's home-base\n"); + return; + } + else { + logger::add(" and unit attack it\n"); + *(map[y2][x2].get_base()) - map[y1][x1].get_unit(); + return; + } + } + bb = (static_cast(map[y1][x1].get_unit()->get_base())); + if (map[y1][x1].get_unit()->get_hp() <= 0) { + int i = (bb)->delete_unit(); + map[y1][x1].delete_unit(); + + } + + + if (!map[y2][x2].is_empty() && map[y2][x2].get_item_type() == Non) return; + (static_cast((map[y1][x1].get_unit()))->set_coords(x2, y2)); + map[y2][x2].set_unit_type(map[y1][x1].get_unit_type()); + map[y2][x2].set_unit(map[y1][x1].get_unit()); + map[y1][x1].set_unit_type(None); + map[y2][x2].get_unit()->add_hp(map[y2][x2].get_land().get_effect((map[y2][x2].get_unit()))); + + bb = (static_cast(map[y2][x2].get_unit()->get_base())); + + if (map[y2][x2].get_item_type() != Non) { + *(map[y2][x2].get_unit()) + map[y2][x2].get_item(); + map[y2][x2].delete_item(); + + } + if (map[y2][x2].get_unit()->get_hp() <= 0) { + int i = (bb)->delete_unit(); + map[y2][x2].delete_unit(); + return; + } + logger::add("\t unit moves! " + map[y2][x2].get_unit()->get_u_info() +"\n"); + } + + + + + field::field() {} + int field::get_item_count() { + int g = 0; + for (int i = 0; i < y_size; i++) { + for (int j = 0; j < x_size; j++) { + if (map[i][j].get_item_type() != Non) + g++; + } + + } + return g; + } + field::field(int x, int y, int **lands) { + if (x <= 0 || y <= 0) throw exception("Field can't be created, y or x is < 0\n"); + max_unit = y * x / 3; + count_units = 0; + x_size = x; + y_size = y; + map = (field_cell**)malloc(sizeof(field_cell*) * y_size); + + for (int i = 0; i < y_size; i++) { + map[i] = (field_cell*)malloc(sizeof(field_cell) * x_size); + + for (int j = 0; j < x_size; j++) { + if (lands[i][j] < 0 || lands[i][j] > 3) throw except("(field::field(**lands)) Wrong land number:" + to_string(lands[i][j])+" for cell ("+to_string(x)+","+to_string(y)+")\n"); + map[i][j] = field_cell(i, j, lands[i][j]); + if (map[i][j].get_item_type() != Non) + map[i][j].delete_item(); + } + + } + return; + + + } + int field::get_x_size() { return x_size; } + int field::get_y_size() { return y_size; } + field::~field() { + for (int i = 0; i < y_size; i++) { + delete(map[i]); + } + delete map; + } \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Field.h b/8382/Kuzina Anastasiya/Field.h new file mode 100644 index 000000000..2a63b89b0 --- /dev/null +++ b/8382/Kuzina Anastasiya/Field.h @@ -0,0 +1,76 @@ +#pragma once +#include +#include "Unit.h" +#include "Base.h" +#include "Land.h" +#include "Item.h" +#include "Logger.h" + +class field_cell { +private: + int x; + int y; + creature type_unit; + unit* tmp_unit; + biome land; + bool is_bas; + Item type_item; + item* tmp_item; + base* tmp_base; + + +public: + field_cell(int new_y, int new_x, int new_land); + void set_unit(unit* new_unit); + void set_unit_type(creature new_type); + Item get_item_type(); + int set_item(int c); + base* get_base(); + item* get_item(); + void delete_item(); + bool is_empty(); + void set_base(base* b); + int get_x(); + int get_y(); + + char get_land_icon(); + int get_land_number(); + biome get_land(); + bool is_base(); + int get_color(); + int get_background(); + creature get_unit_type(); + unit* get_unit(); + void delete_unit(); + +}; + +class field { +private: + int x_size; + int y_size; + field_cell** map; + int max_unit; + int count_units; + + int random_land(int y, int x); +public: + field(int x, int y); + field(); + field(int x, int y, int** lands); + + ~field(); + field_cell* get_cell(int x, int y); + field_cell* find_empty(int x, int y); + int get_item_count(); + void move_unit(int x1, int y1, int x2, int y2); + + + void set_base(int x, int y, base* b); + + int get_x_size(); + int get_y_size(); + + +}; + diff --git a/8382/Kuzina Anastasiya/Graphic.cpp b/8382/Kuzina Anastasiya/Graphic.cpp new file mode 100644 index 000000000..f240c5fa0 --- /dev/null +++ b/8382/Kuzina Anastasiya/Graphic.cpp @@ -0,0 +1,147 @@ +#include "Graphic.h" + +graphic::graphic() { + hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + +} + +void graphic::setTextColor(int text, int background) { + SetConsoleTextAttribute(hConsole, (WORD)((background << 4) | text)); + +} + +void graphic::setTitle() { + SetConsoleTitle(L"Very good game!\0"); + +} + +void graphic::info(unit* camera_men) { + camera_men->get_info(); + cout << "\t This land will hurt you: " << camera_men->beware(); + printf("\n\n"); + (static_cast(camera_men->get_base()))->get_info(); +} + + + +void graphic::camera(field& F, player* tmp, bool inf) { + unit* camera_men = tmp->get_tmp_unit(); + + + for (int m = 0; m < 1000; m++) { + cout << " "; + } + int tmp_x, tmp_y; + + tmp_x = camera_men->get_x(); + tmp_y = camera_men->get_y(); + system("cls"); + + + + setTextColor(lightRed); + cout << "Game does not support Russian and refuses to work!!! Change language to English to play\n"; + setTextColor(); + cout << "\tmove :wasd, change unit: 123, save: c, load: l, create unit: r, exit: 0\n"; + + setTextColor(yellow); + cout << "PLAYER " << tmp->get_name() << " number " << tmp->get_base()->get_number() << "\n\n"; + setTextColor(); + for (int i = tmp_y - 3; i < tmp_y + 3; i++) { + cout << "\t"; + for (int j = tmp_x - 4; j < tmp_x + 5; j++) { + + if (i < 0 || j < 0 || i >= F.get_y_size() || j >= F.get_x_size()) cout << ""; + else if (F.get_cell(j, i)->is_base()) { + setTextColor(15, F.get_cell(j, i)->get_background()); + if(F.get_cell(j, i)->get_base()->get_number() != camera_men->get_base_number()) + setTextColor(lightRed, F.get_cell(j, i)->get_background()); + + cout << F.get_cell(j, i)->get_base()->get_name(); + setTextColor(); + } + else if (F.get_cell(j, i)->get_unit_type() != None) { + setTextColor(lightRed, F.get_cell(j, i)->get_background()); + if (F.get_cell(j, i)->get_unit()->get_base_number() == camera_men->get_base_number()) + setTextColor(15, F.get_cell(j, i)->get_background()); + if(F.get_cell(j, i)->get_unit() == camera_men) + setTextColor(11, F.get_cell(j, i)->get_background()); + + cout << (static_cast(F.get_cell(j, i)->get_unit()))->get_name(); + setTextColor(); + } + else if (F.get_cell(j, i)->get_item_type() != Non) { + setTextColor(13, F.get_cell(j, i)->get_background()); + cout << F.get_cell(j, i)->get_item()->get_name(); + setTextColor(); + } + else { + setTextColor(F.get_cell(j, i)->get_color(), F.get_cell(j, i)->get_background()); + cout << F.get_cell(j, i)->get_land_icon(); + setTextColor(); + } + + } + printf("\n"); + + } + printf("\n"); + if (inf) { + info(camera_men); + } + +} + +void graphic::help() { + system("cls"); + setTextColor(yellow); + cout << "Here is everething about this game!\n"; + cout << "GAME KNOWS ONLY ENGLISH! other lenguages will be ignored ;)\n"; + setTextColor(); + cout << "This game is for 2 players\n"; + cout << "\tEvery player has own base and units\n"; + cout << "\tMaximum you can have 3 units. Every one of them has some stats: hp, armor, attack and level\n"; + cout << "\tEvery unit are some animal-type: mammal(wolf, hyena), bird(duck, crow) or amphibia(frog, lizard)\n"; + cout << "\tOn the map they look like their first letters: w, h, d, c, f, l\n"; + setTextColor(10); + cout << "Every map-cell has some landscape: sunflower field ☼, mountain ^, lake ~, lance forest ♠\n"; + setTextColor(); + cout << "\tMammals hurts in lakes and heal in lance forests\n"; + cout << "\tBirds hurts in lance forestd and heal in mountains\n"; + cout << "\tAmphibias hurts in mountains and heal in lakes\n"; + setTextColor(lightRed); + cout << "Bases looks like this ▲ when it's alive and like this X when it's hp = 0\n"; + setTextColor(); + cout << "\tYour base can create units if you press 'r' while game is on and it's alive\n"; + cout << "\tYou can change your current unit by pressing: 1, 2, 3, but only if it's alive\n"; + cout << "\tYour units and base are white and your cerrent unit are light blue\n"; + cout << "\tOther payers units and are red\n"; + setTextColor(13); + cout << "In some map-cells you can find items: heart , xp-bottle $, armor-piece * and zatochka ♦\n"; + setTextColor(); + cout << "\tAll of them colored pink and give you: +4hp, +20xp, +1armor, +1attack if you don't have maximum for your level\n"; + setTextColor(yellow); + cout << "You can move by pressing w - go up, a - go left, d - go right, s - go down\n"; + setTextColor(); + cout << "\tNeather you can attack bases and units (not yours)\n\tJust try to move in cell, where is alredy something: you will attack it\n"; + cout << "\tBut if you are weaker - you can hurt youself\n"; + cout << "\tWhen smbd attack you - your armor protect you, but it slowly break down (-1 per attack)\n"; + setTextColor(11); + cout << "You can save your game by pressing c and load it by pressing l\n"; + setTextColor(10); + cout << "There is 2 levels small-size and big-size\n"; + setTextColor(12); + cout << "And 2 gamemodes: normal and hardcore\n"; + setTextColor(); + cout << "\tIn normal each of you have 5 moves per raund and you win if other players-base is dead\n"; + cout << "\tIn hardcore you have 3 moves per raund, bases are already dead and you win if other-players units are dead\n"; + setTextColor(11); + cout << "Press any key to return to menu!\n"; + setTextColor(); + char in; + + in = _getch(); + system("cls"); + +} + diff --git a/8382/Kuzina Anastasiya/Graphic.h b/8382/Kuzina Anastasiya/Graphic.h new file mode 100644 index 000000000..e4bb8f511 --- /dev/null +++ b/8382/Kuzina Anastasiya/Graphic.h @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include +#include "iostream" +#include "Field.h" +#include "Player.h" +#include + +enum consoleColor { + black = 0, + blue = 1, + green = 2, + cyan = 3, + red = 4, + magenta = 5, + brown = 6, + lightGray = 7, + darkGray = 8, + lightBlue = 9, + lightGreen = 10, + lightCyan = 11, + lightRed = 12, + lightMagenta = 13, + yellow = 14, + white = 15 +}; + +class graphic { + VOID WINAPI Sleep(DWORD dwMilliseconds); + HANDLE hConsole; +public: + graphic(); + void help(); + void setTextColor(int text = 15, int background = 0); + void setTitle(); + void info(unit* camera_men); + void camera(field& F, player* tmp, bool inf = true); + +}; \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Item.cpp b/8382/Kuzina Anastasiya/Item.cpp new file mode 100644 index 000000000..2ee54cf19 --- /dev/null +++ b/8382/Kuzina Anastasiya/Item.cpp @@ -0,0 +1,35 @@ +#include "Item.h" +#include + +item::item() { + name = 'I'; + effect = 0; +} + +int item::get_effect() { + return effect; +} + +char item::get_name() { + return name; +} + +exp_bottle::exp_bottle() : item() { + name = '$'; + effect = 20; +} + +hp_pill::hp_pill() : item() { + name = 3; + effect = 4; +} + +zatochka::zatochka() : item() { + name = 4; + effect = 1; +} + +rpill::rpill() : item() { + name = '*'; + effect = 1; +} \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Item.h b/8382/Kuzina Anastasiya/Item.h new file mode 100644 index 000000000..6cced3479 --- /dev/null +++ b/8382/Kuzina Anastasiya/Item.h @@ -0,0 +1,39 @@ +#pragma once +enum Item {Non, Exp, Pill, Zatochka, Rand}; + +class item { +protected: + char name; + int effect; +public: + item(); + int get_effect(); + char get_name(); +}; + + +// , , , a + + +class exp_bottle :public item { +public: + exp_bottle(); + +}; + +class hp_pill : public item { +public: + hp_pill(); + +}; + +class zatochka :public item { +public: + zatochka(); +}; + +class rpill : public item { +public: + rpill(); + +}; \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Land.cpp b/8382/Kuzina Anastasiya/Land.cpp new file mode 100644 index 000000000..069689c2a --- /dev/null +++ b/8382/Kuzina Anastasiya/Land.cpp @@ -0,0 +1,77 @@ +#include "Land.h" + + +biome::biome() { + icon = 'M'; + color = 15; + background = 0; +} + +char biome::get_icon() { + return icon; +} + +int biome::get_background() { + return background; +} + +int biome::get_color() { + return color; +} + +int biome::get_effect(unit* uni) { + if (icon == 'M')return 0; + char nam = uni->get_name(); + switch (nam) { + case 'w': {} + case 'h': { + if (icon == 15) { return 0; } + if (icon == 126) { return -3; } + if (icon == 6) { return 1; } + if (icon == 94) { return 0; } + break; + } + case 'l': {} + case 'f': { + if (icon == 15) { return 0; } + if (icon == 126) { return 1; } + if (icon == 6) { return 0; } + if (icon == 94) { return -3; } + break; + } + case 'c': {} + case 'd': { + if (icon == 15) { return 0; } + if (icon == 126) { return 0; } + if (icon == 6) { return -3; } + if (icon == 94) { return 1; } + break; + } + } + return 0; +} + +sunflower::sunflower():biome(){ + icon = 15; + color = 14; + background = 2; +} + +lake::lake() : biome() { + icon = 126; + color = 9; + background = 1; +} + +forest::forest() : biome() { + icon = 6;//24 6 + color = 4;//2 + background = 6;//6 +} + +mountain::mountain() : biome() { + icon = 94; //251 30 !94! 94 + color = 7; + background = 8;//8 +} + diff --git a/8382/Kuzina Anastasiya/Land.h b/8382/Kuzina Anastasiya/Land.h new file mode 100644 index 000000000..24644e9dd --- /dev/null +++ b/8382/Kuzina Anastasiya/Land.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include "Unit.h" +enum landscape { Mountain = 1, Lake = 2, Forest = 3, Sunflower = 5}; + + +// : - , - , - , - +// : +// : +//: , , , , + +// +//+: +//0: +//-: +class biome { +protected: + char icon; + int color; + int background; + +public: + biome(); + int get_color(); + int get_background(); + char get_icon(); + int get_effect(unit* uni); +}; + +class sunflower:public biome { +public: + sunflower(); +}; + +class lake :public biome { +public: + lake(); +}; + +class forest :public biome { +public: + forest(); +}; + +class mountain :public biome { +public: + mountain(); +}; \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Logger.cpp b/8382/Kuzina Anastasiya/Logger.cpp new file mode 100644 index 000000000..42a74628c --- /dev/null +++ b/8382/Kuzina Anastasiya/Logger.cpp @@ -0,0 +1,53 @@ +#include "Logger.h" + + +string f() { + time_t rawtime; + struct tm* timeinfo; + string name; + + time(&rawtime); // , + timeinfo = localtime(&rawtime); // , + name = asctime(timeinfo); + for (int i = 0; i < name.size(); i++) { + if (name[i] == ' ') name[i] = ' '; + if (name[i] == ':') name[i] = '-'; + } + name.pop_back(); + name.push_back('.'); + name.push_back('t'); + name.push_back('x'); + name.push_back('t'); + name.push_back('\0'); + name = "Loggs\\" + name; + + return name; +} +string logger::name = f(); +int logger::ForCout = 1; + + + +void logger::set_name() { + + ofstream fout(name); + fout << name << "\n\n"; + fout.close(); + //cout << name; +} + +void logger::add(string s) { + if (ForCout < 3) file(s); + if (ForCout > 1) console(s); +} + +void logger:: file(string s) { + ofstream fout(name, ios_base::app); + fout << s << ""; + fout.close(); +} + +void logger::console(string s) { + cout << s; + +} \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Logger.h b/8382/Kuzina Anastasiya/Logger.h new file mode 100644 index 000000000..925b36c45 --- /dev/null +++ b/8382/Kuzina Anastasiya/Logger.h @@ -0,0 +1,18 @@ +#pragma once +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +using namespace std; + +class logger { +public: + static int ForCout; + static string name; + void static set_name(); + void static add(string s); + void static file(string s); + void static console(string s); +}; +string f(); \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Loggs/Mon Jun 8 11-59-20 2020.txt b/8382/Kuzina Anastasiya/Loggs/Mon Jun 8 11-59-20 2020.txt new file mode 100644 index 000000000..ac45e1a24 Binary files /dev/null and b/8382/Kuzina Anastasiya/Loggs/Mon Jun 8 11-59-20 2020.txt differ diff --git a/8382/Kuzina Anastasiya/OOP.cpp b/8382/Kuzina Anastasiya/OOP.cpp new file mode 100644 index 000000000..3a4498281 --- /dev/null +++ b/8382/Kuzina Anastasiya/OOP.cpp @@ -0,0 +1,894 @@ +#pragma once +#define _CRT_SECURE_NO_WARNINGS +#include +using namespace std; +#include +#include +#include +#include "Field.h" +#include "Logger.h" +#include "Rules.h" +#include "Graphic.h" +#include "Player.h" +#include "Exept.h" + + +template +class game { +private: + T* rul; + char in; + field* F; + player* first; + player* second; + int level; + + graphic* gui; + + int load_number(ifstream& fin) { + int num = 0; + bool negative = false; + + fin.get(in); + if (in == '-') { + negative = true; + fin.get(in); + } + while (in != ' ' && in != '\n') { + if (in == '\n') break; + if (isdigit(in)) num = num * 10 + in - '0'; + else throw except("(game::load_number) Expect to read _number_ or _number\\n, but read other symbols!\n"); + fin.get(in); + } + if (negative) num = num * -1; + + + return num; + } + player* load_player(ifstream& fin) { + string w = ""; + int num = 0; + + fin.get(in); + + if (in == 'p') { + fin.get(in); + if (in != ' ') return NULL; + + fin.get(in); + while (in != ' ') { + if (in == '\n') return NULL; + w.push_back(in); + fin.get(in); + } + + fin.get(in); + while (in != ' ' && in != '\n') { + if (in == '\n') break; + if (isdigit(in)) num = num * 10 + in - '0'; + else break; + fin.get(in); + } + } + player* ple = new player(w, num); + return ple; + } + void load_item(ifstream& fin) { + int y = 0, x = 0, type = 0; + + fin.get(in); + + if (in == 'i') { + fin.get(in); + if (in != ' ') return; + + y = load_number(fin); + x = load_number(fin); + type = load_number(fin); + + printf("i %d %d %d\n", y, x, type); + + if (x < 0 || y < 0 || x >= F->get_x_size() || y >= F->get_y_size()) return; + F->get_cell(x, y)->set_item(type); + } + else throw except("(game::load_item) Expected first-char i, was reseved " + to_string(in) + "\n"); + + + return; + } + base* load_base(ifstream& fin) { + int y = 0, x = 0, number = 0, hp = 0; + + fin.get(in); + + if (in == 'b') { + fin.get(in); + if (in != ' ') throw except("(game::load_base) expected _ after b, but get: " + to_string(in) + "\n"); + + y = load_number(fin); + x = load_number(fin); + number = load_number(fin); + hp = load_number(fin); + + printf("b %d %d %d %d\n", y, x, number, hp); + } + else throw except("(game::load_base) Expected first-char b, was reseved " + to_string(in) + "\n"); + if (x < 0 || y < 0 || x >= F->get_x_size() || y >= F->get_y_size()) throw except("(game::load_base) Wrong base-coords(" + to_string(x) + "," + to_string(y) + "), dosn't fit field sizes(" + to_string(F->get_x_size()) + "," + to_string(F->get_y_size()) + ")!\n"); + + base* b = new base(number); + b->set_coords(x, y); + b->set_hp(hp); + return b; + } + void load_unit(ifstream& fin, base* b, int i) { + char name = ' '; + int y = 0, x = 0, hp = 0, hp_max = 0, + armor = 0, attack = 0, xp = 0, xp_level = 1, level = 1; + + fin.get(in); + + if (in == 'u') { + fin.get(in); + if (in != ' ') throw except("(game::load_base) expected _ after u, but get: " + to_string(in) + "\n");; + + y = load_number(fin); + x = load_number(fin); + + fin.get(in); + name = in; + fin.get(in); + + hp = load_number(fin); + hp_max = load_number(fin); + armor = load_number(fin); + attack = load_number(fin); + xp = load_number(fin); + xp_level = load_number(fin); + level = load_number(fin); + + if (x < 0 || y < 0 || x >= F->get_x_size() || y >= F->get_y_size()) return; + printf("u %d %d %c %d %d %d %d %d %d %d\n", y, x, name, hp, hp_max, armor, attack, xp, xp_level, level); + b->set_unit(y, x, name, hp, hp_max, armor, attack, xp, xp_level, level, i); + + + } + else throw except("(game::load_unit) Expected first-char u, was reseved " + to_string(in) + "\n"); + + return; + } + bool level_load() { + bool i = false; + if (level == 1) i = level1_load(); + if (level == 2) i = level2_load(); + if (level == 3) i = load_game(); + + if (!i) return false; + rul = new T; + rul->starts(F); + return true; + } + bool level1_load() { + try { + level = 1; + first = new player("first", 1); + second = new player("second", 2); + F = new field(10, 10); + first->get_base()->set_coords(2, 2); + second->get_base()->set_coords(8, 8); + F->set_base(8, 8, second->get_base()); + F->set_base(2, 2, first->get_base()); + + F->find_empty(8, 8)->set_unit(second->get_base()->get_unit(0)); + F->find_empty(8, 8)->set_unit(second->get_base()->get_unit(1)); + F->find_empty(8, 8)->set_unit(second->get_base()->get_unit(2)); + F->find_empty(2, 2)->set_unit(first->get_base()->get_unit(0)); + F->find_empty(2, 2)->set_unit(first->get_base()->get_unit(1)); + F->find_empty(2, 2)->set_unit(first->get_base()->get_unit(2)); + logger::add("Level 1 sucessfully load\n"); + return true; + } + catch (except &ex) { + level = 1; + cout << "(game::level1_load) Something goes wrong with loading first level!\n\t" << ex.what() << "\n"; + return false; + } + return true; + } + bool level2_load() { + try { + first = new player("first", 1); + second = new player("second", 2); + F = new field(50, 30); + first->get_base()->set_coords(25, 25); + second->get_base()->set_coords(5, 5); + F->set_base(5, 5, second->get_base()); + F->set_base(25, 25, first->get_base()); + + F->find_empty(5, 5)->set_unit(second->get_base()->get_unit(0)); + F->find_empty(5, 5)->set_unit(second->get_base()->get_unit(1)); + F->find_empty(5, 5)->set_unit(second->get_base()->get_unit(2)); + F->find_empty(25, 25)->set_unit(first->get_base()->get_unit(0)); + F->find_empty(25, 25)->set_unit(first->get_base()->get_unit(1)); + F->find_empty(25, 25)->set_unit(first->get_base()->get_unit(2)); + logger::add("Level 1 sucessfully load\n"); + return true; + } + catch (except &ex) { + level = 1; + cout << "(game::level2_load) Something goes wrong with loading second level!\n\t" << ex.what() << "\n"; + return false; + } + } + void level_unload() { + + level = 1; + delete first; + delete second; + delete F; + logger::add("Level sucessfully unload\n"); + } + void save_game() { + if (level == 0) return; + //ПРОВЕРКА ЗАГРУЖЕНА ЛИ ИГРА + string name; + time_t rawtime; + struct tm* timeinfo; + string SF = "SaveFiles\\"; + + time(&rawtime); // текущая дата, выраженная в секундах + timeinfo = localtime(&rawtime); // текущая дата, представленная в нормальной форме + name = asctime(timeinfo); + for (int i = 0; i < name.size(); i++) { + if (name[i] == ' ') name[i] = '-'; + if (name[i] == ':') name[i] = '-'; + } + + name.pop_back(); + name.pop_back(); + name.pop_back(); + name.pop_back(); + name.pop_back(); + name.pop_back(); + name.push_back('.'); + name.push_back('t'); + name.push_back('x'); + name.push_back('t'); + + name = SF + name; + ofstream fout(name); + + //==================================================== + if (level == 0) return; + fout << "p " << first->get_name() << " " << first->get_base()->get_number() << "\n"; + fout << "p " << second->get_name() << " " << second->get_base()->get_number() << "\n"; + + int x = F->get_x_size(); + int y = F->get_y_size(); + fout << x << " " << y << "\n"; + for (int i = 0; i < y; i++) { + for (int j = 0; j < x; j++) { + fout << F->get_cell(j, i)->get_land_number() ; + if (j < x - 1) fout << " "; + } + fout << "\n"; + } + fout << F->get_item_count() << "\n"; + for (int i = 0; i < y; i++) { + for (int j = 0; j < x; j++) { + if (F->get_cell(j, i)->get_item_type() != Non) { + fout << "i " << i << " " << j << " " << F->get_cell(j, i)->get_item_type() << "\n"; + } + } + } + base* bb; + unit* uu; + + for (int i = 0; i < y; i++) { + for (int j = 0; j < x; j++) { + if (F->get_cell(j, i)->is_base()) { + bb = F->get_cell(j, i)->get_base(); + fout << "b " << i << " " << j << " " << bb->get_number() <<" "<< bb->get_hp() << "\n"; + fout << bb->how_many_alive() << "\n"; + for (int k = 0; k < 3; k++) { + uu = bb->get_unit(k); + if (bb->is_unit_alive(k)) { + fout << "u " << uu->get_y() << " " << uu->get_x() << " " << uu->get_name() << " " << uu->get_hp() << " " << uu->get_hp_max() << " "; + fout << uu->get_armor() << " " << uu->get_attack() << " " << uu->get_xp() << " " << uu->get_xp_for_next_level() << " " << uu->get_level() << "\n"; + } + } + + } + } + } + + + //ПРАВИЛА + fout.close(); + } + bool load_game() { + try { + string fname = ""; + cout << "Enter load-file name:\n"; + cin >> fname; + fname = "SaveFiles\\" + fname; + char buff[4]; + char in; + int num = 0; + ifstream finn(fname); + string w = ""; + if (!finn.is_open()) { + throw except("(game::load_game) File with name \"" + fname + "\" can't be found or opened\n"); + } + else { + + first = load_player(finn); + if (first == NULL) { + throw except("(game::load_game) First player can't be load: input structure corrapted\n"); + } + + second = load_player(finn); + if (second == NULL) { + throw except("(game::load_game) Second player can't be load: input structure corrapted\n"); + } + + int w = load_number(finn); + int h = load_number(finn); + printf("[%d], [%d]\n\n", w, h); + if (w <= 0 || h < 0) throw except("(game::load_game) Currupted field-size: w = " + to_string(w) + " h = " + to_string(h) + "\n"); + + int** fieldMatrix = new int* [h]; + for (int i = 0; i < h; i++) { + fieldMatrix[i] = new int[w]; + for (int j = 0; j < w; j++) { + fieldMatrix[i][j] = load_number(finn); + printf("%d", fieldMatrix[i][j]); + } + printf("\n"); + } + + F = new field(w, h, fieldMatrix); + + int itemCounter = load_number(finn); + printf("%d\n", itemCounter); + + + for (int i = 0; i < itemCounter; i++) { + load_item(finn); + } + + base* b; + + b = load_base(finn); + b->delete_unit(0); + b->delete_unit(1); + b->delete_unit(2); + int n = load_number(finn); + if (n < 0 || n > 3) throw except("(game::load_game) Incorrect units number: " + to_string(n) + "\n"); + + + if (first->get_base()->get_number() == b->get_number()) { + first->set_base(b); + F->set_base(b->get_x(), b->get_y(), first->get_base()); + for (int i = 0; i < n; i++) { + load_unit(finn, first->get_base(), i); //base_number 0 + if (first->get_base()->is_unit_alive(i)) { + F->get_cell(first->get_base()->get_unit(i)->get_x(), first->get_base()->get_unit(i)->get_y())->set_unit(first->get_base()->get_unit(i)); + } + } + first->set_tmp_unit(first->get_base()->is_any_alive()); + } + else if (second->get_base()->get_number() == b->get_number()) { + second->set_base(b); + F->set_base(b->get_x(), b->get_y(), second->get_base()); + for (int i = 0; i < n; i++) { + load_unit(finn, second->get_base(), i); //base_number 0 + if (second->get_base()->is_unit_alive(i)) { + F->get_cell(second->get_base()->get_unit(i)->get_x(), second->get_base()->get_unit(i)->get_y())->set_unit(second->get_base()->get_unit(i)); + } + } + second->set_tmp_unit(second->get_base()->is_any_alive()); + } + + else throw except("(game::load_game) Loaded base-number: "+to_string(b->get_number())+" doesn't match any players base-numbers: "+to_string(first->get_base()->get_number())+" and "+to_string(second->get_base()->get_number())+"\n"); + + b = load_base(finn); + b->delete_unit(0); + b->delete_unit(1); + b->delete_unit(2); + + n = load_number(finn); + if (n < 0 || n > 3) throw except("(game::load_game) Incorrect units number: " + to_string(n) + "\n"); + + + if (first->get_base()->get_number() == b->get_number()) { + first->set_base(b); + F->set_base(b->get_x(), b->get_y(), first->get_base()); + for (int i = 0; i < n; i++) { + load_unit(finn, first->get_base(), i); //base_number 0 + if (first->get_base()->is_unit_alive(i)) { + F->get_cell(first->get_base()->get_unit(i)->get_x(), first->get_base()->get_unit(i)->get_y())->set_unit(first->get_base()->get_unit(i)); + } + } + first->set_tmp_unit(first->get_base()->is_any_alive()); + } + else if (second->get_base()->get_number() == b->get_number()) { + second->set_base(b); + F->set_base(b->get_x(), b->get_y(), second->get_base()); + for (int i = 0; i < n; i++) { + load_unit(finn, second->get_base(), i); //base_number 0 + if (second->get_base()->is_unit_alive(i)) { + F->get_cell(second->get_base()->get_unit(i)->get_x(), second->get_base()->get_unit(i)->get_y())->set_unit(second->get_base()->get_unit(i)); + } + } + second->set_tmp_unit(second->get_base()->is_any_alive()); + + + } + else throw except("(game::load_game) Loaded base-number: " + to_string(b->get_number()) + " doesn't match any players base-numbers: " + to_string(first->get_base()->get_number()) + " and " + to_string(second->get_base()->get_number()) + "\n"); + + + + + finn.close(); + level = 3; + return true; + } + + } + catch (except &ex) { + cout << "Something goes wrong with loading game!\n " << ex.what() << "\n"; + level = 1; + return false; + } + } + +public: + game(graphic* e, int leveli) { + level = leveli; + in = 1; + gui = e; + } + void training() { + try { + F = new field(3, 3); + first = new player("YOU", 404); + first->get_base()->delete_unit(1); + first->get_base()->delete_unit(2); + first->get_tmp_unit()->set_hp(500); + first->set_tmp_unit(0); + F->find_empty(0, 0)->set_unit(first->get_base()->get_unit(0)); + gui->camera(*F, first, false); + gui->setTextColor(12); + cout << "THIS IS YOU. FRESH AND CLEAN\n Try to move with 'wasd'\n"; + int x = first->get_tmp_unit()->get_x(); + int y = first->get_tmp_unit()->get_y(); + int steps = 0; + char iter = 'e'; + + while (steps < 5) { + x = first->get_tmp_unit()->get_x(); + y = first->get_tmp_unit()->get_y(); + iter = _getch(); + if (iter == 's') { + F->move_unit(x, y, x, y + 1); + steps++; + } + else if (iter == 'w') { + F->move_unit(x, y, x, y - 1); + steps++; + } + else if (iter == 'a') { + F->move_unit(x, y, x - 1, y); + steps++; + } + else if (iter == 'd') { + F->move_unit(x, y, x + 1, y); + steps++; + } + + gui->camera(*F, first, false); + cout << "THIS IS YOU. FRESH AND CLEAN\n Try to move with 'wasd'\n"; + } + + field_cell* e = F->find_empty(0, 0); + first->get_base()->set_coords(e->get_x(), e->get_y()); + F->set_base(e->get_x(), e->get_y(), first->get_base()); + + gui->camera(*F, first, false); + cout << "Great job! Now look: it's yours base, you can create units with it! Press r to make one!\n"; + iter = 'q'; + while (iter != 'r') { + iter = _getch(); + if (iter == 'r') { + int nunit = first->get_base()->create_unit(); + if (nunit != -1) { + e = F->find_empty(first->get_base()->get_x(), first->get_base()->get_y()); + e->set_unit(first->get_base()->get_unit(nunit)); + e->get_unit()->set_hp(500); + } + } + gui->camera(*F, first, false); + cout << "Great job! Now look: it's yours base, you can create units with it! Press r to make one!\n"; + } + cout << "Cool! Look this is your bro, you can't attack him (even if you try)\n Now: try to press 1-2-3 to swech between them and walk!\n"; + steps = 0; + while (steps < 4) { + x = first->get_tmp_unit()->get_x(); + y = first->get_tmp_unit()->get_y(); + iter = _getch(); + if (iter == '1') first->set_tmp_unit(0); + else if (iter == '2') first->set_tmp_unit(1); + else if (iter == '3') first->set_tmp_unit(2); + else if (iter == 's') { + F->move_unit(x, y, x, y + 1); + steps++; + } + else if (iter == 'w') { + F->move_unit(x, y, x, y - 1); + steps++; + } + else if (iter == 'a') { + F->move_unit(x, y, x - 1, y); + steps++; + } + else if (iter == 'd') { + F->move_unit(x, y, x + 1, y); + steps++; + } + + gui->camera(*F, first, false); + cout << "Cool! Look this is your bro, you can't attack him (even if you try)\n Now: try to press 1-2-3 to swech between them and walk!\n"; + } + cout << "Brilliant! Okey, now i'll show you some more info about you and your base!\n Pay attention for landscape!(just walk arounde while i prepare next test for you)\n"; + steps = 0; + while (steps < 5) { + x = first->get_tmp_unit()->get_x(); + y = first->get_tmp_unit()->get_y(); + iter = _getch(); + if (iter == '1') first->set_tmp_unit(0); + else if (iter == '2') first->set_tmp_unit(1); + else if (iter == '3') first->set_tmp_unit(2); + else if (iter == 's') { + F->move_unit(x, y, x, y + 1); + steps++; + } + else if (iter == 'w') { + F->move_unit(x, y, x, y - 1); + steps++; + } + else if (iter == 'a') { + F->move_unit(x, y, x - 1, y); + steps++; + } + else if (iter == 'd') { + F->move_unit(x, y, x + 1, y); + steps++; + } + + gui->camera(*F, first); + cout << "Brilliant! Okey, now i'll show you some more info about you and your base!\n Pay attention for landscape!(just walk arounde while i prepare next test for you)\n"; + + } + int f = 0; + base* b = new base(-2); + b->delete_unit(1); + b->delete_unit(2); + e = F->find_empty(0, 0); + e->set_unit(b->get_unit(0)); + e->get_unit()->set_all(10, 10, 3, 3, 5, 1, 1); + + + gui->camera(*F, first); + cout << "How are you so far? Nice! Let's try something new!\n It wasn't easy, but i found immobilized enemy for you!\n Kill him now, just try to 'walk' in him\n"; + while (!e->is_empty()) { + x = first->get_tmp_unit()->get_x(); + y = first->get_tmp_unit()->get_y(); + iter = _getch(); + if (iter == '1') first->set_tmp_unit(0); + else if (iter == '2') first->set_tmp_unit(1); + else if (iter == '3') first->set_tmp_unit(2); + else if (iter == 's') { + F->move_unit(x, y, x, y + 1); + steps++; + } + else if (iter == 'w') { + F->move_unit(x, y, x, y - 1); + steps++; + } + else if (iter == 'a') { + F->move_unit(x, y, x - 1, y); + steps++; + } + else if (iter == 'd') { + F->move_unit(x, y, x + 1, y); + steps++; + } + gui->camera(*F, first); + cout << "How are you so far? Nice! Let's try something new!\n It wasn't easy, but i found immobilized enemy for you!\n Kill him now, just try to 'walk' in him\n"; + + } + cout << "Wow you are so smart! Now i hope, you can play some real games! See you soon(press any key to exit)\n"; + iter = _getch(); + + delete first; + delete F; + delete b; + system("cls"); + return; + } + catch (except & ex) { + cout << "Something while training goes wrong!\n" << ex.what() << "\n"; + return; + } + + } + void game_on() { + try { + bool allright = level_load(); + if (!allright) throw - 1; + } + catch (int) { + cout << "Can't start game after loading problems, return to menu\n" << "\n"; + return; + } + + char iter = '1'; + int y = first->get_tmp_unit()->get_y(); + int x = first->get_tmp_unit()->get_x(); + player* tmp = first; + + int steps = 0; + try { + while (iter != '0') { + // проблема - русский язык + if (rul->who_lose(F) != -1) { + system("cls"); + cout << "player " << rul->who_lose(F) << " lose :(\n\tGame over!\n"; + level_unload(); + + return; + } + + if (tmp->get_tmp_unit() == NULL) { + system("cls"); + cout << "YOU DEAD!!!\n"; + int f = tmp->is_any_alive(); + if (f == -1) { + system("cls"); + cout << "player " << rul->who_lose(F) << " lose :(\n\tGame over!\n"; + level_unload(); + return; + } + tmp->set_tmp_unit(f); + + } + x = tmp->get_tmp_unit()->get_x(); + y = tmp->get_tmp_unit()->get_y(); + gui->camera(*F, tmp); + + iter = _getch(); + if (iter == 's') { + F->move_unit(x, y, x, y + 1); + steps++; + } + else if (iter == 'w') { + F->move_unit(x, y, x, y - 1); + steps++; + } + else if (iter == 'a') { + F->move_unit(x, y, x - 1, y); + steps++; + } + else if (iter == 'd') { + F->move_unit(x, y, x + 1, y); + steps++; + } + else if (iter == 'l') { + logger::add("=>Player ask to load game\n"); + system("cls"); + cout << "Save current game before loading? Press 1 to save\n"; + iter = _getch(); + if (iter == '1') save_game(); + level_unload(); + level = 3; + try { + bool allright = level_load(); + if (!allright) throw - 1; + } + catch (int) { + cout << "Can't start game after loading problems, return to menu\n" << "\n"; + return; + } + iter = 'w'; + tmp = first; + steps = 0; + } + else if (iter == '1') tmp->set_tmp_unit(0); + else if (iter == '2') tmp->set_tmp_unit(1); + else if (iter == '3') tmp->set_tmp_unit(2); + else if (iter == 'r') { + logger::add("=>Player " + to_string(tmp->get_base()->get_number()) + " ask for mor units\n"); + int nunit = tmp->get_base()->create_unit(); + if (nunit != -1) { + logger::add("=>Player get new unit\n"); + F->find_empty(tmp->get_base()->get_x(), tmp->get_base()->get_y())->set_unit(tmp->get_base()->get_unit(nunit)); + } + steps++; + } + else if (iter == 'c') { + save_game(); + logger::add("=>Player saved game\n"); + } + else if (iter == '0') { + logger::add("=>Player exit game "); + system("cls"); + cout << "Save current game before exit? Press 1 to save\n"; + iter = _getch(); + if (iter == '1') { + save_game(); + logger::add(" and save in"); + } + logger::add("\n"); + level_unload(); + return; + } + try { + if (rul->who_lose(F) != -1) { + system("cls"); + cout << "player " << rul->who_lose(F) << " lose :(\n\tGame over!\n"; + level_unload(); + return; + } + } + catch (except & ex) { + cout << "(game::game_on) Something goes wrong with ruls:\n\t" << ex.what() << "\n"; + level_unload(); + + return; + } + if (tmp->get_tmp_unit() == NULL) { + system("cls"); + cout << "YOU DEAD!!!\n"; + int f = tmp->is_any_alive(); + if (f == -1) { + system("cls"); + cout << "player " << rul->who_lose(F) << " lose :(\n\tGame over!\n"; + level_unload(); + return; + } + tmp->set_tmp_unit(f); + + } + + if (rul->who_next(steps) == -1) { + if (tmp == first) tmp = second; + else tmp = first; + steps = 0; + } + + + } + } + catch (except ex) { + cout << "(game::game_on) Something goes wrong while playing!\n\t" << ex.what() << "\n"; + level_unload(); + return; + + } + } + +}; + +class menu { + bool hardcore; + int level; + graphic* gui; + +public: + menu() { + logger::set_name(); + hardcore = false; + gui = new graphic(); + gui->setTitle(); + level = 1; + } + void start() { + char in = 'q'; + while (in != '0') { + gui->setTextColor(yellow); + cout << "Hello, Player! How are you? Here is something you can do:\n "; + gui->setTextColor(12); + cout << "Press 1 to start game\n "; + gui->setTextColor(); + cout << "Press 2 for choosing level or loading game\n "; + cout << "Press 3 for help\n "; + cout << "Press 4 for training\n "; + cout << "Press 5 for choosing hardcore mode!\n "; + cout << "Press 0 for exit\n "; + in = _getch(); + switch (in) { + case '1': { + logger::add("=>Player starts game!\n"); + if (hardcore) { + game hard = game(gui, level); + hard.game_on(); + } + else { + game easy = game(gui, level); + easy.game_on(); + } + break; + } + case '2': { + in = 'q'; + + while (in != '1' && in != '2' && in != '3') { + system("cls"); + cout << "If you want small level press 1\nif you want big level press 2\nif you want to load game press 3\n"; + in = _getch(); + if (in == '1') level = 1; + if (in == '2') level = 2; + if (in == '3') level = 3; + } + cout << "Now in menu just choose 'start game' and it will be this level\n"; + logger::add("=>Player choose " + to_string(level) + " level\n"); + break; + } + case '3': { + logger::add("=>Player call help\n"); + gui->help(); + break; + } + case '4': { + logger::add("=>Player start training\n"); + if (hardcore) { + game hard = game(gui, level); + hard.training(); + + return; + } + else { + game easy = game(gui, level); + easy.training(); + return; + } + + break; + } + case '5': { + hardcore = true; + cout << "HARDCOREEE\n"; + logger::add("=>Player choose hardcore mode\n"); + break; + } + case '0': { + logger::add("=>Player exit game\n"); + system("cls"); + cout << "BYE :*\n"; + return; + } + default: { + logger::add("=>Player press " + to_string(in) + " in menu\n"); + system("cls"); + cout << "OOOPS, you press wrong button, try again!\n\n"; + } + + } + + + } + + + } + + + +}; + + +int main(){ + menu* everething = new menu(); + everething->start(); + return 0; +} + diff --git a/8382/Kuzina Anastasiya/OOP.exe b/8382/Kuzina Anastasiya/OOP.exe new file mode 100644 index 000000000..6df09b3ee Binary files /dev/null and b/8382/Kuzina Anastasiya/OOP.exe differ diff --git a/8382/Kuzina Anastasiya/Player.cpp b/8382/Kuzina Anastasiya/Player.cpp new file mode 100644 index 000000000..df75e46c8 --- /dev/null +++ b/8382/Kuzina Anastasiya/Player.cpp @@ -0,0 +1,31 @@ +#include "Player.h" + +player::player(string new_name, int bn) { + name = new_name; + my = new base(bn); + my->set_owner(this); +} + +player::~player() { + delete my; +} + +base* player::get_base() { return my; } + +unit* player::get_tmp_unit() { return my->get_tmp_unit(); } + +void player::set_tmp_unit(int i) { + my->set_tmp_unit(i); +} + +string player::get_name() { return name; } + +int player::is_any_alive() { + return my->is_any_alive(); +} + +void player::set_base(base* b) { + delete (my); + my = b; + my->set_owner(this); +} \ No newline at end of file diff --git a/8382/Kuzina Anastasiya/Player.h b/8382/Kuzina Anastasiya/Player.h new file mode 100644 index 000000000..f1fd3d710 --- /dev/null +++ b/8382/Kuzina Anastasiya/Player.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include "Base.h" + + +class player { + base* my; + string name; +public: + player(string new_name, int bn); + ~player(); + base* get_base(); + unit* get_tmp_unit(); + void set_tmp_unit(int i); + string get_name(); + int is_any_alive(); + void set_base(base* b); +}; diff --git a/8382/Kuzina Anastasiya/Rules.cpp b/8382/Kuzina Anastasiya/Rules.cpp new file mode 100644 index 000000000..0f31c3f33 --- /dev/null +++ b/8382/Kuzina Anastasiya/Rules.cpp @@ -0,0 +1,74 @@ +#include "Rules.h" + + +//basic one +int ruls1::who_lose(field* F) { + bool is_no_base = true; + for (int i = 0; i < F->get_y_size(); i++) { + for (int j = 0; j < F->get_x_size(); j++) { + if (F->get_cell(j, i)->is_base()) { + is_no_base = false; + if (F->get_cell(j, i)->get_base()->get_hp() <= 0) return F->get_cell(j, i)->get_base()->get_number(); + + } + + } + + } + if (is_no_base) throw exception("(ruls1::who_lose) There is no bases on the field!\n"); + return -1; + +} +int ruls1::who_next(int i) { + if (i > 4) return -1; + return 1; + +} +int ruls1::starts(field* F) { + return 1; + +} + + + +int ruls2::who_lose(field* F) { + bool is_no_base = true; + for (int i = 0; i < F->get_y_size(); i++) { + for (int j = 0; j < F->get_x_size(); j++) { + if (F->get_cell(j, i)->is_base()) { + is_no_base = false; + if (F->get_cell(j, i)->get_base()->is_any_alive() == -1) return F->get_cell(j, i)->get_base()->get_number(); + + } + + } + + } + if (is_no_base) throw exception("(ruls2::who_lose) There is no bases on the field!\n"); + return -1; + +} +int ruls2::who_next(int i) { + if (i > 2) return -1; + return 1; + +} +int ruls2::starts(field* F) { + bool is_no_base = true; + for (int i = 0; i < F->get_y_size(); i++) { + for (int j = 0; j < F->get_x_size(); j++) { + if (F->get_cell(j, i)->is_base()) { + is_no_base = false; + F->get_cell(j, i)->get_base()->set_hp(0); + + } + + } + + } + if (is_no_base) throw except("(ruls2::starts)There is no bases on the field!\n"); + return -1; + + +} + diff --git a/8382/Kuzina Anastasiya/Rules.h b/8382/Kuzina Anastasiya/Rules.h new file mode 100644 index 000000000..4611fce74 --- /dev/null +++ b/8382/Kuzina Anastasiya/Rules.h @@ -0,0 +1,28 @@ +#pragma once +#include "Field.h" + +class Iwinrul { +public: + virtual int who_lose(field* F) =0;//return num of winers-base if win or -1 + virtual int who_next(int i)=0; //return 1 if same and -1 if other one + virtual int starts(field* F)=0; +}; + + +class ruls1: public Iwinrul { +public: + + virtual int who_lose(field* F); + virtual int who_next(int i); + virtual int starts(field* F); + +}; + +class ruls2 : public Iwinrul { + // -, +public: + virtual int who_lose(field* F); + virtual int who_next(int i); + virtual int starts(field* F); +}; + diff --git a/8382/Kuzina Anastasiya/SaveFiles/Mon.txt b/8382/Kuzina Anastasiya/SaveFiles/Mon.txt new file mode 100644 index 000000000..70da18bb2 --- /dev/null +++ b/8382/Kuzina Anastasiya/SaveFiles/Mon.txt @@ -0,0 +1,27 @@ +p first 1 +p second 2 +10 10 +2 2 1 1 2 2 2 0 0 3 +2 2 3 3 1 3 3 3 1 1 +0 0 0 0 0 0 0 0 0 0 +0 0 0 0 2 2 2 1 2 2 +1 1 1 1 0 0 0 3 0 0 +2 3 1 3 1 0 0 0 0 2 +3 0 1 1 1 1 0 0 2 2 +3 3 0 1 1 0 0 0 0 0 +2 1 3 0 0 0 0 2 0 0 +3 2 0 2 2 2 2 1 1 1 +5 +i 0 0 2 +i 1 7 4 +i 1 8 2 +i 2 2 1 +i 4 5 3 +b 2 2 1 200 +2 +u 1 2 w 10 10 5 10 0 20 1 +u 2 1 l 10 10 5 10 0 20 1 +b 8 8 2 200 +1 +u 8 9 c 5 5 10 5 0 20 1 + diff --git a/8382/Kuzina Anastasiya/Unit.cpp b/8382/Kuzina Anastasiya/Unit.cpp new file mode 100644 index 000000000..413b40265 --- /dev/null +++ b/8382/Kuzina Anastasiya/Unit.cpp @@ -0,0 +1,205 @@ +#pragma once +#include +#include +#include "Unit.h" +#include "Feature.h" +#include "Logger.h" +#include "Exept.h" +#define RESET "\033[0m" +#define RED "\033[1;31m" +using namespace std; + + unit::unit() { + base_number = -1; + ubase = NULL; + hp = feature(10); + armor = feature(10); + attack = feature(10); + level = ulevel(); + name = 'u'; + x = -1; + y = -1; + return; + } + void unit::set_info(int new_hp, int new_attack, int new_armor) { + hp = feature(new_hp); + attack = feature(new_attack); + armor = feature(new_armor); + } + void unit::get_info() { + cout << "\tName: " << name << "(" << x << ", " << y << ")\n"; + cout << "\tHP: " << hp.get_tmp() << " Attack : " << attack.get_tmp() << " Armor : " << armor.get_tmp() << endl; + cout << "\tLevel : " << level.get_tmp_level() <<" Xp: "<< level.get_tmp_xp() << " Xp for level up: " << level.get_xp_for_next_level() << "\n"; + } + void unit::set_hp(int nhp) { + hp.set_tmp(nhp); + } + bool unit::add_hp(int num) { + if (hp.add(num)) { + return true; + } + return false; + } + void unit::add_armor(int num) { + armor.add(num); + } + void unit::add_attack(int num) { + attack.add(num); + } + void unit::add_xp(int num) { + if (level.plus_xp(num)) { + hp.set_max(hp.get_max() + 10); + hp.set_tmp(hp.get_tmp() + 5); + armor.set_max(armor.get_max() + 2); + attack.set_max(attack.get_max() + 2); + } + } + void unit::set_type(creature t) { + if (t == None) throw except("(unit::set_type) You can't set unit None-type\n"); + type = t; + } + void unit::set_base_nubmer(int t) { base_number = t; } + int unit::get_base_number() { return base_number; } + void unit::set_base(void* bs) { ubase = bs; } + void* unit::get_base() { return ubase; } + + void unit::set_name(char new_name) { + name = new_name; + } + void unit::set_coords(int new_x, int new_y) { + if (new_x < 0 || new_y < 0) throw except("Trying to set unit negativ coords: ("+to_string(x)+","+to_string(y)+")\n"); + x = new_x; + y = new_y; + } + int unit::get_hp() { return hp.get_tmp(); } + int unit::get_armor() { return armor.get_tmp(); } + int unit::get_attack() { return attack.get_tmp(); } + char unit::get_name() { return name; } + int unit::get_x() { return x; } + creature unit::get_type() { return type; } + int unit::get_y() { return y; } + + char unit::beware() { + switch (name){ + case 'w': + case 'h': return 126; + case 'f': + case 'l': return 94; + case 'd': + case 'c': return 6; + } + throw except("(unit::beware) Incorrect unit name -"+ to_string(name) +"- doesn't match any creature type"); + } + + unit operator+(unit& un, item* it) { + + char c = it->get_name(); + if (c == '$') + un.add_xp(it->get_effect()); + else if (c == 3) + un.add_hp(it->get_effect()); + else if (c == '*') + un.add_armor(it->get_effect()); + else if (c == 4) + un.add_attack(it->get_effect()); + else throw except("(unit::operatior+) Unexpected item name -"+ to_string(c)+", doesn't match any item types\n"); + logger::add("\t"+un.get_u_info()+"find some item\n"); + return un; + } + + unit operator-(unit& un, unit* it) { + logger::add("\tfight between "+ un.get_u_info() + " and " + it->get_u_info() + "\n"); + int att = it->get_attack(); + int arr = un.get_armor(); + it->add_xp(10); + un.add_armor(-1); + if (arr > att) { + it->add_hp(-1); + logger::add("\t\t" + it->get_u_info() + " is weaker and get -1 hp\n"); + return un; + } + logger::add("\t\t" + un.get_u_info() + " is weaker and get " + to_string(arr - att)+" hp\n"); + un.add_hp(arr - att); + return un; + } + + int unit::set_all(int hdp, int hp_max, int aror, int atack, int x, int xp_level, int lvel) { + + hp.set_tmp(hdp); + hp.set_tmp(hp_max); + armor.set_tmp(aror); + attack.set_tmp(atack); + + while (level.get_tmp_xp() != x || level.get_tmp_level() != lvel) { + level.plus_xp(1); + if (level.get_tmp_level() > lvel) return 1; + } + + + return 1; + } + + int unit::get_hp_max() { return hp.get_max(); } + int unit::get_armor_max() { return armor.get_max(); } + int unit::get_attack_max() { return attack.get_max(); } + int unit::get_level() { return level.get_tmp_level(); } + int unit::get_xp() { return level.get_tmp_xp(); } + int unit::get_xp_for_next_level() { return level.get_xp_for_next_level(); } + string unit::get_u_info() { + string inf = " "; + inf.push_back(name); + inf = inf + " base-" + to_string(base_number) + " "; + inf = inf + "(" + to_string(x) + "," + to_string(y) + ")"; + return inf; + + } + + + mammal::mammal():unit(){ + set_name('m'); + } + + wolf::wolf() :mammal() { + set_name('w'); + set_info(15, 5, 5); + set_type(Wolf); + } + + hyena::hyena() :mammal() { + set_name('h'); + set_info(20, 2, 20); + set_type(Hyena); + } + + bird::bird() :unit() { + set_name('b'); + } + + crow::crow() :bird() { + set_name('c'); + set_info(15, 5, 15); + set_type(Crow); + } + + duck::duck() : bird() { + set_name('d'); + set_info(20, 10, 5); + set_type(Duck); + } + + amphibia::amphibia() :unit() { + set_name('a'); + } + + frog::frog() :amphibia() { + set_name('f'); + set_info(5, 5, 10); + set_type(Frog); + } + + lizard::lizard() :amphibia() { + set_name('l'); + set_info(10, 10, 5); + set_type(Lizard); + } + diff --git a/8382/Kuzina Anastasiya/Unit.h b/8382/Kuzina Anastasiya/Unit.h new file mode 100644 index 000000000..8dc60f918 --- /dev/null +++ b/8382/Kuzina Anastasiya/Unit.h @@ -0,0 +1,94 @@ +#pragma once +#include +#include "Feature.h" +#include "Item.h" +enum creature {Hyena, Wolf, Crow, Duck, Frog, Lizard, None }; + +class unit { +private: + feature hp; + feature armor; + feature attack; + ulevel level; + creature type; + int base_number; + void* ubase; + + char name; + + int x; + int y; + +public: + unit(); + void set_info(int new_hp, int new_attack, int new_armor); + char beware(); + std::string get_u_info(); + int set_all(int hdp, int hp_max, int armor, int attack, int xp, int xp_level, int level); + void get_info(); + + int get_base_number(); + void set_base_nubmer(int n); + void set_base(void* b); + void* get_base(); + creature get_type(); + void set_type(creature t); + void set_name(char new_name); + void set_coords(int new_x, int new_y); + void set_hp(int nhp); + bool add_hp(int num); + void add_attack(int num); + void add_armor(int num); + void add_xp(int num); + friend unit operator+(unit& un, item* it); + friend unit operator-(unit& un, unit* it); + int get_hp(); + int get_armor(); + int get_attack(); + int get_hp_max(); + int get_armor_max(); + int get_attack_max(); + int get_level(); + int get_xp(); + int get_xp_for_next_level(); + + char get_name(); + int get_x(); + int get_y(); + +}; + + + +class mammal : public unit { +public: mammal(); +}; +class wolf : public mammal { +public: wolf(); +}; +class hyena : public mammal { +public: hyena(); +}; + +class bird : public unit { +public: + bird(); +}; +class crow : public bird { +public: + crow(); +}; +class duck : public bird { +public: + duck(); +}; + +class amphibia : public unit { +public: amphibia(); +}; +class frog : public amphibia { +public: frog(); +}; +class lizard : public amphibia { +public: lizard(); +};