-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.cc
executable file
·176 lines (141 loc) · 4.05 KB
/
move.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "shared.h"
#include <string>
#include <iostream>
#include <sstream>
InvalidInput::InvalidInput(std::string message): message{message} {}
void InvalidInput::printMessage() const {std::cout << message << std::endl;};
int alphaToNum(char c) {
switch(c) {
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case 'e': return 4;
case 'f': return 5;
case 'g': return 6;
case 'h': return 7;
default:
throw InvalidInput{"Column out of scope."};
}
}
bool validPiece(std::string str) {
if (str.length() == 1) {
switch(str[0]) {
case 'r':
case 'R':
case 'q':
case 'Q':
case 'n':
case 'N':
case 'b':
case 'B':
case 'p':
case 'P':
case 'k':
case 'K':
return true;
default:
return false;
}
} else {
throw InvalidInput("Invalid input.");
}
}
Position convertToPos(std::string str) {
int col, row;
// string represents a position
if (str.length() == 2) {
col = alphaToNum(str[0]);
row = 0;
// check if the row is inputted as a proper digit under 8
if (isdigit(str[1])) {
row = str[1] - '0' - 1;
if (row > 7 || row < 0) {
throw InvalidInput{"Row out of scope."};
}
} else {
throw InvalidInput{"Row not a digit."};
}
} else {
throw InvalidInput("Invalid input.");
}
return Position{row, col};
}
Colour convertToColour(std::string colour) {
//convert to lowercase
for (int i = 0; i < colour.length(); i++) colour[i] = tolower(colour[i]);
if (colour == "white") {
return WHITE;
}
else if (colour == "black") {
return BLACK;
} else {
throw InvalidInput("Invalid colour.");
}
}
bool Move::validPromotionPiece(std::string str) const {
if (str.length() == 1) {
switch(str[0]) {
case 'r':
case 'R':
case 'q':
case 'Q':
case 'n':
case 'N':
case 'b':
case 'B':
return true;
default:
return false;
}
} else {
throw InvalidInput("Invalid input.");
}
}
void Move::convertFormat(std::string str, int counter) {
// check if it is a curPost or newPos
if (counter == 0) {
curPos = convertToPos(str);
}
else if (counter == 1) {
newPos = convertToPos(str);
}
//string represents a piece that pawn should be promoted to
else if (counter == 2 && Move::validPromotionPiece(str)) {
promoteTo = str[0];
}
}
Move::Move(Position curPos, Position newPos, Piece* piece): curPos{curPos}, newPos{newPos}, piece{piece}, promotion{false} {}
Move::Move(std::string input) {
piece = nullptr;
int counter = 0;
std::stringstream ss{input};
std::string s;
while(ss >> s) {
if (counter < 3) Move::convertFormat(s, counter);
else {
throw InvalidInput{"Too much input."};
}
counter++;
}
if (counter < 2) throw InvalidInput{"Not enough input."};
else if (counter == 2) promotion = false;
else if (counter == 3) promotion = true;
}
Position Move::getCurPos() const {return curPos;}
Position Move::getNewPos() const {return newPos;}
bool Move::getPromotion() const {return promotion;}
char Move::getPromoteTo() const {return promoteTo;}
Piece* Move::getPiece() const {return piece;}
void Move::setPiece(Piece* newPiece) {piece = newPiece;}
/*void test() {
try {
std::string str = "fds e5 e4";
Move m{str};
std::cout << m.getCurPos().col << " " << m.getCurPos().row << std::endl;
std::cout << m.getNewPos().col << " " << m.getNewPos().row << std::endl;
std::cout << m.getPromotion() << std::endl;
} catch (InvalidInput e) {
e.printMessage();
}
}*/