-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathColor.h
More file actions
76 lines (60 loc) · 1.63 KB
/
Color.h
File metadata and controls
76 lines (60 loc) · 1.63 KB
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
#ifndef LIBCHESS_COLOR_H
#define LIBCHESS_COLOR_H
#include <iostream>
#include <optional>
#include "internal/MetaValueType.h"
namespace libchess {
class Color : public MetaValueType<int> {
public:
class Value {
public:
enum ColorValue : value_type
{
WHITE = 0,
BLACK = 1
};
};
constexpr explicit Color(value_type value) : MetaValueType<value_type>(value) {
}
constexpr Color operator!() const {
return Color{!value()};
}
constexpr char to_char() const {
switch (value()) {
case Value::WHITE:
return 'w';
case Value::BLACK:
return 'b';
default:
return '-';
}
}
constexpr static std::optional<Color> from(char c) {
switch (c) {
case 'w':
return Color{Value::WHITE};
case 'b':
return Color{Value::BLACK};
case 'W':
return Color{Value::WHITE};
case 'B':
return Color{Value::BLACK};
default:
return {};
}
}
};
inline std::ostream& operator<<(std::ostream& ostream, Color color) {
return ostream << color.to_char();
}
namespace constants {
constexpr static Color WHITE{Color::Value::WHITE};
constexpr static Color BLACK{Color::Value::BLACK};
constexpr static Color COLORS[]{WHITE, BLACK};
} // namespace constants
} // namespace libchess
namespace std {
template <>
struct hash<libchess::Color> : public hash<libchess::Color::value_type> {};
} // namespace std
#endif // LIBCHESS_COLOR_H