-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.h
More file actions
89 lines (74 loc) · 2.61 KB
/
move.h
File metadata and controls
89 lines (74 loc) · 2.61 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
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include "types.h"
#include "bitboard.h"
class Move {
public:
PieceType movingPiece;
u8 fromCell;
u8 toCell;
Move inverse() const {
return Move{movingPiece, inverseShift(fromCell), inverseShift(toCell)};
}
PieceRange moveRange() const {
switch (movingPiece) {
case PieceType::WhitePawn:
case PieceType::WhiteKnight:
case PieceType::WhiteRook:
case PieceType::WhiteBishop:
case PieceType::WhiteCar:
return PieceRange::White;
case PieceType::BlackPawn:
case PieceType::BlackKnight:
case PieceType::BlackRook:
case PieceType::BlackBishop:
case PieceType::BlackCar:
return PieceRange::Black;
default:
return PieceRange::NoRange;
}
}
bool operator<(const Move &other) const {
// Required to use Move as a map key
unsigned int left = fromCell + (toCell << 7u);
unsigned int right = other.fromCell + (other.toCell << 7u);
return left < right;
}
private:
u8 inverseShift(u8 cell) const {
switch (cell / 8) {
case 0: return static_cast<u8>(cell + 56);
case 1: return static_cast<u8>(cell + 40);
case 2: return static_cast<u8>(cell + 24);
case 3: return static_cast<u8>(cell + 8);
case 4: return static_cast<u8>(cell - 8);
case 5: return static_cast<u8>(cell - 24);
case 6: return static_cast<u8>(cell - 40);
case 7: return static_cast<u8>(cell - 56);
default: return 0xFF;
}
}
};
class MoveList {
public:
std::vector<Move> moves;
u64 carIdx = 0;
MoveList(std::vector<Move> moveList) : moves(moveList) {}
};
std::ostream& operator<<(std::ostream &stream, const Move &move) {
return stream
<< static_cast<char>('A' + move.fromCell % 8)
<< static_cast<char>('1' + move.fromCell / 8)
<< static_cast<char>('A' + move.toCell % 8)
<< static_cast<char>('1' + move.toCell / 8);
}
void operator>>(char* input, Move &move) {
// Read move in and make it upper-case
std::string str(input);
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
// Set move relative to ASCII codes used
move.fromCell = static_cast<u8>((str[1] - '1') * 8 + (str[0] - 'A'));
move.toCell = static_cast<u8>((str[3] - '1') * 8 + (str[2] - 'A'));
}
inline bool operator==(const volatile Move &lhs, const Move &rhs) {
return lhs.fromCell == rhs.fromCell && lhs.toCell == rhs.toCell;
}