-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cpp
More file actions
61 lines (50 loc) · 1.59 KB
/
Copy pathMove.cpp
File metadata and controls
61 lines (50 loc) · 1.59 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
#include "Errors.h"
#include "Move.h"
#include <sstream>
#include <iostream>
Move::Move(const std::string& input) {
std::istringstream move_ln(input);
//check move number
std::string buffer;
if (!(move_ln >> buffer) || buffer.empty() || buffer.size() != 1) {
throw ParseError("invalid number.");
}
int temp = stoi(buffer);
if (temp < 1 || temp > 9 || std::to_string(temp) != buffer) {
throw ParseError("invalid number.");
}
number = temp;
move_ln >> std::ws;
//check player code
if (!(move_ln >> buffer) || buffer.length() != 1) {
throw ParseError("invalid player.");
}
if ((toupper(buffer[0]) != 'X' && toupper(buffer[0]) != 'O')) {
throw ParseError("invalid player.");
}
player = toupper(buffer[0]);
move_ln >> std::ws;
//check square code
if (!(move_ln >> buffer) || buffer.length() != 2) {
throw ParseError("invalid location.");
}
buffer[0] = toupper(buffer[0]);
if ((buffer[0] != 'A' && buffer[0] != 'B' && buffer[0] != 'C') ||
(buffer[1] != '1' && buffer[1] != '2' && buffer[1] != '3')) {
throw ParseError("invalid location.");
}
row = buffer[0] - 64;
column = buffer[1] - '0';
move_ln >> std::ws;
//check comment validity
if (!move_ln.eof()) {
if (move_ln.peek() != '#') {
throw ParseError("invalid format.");
}
}
}
std::string Move::to_string() const {
std::string str(1, player);
std::string str1(1, '@'+row);
return std::to_string(number) + " " + str + " " + str1 + std::to_string(column);
}