-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimal.cpp
146 lines (129 loc) · 2.35 KB
/
Animal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "Animal.h"
#include <vector>
Animal::Animal()
{
this->x = 1;
this->y = 1;
this->id = '/';
this->move_value = 0;
}
Animal::Animal(int x, int y)
{
this->x = x;
this->y = y;
}
Animal::Animal(int x, int y, char id, int move_value)
{
this->x = x;
this->y = y;
this->id = id;
this->move_value = move_value;
}
Animal::~Animal()
{
;
}
int Animal::GetX()
{
return this->x;
}
int Animal::GetY()
{
return this->y;
}
char Animal::GetId()
{
return this->id;
}
int Animal::GetMoveValue()
{
return this->move_value;
}
void Animal::SaveInBoard(char** myBoard)
{
*(*(myBoard + this->y) + this->x) = id;
}
void Animal::RemoveFromBoard(char** myBoard)
{
*(*(myBoard + this->y) + this->x) = ' ';
}
bool Animal::GoUp(int height, char** myBoard, const int move_value)
{
int check = this->y - move_value;
if (check >= 0)
{
RemoveFromBoard(myBoard);
this->y-=move_value;
return true;
}
else return false;
}
bool Animal::GoDown(int height, char** myBoard, const int move_value)
{
int check = this->y + move_value;
if (check < height)
{
RemoveFromBoard(myBoard);
this->y+=move_value;
return true;
}
else return false;
}
bool Animal::GoLeft(int width, char **myBoard, const int move_value)
{
int check = this->x - move_value;
if (check >= 0)
{
RemoveFromBoard(myBoard);
this->x-=move_value;
return true;
}
return false;
}
bool Animal::GoRight(int width, char** myBoard, const int move_value)
{
int check = this->x + move_value;
if (check < width)
{
RemoveFromBoard(myBoard);
this->x+=move_value;
return true;
}
return false;
}
int Animal::Collision(std::vector<Animal> &table, int size, int j)
{
for (int i = 0; i < size; i++)
{
if (i == j) ;
else if (table[i].x == this->x && table[i].y == this->y)
{
//wypisz zabite zwierze
//table.erase(table.begin() + i, table.begin() + i + 1);
return i;
}
}
return -1;
}
int Animal::EatGrass(std::vector<Grass> &table, int grass_amount )
{
for (int i = 0; i < grass_amount; i++)
{
if (this->x == table[i].GetX() && this->y == table[i].GetY())
{
table.erase(table.begin() + i, table.begin() + i + 1);
return i;
}
}
return -1;
}
ostream& operator<<(ostream& os, Animal& anim)
{
os << "x: " << anim.x << " " << "y: " << anim.y << endl;
return os;
}
Animal Animal::operator++(int) //funkcja do postinkrementacji
{
Animal newA((this->x)++, (this->y)++);
return newA;
}