Skip to content

Ivchenko Anton lr 2&3 #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Armor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

class Armor {
private:
int armor;
public:
Armor(int a) {
armor = a;
};
int getArmorValue() {
return armor;
}
void setArmor(int a) {
armor = a;
}
int reduceHealth(int d) {
armor = armor - d;
return armor;

}
~Armor() {};

};
21 changes: 21 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Damage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

class Damage {

private:
int damage;
public:
Damage(int a) {
damage = a;
};
int getDamageValue() {
return damage;
}
void setDamage(int a) {
damage = a;
}
void raiseDamage(int value) {
damage = damage + value;
}
~Damage() {};
};
190 changes: 190 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Field.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#include <iostream>
#include "Field.h"

Field::Field(int max, int _h, int _w)
{

this->h = _h;
this->w = _w;
this->qmax = max;
cur = 0;

field = new Object * *[h];

for (int i = 0; i < h; i++) {

field[i] = new Object * [w];

for (int j = 0; j < w; j++)

field[i][j] = nullptr;

}std::cout << "Field created\n";

};
void Field::formLandscape() {

for (int i = 0; i < h; i++) {

for (int j = 0; j < w; j++) {

int a = rand() % 20;
int b = rand() % 1000;
if (a < 10)
field[i][j] = new Plain;
else if (a < 14)
field[i][j] = new Forest;
else if (a < 17)
field[i][j] = new Lake;
else if (a < 20)
field[i][j] = new Mountain;

if (b < 5) {
Neutral* ntr = new LegendaryItem;
field[i][j]->setNeutral(ntr);

}
else if (b < 30) {
Neutral* ntr = new ArmorKit;
field[i][j]->setNeutral(ntr);
}
else if (b < 40) {
Neutral* ntr = new WeaponKit;
field[i][j]->setNeutral(ntr);
}
else if (b < 50) {
Neutral* ntr = new SpeedBoost;
field[i][j]->setNeutral(ntr);
}
}

}


};

Object* Field::getCoords(int x, int y) {

return field[x][y];

}
Base* Field::SetBase( int x, int y) {

Base* base = new Base(x,y);
field[x][y]->~Object();
field[x][y] = base;

return base;

};
void Field::AddObj(Unit* a, int x, int y) {


if ((cur < qmax) && (0 < x < h) && (0 < y < h) && field[x][y]->getUnit() == nullptr && field[x][y]->IsAvailable()) {

field[x][y]->SetUnit(a);
a->setCoordinats(x, y);
cur++;
std::cout << "\nUnit added\n";
}
else
std::cout << "Incorrect arguments";
}

void Field::RemObj(Unit* a, int x, int y) {

if ((0 < x < h) && (0 < y < h) && (field[x][y]->getUnit() == a)) {

field[x][y]->RemoveUnit();
cur--;
std::cout << "\nUnit removed\n";
}
else
std::cout << "Incorrect arguments";

};

void Field::Moving(Unit* a, int posx, int posy, int dx, int dy) {

if ((posx + dx > h || posx + dx < 0) && (posy + dy > w || posy + dy < 0)) {


std::cout << "Incorrect arguments"; return;

}
/*if ((abs(dx) > a->getCharactresistics()->getSpeed())) {

field[posx][posy]->RemoveUnit();
int newx = posx + (a->getCharactresistics()->getSpeed() * dx / abs(dx));
int newy = posx + (a->getCharactresistics()->getSpeed() * dy / abs(dy));

field[posx + newx][posy + newy]->SetUnit(a);

a->setCoordinats(posx + newx, posy + newy);

std::cout << "\nUnit moved\n";
}
*/
else {

RemObj(a, posx, posy);

AddObj(a, posx + dx, posy + dy);

std::cout << "\nUnit moved\n";

}



};

Field* Field::CopyField(Field* a) {

for (int i = 0; i < h; i++) {

for (int k = 0; k < w; k++) {

a->SetUnit(field[i][k], i, k);

}
}return a;

};


void Field::SetUnit(Object* a, int b, int c) {

field[b][c] = a;

};


void Field::PrintField() {

for (int i = 0; i < h; i++) {

std::cout << '\n';

for (int k = 0; k < w; k++) {

if (field[i][k]->getUnit() != nullptr)

std::cout << field[i][k]->getUnit()->getid() << ' ';

else if (field[i][k]->getNeutral() != nullptr)
std::cout << field[i][k]->getNeutral()->getType() << ' ';

else
std::cout << field[i][k]->getObj() << ' ';
}
};

};

Field::~Field() {

delete[] field;
std::cout << "\nField deleted\n";

};
31 changes: 31 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Field.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include "Unit.h"
#include "Object.h"

class Field {

private:

int h;
int w;
int cur;
int qmax;
Object*** field;

public:

Field();
Field(int max, int _h, int _w);
Base* SetBase(int x, int y);
void AddObj(Unit* a, int x, int y);
void RemObj(Unit* a, int x, int y);
void Moving(Unit* a, int posx, int posy, int dx, int dy);
Field* CopyField(Field* a);
void SetUnit(Object* a, int b, int c);
void PrintField();
Object* getCoords(int, int);
void formLandscape();
void SetNeutralObjs();
~Field();

};
16 changes: 16 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Flyweight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "Flyweight.h"

TypeOfUnit::TypeOfUnit() {

Atributes* infantry = new Atributes(10, 10, 3);
Atributes* archers = new Atributes(15, 5, 2);
Atributes* cavalry = new Atributes(10, 8, 5);
my_map['i'] = infantry;
my_map['a'] = archers;
my_map['c'] = cavalry;

}
Atributes* TypeOfUnit::getType(char a) {
return my_map[a];
}
42 changes: 42 additions & 0 deletions 8304/Ivchenko_Anton/lab2/Flyweight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "Damage.h"
#include "Armor.h"
#include <map>


class Atributes {
private:
Damage* damage;
Armor* armor;
int speed;
public:
Atributes() {};
Atributes(int a, int b, int c) {

damage = new Damage(a);
armor = new Armor(b);
speed = c;

}
Damage* getDamage() {
return damage;
}
Armor* getArmor() {
return armor;
}
int getSpeed() {
return speed;
}
~Atributes() {};

};
class TypeOfUnit {

private:
std::map <char, Atributes*> my_map;
public:
TypeOfUnit();
Atributes* getType(char a);
~TypeOfUnit() {};
};
Loading