This commit is contained in:
2019-11-06 11:19:23 -07:00
parent 9204af78c9
commit 57010211a1
31 changed files with 71 additions and 81 deletions

View File

@@ -6,8 +6,8 @@ LifeSimulator::LifeSimulator(std::uint8_t sizeX, std::uint8_t sizeY) :
// Initializing vectors with padding for easy searches // Initializing vectors with padding for easy searches
for (std::uint8_t i = 0; i < sizeY + 2; i++) for (std::uint8_t i = 0; i < sizeY + 2; i++)
{ {
currentScreen.push_back(std::vector<bool>()); currentScreen.emplace_back();
nextScreen.push_back(std::vector<bool>()); nextScreen.emplace_back();
for (std::uint8_t j = 0; j < sizeX + 2; j++) for (std::uint8_t j = 0; j < sizeX + 2; j++)
{ {
currentScreen[i].push_back(false); currentScreen[i].push_back(false);
@@ -18,15 +18,15 @@ LifeSimulator::LifeSimulator(std::uint8_t sizeX, std::uint8_t sizeY) :
void LifeSimulator::insertPattern(const Pattern& pattern, std::uint8_t startX, std::uint8_t startY) void LifeSimulator::insertPattern(const Pattern& pattern, std::uint8_t startX, std::uint8_t startY)
{ {
// Adding offset for padded borders
startX += 1; startX += 1;
startY += 1; startY += 1;
std::cout << pattern.getCell(0, 0) << std::endl;
if ((pattern.getSizeY() + startY) <= (currentScreen.size() - 1) && (pattern.getSizeX() + startX) <= (currentScreen[0].size() - 1)) if (((currentScreen.size() - 1) >= pattern.getSizeY() + startY) &&
(pattern.getSizeX() + startX) <= (currentScreen[0].size() - 1))
{ {
for (int y = 0; y < pattern.getSizeY(); y++) for (std::uint8_t y = 0; y < pattern.getSizeY(); y++)
{ {
for (int x = 0; x < pattern.getSizeX(); x++) for (std::uint8_t x = 0; x < pattern.getSizeX(); x++)
{ {
if (pattern.getCell(x, y)) if (pattern.getCell(x, y))
{ {
@@ -51,32 +51,23 @@ void LifeSimulator::update()
nextScreen[i][j] = false; nextScreen[i][j] = false;
} }
} }
// For loop to update nextScreen vector
for (int i = 1; i < currentScreen.size() - 1; i++) for (int i = 1; i < currentScreen.size() - 1; i++)
{ {
for (int j = 1; j < currentScreen[0].size() - 1; j++) for (int j = 1; j < currentScreen[0].size() - 1; j++)
{ {
// Checking each pixel int liveCount = 0;
int count = 0;
for (int y = -1; y < 2; y++) for (int y = -1; y < 2; y++)
{ {
for (int x = -1; x < 2; x++) for (int x = -1; x < 2; x++)
{ {
if (y == 0 && x == 0) if (!(y == 0 && x == 0))
;
else
{
if (currentScreen[i + y][j + x]) if (currentScreen[i + y][j + x])
count++; liveCount++;
}
} }
} }
/*
* Any live cell with two or three live neighbours lives on to the next generation. if (liveCount > 1 && liveCount < 4)
* Any live cell with more than three live neighbours dies, as if by overpopulation.
* Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
if (count > 1 && count < 4)
{ {
if (currentScreen[i][j]) if (currentScreen[i][j])
{ {
@@ -84,7 +75,7 @@ void LifeSimulator::update()
} }
else else
{ {
if (count == 3) if (liveCount == 3)
{ {
nextScreen[i][j] = true; nextScreen[i][j] = true;
} }

View File

@@ -21,9 +21,9 @@ class LifeSimulator
void insertPattern(const Pattern& pattern, std::uint8_t startX, std::uint8_t startY); void insertPattern(const Pattern& pattern, std::uint8_t startX, std::uint8_t startY);
void update(); void update();
std::uint8_t getSizeX() const { return sizeX; }; [[nodiscard]] std::uint8_t getSizeX() const { return sizeX; };
std::uint8_t getSizeY() const { return sizeY; }; [[nodiscard]] std::uint8_t getSizeY() const { return sizeY; };
bool getCell(std::uint8_t x, std::uint8_t y) const { return currentScreen[y + 1][x + 1]; }; [[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const { return currentScreen[y + 1][x + 1]; };
; ;
}; };

View File

@@ -15,9 +15,9 @@
class Pattern class Pattern
{ {
public: public:
virtual std::uint8_t getSizeX() const = 0; [[nodiscard]] virtual std::uint8_t getSizeX() const = 0;
virtual std::uint8_t getSizeY() const = 0; [[nodiscard]] virtual std::uint8_t getSizeY() const = 0;
virtual bool getCell(std::uint8_t x, std::uint8_t y) const = 0; [[nodiscard]] virtual bool getCell(std::uint8_t x, std::uint8_t y) const = 0;
}; };
#endif //CS3460_CPP_PATTERN_HPP #endif //CS3460_CPP_PATTERN_HPP

View File

@@ -12,19 +12,19 @@ class PatternAcorn : public Pattern
private: private:
std::uint8_t X; std::uint8_t X;
std::uint8_t Y; std::uint8_t Y;
std::array<std::array<bool, 4>, 4> cells; std::array<std::array<bool, 4>, 4> cells{};
public: public:
PatternAcorn(); PatternAcorn();
std::uint8_t getSizeX() const [[nodiscard]] std::uint8_t getSizeX() const override
{ {
return X; return X;
}; };
std::uint8_t getSizeY() const [[nodiscard]] std::uint8_t getSizeY() const override
{ {
return Y; return Y;
}; };
bool getCell(std::uint8_t x, std::uint8_t y) const [[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const override
{ {
return cells[x][y]; return cells[x][y];
}; };

View File

@@ -10,18 +10,24 @@
class PatternBlinker : public Pattern class PatternBlinker : public Pattern
{ {
private: private:
uint8_t X; std::uint8_t X;
uint8_t Y; std::uint8_t Y;
std::array<std::array<bool, 5>, 5> cells; std::array<std::array<bool, 5>, 5> cells{};
public: public:
PatternBlinker(); PatternBlinker();
[[nodiscard]] std::uint8_t getSizeX() const override
std::uint8_t getSizeX() const { return X; }; {
return X;
std::uint8_t getSizeY() const { return Y; }; };
[[nodiscard]] std::uint8_t getSizeY() const override
bool getCell(std::uint8_t x, std::uint8_t y) const { return cells[y][x]; }; {
return Y;
};
[[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const override
{
return cells[x][y];
};
}; };
#endif //CS3460_CPP_PATTERNBLINKER_HPP #endif //CS3460_CPP_PATTERNBLINKER_HPP

View File

@@ -11,19 +11,19 @@ class PatternBlock : public Pattern
private: private:
std::uint8_t X; std::uint8_t X;
std::uint8_t Y; std::uint8_t Y;
std::array<std::array<bool, 4>, 4> cells; std::array<std::array<bool, 4>, 4> cells{};
public: public:
PatternBlock(); PatternBlock();
std::uint8_t getSizeX() const [[nodiscard]] std::uint8_t getSizeX() const override
{ {
return X; return X;
}; };
std::uint8_t getSizeY() const [[nodiscard]] std::uint8_t getSizeY() const override
{ {
return Y; return Y;
}; };
bool getCell(std::uint8_t x, std::uint8_t y) const [[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const override
{ {
return cells[x][y]; return cells[x][y];
}; };

View File

@@ -14,10 +14,9 @@ PatternGlider::PatternGlider() :
cells[i][j] = false; cells[i][j] = false;
} }
} }
cells[1][2] = true;
cells[3][1]; cells[2][3] = true;
cells[3][2]; cells[3][1] = true;
cells[3][3]; cells[3][2] = true;
cells[2][3]; cells[3][3] = true;
cells[1][2];
} }

View File

@@ -7,26 +7,24 @@
#include "Pattern.hpp" #include "Pattern.hpp"
#include <array>
class PatternGlider : public Pattern class PatternGlider : public Pattern
{ {
private: private:
std::uint8_t X; std::uint8_t X;
std::uint8_t Y; std::uint8_t Y;
std::array<std::array<bool, 5>, 5> cells; std::array<std::array<bool, 5>, 5> cells{};
public: public:
PatternGlider(); PatternGlider();
std::uint8_t getSizeX() const [[nodiscard]] std::uint8_t getSizeX() const override
{ {
return X; return X;
}; };
std::uint8_t getSizeY() const [[nodiscard]] std::uint8_t getSizeY() const override
{ {
return Y; return Y;
}; };
bool getCell(std::uint8_t x, std::uint8_t y) const [[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const override
{ {
return cells[x][y]; return cells[x][y];
}; };

View File

@@ -12,19 +12,19 @@ class PatternGosperGliderGun : public Pattern
private: private:
std::uint8_t X; std::uint8_t X;
std::uint8_t Y; std::uint8_t Y;
std::array<std::array<bool, 38>, 11> cells; std::array<std::array<bool, 4>, 4> cells{};
public: public:
PatternGosperGliderGun(); PatternGosperGliderGun();
std::uint8_t getSizeX() const [[nodiscard]] std::uint8_t getSizeX() const override
{ {
return X; return X;
}; };
std::uint8_t getSizeY() const [[nodiscard]] std::uint8_t getSizeY() const override
{ {
return Y; return Y;
}; };
bool getCell(std::uint8_t x, std::uint8_t y) const [[nodiscard]] bool getCell(std::uint8_t x, std::uint8_t y) const override
{ {
return cells[x][y]; return cells[x][y];
}; };

View File

@@ -22,5 +22,5 @@ void RendererConsole::render(const LifeSimulator& simulation)
} }
} }
} }
rlutil::showcursor(); //rlutil::showcursor();
} }

View File

@@ -57,8 +57,6 @@ PatternGlider.hpp
/Users/bradybodily/Repositories/CS3460/Hw6/PatternGlider.hpp /Users/bradybodily/Repositories/CS3460/Hw6/PatternGlider.hpp
Pattern.hpp Pattern.hpp
/Users/bradybodily/Repositories/CS3460/Hw6/Pattern.hpp /Users/bradybodily/Repositories/CS3460/Hw6/Pattern.hpp
array
-
/Users/bradybodily/Repositories/CS3460/Hw6/PatternGosperGliderGun.cpp /Users/bradybodily/Repositories/CS3460/Hw6/PatternGosperGliderGun.cpp
PatternGosperGliderGun.hpp PatternGosperGliderGun.hpp

Binary file not shown.

View File

@@ -57,8 +57,6 @@ PatternGlider.hpp
/Users/bradybodily/Repositories/CS3460/Hw6/PatternGlider.hpp /Users/bradybodily/Repositories/CS3460/Hw6/PatternGlider.hpp
Pattern.hpp Pattern.hpp
/Users/bradybodily/Repositories/CS3460/Hw6/Pattern.hpp /Users/bradybodily/Repositories/CS3460/Hw6/Pattern.hpp
array
-
/Users/bradybodily/Repositories/CS3460/Hw6/PatternGosperGliderGun.cpp /Users/bradybodily/Repositories/CS3460/Hw6/PatternGosperGliderGun.cpp
PatternGosperGliderGun.hpp PatternGosperGliderGun.hpp

Binary file not shown.

View File

@@ -14,26 +14,26 @@
int main() int main()
{ {
// Renderer and Simulator
RendererConsole rendererConsole = RendererConsole(); RendererConsole rendererConsole = RendererConsole();
LifeSimulator lifeSimulator = LifeSimulator(100, 40); LifeSimulator lifeSimulator = LifeSimulator(100, 100);
// Objects
PatternGosperGliderGun patternGosperGliderGun = PatternGosperGliderGun();
PatternBlock patternBlock = PatternBlock();
PatternGlider patternGlider = PatternGlider();
PatternBlinker patternBlinker = PatternBlinker();
PatternAcorn patternAcorn = PatternAcorn();
// Adding objects to simulator PatternBlinker patternBlinker = PatternBlinker();
lifeSimulator.insertPattern(patternGosperGliderGun, 20, 20);
lifeSimulator.insertPattern(patternBlock, 0, 10);
lifeSimulator.insertPattern(patternGlider, 5, 10);
lifeSimulator.insertPattern(patternBlinker, 50, 10); lifeSimulator.insertPattern(patternBlinker, 50, 10);
PatternGlider patternGlider = PatternGlider();
lifeSimulator.insertPattern(patternGlider, 5, 10);
PatternGosperGliderGun patternGosperGliderGun = PatternGosperGliderGun();
lifeSimulator.insertPattern(patternGosperGliderGun, 20, 20);
PatternBlock patternBlock = PatternBlock();
lifeSimulator.insertPattern(patternBlock, 0, 10);
PatternAcorn patternAcorn = PatternAcorn();
lifeSimulator.insertPattern(patternAcorn, 0, 23); lifeSimulator.insertPattern(patternAcorn, 0, 23);
// Animation Demonstration
int x = 0; int x = 0;
while (x < 200) while (x < 400)
{ {
rendererConsole.render(lifeSimulator); rendererConsole.render(lifeSimulator);
lifeSimulator.update(); lifeSimulator.update();