diff --git a/Conways.sln b/Conways.sln new file mode 100644 index 0000000..508f212 --- /dev/null +++ b/Conways.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Conways", "Conways\Conways.vcxproj", "{9035B83C-F117-480E-9DEB-435AA0EBEA3F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x64.ActiveCfg = Debug|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x64.Build.0 = Debug|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x86.ActiveCfg = Debug|Win32 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x86.Build.0 = Debug|Win32 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x64.ActiveCfg = Release|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x64.Build.0 = Release|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x86.ActiveCfg = Release|Win32 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Conways/Conways.vcxproj b/Conways/Conways.vcxproj new file mode 100644 index 0000000..4fe71f9 --- /dev/null +++ b/Conways/Conways.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {9035B83C-F117-480E-9DEB-435AA0EBEA3F} + Win32Proj + Conways + 8.1 + Conways + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\lib;$(LibraryPath) + + + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + false + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\lib;$(LibraryPath) + + + false + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;%(AdditionalIncludeDirectories) + + + Console + true + sfml-graphics-d.lib;sfml-audio-d.lib;sfml-network-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + sfml-graphics-d.lib;sfml-audio-d.lib;sfml-network-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + sfml-graphics.lib;sfml-audio.lib;sfml-network.lib;sfml-system.lib;sfml-window.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + sfml-graphics.lib;sfml-audio.lib;sfml-network.lib;sfml-system.lib;sfml-window.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file diff --git a/Conways/Conways.vcxproj.filters b/Conways/Conways.vcxproj.filters new file mode 100644 index 0000000..f77c336 --- /dev/null +++ b/Conways/Conways.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/Conways/Node.cpp b/Conways/Node.cpp new file mode 100644 index 0000000..a484de4 --- /dev/null +++ b/Conways/Node.cpp @@ -0,0 +1,87 @@ +#include "Node.h" + + + +Node::Node(sf::Vector2i position_) { + position = position_; + curr_state = false; + next_state = false; +} + + +Node::~Node() { +} + +void Node::Revive() { + next_state = true; + curr_state = true; +} + +bool Node::CurrentState() { + return curr_state; +} + +void Node::ShiftState() { + curr_state = next_state; +} + +void Node::Update(std::vector *node_vec) { + + int neighbors = 0; + // x , y + 1 + // x + 1 , y + // x , y - 1 + // x - 1 , y + if (position.x < x_bound - 1 && position.x > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x, position.y + 1))).CurrentState()) + neighbors++; + } + if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y < y_bound - 1 && position.y > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y))).CurrentState()) + neighbors++; + } + if (position.x < x_bound - 1 && position.x > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x, position.y - 1))).CurrentState()) + neighbors++; + } + if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y < y_bound - 1 && position.y > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y))).CurrentState()) + neighbors++; + } + + // x + 1, y + 1 + // x + 1, y - 1 + // x - 1, y + 1 + // x - 1, y - 1 + + if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y + 1))).CurrentState()) + neighbors++; + } + if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y - 1))).CurrentState()) + neighbors++; + } + if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y + 1))).CurrentState()) + neighbors++; + } + if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { + if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y - 1))).CurrentState()) + neighbors++; + } + + if (neighbors == 3 || (neighbors == 2 && curr_state)) { + next_state = true; + } + else + next_state = false; +} + +sf::Vector2i Node::linear_to_multi(int position_) { + return sf::Vector2i(position_ % x_bound, position_ / x_bound); +} + +int Node::multi_to_linear(sf::Vector2i position) { + return position.x * x_bound + position.y; +} diff --git a/Conways/Node.h b/Conways/Node.h new file mode 100644 index 0000000..30866e6 --- /dev/null +++ b/Conways/Node.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +class Node { +public: + static const int x_bound = 150; + static const int y_bound = 150; + + Node(sf::Vector2i position_); + ~Node(); + + void Revive(); + bool CurrentState(); + void ShiftState(); + void Update(std::vector *node_vec); + +private: + + sf::Vector2i linear_to_multi(int position_); + int multi_to_linear(sf::Vector2i position); + sf::Vector2i position; + bool curr_state; + bool next_state; +}; + diff --git a/Conways/main.cpp b/Conways/main.cpp new file mode 100644 index 0000000..6fbaecd --- /dev/null +++ b/Conways/main.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include "Node.h" + +const int WINDOW_X = 600; +const int WINDOW_Y = 800; + +float elap_time() { + static __int64 start = 0; + static __int64 frequency = 0; + + if (start == 0) { + QueryPerformanceCounter((LARGE_INTEGER*)&start); + QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); + return 0.0f; + } + + __int64 counter = 0; + QueryPerformanceCounter((LARGE_INTEGER*)&counter); + return (float)((counter - start) / double(frequency)); +} + +int main() { + + std::mt19937 rng(time(NULL)); + std::uniform_int_distribution rgen(0, 4); + + std::vector node_vec; + + for (int x = 0; x < Node::x_bound; x++) { + for (int y = 0; y < Node::y_bound; y++) { + node_vec.push_back(Node(sf::Vector2i(x, y))); + if (rgen(rng) == 1) { + node_vec.at(node_vec.size() - 1).Revive(); + } + } + } + + + for (int i = 0; i < node_vec.size(); i++) { + node_vec.at(i).ShiftState(); + } + + sf::RectangleShape live_node; + sf::RectangleShape dead_node; + + live_node.setFillColor(sf::Color::Blue); + dead_node.setFillColor(sf::Color::Green); + + live_node.setSize(sf::Vector2f(WINDOW_X / Node::x_bound, WINDOW_Y / Node::y_bound)); + dead_node.setSize(sf::Vector2f(WINDOW_X / Node::x_bound, WINDOW_Y / Node::y_bound)); + + + + + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Classic Games"); + + float step_size = 0.0005f; + double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0; + + + while (window.isOpen()) { + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) + window.close(); + } + + elapsed_time = elap_time(); + delta_time = elapsed_time - current_time; + current_time = elapsed_time; + if (delta_time > 0.02f) + delta_time = 0.02f; + accumulator_time += delta_time; + + while ((accumulator_time - step_size) >= step_size) { + accumulator_time -= step_size; + + } + + window.clear(sf::Color::Black); + + + + for (int i = 0; i < node_vec.size(); i++) { + node_vec.at(i).Update(&node_vec); + } + + for (int i = 0; i < node_vec.size(); i++) { + node_vec.at(i).ShiftState(); + } + + for (int i = 0; i < node_vec.size(); i++) { + if (node_vec.at(i).CurrentState() == true) { + live_node.setPosition((i % Node::x_bound) * live_node.getGlobalBounds().width, (i / Node::x_bound) * live_node.getGlobalBounds().height); + window.draw(live_node); + } + else { + //dead_node.setPosition(i % Node::x_bound * dead_node.getGlobalBounds().width, i / Node::x_bound * dead_node.getGlobalBounds().height); + //window.draw(live_node); + } + } + + window.display(); + + + + } + return 0; + +} + diff --git a/Conways/~AutoRecover.Conways.vcxproj b/Conways/~AutoRecover.Conways.vcxproj new file mode 100644 index 0000000..4fe71f9 --- /dev/null +++ b/Conways/~AutoRecover.Conways.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {9035B83C-F117-480E-9DEB-435AA0EBEA3F} + Win32Proj + Conways + 8.1 + Conways + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\lib;$(LibraryPath) + + + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + false + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\lib;$(LibraryPath) + + + false + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;%(AdditionalIncludeDirectories) + + + Console + true + sfml-graphics-d.lib;sfml-audio-d.lib;sfml-network-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + sfml-graphics-d.lib;sfml-audio-d.lib;sfml-network-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx32\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + sfml-graphics.lib;sfml-audio.lib;sfml-network.lib;sfml-system.lib;sfml-window.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + sfml-graphics.lib;sfml-audio.lib;sfml-network.lib;sfml-system.lib;sfml-window.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file