├── RCube
├── Images
│ ├── MoveEstimate.PNG
│ └── MovesPerPiece.PNG
├── CMakeLists.txt
├── CubeViewer.h
├── RCube.vcxproj.filters
├── CubeViewer.cpp
├── Source.cpp
├── Face.cpp
├── TinyPngOut.hpp
├── Face.h
├── TinyPngOut.cpp
├── RCube.vcxproj
├── Cube.h
└── Cube.cpp
├── CMakeLists.txt
├── RCube.sln
├── .gitattributes
├── README.md
├── .gitignore
└── LICENSE
/RCube/Images/MoveEstimate.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShellPuppy/RCube/HEAD/RCube/Images/MoveEstimate.PNG
--------------------------------------------------------------------------------
/RCube/Images/MovesPerPiece.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ShellPuppy/RCube/HEAD/RCube/Images/MovesPerPiece.PNG
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.16)
2 | project(RCube VERSION 0.1 LANGUAGES CXX)
3 | add_subdirectory(RCube)
4 |
--------------------------------------------------------------------------------
/RCube/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_executable(RCube
2 | Cube.cpp
3 | Cube.h
4 | CubeViewer.cpp
5 | CubeViewer.h
6 | Face.cpp
7 | Face.h
8 | Source.cpp
9 | TinyPngOut.cpp
10 | TinyPngOut.hpp
11 | )
12 |
--------------------------------------------------------------------------------
/RCube/CubeViewer.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 |
4 | #include "Face.h"
5 | #include "TinyPngOut.hpp" //https://www.nayuki.io/page/tiny-png-output
6 |
7 |
8 |
9 | class CubeViewer
10 | {
11 |
12 | //Collor palette
13 | const static uint8_t palette[6][3];
14 |
15 | public:
16 |
17 | static void ExportFaceDiagram(Face& face,std::string FileName, int ImageWidth, bool IncludeGridlines);
18 |
19 |
20 |
21 | };
22 |
23 |
--------------------------------------------------------------------------------
/RCube.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.136
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RCube", "RCube\RCube.vcxproj", "{471B88D1-6DCC-4FCF-85FF-3E1D293395D0}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Debug|x64.ActiveCfg = Debug|x64
17 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Debug|x64.Build.0 = Debug|x64
18 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Debug|x86.ActiveCfg = Debug|Win32
19 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Debug|x86.Build.0 = Debug|Win32
20 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Release|x64.ActiveCfg = Release|x64
21 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Release|x64.Build.0 = Release|x64
22 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Release|x86.ActiveCfg = Release|Win32
23 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {BFA51AEB-063E-49AA-A4CD-2210C6CE3482}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/RCube/RCube.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 | Source Files
26 |
27 |
28 | Source Files
29 |
30 |
31 | Source Files
32 |
33 |
34 |
35 |
36 | Header Files
37 |
38 |
39 | Header Files
40 |
41 |
42 | Header Files
43 |
44 |
45 | Header Files
46 |
47 |
48 |
--------------------------------------------------------------------------------
/RCube/CubeViewer.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "CubeViewer.h"
3 | #include
4 |
5 | //Color Palette
6 | const uint8_t CubeViewer::palette[6][3] =
7 | {
8 | {0x00,0xFF,0x00}, //Green
9 | {0xFF,0x00,0x00}, //Red
10 | {0x00,0x00,0xFF}, //Blue
11 | {0xFF,0x80,0x40}, //Orange
12 | {0xFF,0xFF,0xFF}, //White
13 | {0xFF,0xFF,0x00} //Yellow
14 | };
15 |
16 | void CubeViewer::ExportFaceDiagram(Face& face, std::string FileName, int ImageWidth, bool IncludeGridlines)
17 | {
18 | if (ImageWidth <= 0) return;
19 |
20 | int pixelcount = ImageWidth * ImageWidth; //number of pixels in the output image
21 | int datacount = 3 * pixelcount; //Number of bytes in the output image
22 |
23 | uint8_t* pixels = new uint8_t[datacount];
24 |
25 | //Disable gridlines if the image is too small
26 | if ((uint)ImageWidth <= face.RowSize * 2) IncludeGridlines = false;
27 |
28 | //Compute scale between cube size and image size
29 | double wp = ((double)face.RowSize) / ((double)ImageWidth);
30 |
31 | double intpart;
32 | double fpx, fpy;
33 | uint px, py;
34 | double linewidth = .02;
35 | int colorid;
36 | int iptr = 0;
37 |
38 | for (int y = ImageWidth-1; y >=0 ; --y)
39 | {
40 | for (int x = 0; x < ImageWidth; ++x)
41 | {
42 | iptr = 3*((ImageWidth - y - 1) * ImageWidth + x);
43 |
44 | //Compute face coordinates from image coordinates
45 | py = (int)(y * wp);
46 | px = (int)(x * wp);
47 |
48 | //Get the colorid from the face
49 | colorid = face.GetRC(py, px);
50 |
51 | pixels[iptr] = palette[colorid][0];
52 | pixels[iptr+1] = palette[colorid][1];
53 | pixels[iptr+2] = palette[colorid][2];
54 |
55 | if (IncludeGridlines)
56 | {
57 | //Figure out if the pixel hits a grid line
58 | fpx = modf((x * wp), &intpart);
59 | fpy = modf((y * wp), &intpart);
60 | if (((fpx <= linewidth) || (fpx >= (1 - linewidth)) || (fpy <= linewidth) || (fpy >= (1 - linewidth))))
61 | {
62 | pixels[iptr] = 0x00;
63 | pixels[iptr + 1] = 0x00;
64 | pixels[iptr + 2] = 0x00;
65 | }
66 | }
67 |
68 | }
69 | }
70 |
71 |
72 |
73 | //Write .png file
74 | std::ofstream out(FileName, std::ios::binary);
75 |
76 | TinyPngOut pngout(static_cast(ImageWidth), static_cast(ImageWidth), out);
77 |
78 | pngout.write(pixels, static_cast(pixelcount));
79 |
80 |
81 | delete[] pixels;
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/RCube/Source.cpp:
--------------------------------------------------------------------------------
1 | #include "Cube.h"
2 | #include "CubeViewer.h"
3 | #include
4 | #include
5 |
6 | void StartNewCube()
7 | {
8 | unsigned int n = 0; //Cube size
9 | unsigned int seed = 0; //Random seed
10 | int tmp = 0;
11 |
12 | printf("Starting a new cube\n");
13 |
14 | do
15 | {
16 | //Limit cube size from 1 to 65536
17 | printf("Cube Size (1-65536) : ");
18 | tmp = scanf("%u", &n);
19 | } while (n < 1 || n > 65536);
20 |
21 | printf("Choose a random seed: ");
22 | tmp = scanf("%u", &seed);
23 |
24 | //Create a new cube
25 | printf("Generating Cube...\n");
26 | Cube cube(n);
27 |
28 | //Scramble the cube using the seed value
29 | printf("Scrambling Cube...\n");
30 | cube.Scramble(seed);
31 |
32 | //No need to save progress for smaller cubes
33 | if (n >= 32768)
34 | {
35 | printf("Saving enabled\n");
36 | cube.SaveEnabled = true;
37 | cube.SaveCubeState();
38 | }
39 |
40 | //Print stats before solving
41 | cube.PrintStats();
42 |
43 | cube.MovesPerFrame = 0;
44 |
45 | cube.SaveEnabled = false;
46 |
47 | //Solve it!
48 | printf("Solving 3.0\n");
49 | cube.Solve();
50 |
51 | //Print stats after solving
52 | cube.PrintStats();
53 |
54 |
55 | printf("Done\n");
56 | tmp = scanf("%i", &tmp);
57 | }
58 |
59 | void LoadExistingCube()
60 | {
61 | Cube cube;
62 |
63 | //Load the cube from the save state
64 | cube.LoadCubeState();
65 |
66 | //Continue solving
67 | cube.Solve();
68 |
69 | cube.PrintStats();
70 |
71 | printf("Done\n");
72 | int tmp = scanf("%i", &tmp);
73 | }
74 |
75 | void ExampleImageOutput()
76 | {
77 | //Create a small cube
78 | Cube cube(32);
79 |
80 | //Scramble the cube with a random seed
81 | cube.Scramble(1234);
82 |
83 | //Create instance of a cube viewer
84 |
85 | //Export images of each face
86 |
87 | //Specify cube face, filename, image size, include gridlines
88 | CubeViewer::ExportFaceDiagram(cube.faces[0], "Front Face.png", 1024, true);
89 | CubeViewer::ExportFaceDiagram(cube.faces[1], "Right Face.png", 1024, true);
90 | CubeViewer::ExportFaceDiagram(cube.faces[2], "Back Face.png", 1024, true);
91 | CubeViewer::ExportFaceDiagram(cube.faces[3], "Left Face.png", 1024, true);
92 | CubeViewer::ExportFaceDiagram(cube.faces[4], "Top Face.png", 1024, true);
93 | CubeViewer::ExportFaceDiagram(cube.faces[5], "Bottom Face.png", 1024, true);
94 | }
95 |
96 |
97 | void Omega()
98 | {
99 |
100 | Cube* cube = nullptr;
101 |
102 | std::ofstream out("kvalue1.csv", std::ios::app);
103 |
104 | for (int i = 0; i < 100; i++)
105 | {
106 | for (int n = 4; n <= 2048; n*=2)
107 | {
108 | //create cube(n)
109 | cube = new Cube(n);
110 |
111 | cube->Scramble(i);
112 |
113 | cube->Solve();
114 |
115 | printf("%i : %.7f\n", n, cube->Hours * 3600.0);
116 |
117 | out << n << "," << cube->MoveCount << "," << (cube->Hours * 3600.0) << std::endl;
118 |
119 | delete cube;
120 |
121 | out.flush();
122 | }
123 | }
124 |
125 | out.close();
126 |
127 |
128 | }
129 |
130 | int main()
131 | {
132 | //Omega();
133 |
134 | //Start a new cube and solve it
135 | StartNewCube();
136 |
137 | //Uncomment to load an existing cube from a save state
138 | //LoadExistingCube();
139 |
140 | //Uncomment to run the example image output
141 | //ExampleImageOutput();
142 |
143 | return EXIT_SUCCESS;
144 | }
--------------------------------------------------------------------------------
/RCube/Face.cpp:
--------------------------------------------------------------------------------
1 | #include "Face.h"
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | Face::Face()
13 | {
14 | data = nullptr;
15 | RowSize = 0;
16 | MemRowSize = 0;
17 | BS = 0;
18 | DataSize = 0;
19 | orientation = 0;
20 | RotatefaceCW(0);
21 | }
22 |
23 | void Face::Initialize(byte index, uint rsize, uint msize)
24 | {
25 | //if(rsize > 1024) printf("Initializing Face %i\n", (int)index);
26 |
27 | //Index of this face
28 | id = index;
29 |
30 | //Length of a row of memory (must be a power of 2)
31 | MemRowSize = msize;
32 |
33 | //Length of a side of the cube
34 | RowSize = rsize;
35 | R1 = RowSize - 1;
36 |
37 | //Compute the bit-shift size for the length of a row
38 | BS = (int)log2(MemRowSize);
39 |
40 | //Compute the actual size of the array in bytes
41 | DataSize = ((uint64)MemRowSize) * ((uint64)MemRowSize);
42 |
43 | //Initialize array
44 | data = new byte[DataSize];
45 |
46 | //Failed to allocate array
47 | if (data == nullptr)
48 | {
49 | printf("ERROR ALLOCATING ARRAY");
50 | }
51 |
52 | //Set all values on this face to match the faceindex
53 | Paint(index);
54 |
55 | }
56 |
57 | //Verify the number of pieces on this face match what was saved and loaded from file
58 | bool Face::VerifyCounts()
59 | {
60 | uint Counts[6];
61 | GetCounts(Counts);
62 |
63 | for (int i = 0; i < 6; ++i)
64 | {
65 | printf("Face %i : Color %i : %u %u\n",id, i, Counts[i], PieceCount[i]);
66 | if (Counts[i] != PieceCount[i]) return false;
67 | }
68 |
69 | return true;
70 | }
71 |
72 | void Face::SaveFaceState()
73 | {
74 | printf("Saving face %i\n", id);
75 |
76 | //GetCounts(PieceCount);
77 | std::string name = "face" + std::to_string(id) + ".bin";
78 | std::ofstream out(name, std::ios::out | std::ios::binary);
79 | out.write((char*)this, sizeof(Face));
80 | out.write((char*)data, DataSize);
81 | out.flush();
82 | out.close();
83 | }
84 |
85 | void Face::LoadFaceState(byte faceid)
86 | {
87 | printf("Loading face %i\n", faceid);
88 | std::string name = "face" + std::to_string(faceid) + ".bin";
89 | std::ifstream in(name, std::ios::in | std::ios::binary);
90 | in.read((char*)this, sizeof(Face));
91 | data = new byte[DataSize];
92 | in.read((char*)data, DataSize);
93 | in.close();
94 | }
95 |
96 | //Paint the entire face this color
97 | void Face::Paint(byte color)
98 | {
99 | std::memset(data, color, DataSize);
100 | }
101 |
102 | //Counts the number of pieces on a face of a spefic color
103 | uint Face::Count(byte color)
104 | {
105 | uint result = 0;
106 | for(uint r =0;rid) return false;
135 | }
136 | }
137 |
138 | return true;
139 | }
140 |
141 | Face::~Face()
142 | {
143 | //Cleanup
144 | if (data != nullptr) delete[] data;
145 |
146 | }
147 |
--------------------------------------------------------------------------------
/RCube/TinyPngOut.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Tiny PNG Output (C++)
3 | *
4 | * Copyright (c) 2018 Project Nayuki
5 | * https://www.nayuki.io/page/tiny-png-output
6 | *
7 | * This program is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with this program (see COPYING.txt and COPYING.LESSER.txt).
19 | * If not, see .
20 | */
21 |
22 | #pragma once
23 |
24 | #include
25 | #include
26 | #include
27 |
28 |
29 | /*
30 | * Takes image pixel data in raw RGB8.8.8 format and writes a PNG file to a byte output stream.
31 | */
32 | class TinyPngOut final {
33 |
34 | /*---- Fields ----*/
35 |
36 | // Immutable configuration
37 | private: std::uint32_t width; // Measured in pixels
38 | private: std::uint32_t height; // Measured in pixels
39 | private: std::uint32_t lineSize; // Measured in bytes, equal to (width * 3 + 1)
40 |
41 | // Running state
42 | private: std::ostream &output;
43 | private: std::uint32_t positionX; // Next byte index in current line
44 | private: std::uint32_t positionY; // Line index of next byte
45 | private: std::uint32_t uncompRemain; // Number of uncompressed bytes remaining
46 | private: std::uint16_t deflateFilled; // Bytes filled in the current block (0 <= n < DEFLATE_MAX_BLOCK_SIZE)
47 | private: std::uint32_t crc; // Primarily for IDAT chunk
48 | private: std::uint32_t adler; // For DEFLATE data within IDAT
49 |
50 |
51 |
52 | /*---- Public constructor and method ----*/
53 |
54 | /*
55 | * Creates a PNG writer with the given width and height (both non-zero) and byte output stream.
56 | * TinyPngOut will leave the output stream still open once it finishes writing the PNG file data.
57 | * Throws an exception if the dimensions exceed certain limits (e.g. w * h > 700 million).
58 | */
59 | public: explicit TinyPngOut(std::uint32_t w, std::uint32_t h, std::ostream &out);
60 |
61 |
62 | /*
63 | * Writes 'count' pixels from the given array to the output stream. This reads count*3
64 | * bytes from the array. Pixels are presented from top to bottom, left to right, and with
65 | * subpixels in RGB order. This object keeps track of how many pixels were written and
66 | * various position variables. It is an error to write more pixels in total than width*height.
67 | * Once exactly width*height pixels have been written with this TinyPngOut object,
68 | * there are no more valid operations on the object and it should be discarded.
69 | */
70 | public: void write(const std::uint8_t pixels[], size_t count);
71 |
72 |
73 |
74 | /*---- Private checksum methods ----*/
75 |
76 | // Reads the 'crc' field and updates its value based on the given array of new data.
77 | private: void crc32(const std::uint8_t data[], size_t len);
78 |
79 |
80 | // Reads the 'adler' field and updates its value based on the given array of new data.
81 | private: void adler32(const std::uint8_t data[], size_t len);
82 |
83 |
84 |
85 | /*---- Private utility members ----*/
86 |
87 | private: template
88 | void write(const std::uint8_t (&data)[N]) {
89 | output.write(reinterpret_cast(data), sizeof(data));
90 | }
91 |
92 |
93 | private: static void putBigUint32(std::uint32_t val, std::uint8_t array[4]);
94 |
95 |
96 | private: static constexpr std::uint16_t DEFLATE_MAX_BLOCK_SIZE = 65535;
97 |
98 | };
99 |
--------------------------------------------------------------------------------
/RCube/Face.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 |
5 | typedef unsigned char byte;
6 | typedef unsigned int uint;
7 | typedef unsigned long long uint64;
8 |
9 | class Face
10 | {
11 | friend class Cube;
12 |
13 | uint R1; // RowSize - 1
14 | uint MemRowSize; // Memory row length (must be a power of 2) 32,64,128...65536
15 | uint BS; // bit shift = log2(memsize)
16 | uint64 DataSize; // size of data array in bytes = MemRowSize * MemRowSize
17 | byte* data; // face data
18 | int orientation; // Virtual orientation of this face [0,1,2,3] - 90 degree clockwise rotations
19 | uint PieceCount[6]; // Keep track of the number of each piece on this face (used for validating the integrity of the cube)
20 |
21 | public:
22 | uint RowSize; // cube row length
23 |
24 | byte id; //this face number (0-5)
25 |
26 | inline void RotatefaceCW(int r);
27 |
28 | inline const byte GetRC(const uint r, const uint c) const;
29 |
30 | inline const byte GetRCQ(const uint r, const uint c, int q) const;
31 |
32 | inline void SetRC(const uint r, const uint c, const byte v);
33 |
34 | void SetRCQ(const uint r, const uint c, int q, const byte v);
35 |
36 | const int GetDelta(const uint d) const;
37 |
38 | const int GetPos(const uint r, const uint c) const;
39 |
40 | void Initialize(byte index, uint rsize, uint msize);
41 |
42 | bool VerifyCounts();
43 |
44 | void SaveFaceState();
45 |
46 | void LoadFaceState(byte faceid);
47 |
48 | void Paint(byte color);
49 |
50 | uint Count(byte color);
51 |
52 | void GetCounts(uint*);
53 |
54 | bool IsFaceSolved();
55 |
56 | Face();
57 |
58 | ~Face();
59 | };
60 |
61 | //Virtually rotates this face by q * 90 degrees (this does not affect other cube faces!)
62 | inline void Face::RotatefaceCW(const int q)
63 | {
64 | orientation = (orientation + q) & 3;
65 | }
66 |
67 | //Gets the value of this face at coordinates r = row, c = column
68 | inline const byte Face::GetRC(const uint r, const uint c) const
69 | {
70 | switch (orientation)
71 | {
72 | case 0:
73 | return data[(r << BS) + c];
74 | case 1:
75 | return data[(c << BS) + (R1 - r)];
76 | case 2:
77 | return data[((R1 - r) << BS) + (R1 - c)];
78 | case 3:
79 | return data[((R1 - c) << BS) + r];
80 | default:
81 | return 0;
82 | }
83 | }
84 |
85 | //Gets the value of this face at coordinates r,c + an additional rotation q (q=90 cw turn)
86 | inline const byte Face::GetRCQ(const uint r, const uint c, int q) const
87 | {
88 | q = (orientation - q) & 3;
89 |
90 | switch (q)
91 | {
92 | case 0:
93 | return data[(r << BS) + c];
94 | case 1:
95 | return data[(c << BS) + (R1 - r)];
96 | case 2:
97 | return data[((R1 - r) << BS) + (R1 - c)];
98 | case 3:
99 | return data[((R1 - c) << BS) + r];
100 | }
101 |
102 | return 0;
103 |
104 | }
105 |
106 | //Sets the value of this face at coordinates r = row, c = column
107 | inline void Face::SetRC(const uint r, const uint c, const byte v)
108 | {
109 | switch (orientation)
110 | {
111 | case 0:
112 | data[(r << BS) + c] = v;
113 | return;
114 | case 1:
115 | data[(c << BS) + (R1 - r)] = v;
116 | return;
117 | case 2:
118 | data[((R1 - r) << BS) + (R1 - c)] = v;
119 | return;
120 | case 3:
121 | data[((R1 - c) << BS) + r] = v;
122 | return;
123 | }
124 | }
125 |
126 | //Sets the value of this face at coordinates r = row, c = column
127 | inline void Face::SetRCQ(const uint r, const uint c, int q, const byte v)
128 | {
129 | q = (orientation - q) & 3;
130 |
131 | switch (q)
132 | {
133 | case 0:
134 | data[(r << BS) + c] = v;
135 | return;
136 | case 1:
137 | data[(c << BS) + (R1 - r)] = v;
138 | return;
139 | case 2:
140 | data[((R1 - r) << BS) + (R1 - c)] = v;
141 | return;
142 | case 3:
143 | data[((R1 - c) << BS) + r] = v;
144 | return;
145 | }
146 | }
147 |
148 | //Returns the offset needed to traverse the data array in a given direction.
149 | inline const int Face::GetDelta(const uint d) const
150 | {
151 | switch (d)
152 | {
153 | case 0: //Right
154 | return GetPos(0, 1) - GetPos(0, 0);
155 | case 1: //Down
156 | return GetPos(0, 0) - GetPos(1, 0);
157 | case 2: //left
158 | return GetPos(0, 0) - GetPos(0, 1);
159 | case 3: // Up
160 | return GetPos(1, 0) - GetPos(0, 0);
161 | default:
162 | return 0;
163 | }
164 |
165 | }
166 |
167 | //Returns the array index of data that corrisponds to row (r) and column (c)
168 | inline const int Face::GetPos(const uint r, const uint c) const
169 | {
170 | switch (orientation)
171 | {
172 | case 0:
173 | return (r << BS) + c;
174 | case 1:
175 | return (c << BS) + (R1 - r);
176 | case 2:
177 | return (((R1 - r) << BS) + (R1 - c));
178 | case 3:
179 | return ((R1 - c) << BS) + r;
180 | default:
181 | return 0;
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RCube
2 |
3 | Rubik's Cube solver for very large cubes.
4 |
5 | Solves any cube of any size from 1 to 65536. The code can be easily modified to handle even larger cubes. However the primary limitation is the amount of memory on a system and the length of time it takes to solve.
6 | Solve time grows ~N^2 which means it takes 4-5 times longer to solve a cube that is 2x larger in each dimension.
7 |
8 | A 1024 layered cube can be solved in about 1.5 seconds.
9 | A 16384 layered cube can be solved in about 20 minutes.
10 |
11 | [Youtube: Solving 65536 Layers](https://youtu.be/y7J3sNR8aC4 )
12 | [Youtube: Solving 32768 Layers](https://www.youtube.com/watch?v=xOJtLb_rPVg)
13 |
14 | [Image of the front face of a 32768 cube](https://www.easyzoom.com/image/146053)
15 |
16 | ## Solve Method
17 | Solves the centers then corners then edges
18 |
19 | ### Centers
20 | The centers are solved in 15 stages where each stage moves all the pieces of a certain color from one face to the desired face. For example: a stage moves all green pieces on the white face to the green face. Repeat this for all colors and faces.
21 |
22 | The solver uses the commutator described [here](https://www.jaapsch.net/puzzles/cube7.htm) which can commute center pieces from one quadrant of a face to a quandrant on another face. A very important property of this commutator is that can be modified to move many pieces in single row at the same time. For very large cubes this means it can move hundreds if not thousands of pieces in a single operation. The average number of moves k = (2 * P + 5) / P where P is the number of pieces that can be moved per operation. K quickly approaches 2 as the cube size increases.
23 |
24 | ### Corners
25 | The corners are solved using a basic brute force method of moving the corner into place and then rotating until the faces were oriented correctly. Will Smith can explain: https://www.youtube.com/watch?v=WBzkDrC9vQs
26 |
27 | ### Edges
28 | The edges are solved by moving every pair of edges to the front face, then swapping desired pieces from the left edge to the right edge. A number of functions where needed to fix or prevent parity issues.
29 |
30 | ### Face Rotation Optomization
31 | Face rotations are essentially free. Instead of moving all the pieces on a given face (N^2 pieces) to perform a rotation, the solver simply changes the coordinate system that is uses to read/write to the face. This saves an enormous amount of data swapping.
32 |
33 | ```c++
34 | //Virtually rotate this face by q * 90 degrees
35 | inline void Face::RotatefaceCW(const int q)
36 | {
37 | orientation = (orientation + q) & 3;
38 | }
39 | //Gets the value of this face at coordinates r = row, c = column
40 | inline const byte Face::GetRC(const uint r, const uint c) const
41 | {
42 | switch (orientation) {
43 | case 0:
44 | return data[(r << BS) + c];
45 | case 1:
46 | return data[(c << BS) + (R1 - r)];
47 | case 2:
48 | return data[((R1 - r) << BS) + (R1 - c)];
49 | case 3:
50 | return data[((R1 - c) << BS) + r];
51 | default:
52 | return 0;
53 | }
54 | }
55 | ```
56 |
57 | ## Image Export
58 | This code includes a simple image exporter that is used render each face into an image.
59 |
60 | https://www.nayuki.io/page/tiny-png-output
61 |
62 | https://github.com/nayuki/Nayuki-web-published-code
63 |
64 | Tiny PNG Output is a small standalone library, available in C and C++, which takes RGB8.8.8 pixels and writes a PNG file.
65 |
66 | ## Total Move Estimation
67 | The total number of moves can be estimated using a simple formula. Assuming the cube is sufficiently randomized, we can expect the first face to be 1/6 solved and therefore 5/6 of the pieces will have to be moved. The second face will be 1/5 solved and require 4/5 of the pieces to be moved. The third face 3/4, Fourth face 2/3, Fifth face 1/2 and the last face will be completely solved. The average number of moves (k) to move single piece can be estimated experimentally as ~2.1. The value of k decreases as the size of the cube increases. The value can never be less than 2 since the commutator requires each piece to be moved at least 2 times.
68 |
69 | 
70 |
71 | ## Efficiency
72 | This algorithm is optimized for very large cubes. However it is terrible for small cubes. The primary optimizations were focused on solving the centers as fast as possible and no consideration was given to solving the edges. However the size of the edges are insignificant compared to the centers as the cube size increases. The graph below shows how the average number of moves per piece decreases with larger cubes. *Note: this is not a graph of k. It is a graph of (Total Moves) / (Number of Pieces)*
73 |
74 |
75 | 
76 |
77 | ## Build and Run
78 | - Install CMake or an IDE with CMake support
79 | - Clone this repository and open directory in a terminal
80 | ```sh
81 | cmake -B build -S .
82 | cd build
83 | cmake --build .
84 | RCube/RCube
85 | ```
86 |
--------------------------------------------------------------------------------
/RCube/TinyPngOut.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Tiny PNG Output (C++)
3 | *
4 | * Copyright (c) 2018 Project Nayuki
5 | * https://www.nayuki.io/page/tiny-png-output
6 | *
7 | * This program is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * This program is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with this program (see COPYING.txt and COPYING.LESSER.txt).
19 | * If not, see .
20 | */
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include "TinyPngOut.hpp"
27 |
28 | using std::uint8_t;
29 | using std::uint16_t;
30 | using std::uint32_t;
31 | using std::uint64_t;
32 | using std::size_t;
33 |
34 |
35 | TinyPngOut::TinyPngOut(uint32_t w, uint32_t h, std::ostream &out) :
36 | // Set most of the fields
37 | width(w),
38 | height(h),
39 | output(out),
40 | positionX(0),
41 | positionY(0),
42 | deflateFilled(0),
43 | adler(1) {
44 |
45 | // Check arguments
46 | if (width == 0 || height == 0)
47 | throw std::domain_error("Zero width or height");
48 |
49 | // Compute and check data siezs
50 | uint64_t lineSz = static_cast(width) * 3 + 1;
51 | if (lineSz > UINT32_MAX)
52 | throw std::length_error("Image too large");
53 | lineSize = static_cast(lineSz);
54 |
55 | uint64_t uncompRm = (uint64_t)lineSize * (uint64_t)height;
56 | if (uncompRm > UINT32_MAX)
57 | throw std::length_error("Image too large");
58 | uncompRemain = static_cast(uncompRm);
59 |
60 | uint32_t numBlocks = uncompRemain / DEFLATE_MAX_BLOCK_SIZE;
61 | if (uncompRemain % DEFLATE_MAX_BLOCK_SIZE != 0)
62 | numBlocks++; // Round up
63 | // 5 bytes per DEFLATE uncompressed block header, 2 bytes for zlib header, 4 bytes for zlib Adler-32 footer
64 | uint64_t idatSize = static_cast(numBlocks) * 5 + 6;
65 | idatSize += uncompRemain;
66 | if (idatSize > static_cast(INT32_MAX))
67 | throw std::length_error("Image too large");
68 |
69 | // Write header (not a pure header, but a couple of things concatenated together)
70 | uint8_t header[] = { // 43 bytes long
71 | // PNG header
72 | 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
73 | // IHDR chunk
74 | 0x00, 0x00, 0x00, 0x0D,
75 | 0x49, 0x48, 0x44, 0x52,
76 | 0, 0, 0, 0, // 'width' placeholder
77 | 0, 0, 0, 0, // 'height' placeholder
78 | 0x08, 0x02, 0x00, 0x00, 0x00,
79 | 0, 0, 0, 0, // IHDR CRC-32 placeholder
80 | // IDAT chunk
81 | 0, 0, 0, 0, // 'idatSize' placeholder
82 | 0x49, 0x44, 0x41, 0x54,
83 | // DEFLATE data
84 | 0x08, 0x1D,
85 | };
86 | putBigUint32(width, &header[16]);
87 | putBigUint32(height, &header[20]);
88 | putBigUint32((uint32_t)idatSize, &header[33]);
89 | crc = 0;
90 | crc32(&header[12], 17);
91 | putBigUint32(crc, &header[29]);
92 | write(header);
93 |
94 | crc = 0;
95 | crc32(&header[37], 6); // 0xD7245B6B
96 | }
97 |
98 |
99 | void TinyPngOut::write(const uint8_t pixels[], size_t count) {
100 | if (count > SIZE_MAX / 3)
101 | throw std::length_error("Invalid argument");
102 | count *= 3; // Convert pixel count to byte count
103 | while (count > 0) {
104 | if (pixels == nullptr)
105 | throw std::invalid_argument("Null pointer");
106 | if (positionY >= height)
107 | throw std::logic_error("All image pixels already written");
108 |
109 | if (deflateFilled == 0) { // Start DEFLATE block
110 | uint16_t size = DEFLATE_MAX_BLOCK_SIZE;
111 | if (uncompRemain < size)
112 | size = static_cast(uncompRemain);
113 | const uint8_t header[] = { // 5 bytes long
114 | static_cast(uncompRemain <= DEFLATE_MAX_BLOCK_SIZE ? 1 : 0),
115 | static_cast(size >> 0),
116 | static_cast(size >> 8),
117 | static_cast((size >> 0) ^ 0xFF),
118 | static_cast((size >> 8) ^ 0xFF),
119 | };
120 | write(header);
121 | crc32(header, sizeof(header) / sizeof(header[0]));
122 | }
123 | assert(positionX < lineSize && deflateFilled < DEFLATE_MAX_BLOCK_SIZE);
124 |
125 | if (positionX == 0) { // Beginning of line - write filter method byte
126 | uint8_t b[] = {0};
127 | write(b);
128 | crc32(b, 1);
129 | adler32(b, 1);
130 | positionX++;
131 | uncompRemain--;
132 | deflateFilled++;
133 |
134 | } else { // Write some pixel bytes for current line
135 | uint16_t n = DEFLATE_MAX_BLOCK_SIZE - deflateFilled;
136 | if (lineSize - positionX < n)
137 | n = static_cast(lineSize - positionX);
138 | if (count < n)
139 | n = static_cast(count);
140 | if (static_cast::type>(std::numeric_limits::max()) < std::numeric_limits::max())
141 | n = std::min(n, static_cast(std::numeric_limits::max()));
142 | assert(n > 0);
143 | output.write(reinterpret_cast(pixels), static_cast(n));
144 |
145 | // Update checksums
146 | crc32(pixels, n);
147 | adler32(pixels, n);
148 |
149 | // Increment positions
150 | count -= n;
151 | pixels += n;
152 | positionX += n;
153 | uncompRemain -= n;
154 | deflateFilled += n;
155 | }
156 |
157 | if (deflateFilled >= DEFLATE_MAX_BLOCK_SIZE)
158 | deflateFilled = 0; // End current block
159 |
160 | if (positionX == lineSize) { // Increment line
161 | positionX = 0;
162 | positionY++;
163 | if (positionY == height) { // Reached end of pixels
164 | uint8_t footer[] = { // 20 bytes long
165 | 0, 0, 0, 0, // DEFLATE Adler-32 placeholder
166 | 0, 0, 0, 0, // IDAT CRC-32 placeholder
167 | // IEND chunk
168 | 0x00, 0x00, 0x00, 0x00,
169 | 0x49, 0x45, 0x4E, 0x44,
170 | 0xAE, 0x42, 0x60, 0x82,
171 | };
172 | putBigUint32(adler, &footer[0]);
173 | crc32(&footer[0], 4);
174 | putBigUint32(crc, &footer[4]);
175 | write(footer);
176 | }
177 | }
178 | }
179 | }
180 |
181 |
182 | void TinyPngOut::crc32(const uint8_t data[], size_t len) {
183 | crc = ~crc;
184 | for (size_t i = 0; i < len; i++) {
185 | for (int j = 0; j < 8; j++) { // Inefficient bitwise implementation, instead of table-based
186 | uint32_t bit = (crc ^ (data[i] >> j)) & 1;
187 | crc = (crc >> 1) ^ ((~bit + 1) & UINT32_C(0xEDB88320));
188 | }
189 | }
190 | crc = ~crc;
191 | }
192 |
193 |
194 | void TinyPngOut::adler32(const uint8_t data[], size_t len) {
195 | uint32_t s1 = adler & 0xFFFF;
196 | uint32_t s2 = adler >> 16;
197 | for (size_t i = 0; i < len; i++) {
198 | s1 = (s1 + data[i]) % 65521;
199 | s2 = (s2 + s1) % 65521;
200 | }
201 | adler = s2 << 16 | s1;
202 | }
203 |
204 |
205 | void TinyPngOut::putBigUint32(uint32_t val, uint8_t array[4]) {
206 | for (int i = 0; i < 4; i++)
207 | array[i] = static_cast(val >> ((3 - i) * 8));
208 | }
209 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | build/
27 | [Bb]in/
28 | [Oo]bj/
29 | [Ll]og/
30 |
31 | # Visual Studio 2015/2017 cache/options directory
32 | .vs/
33 | # Uncomment if you have tasks that create the project's static files in wwwroot
34 | #wwwroot/
35 |
36 | # Visual Studio 2017 auto generated files
37 | Generated\ Files/
38 |
39 | # MSTest test Results
40 | [Tt]est[Rr]esult*/
41 | [Bb]uild[Ll]og.*
42 |
43 | # NUNIT
44 | *.VisualState.xml
45 | TestResult.xml
46 |
47 | # Build Results of an ATL Project
48 | [Dd]ebugPS/
49 | [Rr]eleasePS/
50 | dlldata.c
51 |
52 | # Benchmark Results
53 | BenchmarkDotNet.Artifacts/
54 |
55 | # .NET Core
56 | project.lock.json
57 | project.fragment.lock.json
58 | artifacts/
59 |
60 | # StyleCop
61 | StyleCopReport.xml
62 |
63 | # Files built by Visual Studio
64 | *_i.c
65 | *_p.c
66 | *_h.h
67 | *.ilk
68 | *.meta
69 | *.obj
70 | *.iobj
71 | *.pch
72 | *.pdb
73 | *.ipdb
74 | *.pgc
75 | *.pgd
76 | *.rsp
77 | *.sbr
78 | *.tlb
79 | *.tli
80 | *.tlh
81 | *.tmp
82 | *.tmp_proj
83 | *_wpftmp.csproj
84 | *.log
85 | *.vspscc
86 | *.vssscc
87 | .builds
88 | *.pidb
89 | *.svclog
90 | *.scc
91 |
92 | # Chutzpah Test files
93 | _Chutzpah*
94 |
95 | # Visual C++ cache files
96 | ipch/
97 | *.aps
98 | *.ncb
99 | *.opendb
100 | *.opensdf
101 | *.sdf
102 | *.cachefile
103 | *.VC.db
104 | *.VC.VC.opendb
105 |
106 | # Visual Studio profiler
107 | *.psess
108 | *.vsp
109 | *.vspx
110 | *.sap
111 |
112 | # Visual Studio Trace Files
113 | *.e2e
114 |
115 | # TFS 2012 Local Workspace
116 | $tf/
117 |
118 | # Guidance Automation Toolkit
119 | *.gpState
120 |
121 | # ReSharper is a .NET coding add-in
122 | _ReSharper*/
123 | *.[Rr]e[Ss]harper
124 | *.DotSettings.user
125 |
126 | # JustCode is a .NET coding add-in
127 | .JustCode
128 |
129 | # TeamCity is a build add-in
130 | _TeamCity*
131 |
132 | # DotCover is a Code Coverage Tool
133 | *.dotCover
134 |
135 | # AxoCover is a Code Coverage Tool
136 | .axoCover/*
137 | !.axoCover/settings.json
138 |
139 | # Visual Studio code coverage results
140 | *.coverage
141 | *.coveragexml
142 |
143 | # NCrunch
144 | _NCrunch_*
145 | .*crunch*.local.xml
146 | nCrunchTemp_*
147 |
148 | # MightyMoose
149 | *.mm.*
150 | AutoTest.Net/
151 |
152 | # Web workbench (sass)
153 | .sass-cache/
154 |
155 | # Installshield output folder
156 | [Ee]xpress/
157 |
158 | # DocProject is a documentation generator add-in
159 | DocProject/buildhelp/
160 | DocProject/Help/*.HxT
161 | DocProject/Help/*.HxC
162 | DocProject/Help/*.hhc
163 | DocProject/Help/*.hhk
164 | DocProject/Help/*.hhp
165 | DocProject/Help/Html2
166 | DocProject/Help/html
167 |
168 | # Click-Once directory
169 | publish/
170 |
171 | # Publish Web Output
172 | *.[Pp]ublish.xml
173 | *.azurePubxml
174 | # Note: Comment the next line if you want to checkin your web deploy settings,
175 | # but database connection strings (with potential passwords) will be unencrypted
176 | *.pubxml
177 | *.publishproj
178 |
179 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
180 | # checkin your Azure Web App publish settings, but sensitive information contained
181 | # in these scripts will be unencrypted
182 | PublishScripts/
183 |
184 | # NuGet Packages
185 | *.nupkg
186 | # The packages folder can be ignored because of Package Restore
187 | **/[Pp]ackages/*
188 | # except build/, which is used as an MSBuild target.
189 | !**/[Pp]ackages/build/
190 | # Uncomment if necessary however generally it will be regenerated when needed
191 | #!**/[Pp]ackages/repositories.config
192 | # NuGet v3's project.json files produces more ignorable files
193 | *.nuget.props
194 | *.nuget.targets
195 |
196 | # Microsoft Azure Build Output
197 | csx/
198 | *.build.csdef
199 |
200 | # Microsoft Azure Emulator
201 | ecf/
202 | rcf/
203 |
204 | # Windows Store app package directories and files
205 | AppPackages/
206 | BundleArtifacts/
207 | Package.StoreAssociation.xml
208 | _pkginfo.txt
209 | *.appx
210 |
211 | # Visual Studio cache files
212 | # files ending in .cache can be ignored
213 | *.[Cc]ache
214 | # but keep track of directories ending in .cache
215 | !?*.[Cc]ache/
216 |
217 | # Others
218 | ClientBin/
219 | ~$*
220 | *~
221 | *.dbmdl
222 | *.dbproj.schemaview
223 | *.jfm
224 | *.pfx
225 | *.publishsettings
226 | orleans.codegen.cs
227 |
228 | # Including strong name files can present a security risk
229 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
230 | #*.snk
231 |
232 | # Since there are multiple workflows, uncomment next line to ignore bower_components
233 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
234 | #bower_components/
235 |
236 | # RIA/Silverlight projects
237 | Generated_Code/
238 |
239 | # Backup & report files from converting an old project file
240 | # to a newer Visual Studio version. Backup files are not needed,
241 | # because we have git ;-)
242 | _UpgradeReport_Files/
243 | Backup*/
244 | UpgradeLog*.XML
245 | UpgradeLog*.htm
246 | ServiceFabricBackup/
247 | *.rptproj.bak
248 |
249 | # SQL Server files
250 | *.mdf
251 | *.ldf
252 | *.ndf
253 |
254 | # Business Intelligence projects
255 | *.rdl.data
256 | *.bim.layout
257 | *.bim_*.settings
258 | *.rptproj.rsuser
259 | *- Backup*.rdl
260 |
261 | # Microsoft Fakes
262 | FakesAssemblies/
263 |
264 | # GhostDoc plugin setting file
265 | *.GhostDoc.xml
266 |
267 | # Node.js Tools for Visual Studio
268 | .ntvs_analysis.dat
269 | node_modules/
270 |
271 | # Visual Studio 6 build log
272 | *.plg
273 |
274 | # Visual Studio 6 workspace options file
275 | *.opt
276 |
277 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
278 | *.vbw
279 |
280 | # Visual Studio LightSwitch build output
281 | **/*.HTMLClient/GeneratedArtifacts
282 | **/*.DesktopClient/GeneratedArtifacts
283 | **/*.DesktopClient/ModelManifest.xml
284 | **/*.Server/GeneratedArtifacts
285 | **/*.Server/ModelManifest.xml
286 | _Pvt_Extensions
287 |
288 | # Paket dependency manager
289 | .paket/paket.exe
290 | paket-files/
291 |
292 | # FAKE - F# Make
293 | .fake/
294 |
295 | # JetBrains Rider
296 | .idea/
297 | *.sln.iml
298 |
299 | # CodeRush personal settings
300 | .cr/personal
301 |
302 | # Python Tools for Visual Studio (PTVS)
303 | __pycache__/
304 | *.pyc
305 |
306 | # Cake - Uncomment if you are using it
307 | # tools/**
308 | # !tools/packages.config
309 |
310 | # Tabs Studio
311 | *.tss
312 |
313 | # Telerik's JustMock configuration file
314 | *.jmconfig
315 |
316 | # BizTalk build output
317 | *.btp.cs
318 | *.btm.cs
319 | *.odx.cs
320 | *.xsd.cs
321 |
322 | # OpenCover UI analysis results
323 | OpenCover/
324 |
325 | # Azure Stream Analytics local run output
326 | ASALocalRun/
327 |
328 | # MSBuild Binary and Structured Log
329 | *.binlog
330 |
331 | # NVidia Nsight GPU debugger configuration file
332 | *.nvuser
333 |
334 | # MFractors (Xamarin productivity tool) working folder
335 | .mfractor/
336 |
337 | # Local History for Visual Studio
338 | .localhistory/
339 |
340 | # BeatPulse healthcheck temp database
341 | healthchecksdb
--------------------------------------------------------------------------------
/RCube/RCube.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 15.0
23 | {471B88D1-6DCC-4FCF-85FF-3E1D293395D0}
24 | RCube
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | MultiByte
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | MultiByte
40 |
41 |
42 | Application
43 | true
44 | v142
45 | MultiByte
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 | false
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | $(IncludePath)
75 | $(LibraryPath)
76 |
77 |
78 |
79 | Level3
80 | Disabled
81 | true
82 | true
83 |
84 |
85 | %(AdditionalDependencies)
86 | LinkVerbose
87 |
88 |
89 |
90 |
91 | Level4
92 | Disabled
93 | true
94 | /wd4996 %(AdditionalOptions)
95 |
96 |
97 |
98 |
99 |
100 |
101 | Level3
102 | MaxSpeed
103 | true
104 | true
105 | true
106 | true
107 |
108 |
109 | true
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | MaxSpeed
117 | true
118 | true
119 | false
120 | true
121 | AnySuitable
122 | Speed
123 | /wd4996 %(AdditionalOptions)
124 | true
125 | false
126 | NoExtensions
127 | Precise
128 | Default
129 | false
130 | Sync
131 | false
132 | true
133 | Default
134 | true
135 | false
136 |
137 |
138 | false
139 | true
140 |
141 |
142 | true
143 | true
144 | UseLinkTimeCodeGeneration
145 | false
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
--------------------------------------------------------------------------------
/RCube/Cube.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include "Face.h"
5 | #include "CubeViewer.h"
6 | #include
7 |
8 | typedef unsigned long long uint64;
9 |
10 | class Cube
11 | {
12 | const byte F = 0x00; //Front
13 | const byte R = 0x01; //Right
14 | const byte B = 0x02; //Back
15 | const byte L = 0x03; //Left
16 | const byte U = 0x04; //Up
17 | const byte D = 0x05; //Down
18 |
19 | uint R1; //Rowsize - 1
20 | uint Mid; //Midpoint
21 | bool IsEven; //Is this an even layered cube
22 |
23 | int Stage; //stage of the solve (used for recovering from a restart)
24 | int QState; //current quadrant being solved
25 | uint Itteration;//itteration of the current stage
26 | bool EdgeState[12];//Current solve state of each egde
27 |
28 | inline void RotateX(uint c, int step);
29 | inline void RotateY(uint c, int step);
30 | inline void RotateZ(uint c, int step);
31 |
32 | const static byte cmap[30][6]; //Parameters for center commutators
33 | const static byte EdgeColorMap[24]; //Pairs of edge colors
34 | const static byte corners[8][3]; //Corner color definitions
35 | const static byte EdgeRotMap[6][4]; //Edge rotation map
36 | public:
37 |
38 | #pragma region Stats and Info
39 |
40 | std::chrono::high_resolution_clock::time_point ProcessStartTime;
41 |
42 | uint64 MoveCount; //Total number of moves made ( rotations of 2 or 3 are counted as 1 )
43 | double Hours; //Total processing hours
44 |
45 | inline double CurrentProcessDuration();
46 |
47 | uint64 PieceCount(); //Number of physical pieces in a cube
48 |
49 | int FrameNumber; //Number to use when exporting a frame
50 | int MoveCounter; //Number of moves since the last frame export
51 | int MovesPerFrame; //Number of moves per exported frame
52 |
53 |
54 | #pragma endregion
55 |
56 | uint RowSize; //size of cube side
57 |
58 | Face *faces; //array of 6 faces
59 |
60 | bool SaveEnabled; //allow saving the cube state
61 |
62 | void Initalize(uint rsize);
63 |
64 | void Cleanup();
65 |
66 | inline void ExportFrame();
67 |
68 | inline void Move(byte f, int d, int q);
69 |
70 | void Reset();
71 |
72 | void Scramble(int);
73 |
74 | void Solve();
75 |
76 | #pragma region Solving Centers
77 |
78 | void SolveCenters();
79 |
80 | void AlignTrueCenters();
81 |
82 | bool IsOpposite(byte src, byte dst);
83 |
84 | void PushCenterPieces(byte src, byte dst, byte color);
85 |
86 | void OptomizedMovement(uint* mstack, int stkptr, uint r, byte src, byte dst, int sq, int dq);
87 |
88 | int FindCommutatorMap(byte src, byte dst);
89 |
90 | #pragma endregion
91 |
92 | #pragma region Solving Edges
93 |
94 | void SolveEdgesOdd();
95 |
96 | void SolveEdgesEven();
97 |
98 | void FlipRightEdge();
99 |
100 | void UnFlipRightEdge();
101 |
102 | void FlipLeftEdge();
103 |
104 | void UnFlipLeftEdge();
105 |
106 | void MoveCenterEdge(bool flipped);
107 |
108 | void GetLeftEdgeColors(int row, byte & l1, byte & l2);
109 |
110 | void GetRightEdgeColors(int row, byte & l1, byte & l2);
111 |
112 | void FixParity(int row);
113 |
114 | void SetDestinationEdge(int e, bool set);
115 |
116 | void SetSourceEdge(int edge, bool set);
117 |
118 | void UpdateEdgeRotation(byte faceid, int steps);
119 |
120 | #pragma endregion
121 |
122 | #pragma region Solving Corners
123 | void SolveCorners();
124 |
125 | void FlipCorners();
126 |
127 | void GetCorner(int cr, byte & c0, byte & c1, byte & c2);
128 |
129 | bool IsCorner(int cr, byte c0, byte c1, byte c2);
130 |
131 | int FindCorner(int cr);
132 | #pragma endregion
133 |
134 | #pragma region Cube State
135 |
136 | void SaveCubeState();
137 | void LoadCubeState();
138 | void PrintStats();
139 |
140 | bool IsCubeSolved();
141 |
142 | #pragma endregion
143 |
144 | Cube(int);
145 | Cube();
146 | ~Cube();
147 | };
148 |
149 | inline void Cube::ExportFrame()
150 | {
151 | std::string name = std::to_string(FrameNumber);
152 | for (byte i = 0; i < 6; i++)
153 | {
154 | std::string filename = "face" + std::to_string(i) + "//F" + std::to_string(i) + "_" + std::string(6 - name.length(), '0') + name + ".png";
155 | std::cout << filename << std::endl;
156 | CubeViewer::ExportFaceDiagram(faces[i], filename, 1000, false);
157 | }
158 | FrameNumber++;
159 | }
160 |
161 | //Rotates a Face at Depth d by q steps
162 | inline void Cube::Move(const byte face, const int depth, const int q)
163 | {
164 | //Keep track of move counts
165 | MoveCount++;
166 |
167 | MoveCounter++;
168 |
169 | if (MovesPerFrame > 0 && MoveCounter >= MovesPerFrame)
170 | {
171 | MoveCounter = 0;
172 | //ExportFrame();
173 | }
174 |
175 | switch (face)
176 | {
177 | case 0: //F
178 | RotateZ(depth, q);
179 | return;
180 | case 1: //R
181 | RotateX(R1 - depth, q);
182 | return;
183 | case 2: //B
184 | RotateZ(R1 - depth, -q);
185 | return;
186 | case 3: //L
187 | RotateX(depth, -q);
188 | return;
189 | case 4: //U
190 | RotateY(R1 - depth, -q);
191 | return;
192 | case 5: //D
193 | RotateY(depth, q);
194 | return;
195 | }
196 | }
197 |
198 | //Rotates a slice in the Y-Z plane (Left and Right faces) by (1,2,3,-1,-2,-3)
199 | inline void Cube::RotateX(const uint index, const int step)
200 | {
201 | if (index == 0) faces[3].RotatefaceCW(-step);
202 | if (index == R1) faces[1].RotatefaceCW(step);
203 |
204 | byte b[4];
205 | byte* f0, * f4, * f2, * f5;
206 | uint p0, p4, p2, p5;
207 | int d0, d4, d2, d5;
208 | int i0, i1, i2, i3;
209 |
210 | f0 = faces[0].data;
211 | f4 = faces[4].data;
212 | f2 = faces[2].data;
213 | f5 = faces[5].data;
214 |
215 | p0 = faces[0].GetPos(0, index);
216 | p4 = faces[4].GetPos(0, index);
217 | p2 = faces[2].GetPos(R1, R1 - index);
218 | p5 = faces[5].GetPos(0, index);
219 |
220 | d0 = faces[0].GetDelta(3);
221 | d4 = faces[4].GetDelta(3);
222 | d2 = faces[2].GetDelta(1);
223 | d5 = faces[5].GetDelta(3);
224 |
225 | i0 = (0 - (step & 3)) & 3;
226 | i1 = (1 - (step & 3)) & 3;
227 | i2 = (2 - (step & 3)) & 3;
228 | i3 = (3 - (step & 3)) & 3;
229 |
230 | uint i = 0;
231 |
232 | while (i < RowSize)
233 | {
234 | b[0] = f0[p0];
235 | b[1] = f4[p4];
236 | b[2] = f2[p2];
237 | b[3] = f5[p5];
238 |
239 | f0[p0] = b[i0];
240 | f4[p4] = b[i1];
241 | f2[p2] = b[i2];
242 | f5[p5] = b[i3];
243 |
244 | p0 += d0;
245 | p4 += d4;
246 | p2 += d2;
247 | p5 += d5;
248 |
249 | i++;
250 | }
251 |
252 | }
253 |
254 | //Rotates a slice in the X-Y plane (Top and Bottom faces) by (1,2,3,-1,-2,-3)
255 | inline void Cube::RotateY(const uint index, const int step)
256 | {
257 | if (index == 0) faces[5].RotatefaceCW(step);
258 | if (index == R1) faces[4].RotatefaceCW(-step);
259 |
260 | byte b[4];
261 | byte* f0, * f1, * f2, * f3;
262 | uint p0, p1, p2, p3;
263 | int d0, d1, d2, d3;
264 | int i0, i1, i2, i3;
265 |
266 | f0 = faces[0].data;
267 | f1 = faces[1].data;
268 | f2 = faces[2].data;
269 | f3 = faces[3].data;
270 |
271 | p0 = faces[0].GetPos(index, 0);
272 | p1 = faces[1].GetPos(index, 0);
273 | p2 = faces[2].GetPos(index, 0);
274 | p3 = faces[3].GetPos(index, 0);
275 |
276 | d0 = faces[0].GetDelta(0);
277 | d1 = faces[1].GetDelta(0);
278 | d2 = faces[2].GetDelta(0);
279 | d3 = faces[3].GetDelta(0);
280 |
281 | i0 = (0 - (step & 3)) & 3;
282 | i1 = (1 - (step & 3)) & 3;
283 | i2 = (2 - (step & 3)) & 3;
284 | i3 = (3 - (step & 3)) & 3;
285 |
286 | uint i = 0;
287 |
288 | while (i < RowSize)
289 | {
290 | b[0] = f0[p0];
291 | b[1] = f1[p1];
292 | b[2] = f2[p2];
293 | b[3] = f3[p3];
294 |
295 | f0[p0] = b[i0];
296 | f1[p1] = b[i1];
297 | f2[p2] = b[i2];
298 | f3[p3] = b[i3];
299 |
300 | p0 += d0;
301 | p1 += d1;
302 | p2 += d2;
303 | p3 += d3;
304 |
305 | i++;
306 | }
307 |
308 | }
309 |
310 | //Rotates a slice in the X-Z plane (Front and Back) by (1,2,3,-1,-2,-3)
311 | inline void Cube::RotateZ(const uint index, const int step)
312 | {
313 | if (index == 0) faces[0].RotatefaceCW(step);
314 | if (index == R1) faces[2].RotatefaceCW(-step);
315 |
316 | byte b[4];
317 | byte* f1, * f5, * f3, * f4;
318 | uint p1, p5, p3, p4;
319 | int d1, d5, d3, d4;
320 | int i0, i1, i2, i3;
321 |
322 | f1 = faces[1].data;
323 | f5 = faces[5].data;
324 | f3 = faces[3].data;
325 | f4 = faces[4].data;
326 |
327 | p1 = faces[1].GetPos(0, index);
328 | p5 = faces[5].GetPos(R1 - index, 0);
329 | p3 = faces[3].GetPos(R1, R1 - index);
330 | p4 = faces[4].GetPos(index, R1);
331 |
332 | d1 = faces[1].GetDelta(3);
333 | d5 = faces[5].GetDelta(0);
334 | d3 = faces[3].GetDelta(1);
335 | d4 = faces[4].GetDelta(2);
336 |
337 | i0 = (0 - (step & 3)) & 3;
338 | i1 = (1 - (step & 3)) & 3;
339 | i2 = (2 - (step & 3)) & 3;
340 | i3 = (3 - (step & 3)) & 3;
341 |
342 | uint i = 0;
343 |
344 | while (i < RowSize)
345 | {
346 | b[0] = f1[p1];
347 | b[1] = f5[p5];
348 | b[2] = f3[p3];
349 | b[3] = f4[p4];
350 |
351 | f1[p1] = b[i0];
352 | f5[p5] = b[i1];
353 | f3[p3] = b[i2];
354 | f4[p4] = b[i3];
355 |
356 | p1 += d1;
357 | p5 += d5;
358 | p3 += d3;
359 | p4 += d4;
360 |
361 | i++;
362 | }
363 |
364 | }
365 |
366 |
367 | //Returns the total number of hours since solve was started or restarted
368 | inline double Cube::CurrentProcessDuration()
369 | {
370 |
371 | std::chrono::duration> ProcessTime = std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - ProcessStartTime);
372 |
373 | return ProcessTime.count();
374 |
375 | }
376 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/RCube/Cube.cpp:
--------------------------------------------------------------------------------
1 | #include "Cube.h"
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | //Pairs of edge colors
11 | const byte Cube::EdgeColorMap[24] =
12 | {
13 | 5,2,
14 | 2,3,
15 | 2,4,
16 | 2,1,
17 | 5,1,
18 | 4,1,
19 | 4,3,
20 | 5,3,
21 | 0,5,
22 | 0,1,
23 | 0,4,
24 | 0,3
25 | };
26 |
27 | //Parameters for center commutators
28 | const byte Cube::cmap[30][6] =
29 | {
30 | {0,1,4,5,3,3},
31 | {0,2,3,1,0,2},
32 | {0,3,5,4,1,1},
33 | {0,4,3,1,0,0},
34 | {0,5,1,3,2,2},
35 | {1,0,5,4,1,1},
36 | {1,2,4,5,3,3},
37 | {1,3,5,4,1,1},
38 | {1,4,0,2,0,1},
39 | {1,5,2,0,2,1},
40 | {2,0,1,3,0,2},
41 | {2,1,5,4,1,1},
42 | {2,3,4,5,3,3},
43 | {2,4,1,3,0,2},
44 | {2,5,3,1,2,0},
45 | {3,0,4,5,3,3},
46 | {3,1,4,5,3,3},
47 | {3,2,5,4,1,1},
48 | {3,4,2,0,0,3},
49 | {3,5,0,2,2,3},
50 | {4,0,1,3,2,2},
51 | {4,1,2,0,3,2},
52 | {4,2,3,1,0,2},
53 | {4,3,0,2,1,2},
54 | {4,5,0,2,1,3},
55 | {5,0,3,1,0,0},
56 | {5,1,0,2,3,0},
57 | {5,2,1,3,2,0},
58 | {5,3,2,0,1,0},
59 | {5,4,2,0,1,3}
60 | };
61 |
62 | //Corner color definitions
63 | const byte Cube::corners[8][3] =
64 | {
65 | {4,0,3},
66 | {4,3,2},
67 | {4,1,2},
68 | {4,0,1},
69 | {5,3,2},
70 | {5,0,3},
71 | {5,0,1},
72 | {5,1,2},
73 | };
74 |
75 | //Edge rotation map - used to keep track of edges as the move during the solve
76 | const byte Cube::EdgeRotMap[6][4] =
77 | {
78 | {8,11,10,9},//F
79 | {3,4,9,5}, //R
80 | {0,3,2,1}, //B
81 | {1,6,11,7}, //L
82 | {2,5,10,6}, //U
83 | {0,7,8,4} //D
84 | };
85 |
86 | void Cube::Initalize(uint rsize)
87 | {
88 | //Size of the cube
89 | RowSize = rsize;
90 |
91 | //Size - 1 (used to simplify array indexing)
92 | R1 = RowSize - 1;
93 |
94 | //Mid point = RowSize / 2
95 | Mid = RowSize >> 1;
96 |
97 | //Is the cube an even or odd size
98 | IsEven = (RowSize & 0x01) != 0x01;
99 |
100 | //Force memory size to be a power of 2
101 | int MemSize = (int)pow(2, (ceil(log2(rsize))));
102 |
103 | //Build the faces
104 | faces = new Face[6];
105 | for (char i = 0; i < 6; i++)
106 | {
107 | faces[i].Initialize(i, RowSize, MemSize);
108 | }
109 | }
110 | void Cube::Cleanup()
111 | {
112 | if (faces != nullptr) delete[] faces;
113 | Reset();
114 | }
115 | void Cube::Scramble(int seed)
116 | {
117 | uint rnd;
118 |
119 | //Do the entire process 3 times
120 | for (uint r = 0; r < 3; r++)
121 | {
122 | srand(seed);
123 |
124 | //Rotate a random slice by a random amount
125 | for (uint i = 0; i < 3 * RowSize; i++)
126 | {
127 | rnd = rand() % 3;
128 |
129 | if (rnd == 0) RotateX(rand() % RowSize, (rand() & 3) + 1);
130 | if (rnd == 1) RotateY(rand() % RowSize, (rand() & 3) + 1);
131 | if (rnd == 2) RotateZ(rand() % RowSize, (rand() & 3) + 1);
132 | }
133 |
134 | //Force every row and column to rotate atleast once
135 | for (uint i = 0; i < RowSize; i++)
136 | {
137 | RotateX(i, (rand() & 3) + 1);
138 | RotateY(i, (rand() & 3) + 1);
139 | RotateZ(i, (rand() & 3) + 1);
140 | }
141 |
142 | //Rotate a random slice by a random amount
143 | for (uint i = 0; i < 3 * RowSize; i++)
144 | {
145 | rnd = rand() % 3;
146 |
147 | if (rnd == 0) RotateX(rand() % RowSize, (rand() & 3) + 1);
148 | if (rnd == 1) RotateY(rand() % RowSize, (rand() & 3) + 1);
149 | if (rnd == 2) RotateZ(rand() % RowSize, (rand() & 3) + 1);
150 | }
151 |
152 | seed = (seed + 1) % 0x0FFFFFF;
153 | }
154 |
155 | }
156 |
157 | void Cube::Solve()
158 | {
159 | //Keep track of processing time
160 | ProcessStartTime = std::chrono::high_resolution_clock::now();
161 |
162 | //Cube Size 1 : Trivial case
163 | if (RowSize == 1)
164 | {
165 | MoveCount++;
166 | for (byte i = 0; i < 6; ++i) faces[i].SetRC(0, 0, i);
167 | return;
168 | }
169 |
170 | //Size 2 : Only have to solve corners
171 | if (RowSize == 2)
172 | {
173 | SolveCorners();
174 | return;
175 | }
176 |
177 | //Odd size cubes need to have their center pieces aligned first
178 | AlignTrueCenters();
179 |
180 | //Stage 0 through 14 (solve centers)
181 | if (Stage <= 14)
182 | {
183 | SolveCenters();
184 | }
185 |
186 | //Stage 15 (solve corners)
187 | if (Stage == 15)
188 | {
189 | SaveCubeState();
190 | SolveCorners();
191 | }
192 |
193 | //State 16 (solve edges)
194 | if (Stage == 16)
195 | {
196 | if (IsEven)
197 | {
198 | SolveEdgesEven();
199 | }
200 | else
201 | {
202 | SolveEdgesOdd();
203 | if (!this->IsCubeSolved()) SolveEdgesOdd();
204 | }
205 | }
206 |
207 | //Cube is solved
208 | //Keep track of total hours of processing
209 | Hours += CurrentProcessDuration();
210 | }
211 |
212 | #pragma region Centers
213 |
214 | void Cube::SolveCenters()
215 | {
216 | //Solve each center by 'pushing' pieces to the desired face
217 | //The stages are used to start the solving from a saved state
218 |
219 | //No need to solve centers for cubes less than size 4
220 | if (RowSize < 4)
221 | {
222 | Stage = 15;
223 | return;
224 | }
225 |
226 | //Push R color pieces from F to R
227 | if (Stage == 0)
228 | {
229 | PushCenterPieces(F, R, R);
230 | Stage++;
231 | }
232 |
233 | //Stage = 19; return;
234 |
235 | //Push R color pieces from U to R
236 | if (Stage == 1)
237 | {
238 | SaveCubeState();
239 | PushCenterPieces(U, R, R);
240 | Stage++;
241 | }
242 |
243 | //Push R color pieces from B to R
244 | if (Stage == 2)
245 | {
246 | SaveCubeState();
247 | PushCenterPieces(B, R, R);
248 | Stage++;
249 | }
250 |
251 | //Push R color pieces from L to R
252 | if (Stage == 3)
253 | {
254 | SaveCubeState();
255 | PushCenterPieces(L, R, R);
256 | Stage++;
257 | }
258 |
259 | //Push R color pieces from D to R
260 | if (Stage == 4)
261 | {
262 | SaveCubeState();
263 | PushCenterPieces(D, R, R);
264 | Stage++;
265 | }
266 |
267 | //Push L color pieces from U to L
268 | if (Stage == 5)
269 | {
270 | SaveCubeState();
271 | PushCenterPieces(U, L, L);
272 | Stage++;
273 | }
274 |
275 | //Push L color pieces from D to L
276 | if (Stage == 6)
277 | {
278 | SaveCubeState();
279 | PushCenterPieces(D, L, L);
280 | Stage++;
281 | }
282 |
283 | //Push L color pieces from B to L
284 | if (Stage == 7)
285 | {
286 | SaveCubeState();
287 | PushCenterPieces(B, L, L);
288 | Stage++;
289 | }
290 |
291 | //Push L color pieces from F to L
292 | if (Stage == 8)
293 | {
294 | SaveCubeState();
295 | PushCenterPieces(F, L, L);
296 | Stage++;
297 | }
298 |
299 | //Push F color pieces from B to F
300 | if (Stage == 9)
301 | {
302 | SaveCubeState();
303 | PushCenterPieces(B, F, F);
304 | Stage++;
305 | }
306 |
307 | //Push F color pieces from U to F
308 | if (Stage == 10)
309 | {
310 | SaveCubeState();
311 | PushCenterPieces(U, F, F);
312 | Stage++;
313 | }
314 |
315 | //Push F color pieces from D to F
316 | if (Stage == 11)
317 | {
318 | SaveCubeState();
319 | PushCenterPieces(D, F, F);
320 | Stage++;
321 | }
322 |
323 | //Push D color pieces from U to D
324 | if (Stage == 12)
325 | {
326 | SaveCubeState();
327 | PushCenterPieces(U, D, D);
328 | Stage++;
329 | }
330 |
331 | //Push D color pieces from B to D
332 | if (Stage == 13)
333 | {
334 | SaveCubeState();
335 | PushCenterPieces(B, D, D);
336 | Stage++;
337 | }
338 |
339 | //Push U color pieces from B to U
340 | if (Stage == 14)
341 | {
342 | SaveCubeState();
343 | PushCenterPieces(U, B, B);
344 | Stage++;
345 | }
346 | }
347 |
348 | //Align the center piece of an odd sized cube
349 | void Cube::AlignTrueCenters()
350 | {
351 | if (IsEven) return; //skip this step if its an even size cube
352 |
353 | byte q;
354 |
355 | //Find Front Center piece
356 | for (byte i = 0; i < 6; i++)
357 | {
358 | if (faces[i].GetRC(Mid, Mid) == F) q = i;
359 | }
360 |
361 | //Move Front Center piece to the front
362 | if (q == U) Move(L, Mid, 1);
363 | if (q == D) Move(L, Mid, -1);
364 | if (q == L) Move(U, Mid, -1);
365 | if (q == R) Move(U, Mid, 1);
366 | if (q == B) Move(U, Mid, 2);
367 |
368 | //Find Up Center piece
369 | for (byte i = 0; i < 6; i++)
370 | {
371 | if (faces[i].GetRC(Mid, Mid) == U) q = i;
372 | }
373 |
374 | //Move up to the top
375 | if (q == D) Move(F, Mid, 2);
376 | if (q == L) Move(F, Mid, 1);
377 | if (q == R) Move(F, Mid, -1);
378 | }
379 |
380 | //Return true if src and dst faces are opposites
381 | bool Cube::IsOpposite(byte src, byte dst)
382 | {
383 | if ((src == 0 && dst == 2) || (src == 2 && dst == 0)) return true;
384 | if ((src == 1 && dst == 3) || (src == 3 && dst == 1)) return true;
385 | if ((src == 4 && dst == 5) || (src == 5 && dst == 4)) return true;
386 |
387 | return false;
388 | }
389 |
390 |
391 | //Push center pieces of any color from one face to another using commutators
392 | void Cube::PushCenterPieces(byte src, byte dst, byte color)
393 | {
394 | //Lookup table to find commuator parameters
395 | int map = FindCommutatorMap(src, dst);
396 |
397 | byte srcl = cmap[map][2]; //The face thats 'left' of the src face (in the direction of the destination)
398 | int sq = -(int)cmap[map][4]; //Quadrant to use on the source face
399 | int dq = -(int)cmap[map][5]; //The destination is rotated relative to the source
400 | int d = 1;
401 |
402 | if (IsOpposite(src, dst)) d = 2;
403 |
404 | uint* mstack = new uint[Mid]; //Temporary array to keep track of columns that are being moved
405 |
406 | uint stkptr = 0;
407 | uint pieces = 0;
408 | uint start = Mid;
409 |
410 | //If starting from a save state then set the start point
411 | if (Itteration > 0) start = Itteration;
412 |
413 | for (int quadrant = QState; quadrant < 4; ++quadrant)
414 | {
415 | this->QState = quadrant;
416 |
417 | for (uint r = start; r < R1; ++r)
418 | {
419 | this->Itteration = r;
420 |
421 | if (SaveEnabled)
422 | {
423 | //Save cube state every 1.0 hours
424 | if (CurrentProcessDuration() >= 1.0)
425 | {
426 | SaveCubeState();
427 | }
428 | }
429 |
430 | while (true)
431 | {
432 | pieces = 0;
433 | stkptr = 0;
434 | for (uint c = 1; c < Mid; ++c)
435 | {
436 | if (faces[src].GetRCQ(r, c, sq) == color)
437 | {
438 | pieces++;
439 | if (faces[dst].GetRCQ(r, c, dq) != color)
440 | {
441 | mstack[stkptr++] = c;
442 | }
443 | }
444 | }
445 |
446 | //The row is clear - move on
447 | if (pieces == 0) break;
448 |
449 | //The row is not clear but has no valid moves (rotate the destination face and continue)
450 | if (stkptr <= 0)
451 | {
452 | Move(dst, 0, 1);
453 | continue;
454 | }
455 |
456 | OptomizedMovement(mstack, stkptr, r, src, dst, sq, dq);
457 |
458 | //Move the pieces
459 | //for (uint c = 0; c < stkptr; ++c) Move(srcl, mstack[c], -d);
460 |
461 | //Move(dst, 0, 1);
462 |
463 | //Move(srcl, r, -d);
464 |
465 | //Move(dst, 0, -1);
466 |
467 | //for (uint c = 0; c < stkptr; ++c) Move(srcl, mstack[c], d);
468 |
469 | //Move(dst, 0, 1);
470 |
471 | //Move(srcl, r, d);
472 | }
473 | }
474 |
475 | //reset the start point
476 | start = Mid;
477 |
478 | //Rotate the src face to prepare for the next quadrant
479 | Move(src, 0, 1);
480 | }
481 |
482 | QState = 0;
483 | Itteration = 0;
484 | delete[] mstack;
485 | }
486 |
487 | //Speed up the processing by eliminating uneeded memory swapping
488 | void Cube::OptomizedMovement(uint* mstack, int stkptr, uint r, byte src, byte dst, int sq, int dq)
489 | {
490 | byte a, b;
491 |
492 | for (uint c = 0; c < stkptr; ++c)
493 | {
494 | a = faces[dst].GetRCQ(r, mstack[c], dq);
495 | faces[dst].SetRCQ(r, mstack[c], dq, dst);
496 | b = faces[src].GetRCQ(r, mstack[c], sq - 1);
497 | faces[src].SetRCQ(r, mstack[c], sq - 1, a);
498 | faces[src].SetRCQ(r, mstack[c], sq, b);
499 | }
500 |
501 | MoveCount += (((uint64)stkptr) << 1) + 5;
502 | }
503 |
504 | //Find the correct commuator map
505 | int Cube::FindCommutatorMap(byte src, byte dst)
506 | {
507 | for (int i = 0; i < 30; i++)
508 | if (cmap[i][0] == src && cmap[i][1] == dst) return i;
509 | return 0;
510 | }
511 |
512 | #pragma endregion
513 |
514 | #pragma region Edges
515 |
516 | //Solve the edges for a odd size cube
517 | void Cube::SolveEdgesOdd()
518 | {
519 | byte c0, c1;
520 | byte r0, r1;
521 | byte l0, l1;
522 |
523 | uint* mstack = new uint[RowSize]; //Array to store potential edge pairings
524 | uint mptr;
525 | bool found;
526 |
527 | //Reset the edge solve states
528 | memset(EdgeState, 0, 12);
529 |
530 | //Pre check for edges that have flipped center pieces (avoid parity problems)
531 | for (int de = 0; de < 12; ++de)
532 | {
533 | //Move current edge so that its on the right side front face
534 | SetDestinationEdge(de, true);
535 |
536 | //Fix issue with the right center edge piece being backwards
537 | GetRightEdgeColors(Mid, r0, r1);
538 |
539 | for (int i = 0; i < 12; i++)
540 | {
541 | c0 = EdgeColorMap[2 * i];
542 | c1 = EdgeColorMap[2 * i + 1];
543 |
544 | if (r0 == c1 && r1 == c0)
545 | {
546 | //printf("a %i\n", de);
547 | Move(D, Mid, 1);
548 | FlipRightEdge();
549 | Move(D, Mid, -1);
550 | UnFlipRightEdge();
551 | }
552 | }
553 |
554 |
555 |
556 | SetDestinationEdge(de, false);
557 | }
558 |
559 |
560 | //For each of the 12 edges
561 | for (int de = 0; de < 12; ++de)
562 | {
563 | //Move current edge so that its on the right side front face
564 | SetDestinationEdge(de, true);
565 |
566 | //Get the two colors for this edge
567 | c0 = EdgeColorMap[2 * de];
568 | c1 = EdgeColorMap[2 * de + 1];
569 |
570 | //Loop through all other edges
571 | for (int se = 0; se < 12; ++se)
572 | {
573 | //Skip this edge if its already been solved
574 | if (EdgeState[se]) continue;
575 |
576 | //Move edge so its on the left side of the front face
577 | SetSourceEdge(se, true);
578 |
579 | //Fix issue with the right center edge piece being backwards
580 | GetRightEdgeColors(Mid, r0, r1);
581 | if (r0 == c1 && r1 == c0 && de < 11)
582 | {
583 | //printf("x %i\n", de);
584 | Move(D, Mid, 1);
585 | FlipRightEdge();
586 | Move(D, Mid, -1);
587 | UnFlipRightEdge();
588 | }
589 |
590 | do {
591 | found = false;
592 |
593 | //Step 1a
594 | //Find pieces on the left that can be moved to the right
595 | mptr = 0;
596 | for (uint r = 1; r < R1; ++r)
597 | {
598 | GetLeftEdgeColors(r, l0, l1);
599 | GetRightEdgeColors(r, r0, r1);
600 |
601 | //Piece exists on the left?
602 | if ((l0 == c0 && l1 == c1) || (l0 == c1 && l1 == c0))
603 | {
604 | //No piece exists on the right?
605 | if (!((r0 == c0 && r1 == c1) || (r0 == c1 && r1 == c0)))
606 | {
607 | if (r != Mid) mstack[mptr++] = r;
608 | }
609 | }
610 | }
611 |
612 | //Step 1b
613 | //Move pieces from the left to the right
614 | if (mptr > 0)
615 | {
616 | found = true;
617 | for (uint i = 0; i < mptr; ++i)
618 | {
619 | if (mstack[i] < Mid)
620 | {
621 | Move(D, mstack[i], 1);
622 | Move(D, R1 - mstack[i], 1);
623 | }
624 | }
625 | FlipRightEdge();
626 | for (uint i = 0; i < mptr; ++i)
627 | {
628 | if (mstack[i] < Mid) Move(D, mstack[i], -1);
629 | }
630 | UnFlipRightEdge();
631 | for (uint i = 0; i < mptr; ++i)
632 | {
633 | if (mstack[i] < Mid) Move(D, R1 - mstack[i], -1);
634 | }
635 |
636 |
637 |
638 | for (uint i = 0; i < mptr; ++i)
639 | {
640 | if (mstack[i] >= Mid)
641 | {
642 | Move(D, mstack[i], 1);
643 | Move(D, R1 - mstack[i], 1);
644 | }
645 | }
646 | FlipRightEdge();
647 | for (uint i = 0; i < mptr; ++i)
648 | {
649 | if (mstack[i] >= Mid) Move(D, mstack[i], -1);
650 | }
651 | UnFlipRightEdge();
652 | for (uint i = 0; i < mptr; ++i)
653 | {
654 | if (mstack[i] >= Mid) Move(D, R1 - mstack[i], -1);
655 | }
656 |
657 | }
658 |
659 | //Step 2a
660 | //Find pieces on the left that can be moved to the right
661 | mptr = 0;
662 | for (uint r = 1; r < R1; ++r)
663 | {
664 | GetLeftEdgeColors(r, l0, l1);
665 | GetRightEdgeColors(r, r0, r1);
666 |
667 | //Piece exists on the left?
668 | if ((l0 == c0 && l1 == c1) || (l0 == c1 && l1 == c0))
669 | {
670 | //Also a piece exists on the right?
671 | if (((r0 == c0 && r1 == c1) || (r0 == c1 && r1 == c0)))
672 | {
673 | if (r != Mid) mstack[mptr++] = r;
674 | }
675 | }
676 | }
677 |
678 | //Step 2b
679 | //Move pieces from the left to the right
680 | if (mptr > 0)
681 | {
682 | found = true;
683 | FlipRightEdge();
684 | for (uint i = 0; i < mptr; ++i) { Move(D, mstack[i], 1); }
685 | UnFlipRightEdge();
686 | for (uint i = 0; i < mptr; ++i) { Move(D, mstack[i], -1); }
687 | }
688 |
689 | //Step 3
690 | //Move center edge pieces from left to right
691 | GetLeftEdgeColors(Mid, l0, l1);
692 | if (l0 == c0 && l1 == c1)
693 | {
694 | FlipLeftEdge();
695 | MoveCenterEdge(false);
696 | UnFlipLeftEdge();
697 | }
698 |
699 | if (l0 == c1 && l1 == c0) MoveCenterEdge(false);
700 |
701 | } while (found); //Repeat the process if more additional pieces are brought into the edge after moving
702 |
703 | //Move edge back to its original location
704 | SetSourceEdge(se, false);
705 | }
706 |
707 | //Fix remaining parity issues for this edge
708 | for (uint r = 1; r < Mid; ++r)
709 | {
710 | GetRightEdgeColors(r, r0, r1);
711 | if (r0 == c1 && r1 == c0) FixParity(r);
712 | }
713 |
714 | //Move edge back to the correct face and orientation
715 | SetDestinationEdge(de, false);
716 |
717 | //This edge is now solved
718 | EdgeState[de] = true;
719 | }
720 |
721 | delete[] mstack;
722 | }
723 |
724 | //Solve the edges for a even size cube
725 | void Cube::SolveEdgesEven()
726 | {
727 | byte c0, c1;
728 | byte r0, r1;
729 | byte l0, l1;
730 |
731 | uint* mstack = new uint[RowSize]; //Array to store potential edge pairings
732 | uint mptr;
733 | bool found;
734 |
735 | //Reset the edge solve states
736 | memset(EdgeState, 0, 12);
737 |
738 | //For each of the 12 edges
739 | for (int de = 0; de < 12; ++de)
740 | {
741 | //Move current edge so that its on the right side front face
742 | SetDestinationEdge(de, true);
743 |
744 | //Get the two colors for this edge
745 | c0 = EdgeColorMap[2 * de];
746 | c1 = EdgeColorMap[2 * de + 1];
747 |
748 | //Loop through all other edges
749 | for (int se = 0; se < 12; ++se)
750 | {
751 | //Skip this edge if its already been solved
752 | if (EdgeState[se]) continue;
753 |
754 | //Move edge so its on the left side of the front face
755 | SetSourceEdge(se, true);
756 |
757 | do {
758 | found = false;
759 |
760 | //Step 1a
761 | //Find pieces on the left that can be moved to the right
762 | mptr = 0;
763 | for (uint r = 1; r < R1; ++r)
764 | {
765 | GetLeftEdgeColors(r, l0, l1);
766 | GetRightEdgeColors(r, r0, r1);
767 |
768 | //Piece exists on the left?
769 | if ((l0 == c0 && l1 == c1) || (l0 == c1 && l1 == c0))
770 | {
771 | //No piece exists on the right?
772 | if (!((r0 == c0 && r1 == c1) || (r0 == c1 && r1 == c0)))
773 | {
774 | mstack[mptr++] = r;
775 | }
776 | }
777 | }
778 |
779 | //Step 1b
780 | //Move pieces from the left to the right
781 | if (mptr > 0)
782 | {
783 | found = true;
784 | for (uint i = 0; i < mptr; ++i)
785 | {
786 | if (mstack[i] < Mid)
787 | {
788 | Move(D, mstack[i], 1);
789 | Move(D, R1 - mstack[i], 1);
790 | }
791 | }
792 | FlipRightEdge();
793 | for (uint i = 0; i < mptr; ++i)
794 | {
795 | if (mstack[i] < Mid) Move(D, mstack[i], -1);
796 | }
797 | UnFlipRightEdge();
798 | for (uint i = 0; i < mptr; ++i)
799 | {
800 | if (mstack[i] < Mid) Move(D, R1 - mstack[i], -1);
801 | }
802 |
803 |
804 |
805 | for (uint i = 0; i < mptr; ++i)
806 | {
807 | if (mstack[i] >= Mid)
808 | {
809 | Move(D, mstack[i], 1);
810 | Move(D, R1 - mstack[i], 1);
811 | }
812 | }
813 | FlipRightEdge();
814 | for (uint i = 0; i < mptr; ++i)
815 | {
816 | if (mstack[i] >= Mid) Move(D, mstack[i], -1);
817 | }
818 | UnFlipRightEdge();
819 | for (uint i = 0; i < mptr; ++i)
820 | {
821 | if (mstack[i] >= Mid) Move(D, R1 - mstack[i], -1);
822 | }
823 |
824 | }
825 |
826 | //Step 2a
827 | //Find pieces on the left that can be moved to the right
828 | mptr = 0;
829 | for (uint r = 1; r < R1; ++r)
830 | {
831 | GetLeftEdgeColors(r, l0, l1);
832 | GetRightEdgeColors(r, r0, r1);
833 |
834 | //Piece exists on the left?
835 | if ((l0 == c0 && l1 == c1) || (l0 == c1 && l1 == c0))
836 | {
837 | //Also a piece exists on the right?
838 | if (((r0 == c0 && r1 == c1) || (r0 == c1 && r1 == c0)))
839 | {
840 | mstack[mptr++] = r;
841 | }
842 | }
843 | }
844 |
845 | //Step 2b
846 | //Move pieces from the left to the right
847 | if (mptr > 0 && mptr < RowSize)
848 | {
849 | found = true;
850 | FlipRightEdge();
851 | for (uint i = 0; i < mptr; i++) { Move(D, mstack[i], 1); }
852 | UnFlipRightEdge();
853 | for (uint i = 0; i < mptr; i++) { Move(D, mstack[i], -1); }
854 | }
855 |
856 | } while (found); //Repeat the process if more additional pieces are brought into the edge after moving
857 |
858 | //Move edge back to its original location
859 | SetSourceEdge(se, false);
860 | }
861 |
862 | //Fix remaining parity issues for this edge
863 | for (uint r = 1; r < Mid; ++r)
864 | {
865 | GetRightEdgeColors(r, r0, r1);
866 | if (r0 == c1 && r1 == c0) FixParity(r);
867 | }
868 |
869 | //Move edge back to the correct face and orientation
870 | SetDestinationEdge(de, false);
871 |
872 | //this edge is now solved
873 | EdgeState[de] = true;
874 | }
875 |
876 | delete[] mstack;
877 | }
878 |
879 | //Flips the F-R edge
880 | void Cube::FlipRightEdge()
881 | {
882 | Move(R, 0, 1);
883 | UpdateEdgeRotation(R, 1);
884 | Move(U, 0, 1);
885 | UpdateEdgeRotation(U, 1);
886 | Move(R, 0, -1);
887 | UpdateEdgeRotation(R, -1);
888 | Move(F, 0, 1);
889 | UpdateEdgeRotation(F, 1);
890 | Move(R, 0, -1);
891 | UpdateEdgeRotation(R, -1);
892 | Move(F, 0, -1);
893 | UpdateEdgeRotation(F, -1);
894 | Move(R, 0, 1);
895 | UpdateEdgeRotation(R, 1);
896 | }
897 |
898 | //Un-Flips the F-R edge
899 | void Cube::UnFlipRightEdge()
900 | {
901 | Move(R, 0, -1);
902 | UpdateEdgeRotation(R, -1);
903 | Move(F, 0, 1);
904 | UpdateEdgeRotation(F, 1);
905 | Move(R, 0, 1);
906 | UpdateEdgeRotation(R, 1);
907 | Move(F, 0, -1);
908 | UpdateEdgeRotation(F, -1);
909 | Move(R, 0, 1);
910 | UpdateEdgeRotation(R, 1);
911 | Move(U, 0, -1);
912 | UpdateEdgeRotation(U, -1);
913 | Move(R, 0, -1);
914 | UpdateEdgeRotation(R, -1);
915 | }
916 |
917 | //Flips the F-L edge
918 | void Cube::FlipLeftEdge()
919 | {
920 | Move(L, 0, -1);
921 | UpdateEdgeRotation(L, -1);
922 | Move(U, 0, -1);
923 | UpdateEdgeRotation(U, -1);
924 | Move(L, 0, 1);
925 | UpdateEdgeRotation(L, 1);
926 | Move(F, 0, -1);
927 | UpdateEdgeRotation(F, -1);
928 | Move(L, 0, 1);
929 | UpdateEdgeRotation(L, 1);
930 | Move(F, 0, 1);
931 | UpdateEdgeRotation(F, 1);
932 | Move(L, 0, -1);
933 | UpdateEdgeRotation(L, -1);
934 | }
935 |
936 | //Un Flips the F-L edge
937 | void Cube::UnFlipLeftEdge()
938 | {
939 | Move(L, 0, 1);
940 | UpdateEdgeRotation(L, 1);
941 | Move(F, 0, -1);
942 | UpdateEdgeRotation(F, -1);
943 | Move(L, 0, -1);
944 | UpdateEdgeRotation(L, -1);
945 | Move(F, 0, 1);
946 | UpdateEdgeRotation(F, 1);
947 | Move(L, 0, -1);
948 | UpdateEdgeRotation(L, -1);
949 | Move(U, 0, 1);
950 | UpdateEdgeRotation(U, 1);
951 | Move(L, 0, 1);
952 | UpdateEdgeRotation(L, 1);
953 | }
954 |
955 | //Move the front face center edge from the left side to the right side
956 | void Cube::MoveCenterEdge(bool flipped)
957 | {
958 |
959 | //Find unsolved center edge on the U face 0,1,2,3
960 | int q = -1;
961 |
962 | if (!EdgeState[6]) q = 1;
963 | if (!EdgeState[2]) q = 2;
964 | if (!EdgeState[5]) q = 3;
965 | if (!EdgeState[10]) q = 0;
966 |
967 | if (q >= 0)
968 | {
969 | if (q > 0) Move(U, 0, -q);
970 |
971 | Move(L, Mid, 2);
972 | Move(F, 0, 1);
973 | Move(L, Mid, -1);
974 | Move(F, 0, 2);
975 | Move(L, Mid, 1);
976 | Move(F, 0, 1);
977 | Move(L, Mid, 2);
978 |
979 | if (q > 0) Move(U, 0, q);
980 |
981 | return;
982 | }
983 |
984 | //Find unsolved center edge on the D face 0,1,2,3
985 | q = -1;
986 |
987 | if (!EdgeState[7]) q = 1;
988 | if (!EdgeState[0]) q = 2;
989 | if (!EdgeState[4]) q = 3;
990 | if (!EdgeState[8]) q = 0;
991 |
992 | if (q >= 0)
993 | {
994 | if (q > 0) Move(D, 0, q);
995 |
996 | Move(L, Mid, 2);
997 | Move(F, 0, -1);
998 | Move(L, Mid, 1);
999 | Move(F, 0, 2);
1000 | Move(L, Mid, -1);
1001 | Move(F, 0, -1);
1002 | Move(L, Mid, 2);
1003 |
1004 | if (q > 0) Move(D, 0, -q);
1005 |
1006 | return;
1007 | }
1008 |
1009 | if (!flipped)
1010 | {
1011 | Move(B, 0, 1);
1012 | UpdateEdgeRotation(B, 1);
1013 | MoveCenterEdge(true);
1014 | Move(B, 0, -1);
1015 | UpdateEdgeRotation(B, -1);
1016 | }
1017 | }
1018 |
1019 | //Get the values of an edge piece on the left side of the F face
1020 | void Cube::GetLeftEdgeColors(int row, byte& l0, byte& l1)
1021 | {
1022 | l0 = faces[L].GetRC(row, R1);
1023 | l1 = faces[F].GetRC(row, 0);
1024 | }
1025 |
1026 | //Get the values of an edge piece on the right side of the F face
1027 | void Cube::GetRightEdgeColors(int row, byte& r0, byte& r1)
1028 | {
1029 | r0 = faces[F].GetRC(row, R1);
1030 | r1 = faces[R].GetRC(row, 0);
1031 | }
1032 |
1033 | //Fixes edge parity on a row (front right edge only)
1034 | void Cube::FixParity(int row)
1035 | {
1036 | Move(D, row, -1);
1037 | Move(R, 0, 2);
1038 | Move(U, row, 1);
1039 | Move(F, 0, 2);
1040 | Move(U, row, -1);
1041 | Move(F, 0, 2);
1042 | Move(D, row, 2);
1043 | Move(R, 0, 2);
1044 | Move(D, row, 1);
1045 | Move(R, 0, 2);
1046 | Move(D, row, -1);
1047 | Move(R, 0, 2);
1048 | Move(F, 0, 2);
1049 | Move(D, row, 2);
1050 | Move(F, 0, 2);
1051 | }
1052 |
1053 | //Prepare an edge to be solved - or put the edge back into place set or !set
1054 | void Cube::SetDestinationEdge(int edge, bool set)
1055 | {
1056 | if (set)
1057 | {
1058 | switch (edge)
1059 | {
1060 | case 0: //D-B
1061 | Move(D, 0, -1); UpdateEdgeRotation(D, -1);
1062 | Move(R, 0, 1); UpdateEdgeRotation(R, 1);
1063 | break;
1064 | case 1: //B-L
1065 | Move(B, 0, 2); UpdateEdgeRotation(B, 2);
1066 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1067 | break;
1068 | case 2://B-U
1069 | Move(B, 0, -1); UpdateEdgeRotation(B, -1);
1070 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1071 | break;
1072 | case 3://B-R
1073 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1074 | break;
1075 | case 4://D-R
1076 | Move(R, 0, 1); UpdateEdgeRotation(R, 1);
1077 | break;
1078 | case 5://U-R
1079 | Move(R, 0, -1); UpdateEdgeRotation(R, -1);
1080 | break;
1081 | case 6://U-L
1082 | Move(U, 0, 2); UpdateEdgeRotation(U, 2);
1083 | Move(R, 0, -1); UpdateEdgeRotation(R, -1);
1084 | break;
1085 | case 7://D-L
1086 | Move(D, 0, 2); UpdateEdgeRotation(D, 2);
1087 | Move(R, 0, 1); UpdateEdgeRotation(R, 1);
1088 | break;
1089 | case 8://F-D
1090 | Move(F, 0, -1); UpdateEdgeRotation(F, -1);
1091 | break;
1092 | case 9://F-R
1093 | break;
1094 | case 10://F-U
1095 | Move(F, 0, 1); UpdateEdgeRotation(F, 1);
1096 | break;
1097 | case 11://F-L
1098 | Move(F, 0, 2); UpdateEdgeRotation(F, 2);
1099 | break;
1100 | }
1101 | return;
1102 | }
1103 |
1104 | if (!set)
1105 | {
1106 | switch (edge)
1107 | {
1108 | case 0: //D-B
1109 | Move(R, 0, -1); UpdateEdgeRotation(R, -1);
1110 | Move(D, 0, 1); UpdateEdgeRotation(D, 1);
1111 | break;
1112 | case 1: //B-L
1113 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1114 | Move(B, 0, 2); UpdateEdgeRotation(B, 2);
1115 | break;
1116 | case 2://B-U
1117 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1118 | Move(B, 0, 1); UpdateEdgeRotation(B, 1);
1119 | break;
1120 | case 3://B-R
1121 | Move(R, 0, 2); UpdateEdgeRotation(R, 2);
1122 | break;
1123 | case 4://D-R
1124 | Move(R, 0, -1); UpdateEdgeRotation(R, -1);
1125 | break;
1126 | case 5://U-R
1127 | Move(R, 0, 1); UpdateEdgeRotation(R, 1);
1128 | break;
1129 | case 6://U-L
1130 | Move(R, 0, 1); UpdateEdgeRotation(R, 1);
1131 | Move(U, 0, 2); UpdateEdgeRotation(U, 2);
1132 | break;
1133 | case 7://D-L
1134 | Move(R, 0, -1); UpdateEdgeRotation(R, -1);
1135 | Move(D, 0, 2); UpdateEdgeRotation(D, 2);
1136 | break;
1137 | case 8://F-D
1138 | Move(F, 0, 1); UpdateEdgeRotation(F, 1);
1139 | break;
1140 | case 9://F-R
1141 | break;
1142 | case 10://F-U
1143 | Move(F, 0, -1); UpdateEdgeRotation(F, -1);
1144 | break;
1145 | case 11://F-L
1146 | Move(F, 0, 2); UpdateEdgeRotation(F, 2);
1147 | break;
1148 | }
1149 | }
1150 | }
1151 |
1152 | //Prepare an edge to the source of solved pieces - or put the edge back into place set or !set
1153 | void Cube::SetSourceEdge(int edge, bool set)
1154 | {
1155 | if (set)
1156 | {
1157 | switch (edge)
1158 | {
1159 | case 0: //D-B
1160 | Move(D, 0, 1);
1161 | UpdateEdgeRotation(D, 1);
1162 | Move(L, 0, -1);
1163 | UpdateEdgeRotation(L, -1);
1164 | break;
1165 | case 1: //B-L
1166 | Move(L, 0, 2);
1167 | UpdateEdgeRotation(L, 2);
1168 | break;
1169 | case 2://B-U
1170 | Move(U, 0, -1);
1171 | UpdateEdgeRotation(U, -1);
1172 | Move(L, 0, 1);
1173 | UpdateEdgeRotation(L, 1);
1174 | break;
1175 | case 3://B-R
1176 | Move(B, 0, 2);
1177 | UpdateEdgeRotation(B, 2);
1178 | Move(L, 0, 2);
1179 | UpdateEdgeRotation(L, 2);
1180 | break;
1181 | case 4://D-R
1182 | Move(D, 0, 2);
1183 | UpdateEdgeRotation(D, 2);
1184 | Move(L, 0, -1);
1185 | UpdateEdgeRotation(L, -1);
1186 | break;
1187 | case 5://U-R
1188 | Move(U, 0, 2);
1189 | UpdateEdgeRotation(U, 2);
1190 | Move(L, 0, 1);
1191 | UpdateEdgeRotation(L, 1);
1192 | break;
1193 | case 6://U-L
1194 | Move(L, 0, 1);
1195 | UpdateEdgeRotation(L, 1);
1196 | break;
1197 | case 7://D-L
1198 | Move(L, 0, -1);
1199 | UpdateEdgeRotation(L, -1);
1200 | break;
1201 | case 8://F-D
1202 | Move(D, 0, -1);
1203 | UpdateEdgeRotation(D, -1);
1204 | Move(L, 0, -1);
1205 | UpdateEdgeRotation(L, -1);
1206 | break;
1207 | case 9://F-R
1208 | break;
1209 | case 10://F-U
1210 | Move(U, 0, 1);
1211 | UpdateEdgeRotation(U, 1);
1212 | Move(L, 0, 1);
1213 | UpdateEdgeRotation(L, 1);
1214 | break;
1215 | case 11://F-L
1216 | break;
1217 | }
1218 | return;
1219 | }
1220 |
1221 | if (!set)
1222 | {
1223 | switch (edge)
1224 | {
1225 | case 0: //D-B
1226 | Move(L, 0, 1); UpdateEdgeRotation(L, 1);
1227 | Move(D, 0, -1); UpdateEdgeRotation(D, -1);
1228 | break;
1229 | case 1: //B-L
1230 | Move(L, 0, 2); UpdateEdgeRotation(L, 2);
1231 | break;
1232 | case 2://B-U
1233 | Move(L, 0, -1); UpdateEdgeRotation(L, -1);
1234 | Move(U, 0, 1); UpdateEdgeRotation(U, 1);
1235 | break;
1236 | case 3://B-R
1237 | Move(L, 0, 2); UpdateEdgeRotation(L, 2);
1238 | Move(B, 0, 2); UpdateEdgeRotation(B, 2);
1239 | break;
1240 | case 4://D-R
1241 | Move(L, 0, 1); UpdateEdgeRotation(L, 1);
1242 | Move(D, 0, 2); UpdateEdgeRotation(D, 2);
1243 | break;
1244 | case 5://U-R
1245 | Move(L, 0, -1); UpdateEdgeRotation(L, -1);
1246 | Move(U, 0, 2); UpdateEdgeRotation(U, 2);
1247 | break;
1248 | case 6://U-L
1249 | Move(L, 0, -1); UpdateEdgeRotation(L, -1);
1250 | break;
1251 | case 7://D-L
1252 | Move(L, 0, 1); UpdateEdgeRotation(L, 1);
1253 | break;
1254 | case 8://F-D
1255 | Move(L, 0, 1); UpdateEdgeRotation(L, 1);
1256 | Move(D, 0, 1); UpdateEdgeRotation(D, 1);
1257 | break;
1258 | case 9://F-R
1259 | break;
1260 | case 10://F-U
1261 | Move(L, 0, -1); UpdateEdgeRotation(L, -1);
1262 | Move(U, 0, -1); UpdateEdgeRotation(U, -1);
1263 | break;
1264 | case 11://F-L
1265 | break;
1266 | }
1267 | }
1268 | }
1269 |
1270 | //Keep track of the location of each edge as they are moved around
1271 | //Prevents accidentally disrupting a solved edge
1272 | void Cube::UpdateEdgeRotation(byte faceid, int steps)
1273 | {
1274 | const byte* e = EdgeRotMap[faceid];
1275 |
1276 | if (steps > 0)
1277 | {
1278 | for (int i = 0; i < steps; ++i)
1279 | {
1280 | bool tmp = EdgeState[e[3]];
1281 |
1282 | EdgeState[e[3]] = EdgeState[e[2]];
1283 | EdgeState[e[2]] = EdgeState[e[1]];
1284 | EdgeState[e[1]] = EdgeState[e[0]];
1285 | EdgeState[e[0]] = tmp;
1286 | }
1287 | }
1288 |
1289 | if (steps < 0)
1290 | {
1291 | for (int i = 0; i < abs(steps); ++i)
1292 | {
1293 | bool tmp = EdgeState[e[0]];
1294 | EdgeState[e[0]] = EdgeState[e[1]];
1295 | EdgeState[e[1]] = EdgeState[e[2]];
1296 | EdgeState[e[2]] = EdgeState[e[3]];
1297 | EdgeState[e[3]] = tmp;
1298 | }
1299 | }
1300 |
1301 | }
1302 |
1303 | #pragma endregion
1304 |
1305 | #pragma region Corners
1306 |
1307 | void Cube::SolveCorners()
1308 | {
1309 | int pos = 0;
1310 |
1311 | //Solve the U face corners
1312 | for (int i = 0; i < 4; ++i)
1313 | {
1314 | pos = FindCorner(i);
1315 |
1316 | switch (pos)
1317 | {
1318 | case 0:
1319 | Move(L, 0, 1);
1320 | Move(D, 0, 1);
1321 | Move(L, 0, -1);
1322 | break;
1323 | case 1:
1324 | Move(L, 0, -1);
1325 | Move(D, 0, 2);
1326 | Move(L, 0, 1);
1327 | break;
1328 | case 2:
1329 | Move(R, 0, 1);
1330 | Move(D, 0, 1);
1331 | Move(R, 0, -1);
1332 | Move(D, 0, 2);
1333 | break;
1334 | case 3:
1335 | Move(R, 0, -1);
1336 | Move(D, 0, -1);
1337 | Move(R, 0, 1);
1338 | Move(D, 0, 1);
1339 | break;
1340 | case 4:
1341 | Move(D, 0, 2);
1342 | break;
1343 | case 5:
1344 | Move(D, 0, 1);
1345 | break;
1346 | case 6:
1347 | break;
1348 | case 7:
1349 | Move(D, 0, -1);
1350 | break;
1351 | }
1352 |
1353 | pos = FindCorner(i);
1354 |
1355 | while (!(pos == 3 && faces[U].GetRC(0, R1) == U))
1356 | {
1357 | Move(R, 0, -1);
1358 | Move(D, 0, -1);
1359 | Move(R, 0, 1);
1360 | Move(D, 0, 1);
1361 | pos = FindCorner(i);
1362 | }
1363 |
1364 | if (i < 3) Move(U, 0, -1);
1365 |
1366 | }
1367 |
1368 | //Temporarily move the U face corners to the D face
1369 | Move(L, 0, 2);
1370 | Move(R, 0, 2);
1371 |
1372 |
1373 | //Solve the D face corners
1374 |
1375 | //Put one corner in a known position
1376 | pos = FindCorner(4);
1377 | if (pos == 0) Move(U, 0, -1);
1378 | if (pos == 1) Move(U, 0, 2);
1379 | if (pos == 2) Move(U, 0, 1);
1380 |
1381 | int c[3];
1382 |
1383 | //The remaining corners can end up in 6 different configurations
1384 | c[FindCorner(5)] = 5;
1385 | c[FindCorner(6)] = 6;
1386 | c[FindCorner(7)] = 7;
1387 |
1388 | //Solve each configuration
1389 |
1390 | if (c[0] == 5 && c[1] == 6 && c[2] == 7)
1391 | {
1392 | Move(U, 0, 1);
1393 | }
1394 |
1395 | if (c[0] == 5 && c[1] == 7 && c[2] == 6)
1396 | {
1397 | Move(U, 0, 1);
1398 | FlipCorners();
1399 | FlipCorners();
1400 | Move(U, 0, -1);
1401 | }
1402 |
1403 | if (c[0] == 6 && c[1] == 5 && c[2] == 7)
1404 | {
1405 | Move(U, 0, 2);
1406 | FlipCorners();
1407 | FlipCorners();
1408 | Move(U, 0, 2);
1409 | }
1410 |
1411 | if (c[0] == 6 && c[1] == 7 && c[2] == 5)
1412 | {
1413 | FlipCorners();
1414 | FlipCorners();
1415 | Move(U, 0, 1);
1416 | }
1417 |
1418 | if (c[0] == 7 && c[1] == 5 && c[2] == 6)
1419 | {
1420 | FlipCorners();
1421 | Move(U, 0, 1);
1422 | }
1423 |
1424 | if (c[0] == 7 && c[1] == 6 && c[2] == 5)
1425 | {
1426 | FlipCorners();
1427 | Move(U, 0, -1);
1428 | FlipCorners();
1429 | Move(U, 0, -1);
1430 | }
1431 |
1432 | //Force all of the D face colors in the same direction
1433 | for (int i = 0; i < 4; ++i)
1434 | {
1435 |
1436 | while (faces[U].GetRC(0, R1) != D)
1437 | {
1438 | Move(R, 0, -1);
1439 | Move(D, 0, -1);
1440 | Move(R, 0, 1);
1441 | Move(D, 0, 1);
1442 | }
1443 | Move(U, 0, 1);
1444 | }
1445 |
1446 | //Push the D corners to the D face and bring the U face corners up
1447 | Move(L, 0, 2);
1448 | Move(R, 0, 2);
1449 |
1450 | Stage++;
1451 | }
1452 |
1453 | //Rotate 3 corners on the U face
1454 | void Cube::FlipCorners()
1455 | {
1456 | Move(U, 0, 1);
1457 | Move(R, 0, 1);
1458 | Move(U, 0, -1);
1459 | Move(L, 0, -1);
1460 | Move(U, 0, 1);
1461 | Move(R, 0, -1);
1462 | Move(U, 0, -1);
1463 | Move(L, 0, 1);
1464 | }
1465 |
1466 | //Gets the 3 face colors of a corner
1467 | void Cube::GetCorner(int cr, byte& c0, byte& c1, byte& c2)
1468 | {
1469 | switch (cr)
1470 | {
1471 | case 0:
1472 | c0 = faces[U].GetRC(0, 0);
1473 | c1 = faces[F].GetRC(R1, 0);
1474 | c2 = faces[L].GetRC(R1, R1);
1475 | return;
1476 | case 1:
1477 | c0 = faces[U].GetRC(R1, 0);
1478 | c1 = faces[L].GetRC(R1, 0);
1479 | c2 = faces[B].GetRC(R1, R1);
1480 | return;
1481 | case 2:
1482 | c0 = faces[U].GetRC(R1, R1);
1483 | c1 = faces[R].GetRC(R1, R1);
1484 | c2 = faces[B].GetRC(R1, 0);
1485 | return;
1486 | case 3:
1487 | c0 = faces[U].GetRC(0, R1);
1488 | c1 = faces[F].GetRC(R1, R1);
1489 | c2 = faces[R].GetRC(R1, 0);
1490 | return;
1491 | case 4:
1492 | c0 = faces[D].GetRC(0, 0);
1493 | c1 = faces[L].GetRC(0, 0);
1494 | c2 = faces[B].GetRC(0, R1);
1495 | return;
1496 | case 5:
1497 | c0 = faces[D].GetRC(R1, 0);
1498 | c1 = faces[F].GetRC(0, 0);
1499 | c2 = faces[L].GetRC(0, R1);
1500 | return;
1501 | case 6:
1502 | c0 = faces[D].GetRC(R1, R1);
1503 | c1 = faces[F].GetRC(0, R1);
1504 | c2 = faces[R].GetRC(0, 0);
1505 | return;
1506 | case 7:
1507 | c0 = faces[D].GetRC(0, R1);
1508 | c1 = faces[R].GetRC(0, R1);
1509 | c2 = faces[B].GetRC(0, 0);
1510 | return;
1511 |
1512 | }
1513 | }
1514 |
1515 | //Returns true if the corner in position cr has these three colors
1516 | bool Cube::IsCorner(int cr, byte c0, byte c1, byte c2)
1517 | {
1518 | byte b0, b1, b2;
1519 |
1520 | GetCorner(cr, b0, b1, b2);
1521 |
1522 | return (c0 == b0 || c0 == b1 || c0 == b2) &&
1523 | (c1 == b0 || c1 == b1 || c1 == b2) &&
1524 | (c2 == b0 || c2 == b1 || c2 == b2);
1525 |
1526 | }
1527 |
1528 | //Finds the position of corner cr
1529 | int Cube::FindCorner(int cr)
1530 | {
1531 | for (int i = 0; i < 8; ++i)
1532 | {
1533 | if (IsCorner(i, corners[cr][0], corners[cr][1], corners[cr][2])) return i;
1534 | }
1535 |
1536 | return -1;
1537 | }
1538 |
1539 | #pragma endregion
1540 |
1541 | #pragma region Cube State
1542 |
1543 | void Cube::SaveCubeState()
1544 | {
1545 | if (!SaveEnabled) return;
1546 |
1547 | printf("Saving cube state\n");
1548 |
1549 | //Keep track of total hours of processing
1550 | Hours += CurrentProcessDuration();
1551 |
1552 | printf("Total duration = %.3f hours\n", Hours);
1553 |
1554 | std::ofstream out("cubestate.bin", std::ios::out | std::ios::binary);
1555 | out.write((char*)this, sizeof(Cube));
1556 | out.flush();
1557 | out.close();
1558 |
1559 | for (int i = 0; i < 6; ++i) faces[i].SaveFaceState();
1560 |
1561 | printf("Done saving cube state\n");
1562 |
1563 | //Reset process timer
1564 | ProcessStartTime = std::chrono::high_resolution_clock::now();
1565 | }
1566 |
1567 | void Cube::LoadCubeState()
1568 | {
1569 | printf("Loading cube from file\n");
1570 |
1571 | std::ifstream in("cubestate.bin", std::ios::out | std::ios::binary);
1572 | in.read((char*)this, sizeof(Cube));
1573 | in.close();
1574 |
1575 | faces = new Face[6];
1576 | for (byte b = 0; b < 6; ++b)
1577 | {
1578 | faces[b].LoadFaceState(b);
1579 | }
1580 |
1581 | printf("Done loading cube\n");
1582 |
1583 | }
1584 |
1585 | //Calculate the number of physical pieces
1586 | uint64 Cube::PieceCount()
1587 | {
1588 | if (RowSize <= 1) return 1;
1589 | uint64 result = (uint64)RowSize; //size of a face
1590 | result *= result;
1591 | result *= 6; // 6 faces
1592 | result -= 16; // Corner pieces were counted 3 times
1593 | result -= ((uint64)RowSize - (uint64)2) * 12; //Edge pieces were counted 2 times
1594 | return result;
1595 | }
1596 |
1597 | void Cube::PrintStats()
1598 | {
1599 | printf("\n");
1600 | printf("Cube Size : %i\n", RowSize);
1601 | printf("Total Tiles : %llu\n", ((uint64)RowSize * (uint64)RowSize * 6));
1602 | printf("Total Pieces : %llu\n", PieceCount());
1603 | printf("Total Moves : %llu\n", MoveCount);
1604 | printf("Moves per piece : %f\n", (double)MoveCount / (double)PieceCount());
1605 |
1606 | if (Hours < 1.0)
1607 | {
1608 | double Minutes = Hours * 60.0;
1609 | if (Minutes < 2.0)
1610 | {
1611 | double Seconds = Minutes * 60.0;
1612 | printf("Total Seconds : %.7f\n", Seconds);
1613 | }
1614 | else
1615 | {
1616 | printf("Total Minutes : %.7f\n", Minutes);
1617 | }
1618 | }
1619 | else
1620 | {
1621 | printf("Total Hours : %.7f\n", Hours);
1622 | }
1623 |
1624 |
1625 | if (SaveEnabled)
1626 | {
1627 | printf("Stage : %i\n", Stage);
1628 | printf("Quadrant : %i\n", QState);
1629 | printf("Itteration : %i\n\n", Itteration);
1630 | }
1631 |
1632 | if (IsCubeSolved())
1633 | {
1634 | printf("Cube is solved!\n");
1635 | }
1636 | else
1637 | {
1638 | printf("Cube is NOT solved!\n");
1639 | }
1640 | printf("\n");
1641 | }
1642 |
1643 | //Returns true if all faces are in the solved state
1644 | bool Cube::IsCubeSolved()
1645 | {
1646 | for (int i = 0; i < 6; ++i)
1647 | {
1648 | if (!faces[i].IsFaceSolved()) return false;
1649 | }
1650 |
1651 | return true;
1652 | }
1653 |
1654 | #pragma endregion
1655 |
1656 | void Cube::Reset()
1657 | {
1658 | MoveCount = 0;
1659 | MoveCounter = 0;
1660 | FrameNumber = 0;
1661 | Hours = 0.0;
1662 | memset(EdgeState, 0, 12);
1663 | Stage = 0; //stage of the solve (used for recovering from a restart)
1664 | QState = 0; //current quadrant being solved
1665 | Itteration = 0; //itteration of the current stage
1666 | ProcessStartTime = std::chrono::high_resolution_clock::now(); //Reset process timer
1667 | }
1668 |
1669 | Cube::Cube(int size)
1670 | {
1671 | IsEven = false;
1672 | Mid = 0;
1673 | R1 = 0;
1674 | RowSize = 0;
1675 | faces = nullptr;
1676 | SaveEnabled = false;
1677 | Reset();
1678 | Initalize(size);
1679 | }
1680 |
1681 | Cube::Cube()
1682 | {
1683 | //use this constructor when reloading the cube state
1684 | IsEven = false;
1685 | Mid = 0;
1686 | R1 = 0;
1687 | RowSize = 0;
1688 | faces = nullptr;
1689 | SaveEnabled = false;
1690 | Reset();
1691 | }
1692 |
1693 | Cube::~Cube()
1694 | {
1695 | //Cleanup
1696 | if (faces != nullptr) delete[] faces;
1697 | }
1698 |
--------------------------------------------------------------------------------