-
Notifications
You must be signed in to change notification settings - Fork 4
/
game_field.cpp
166 lines (143 loc) · 4.07 KB
/
game_field.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// game_field.cpp
// GameOfLive
//
// Created by Кирилл on 10.10.17.
// Copyright © 2017 Кирилл. All rights reserved.
//
#include <sstream>
#include "game_field.h"
const char ALIVE_CELL = '#';
const char NO_CELL = '.';
/**
* Makes the position looped in.
* The neighbors of the upper cells are lower,
* the neighbors of the right cells are left.
*
* @param pos Position.
* @param module Loop module (field size).
*
* @return Looped in position.
*/
static int loopCoordinate(int pos, size_t module) {
if (pos < 0)
return (static_cast<int>(module)) - (-pos) % module;
return pos % module;
}
BadGameFieldException::BadGameFieldException(size_t line,
size_t pos,
const std::string& reason) {
std::ostringstream out;
out << reason << " at line " << line << ", position " << pos;
this->reason = out.str();
}
const char* BadGameFieldException::what() const noexcept {
return reason.c_str();
}
GameField::GameField(size_t width, size_t height)
: width(width), height(height) {
field = std::vector<std::vector<bool>>(width);
for (size_t i = 0; i < width; i++)
field[i] = std::vector<bool>(height);
}
GameField::GameField(const std::string& str) {
field.push_back(std::vector<bool>());
size_t line = 0;
size_t maxWidth = 0;
size_t currWidth = 0;
for (size_t i = 0; i < str.size(); i++) {
switch (str[i]) {
case '\n':
if (maxWidth != currWidth)
throw BadGameFieldException(
line, currWidth, "Invalid number of characters in the line");
line++;
currWidth = 0;
field.push_back(std::vector<bool>());
break;
case ALIVE_CELL:
if (line == 0)
maxWidth++;
currWidth++;
field[line].push_back(true);
break;
case NO_CELL:
if (line == 0)
maxWidth++;
currWidth++;
field[line].push_back(false);
break;
case '\r':
continue;
default:
throw BadGameFieldException(
line, currWidth, std::string("Unknown character '") + str[i] + "'");
}
}
if (maxWidth != currWidth && currWidth != 0)
throw BadGameFieldException(line, currWidth,
"Invalid number of characters in the line");
if (maxWidth != 0)
line++;
if (currWidth == 0 && line != 0) {
field.pop_back();
line--;
}
width = line;
height = maxWidth;
}
GameField::SubGameField GameField::operator[](int pos) {
return SubGameField(loopCoordinate(pos, width), (*this));
}
const GameField::SubGameField GameField::operator[](int pos) const {
return SubGameField(loopCoordinate(pos, width),
const_cast<GameField&>(*this));
}
size_t GameField::getWidth() const {
return width;
}
size_t GameField::getHeight() const {
return height;
}
GameField& GameField::operator=(const GameField& copy) {
if (© != this) {
width = copy.width;
height = copy.height;
field = copy.field;
}
return (*this);
}
bool GameField::operator==(const GameField& equal) const {
return field == equal.field;
}
std::ostream& operator<<(std::ostream& stream, const GameField& field) {
for (int i = 0; i < field.width; i++) {
for (int j = 0; j < field.height; j++)
stream << (field.field[i][j] ? ALIVE_CELL : NO_CELL);
if (i != field.width - 1)
stream << std::endl;
}
return stream;
}
GameField::SubGameField::Cell GameField::SubGameField::operator[](int pos) {
return Cell(posX, loopCoordinate(pos, height), field);
}
const GameField::SubGameField::Cell GameField::SubGameField::operator[](
int pos) const {
return Cell(posX, loopCoordinate(pos, height), field);
}
bool GameField::SubGameField::Cell::isLife() const {
return field[posX][posY];
}
size_t GameField::SubGameField::Cell::getX() const {
return posX;
}
size_t GameField::SubGameField::Cell::getY() const {
return posY;
}
void GameField::SubGameField::Cell::bornLife() {
field[posX][posY] = true;
}
void GameField::SubGameField::Cell::kill() {
field[posX][posY] = false;
}