Ошибка в игре "Жизнь" John Conway C++ ---> Недопустимый параметр был передан функции
Во время запуска кода, появляется данная ошибка.
При просмотре отладчиком, выполнение останавливается на строчке : std::for_each(std::begin(it) + coll - 1, std::begin(it) + coll + 2, [&](char& cell) {, в функции size_t calculate(const size_t str, const size_t coll);.
(Реализацию конструктора а так же функции printUniverse указал в конце на всякий случай).
Код :
Source.cpp
#include "Universe/Universe.h"
int main(void)
{
Universe u(100, 25);
u.printUniverse();
for (size_t i = 0; i < 10; ++i) {
u.oneGeneration();
u.printUniverse();
}
return 0;
}
Universe.h
#pragma once
#include <ctime>
#include <thread>
#include <iostream>
#include <algorithm>
#include <vector>
class Universe
{
private:
size_t calculate
(
const size_t str,
const size_t coll
);
void checkRules
(
const size_t str,
const size_t coll
);
public:
Universe
(
const size_t width,
const size_t height,
const size_t population = 5
);
void printUniverse(size_t sleep_for = 200);
void oneGeneration();
~Universe();
};
Universe.cpp
#include "Universe.h"
size_t Universe::calculate
(
const size_t str,
const size_t coll
)
{
size_t live_neighbors = 0;
std::for_each(std::begin(universe) + str - 1, std::begin(universe) + str + 2, [&](auto& it) {
std::for_each(std::begin(it) + coll - 1, std::begin(it) + coll + 2, [&](char& cell) {
if (cell == '*' || cell == 'x')
live_neighbors++;
});
});
return live_neighbors;
}
void Universe::checkRules
(
const size_t str,
const size_t coll
)
{
std::vector<std::vector<char>>::iterator i_str = std::begin(universe) + str;
std::vector<char>::iterator i_coll = std::begin(*i_str) + coll;
size_t live_neighbors = calculate(str, coll);
if (*i_coll == '*')
live_neighbors--;
if (*i_coll == '*' && (live_neighbors < 2 || live_neighbors > 3))
*i_coll = 'x';
else if (*i_coll == ' ' && live_neighbors == 3)
*i_coll = 'o';
}
void Universe::oneGeneration()
{
size_t str = 1, coll = 1;
std::for_each(std::begin(universe) + 1, std::end(universe) - 1, [&](auto& it) {
for (auto cell = std::begin(it) + 1; cell != std::end(it); ++cell)
checkRules(str, coll++);
str++;
});
std::for_each(std::begin(universe) + 1, std::end(universe) - 1, [](auto& it) {
std::for_each(std::begin(it) + 1, std::end(it) - 1, [](char& cell) {
if (cell == 'x')
cell = ' ';
if (cell == 'o')
cell = '*';
});
});
}
Конструктор и printUniverse
Universe::Universe
(
const size_t width,
const size_t height,
const size_t population
)
{
srand(time(nullptr));
universe.resize(height + 2);
std::for_each(std::begin(universe), std::end(universe), [&](auto& it) {
it.resize(width + 2);
});
std::for_each(std::begin(universe) + 1, std::end(universe) - 1, [&](auto& it)
{
std::for_each(std::begin(it) + 1, std::begin(it) + width / population -1, [&](char& cell) {
cell = '*';
});
std::random_shuffle(std::begin(it) + 1, std::end(it) - 1);
});
}
void Universe::printUniverse(size_t sleep_for)
{
std::for_each(std::begin(universe) + 1, std::end(universe) - 1, [](auto& it) {
std::for_each(std::begin(it) + 1, std::end(it) - 1, [](char& cell) {
std::cout << cell;
});
std::cout << std::endl;
});
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_for));
system("cls");
}

