-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWriteCommand.cpp
More file actions
99 lines (88 loc) · 2.82 KB
/
Copy pathWriteCommand.cpp
File metadata and controls
99 lines (88 loc) · 2.82 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
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
//------------------------------------------------------------------------------
// WriteCommand.cpp
//
// Group: Group 11591, study assistant Thomas Neff
//
// Authors: Benjamin Franz Herbert Rauch
// Peter Lorenz
// Rene Hasenburger
//------------------------------------------------------------------------------
//
const int FILE_INDEX = 0;
typedef signed char schar;
#include "fstream" // ostream
#include "WriteCommand.h"
#include "Tile.h"
#include "Exceptions.h"
#include "Game.h"
//------------------------------------------------------------------------------
WriteCommand::WriteCommand() : Command("write")
{
}
//------------------------------------------------------------------------------
WriteCommand::~WriteCommand()
{
}
//------------------------------------------------------------------------------
unsigned int WriteCommand::getParamCnt()
{
return 1;
}
//------------------------------------------------------------------------------
int WriteCommand::execute(Game &board, std::vector<std::string> ¶ms)
{
if(board.getStartTile() == nullptr)
{
throw TraxException(TraxException::BOARD_IS_EMPTY);
}
else
{
// write to file
std::ofstream binary_file;
binary_file.open(params[FILE_INDEX], std::ios::trunc | std::ios::binary);
if(binary_file.fail())
{
throw TraxException(TraxException::CANNOT_WRITE_FILE,
params[FILE_INDEX].c_str());
}
else
{
// write head to binary file
char write_active_player = static_cast<char>(board.getActivePlayer());
schar write_smallest_x = static_cast<schar>(board.getMinX());
schar write_smallest_y = static_cast<schar>(board.getMinY());
schar write_biggest_x = static_cast<schar>(board.getMaxX());
schar write_biggest_y = static_cast<schar>(board.getMaxY());
binary_file << "TRAX";
binary_file << write_active_player;
binary_file << write_smallest_x;
binary_file << write_smallest_y;
binary_file << write_biggest_x;
binary_file << write_biggest_y;
Tile *field_type;
for(; write_smallest_y <= write_biggest_y; write_smallest_y++)
{
for (int x = write_smallest_x; x <= write_biggest_x; x++)
{
field_type = (board.getTileAtPosition(x, write_smallest_y));
// check if field at position x,y is not empty
if(field_type != nullptr)
{
// side: cross (1), curve1 "/" (2), curve2 "\" (3)
binary_file << static_cast<char>(field_type->getSide());
// topcolor: white(1), red(2)
binary_file << static_cast<char>(field_type->getTopColor());
}
else
{
// side: no stone (0); topcolor: no color (0)
binary_file << '\x0';
binary_file << '\x0';
}
}
}
}
binary_file.close();
}
return 0;
}