-
Notifications
You must be signed in to change notification settings - Fork 3
/
Shape.cpp
110 lines (89 loc) · 1.68 KB
/
Shape.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
#include "pch.h"
#include "Shape.h"
#include <fstream>
#include <string>
#include <deps/gsl/include/gsl/gsl>
#include "Cell.h"
#include "Log.h"
bool Shape::Load()
{
ML_METHOD;
_stream = std::ifstream(_path, std::ifstream::in);
if (!_stream.is_open())
{
ML_TRACE("Failed to open file: {}", _path.string());
return false;
}
std::string line{0};
while (std::getline(_stream, line))
{
if (line.empty())
continue;
if (line[0] == '!')
{
_notes.push_back(line);
continue;
}
_textcells.push_back(line);
}
_stream.close();
_name = _path.filename().string();
Dump();
Parse();
return true;
}
void Shape::Dump()
{
#ifdef ML_LOGGING
ML_TRACE(_name);
for (const auto& note : _notes)
{
ML_TRACE(note);
}
for (const auto& cell : _textcells)
{
ML_TRACE(cell);
}
#endif
}
void Shape::Parse()
{
ML_METHOD;
// get the width and height of the shape
// since the file will not always contain full rows
_height = gsl::narrow_cast<uint16_t>(_textcells.size());
_width = 0;
for (const auto& row : _textcells)
{
if (row.size() > _width)
_width = gsl::narrow_cast<uint16_t>(row.size());
}
// set the max dimension (width or height)
if (_width > _height)
{
_maxdim = _width;
}
else
{
_maxdim = _height;
}
// create a vector of Cells that is the right size, initialize to all dead Cells
_cells.resize(_width * _height);
int x = 0;
int y = 0;
for (const auto& row : _textcells)
{
for (const auto& cell : row)
{
if (cell == 'O')
{
// only turn on Cells if they are alive in the file
// otherwise do nothing (leave them dead)
_cells.at(y * _width + x).SetState(Cell::State::Live);
}
x++;
}
x = 0;
y++;
}
}