summaryrefslogtreecommitdiff
path: root/cube.cpp
diff options
context:
space:
mode:
authorOlivier Gayot <og@satcom1.com>2017-01-12 18:01:01 +0100
committerOlivier Gayot <og@satcom1.com>2017-01-12 18:01:01 +0100
commit43d8434bbc47a94f1505374398698b199a34fa08 (patch)
treee14b882b29713449fa2d4b1ae4b489c09297437e /cube.cpp
first commitHEADmaster
Signed-off-by: Olivier Gayot <og@satcom1.com>
Diffstat (limited to 'cube.cpp')
-rw-r--r--cube.cpp194
1 files changed, 194 insertions, 0 deletions
diff --git a/cube.cpp b/cube.cpp
new file mode 100644
index 0000000..dcecd72
--- /dev/null
+++ b/cube.cpp
@@ -0,0 +1,194 @@
+#include "cube.h"
+
+std::string
+to_string(color c)
+{
+ switch (c) {
+ case color::none:
+ return "none";
+ case color::white:
+ return "white";
+ case color::yellow:
+ return "yellow";
+ case color::orange:
+ return "orange";
+ case color::red:
+ return "red";
+ case color::blue:
+ return "blue";
+ case color::green:
+ return "green";
+ default:
+ return "error";
+ }
+}
+
+abstract_face::abstract_face(const abstract_cube &parent):
+ _parent_cube(parent)
+{ }
+
+face1::face1(const cube1 &parent, color c):
+ abstract_face(parent), _color(c)
+{ }
+
+std::string
+face1::to_string() const
+{
+ return std::string("'") + ::to_string(_color) + "'";
+}
+
+face3::face3(const cube3 &parent):
+ abstract_face(parent)
+{ }
+
+std::string
+face3::to_string() const
+{
+ std::ostringstream ss;
+
+ ss << "{" << std::endl;
+ ss << " " << _faces[0]->to_string() << ",";
+ ss << " " << _faces[1]->to_string() << ",";
+ ss << " " << _faces[2]->to_string() << ",";
+ ss << std::endl;
+ ss << " " << _faces[3]->to_string() << "," << std::endl;
+ ss << " " << _faces[4]->to_string() << ",";
+ ss << " " << _faces[5]->to_string() << ",";
+ ss << std::endl;
+ ss << " " << _faces[6]->to_string() << "," << std::endl;
+ ss << " " << _faces[7]->to_string() << ",";
+ ss << " " << _faces[8]->to_string() << ",";
+ ss << "}" << std::endl;
+
+ return ss.str();
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::front() const
+{
+ return _faces[0];
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::back() const
+{
+ return _faces[1];
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::left() const
+{
+ return _faces[2];
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::right() const
+{
+ return _faces[3];
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::up() const
+{
+ return _faces[4];
+}
+
+const std::shared_ptr<abstract_face>
+abstract_cube::down() const
+{
+ return _faces[5];
+}
+
+void
+abstract_cube::flip_right()
+{
+ /* front = left */
+ /* left = back */
+ /* back = right */
+ /* right = front */
+
+ std::shared_ptr<abstract_face> front_save(_faces[0]);
+
+ _faces[0] = _faces[2];
+ _faces[2] = _faces[1];
+ _faces[1] = _faces[3];
+ _faces[3] = front_save;
+}
+
+void
+abstract_cube::flip_left()
+{
+ /* front = right */
+ /* right = back */
+ /* back = left */
+ /* left = front */
+
+ std::shared_ptr<abstract_face> front_save(_faces[0]);
+
+ _faces[0] = _faces[3];
+ _faces[3] = _faces[1];
+ _faces[1] = _faces[2];
+ _faces[2] = front_save;
+}
+
+void
+abstract_cube::flip_up()
+{
+ /* front = down */
+ /* down = back */
+ /* back = up */
+ /* up = front */
+
+ std::shared_ptr<abstract_face> front_save(_faces[0]);
+
+ _faces[0] = _faces[5];
+ _faces[5] = _faces[1];
+ _faces[1] = _faces[4];
+ _faces[4] = front_save;
+}
+
+void
+abstract_cube::flip_down()
+{
+ /* front = up */
+ /* up = back */
+ /* back = down */
+ /* down = front */
+
+ std::shared_ptr<abstract_face> front_save(_faces[0]);
+
+ _faces[0] = _faces[4];
+ _faces[4] = _faces[1];
+ _faces[1] = _faces[5];
+ _faces[5] = front_save;
+}
+
+std::string
+abstract_cube::to_string() const
+{
+ std::ostringstream ss;
+
+ ss << "{" << std::endl;
+ ss << " 'front': " << front()->to_string() << "," << std::endl;
+ ss << " 'back': " << back()->to_string() << "," << std::endl;
+ ss << " 'left': " << left()->to_string() << "," << std::endl;
+ ss << " 'right': " << right()->to_string() << "," << std::endl;
+ ss << " 'up': " << up()->to_string() << "," << std::endl;
+ ss << " 'down': " << down()->to_string() << std::endl;
+ ss << "}" << std::endl;
+
+ return ss.str();
+}
+
+cube3::cube3(color, color, color, color, color, color):
+ abstract_cube()
+{
+ /* TODO use the colors to set everything. */
+
+ _faces[0].reset(new face3(*this));
+ _faces[1].reset(new face3(*this));
+ _faces[2].reset(new face3(*this));
+ _faces[3].reset(new face3(*this));
+ _faces[4].reset(new face3(*this));
+ _faces[5].reset(new face3(*this));
+}