-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.cc
104 lines (82 loc) · 1.92 KB
/
Cell.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
#include "Cell.h"
#include <sstream>
#include <stdexcept>
Cell::Cell(int value, int state) : _value(value), _state(state) { }
bool Cell::operator ==(const Cell& c) const
{
return _state == c._state && _value == c._value;
}
bool Cell::operator !=(const Cell& c) const
{
return !(*this == c);
}
bool Cell::isValidState(int state) const
{
return state == FLAGGED || state == KNOWN || state == UNKNOWN;
}
bool Cell::isValidValue(int value) const
{
return (value >= 0 && value <= 8) || value == MINE;
}
void Cell::setState(int new_state)
{
if (this->isValidState(new_state))
{
this->_state = new_state;
}
else
throw std::invalid_argument("invalid state: "); // + str(new_state));
}
void Cell::setValue(int new_value)
{
if (this->isValidValue(new_value))
{
this->_value = new_value;
}
else
throw std::invalid_argument("invalid value");
}
std::string Cell::toString() const
{
char value_char;
char state_char;
if (_value < 0)
value_char = 'X';
else if (_value == 0)
value_char = '_';
else
value_char = '0' + _value;
if (_state == UNKNOWN)
state_char = '?';
else if (_state == KNOWN)
state_char = '!';
else
state_char = 'F';
std::string s = "|";
s += value_char;
s += state_char;
s += '|';
return s;
}
bool Cell::fromString(const std::string& str)
{
if (str.length() != 4) return false;
if (str[0] != '|' || str[3] != '|') return false;
if (str[1] == 'X')
_value = -1;
else if (str[1] == '_')
_value = 0;
else if (str[1] >= '1' && str[1] <= '9')
_value = str[1] - '0';
else
return false;
if (str[2] == 'F')
_state = Cell::FLAGGED;
else if (str[2] == '!')
_state = Cell::KNOWN;
else if (str[2] == '?')
_state = Cell::UNKNOWN;
else
return false;
return true;
}