Skip to content

Commit

Permalink
v0.3.0-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaVarga committed Aug 28, 2018
1 parent b54ad31 commit facedd3
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 94 deletions.
135 changes: 69 additions & 66 deletions Source/BriansBrain/briansbrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ briansbrain.cpp
Purpose: Implement Brian's Brain cellular automaton.
@author Joshua Varga
@version 1.0
@version 2.0
*/

#include "briansbrain.h"
Expand All @@ -20,112 +20,122 @@ std::string BriansBrain::getName()
return name;
}

// Generates random states states for the first generation of cells.
// Generates random states states for the first generation of cells and initializes information..
void BriansBrain::init()
{
int percent;
srandom();

// Loop through cells and set them to on or off randomly.
for (int i = 0; i < cellCount; i++)
{
percent = random(1, 100);

if (percent > 75)
{
population.push_back(on);
}

else if (percent > 50)
{
population.push_back(dying);
}

else
{
population.push_back(off);
}
// All cells begin off.
population.get()->push_back(off);
}
}

// Replaces the current population with the next generation of cells.
void BriansBrain::update()
{
// Next generation.
std::vector<cell> new_population(cellCount);

generation++;
int neighbours;

// Loop through all cells and change their state in the next generation based on neighbours.
for (int i = 0; i < cellCount; i++)
{
switch (population[i])
switch (population.get()->at(i))
{
case on:
{
new_population[i] = dying;
new_population.get()->at(i) = dying;
break;
}

case dying:
{
new_population[i] = off;
new_population.get()->at(i) = off;
populationSize--;
break;
}

default:
{
{
neighbours = countNeighbours(i);

switch (neighbours)
{
case 2:
{
new_population[i] = on;
new_population.get()->at(i) = on;
populationSize++;
break;
}

default:
{
new_population[i] = off;
new_population.get()->at(i) = off;
break;
}
}
}
}
}

population = new_population;
population.swap(new_population);
}

// Initializes information about the automaton.
void BriansBrain::initInfo()
{
// Resize to the amount of info.
information.resize(2);

// Set the title of the info.
information[0].title = "Population";
information[1].title = "Generation";
}

// Updates automaton information.
void BriansBrain::updateInfo()
{
information[0].value = std::to_string(populationSize);
information[1].value = std::to_string(generation);
}

// Cycles the state of a cell at specific coordinates.
void BriansBrain::cycleCell(int x, int y)
{
int index = getIndex(x, y);

switch (population[index])
{
case on:
if (x < 0 || x > windowSize)
{
population[index] = dying;
break;
return;
}

case dying:
{
population[index] = off;
break;
}
int index = getIndex(x / cellSize, y / cellSize);

default:
switch (population.get()->at(index))
{
population[index] = on;
break;
}
case on:
{
population.get()->at(index) = dying;
break;
}

case dying:
{
population.get()->at(index) = off;
populationSize--;
break;
}

case off:
{
population.get()->at(index) = on;
populationSize++;
break;
}
}
}

// Counts the neighbours in the Moore neighbourhood.
// TODO: Increase performance.
int BriansBrain::countNeighbours(int index)
{
int neighbours = 0;
Expand Down Expand Up @@ -159,43 +169,36 @@ int BriansBrain::countNeighbours(int index)
int north_west = (gridSize * ((y - 1 + gridSize) % gridSize)) +
((x - 1 + gridSize) % gridSize);

if (population[north] == on) neighbours++;
if (population[north_east] == on) neighbours++;
if (population[east] == on) neighbours++;
if (population[south_east] == on) neighbours++;
if (population[south] == on) neighbours++;
if (population[south_west] == on) neighbours++;
if (population[west] == on) neighbours++;
if (population[north_west] == on) neighbours++;
if (population.get()->at(north) == on) neighbours++;
if (population.get()->at(north_east) == on) neighbours++;
if (population.get()->at(east) == on) neighbours++;
if (population.get()->at(south_east) == on) neighbours++;
if (population.get()->at(south) == on) neighbours++;
if (population.get()->at(south_west) == on) neighbours++;
if (population.get()->at(west) == on) neighbours++;
if (population.get()->at(north_west) == on) neighbours++;

return neighbours;
}

// Sets the colour of a cell specified by index.
sf::Color BriansBrain::paint(int index)
{
sf::Color colour;

switch (population[index])
switch (population.get()->at(index))
{
case on:
{
colour = sf::Color::Blue;
break;
return sf::Color::Blue;
}

case dying:
{
colour = sf::Color::White;
break;
return sf::Color::White;
}

default:
{
colour = sf::Color::Black;
break;
return sf::Color::Black;
}
}

return colour;
}
17 changes: 15 additions & 2 deletions Source/BriansBrain/briansbrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ briansbrain.h
Purpose: Implement Brian's Brain cellular automaton.
@author Joshua Varga
@version 1.0
@version 2.0
*/

#ifndef BRIANSBRAIN_H_
Expand All @@ -15,6 +15,8 @@ class BriansBrain : public CellularAutomaton
{
private:
std::string name = "Brian's Brain";
int populationSize = 0;
int generation = 0;

enum cell
{
Expand All @@ -23,7 +25,8 @@ class BriansBrain : public CellularAutomaton
off
};

std::vector<cell> population;
std::unique_ptr<std::vector<cell>> population = std::make_unique<std::vector<cell>>();
std::unique_ptr<std::vector<cell>> new_population = std::make_unique<std::vector<cell>>(cellCount);

public:
/**
Expand All @@ -50,6 +53,16 @@ class BriansBrain : public CellularAutomaton
*/
void update() override;

/**
Initializes information about the automaton.
*/
void initInfo() override;

/**
Updates automaton information.
*/
void updateInfo() override;

/**
Cycles the state of a cell at specific coordinates.
Expand Down
3 changes: 3 additions & 0 deletions Source/Cellular-Automata.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@
<ClInclude Include="Util\config.h" />
<ClInclude Include="Util\random.h" />
</ItemGroup>
<ItemGroup>
<Font Include="..\..\..\..\Desktop\arial.ttf" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
3 changes: 3 additions & 0 deletions Source/Cellular-Automata.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Font Include="..\..\..\..\Desktop\arial.ttf" />
</ItemGroup>
</Project>
28 changes: 16 additions & 12 deletions Source/Custom/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,19 @@ std::string Custom::getName()
// Also prompts user for rules.
void Custom::init()
{
int percent;
srandom();
information.resize(2);

// Loop through cells and set them to on or off randomly.
for (int i = 0; i < cellCount; i++)
{
percent = random(1, 100);
population.push_back(off);
}

if (percent > 69)
{
population.push_back(on);
}
information[0].title = "Population";
information[0].value = std::to_string(populationSize);

else
{
population.push_back(off);
}
}
information[1].title = "Generation";
information[1].value = std::to_string(generation);

std::string rules;

Expand All @@ -91,6 +86,7 @@ void Custom::update()
{
// Next generation.
std::vector<cell> new_population(cellCount);
generation++;

int neighbours;

Expand Down Expand Up @@ -135,6 +131,9 @@ void Custom::update()
}

population = new_population;

information[0].value = std::to_string(populationSize);
information[1].value = std::to_string(generation);
}

// Cycles the state of a cell at specific coordinates.
Expand All @@ -147,15 +146,20 @@ void Custom::cycleCell(int x, int y)
case on:
{
population[index] = off;
populationSize--;
break;
}

default:
{
population[index] = on;
populationSize++;
break;
}
}

information[0].value = std::to_string(populationSize);
information[1].value = std::to_string(generation);
}

// Counts the neighbours in the Moore neighbourhood.
Expand Down
2 changes: 2 additions & 0 deletions Source/Custom/custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Custom : public CellularAutomaton
{
private:
std::string name;
int populationSize = 0;
int generation = 0;

std::vector<int> birth; // Stores neighbours needed for cell birth.
std::vector<int> survival; // Stores neighbours needed for cell survival.
Expand Down
4 changes: 2 additions & 2 deletions Source/Util/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <SFML/Graphics.hpp>

const int windowSize = 800; // Width/Height of the window (pixels);
const int cellSize = 4; // Width/Height of each individual cell (pixels).
const int windowSize = 800; // Width/Height of the window (pixels);
const int cellSize = 1; // Width/Height of each individual cell (pixels).
const int gridSize = windowSize / cellSize; // The amount of cells that fit side by side into the x or y dimension of the window.
const int cellCount = gridSize * gridSize; // Total amount of cells that fit into the window.

Expand Down
Loading

0 comments on commit facedd3

Please sign in to comment.