-
Notifications
You must be signed in to change notification settings - Fork 2
/
move.h
89 lines (72 loc) · 2.79 KB
/
move.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
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 <stdint.h>
#include <cstdlib>
#include "string.h"
enum MoveSpecial {
M_SWAP = -1, //-1 so that adding 1 makes it into a valid move
M_RESIGN = -2,
M_NONE = -3,
M_UNKNOWN = -4,
};
struct Move {
int8_t y, x;
Move(MoveSpecial a = M_UNKNOWN) : y(a), x(120) { } //big x so it will always wrap to y=0 with swap
Move(int X, int Y) : y(Y), x(X) { }
Move(const std::string & str, int size = 0){
if( str == "swap" ){ y = M_SWAP; x = 120; }
else if(str == "resign" ){ y = M_RESIGN; x = 120; }
else if(str == "none" ){ y = M_NONE; x = 120; }
else if(str == "unknown"){ y = M_UNKNOWN; x = 120; }
else{
y = tolower(str[0]) - 'a';
x = atoi(str.c_str() + 1) - 1;
if(size && y >= size)
x += y + 1 - size;
}
}
std::string to_s(int size = 0) const {
if(y == M_UNKNOWN) return "unknown";
if(y == M_NONE) return "none";
if(y == M_SWAP) return "swap";
if(y == M_RESIGN) return "resign";
if(size && y >= size)
return std::string() + char(y + 'a') + to_str(x - y + size);
else
return std::string() + char(y + 'a') + to_str(x + 1);
}
bool operator< (const Move & b) const { return (y == b.y ? x < b.x : y < b.y); }
bool operator<=(const Move & b) const { return (y == b.y ? x <= b.x : y <= b.y); }
bool operator> (const Move & b) const { return (y == b.y ? x > b.x : y > b.y); }
bool operator>=(const Move & b) const { return (y == b.y ? x >= b.x : y >= b.y); }
bool operator==(const MoveSpecial & b) const { return (y == b); }
bool operator==(const Move & b) const { return (y == b.y && x == b.x); }
bool operator!=(const Move & b) const { return (y != b.y || x != b.x); }
bool operator!=(const MoveSpecial & b) const { return (y != b); }
Move operator+ (const Move & b) const { return Move(x + b.x, y + b.y); }
Move & operator+=(const Move & b) { y += b.y; x += b.x; return *this; }
Move operator- (const Move & b) const { return Move(x - b.x, y - b.y); }
Move & operator-=(const Move & b) { y -= b.y; x -= b.x; return *this; }
int z() const { return (x - y); }
int dist(const Move & b) const {
return (abs(x - b.x) + abs(y - b.y) + abs(z() - b.z()))/2;
}
};
struct MoveScore : public Move {
int16_t score;
MoveScore() : score(0) { }
MoveScore(MoveSpecial a) : Move(a), score(0) { }
MoveScore(int X, int Y, int s) : Move(X, Y), score(s) { }
MoveScore operator+ (const Move & b) const { return MoveScore(x + b.x, y + b.y, score); }
};
struct MoveValid : public Move {
int16_t xy;
MoveValid() : Move(), xy(-1) { }
MoveValid(int x, int y, int XY) : Move(x,y), xy(XY) { }
MoveValid(const Move & m, int XY) : Move(m), xy(XY) { }
bool onboard() const { return xy != -1; }
};
struct PairMove {
Move a, b;
PairMove(Move A = M_UNKNOWN, Move B = M_UNKNOWN) : a(A), b(B) { }
PairMove(MoveSpecial A) : a(Move(A)), b(M_UNKNOWN) { }
};