-
Notifications
You must be signed in to change notification settings - Fork 0
/
fairy_tail.cpp
114 lines (92 loc) · 2.67 KB
/
fairy_tail.cpp
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
#include "fairy_tail.hpp"
Fairyland::Fairyland()
: mOutput("output.txt")
, mTurnCount(0)
{
std::ifstream file("input.txt");
check(file.is_open(), "File input.txt not found");
mMaze.resize(gSize);
for (int y = 0; y < gSize; ++y)
{
mMaze[y].resize(gSize);
for (int x = 0; x < gSize; ++x)
{
char c = 0;
file >> c;
check(file.good(), "Invalid input file");
bool passage = true;
switch (c)
{
case '#':
passage = false;
break;
case '@':
mIvanPos.first = x;
mIvanPos.second = y;
break;
case '&':
mElenaPos.first = x;
mElenaPos.second = y;
break;
default:
check(c == '.', "Invalid input file");
}
mMaze[y][x] = passage;
}
}
}
Fairyland::~Fairyland()
{
mOutput << "XX" << std::endl;
}
void Fairyland::check(bool expression, const char* message)
{
if (!expression)
{
std::cerr << message << std::endl;
throw std::runtime_error(message);
}
}
int Fairyland::getTurnCount() const
{
return mTurnCount;
}
bool Fairyland::move(Position& position, Direction direction)
{
switch (direction)
{
case Direction::Up:
position.second -= 1;
return position.second >= 0;
case Direction::Down:
position.second += 1;
return position.second < gSize;
case Direction::Left:
position.first -= 1;
return position.first >= 0;
case Direction::Right:
position.first += 1;
return position.first < gSize;
default:
return true;
}
}
bool Fairyland::canGo(Character name, Direction direction) const
{
Position position = (name == Character::Ivan) ? mIvanPos : mElenaPos;
return move(position, direction) && mMaze[position.second][position.first];
}
bool Fairyland::go(Direction directionIvan, Direction directionElena)
{
check(canGo(Character::Ivan, directionIvan), "Invalid Ivan's direction");
check(canGo(Character::Elena, directionElena), "Invalid Elena's direction");
mOutput << static_cast<char>(directionIvan) << static_cast<char>(directionElena);
check(mOutput.good(), "Cannot write to file output.txt");
mTurnCount += 1;
check(mTurnCount < 1000000, "Too many turns");
const Position lastIvanPos = mIvanPos;
const Position lastElenaPos = mElenaPos;
move(mIvanPos, directionIvan);
move(mElenaPos, directionElena);
return mIvanPos == mElenaPos || lastIvanPos == mElenaPos && lastElenaPos == mIvanPos;
}