-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.h
44 lines (40 loc) · 1.09 KB
/
position.h
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
//
// Created by xinya on 2024/3/8.
//
#ifndef HARBOR_MANAGER__POSITION_H_
#define HARBOR_MANAGER__POSITION_H_
#include "vector.h"
#include "constant.h"
//bool IsInMap(int x, int y) {
// return x >= 0 && x < kN && y >= 0 && y < kN;
//}
struct Position : Vector {
Position() = default;
Position(int x, int y) : Vector(x, y) {};
Position(Vector v) : Vector(v) {};
[[nodiscard]] bool IsInMap() const {
return x >= 0 && x < kN && y >= 0 && y < kN;
};
bool operator==(const Position &pos) const {
return x == pos.x && y == pos.y;
};
bool operator!=(const Position &pos) const {
return !(*this == pos);
};
bool operator<(const Position &pos) const {
return x == pos.x ? y < pos.y : x < pos.x;
};
[[nodiscard]] Position Move(int dir) const {
if (dir == kStay) return *this;
return *this + kDirVec[dir];
};
[[nodiscard]] int Distance(const Position &pos) const {
return std::abs(x - pos.x) + std::abs(y - pos.y);
};
};
struct HashPosition {
size_t operator()(const Position &pos) const {
return pos.x * kN + pos.y;
}
};
#endif //HARBOR_MANAGER__POSITION_H_