-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementPatterns.cpp
More file actions
70 lines (55 loc) · 1.68 KB
/
Copy pathMovementPatterns.cpp
File metadata and controls
70 lines (55 loc) · 1.68 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
//
// Created by sahoo on 25-06-2026.
//
#include "MovementPatterns.h"
// returns true if a move is horizontal
bool MovementPatterns::isHorizontalMove(int startPos, int endPos) {
return (startPos >> 3) == (endPos >> 3);
}
// returns true if a move is vertical
bool MovementPatterns::isVerticalMove(int startPos, int endPos) {
return (startPos & 7) == (endPos & 7);
}
// returns true if a move is diagonal
bool MovementPatterns::isDiagonalMove(int startPos, int endPos) {
int rankDiff = std::abs((startPos >> 3) - (endPos >> 3));
int fileDiff = std::abs((startPos & 7) - (endPos & 7));
return rankDiff == fileDiff;
}
// returns the direction a piece is trying to move
Direction MovementPatterns::getMoveDirection(int startPos, int endPos) {
int startRank = startPos >> 3;
int startFile = startPos & 7;
int endRank = endPos >> 3;
int endFile = endPos & 7;
int rankDiff = endRank - startRank;
int fileDiff = endFile - startFile;
// vertical movement
if (isVerticalMove(startPos, endPos)) {
if (rankDiff > 0) {
return up;
}
return down;
}
// horizontal movement
if (isHorizontalMove(startPos, endPos)) {
if (fileDiff > 0) {
return right;
}
return left;
}
// diagonal movement
if (isDiagonalMove(startPos, endPos)) {
if (rankDiff > 0 && fileDiff > 0) {
return upRight;
}
if (rankDiff > 0 && fileDiff < 0) {
return upLeft;
}
if (rankDiff < 0 && fileDiff > 0) {
return downRight;
}
return downLeft;
}
throw std::invalid_argument("Move has no sliding direction.");
}