-
Notifications
You must be signed in to change notification settings - Fork 17
/
CMove.cc
94 lines (77 loc) · 1.95 KB
/
CMove.cc
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
89
90
91
92
93
#include <sstream>
#include <assert.h>
#include "CMove.h"
std::ostream& operator <<(std::ostream &os, const CMove &rhs)
{
return os << rhs.ToShortString();
} // end of std::ostream& operator <<(std::ostream &os, const CMove &rhs)
static char pieces[] = "kqrbnp.PNBRQK";
std::string CMove::ToShortString() const
{
std::stringstream ss;
assert (m_captured != IV);
ss << m_from;
ss << m_to;
if (m_promoted != EM)
{
ss << (char) tolower(pieces[m_promoted+6]);
}
return ss.str();
}
std::string CMove::ToLongString() const
{
std::stringstream ss;
assert (m_captured != IV);
ss << pieces[m_piece+6];
ss << m_from;
if (m_captured != EM)
ss << "*";
else
ss << "-";
ss << m_to;
if (m_promoted != EM)
{
ss << "=";
ss << (char) tolower(pieces[m_promoted+6]);
}
return ss.str();
}
// Returns NULL if error
const char * CMove::FromString(const char *s)
{
if (m_from.FromString(s) || m_to.FromString(s+2))
return NULL;
s += 4;
m_piece = EM;
m_captured = EM;
m_promoted = EM;
if (m_to.row() == 1)
{
switch (tolower(s[0]))
{
case 'q' : m_promoted = BQ; s++; break;
case 'r' : m_promoted = BR; s++; break;
case 'b' : m_promoted = BB; s++; break;
case 'n' : m_promoted = BN; s++; break;
case ' ' : break;
case '\0': break;
default : return NULL;
}
}
else if (m_to.row() == 8)
{
switch (tolower(s[0]))
{
case 'q' : m_promoted = WQ; s++; break;
case 'r' : m_promoted = WR; s++; break;
case 'b' : m_promoted = WB; s++; break;
case 'n' : m_promoted = WN; s++; break;
case ' ' : break;
case '\0': break;
default : return NULL;
}
}
while (s[0] == ' ')
s++;
return s;
} /* end of FromString */