summaryrefslogtreecommitdiff
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
first commitHEADmaster
Signed-off-by: Olivier Gayot <og@satcom1.com>
-rw-r--r--cube.cpp194
-rw-r--r--cube.h129
2 files changed, 323 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));
+}
diff --git a/cube.h b/cube.h
new file mode 100644
index 0000000..690fe86
--- /dev/null
+++ b/cube.h
@@ -0,0 +1,129 @@
+#pragma once
+
+#include <sstream>
+#include <memory>
+#include <unordered_map>
+
+enum class color {
+ none,
+
+ white,
+ yellow,
+ orange,
+ red,
+ blue,
+ green,
+};
+
+std::string to_string(color c);
+
+class abstract_cube;
+
+class abstract_face {
+public:
+ abstract_face(const abstract_cube &);
+
+ virtual std::string to_string() const = 0;
+
+private:
+ const abstract_cube &_parent_cube;
+};
+
+class cube1;
+
+class face1 final: public abstract_face {
+public:
+ face1(const cube1 &parent, color);
+
+ std::string to_string() const;
+
+private:
+ const color _color;
+};
+
+class cube3;
+
+class face3 final: public abstract_face {
+public:
+ face3(const cube3 &parent);
+
+ std::string to_string() const;
+
+private:
+ std::array<std::shared_ptr<face1>, 9> _faces;
+};
+
+class abstract_cube {
+public:
+ void flip_right();
+ void flip_left();
+ void flip_up();
+ void flip_down();
+
+ /* Return a new face. */
+ const std::shared_ptr<abstract_face> front() const;
+ const std::shared_ptr<abstract_face> back() const;
+ const std::shared_ptr<abstract_face> left() const;
+ const std::shared_ptr<abstract_face> right() const;
+ const std::shared_ptr<abstract_face> up() const;
+ const std::shared_ptr<abstract_face> down() const;
+
+ std::string to_string() const;
+
+protected:
+ abstract_cube() = default;
+
+ std::array<std::shared_ptr<abstract_face>, 6> _faces;
+};
+
+class cube1 final: public abstract_cube {
+};
+
+class cube3 final: public abstract_cube {
+public:
+ cube3(color, color, color, color, color, color);
+
+ cube3(const cube3 &) = default;
+ cube3(cube3 &&) = default;
+
+ /* Front face 90 deg clockwise. */
+ void f();
+ /* Front face 90 deg counter-clockwise. */
+ void F();
+
+ /* Back face 90 deg clockwise. */
+ void b();
+ /* Back face 90 deg counter-clockwise. */
+ void B();
+
+ /* Left face 90 deg clockwise. */
+ void l();
+ /* Left face 90 deg counter-clockwise. */
+ void L();
+
+ /* Right face 90 deg clockwise. */
+ void r();
+ /* Right face 90 deg counter-clockwise. */
+ void R();
+
+ /* Up face 90 deg clockwise. */
+ void u();
+ /* Up face 90 deg counter-clockwise. */
+ void U();
+
+ /* Down face 90 deg clockwise. */
+ void d();
+ /* Down face 90 deg counter-clockwise. */
+ void D();
+
+ /*
+ * +---+---+---+
+ * | 0 | 1 | 2 |
+ * +---+---+---+
+ * | 3 | 4 | 5 |
+ * +---+---+---+
+ * | 6 | 7 | 8 |
+ * +---+---+---+
+ */
+};
+