Notes
Notes - notes.io |
#include <Windows.h>
using namespace std;
int nScreenWidth = 80;
int nScreenHeight = 30;
const int nMaxDepth = 5;
class Rubik {
public:
wchar_t cCube[6*3*3];
Rubik() : cCube{
L'W',L'W',L'W',L'W',L'W',L'W',L'W',L'W',L'W',
L'G',L'G',L'G',L'G',L'G',L'G',L'G',L'G',L'G',
L'R',L'R',L'R',L'R',L'R',L'R',L'R',L'R',L'R',
L'B',L'B',L'B',L'B',L'B',L'B',L'B',L'B',L'B',
L'O',L'O',L'O',L'O',L'O',L'O',L'O',L'O',L'O',
L'Y',L'Y',L'Y',L'Y',L'Y',L'Y',L'Y',L'Y',L'Y'
}
{}
void exchange(int i1, int i2)
{
wchar_t temp = cCube[i1-1];
cCube[i1-1] = cCube[i2-1];
cCube[i2-1] = temp;
}
void rotateCW(int first)
{
int i = first - 1;
wchar_t temp = cCube[i + 2];
cCube[i + 2] = cCube[i];
wchar_t temp2 = cCube[i + 8];
cCube[i + 8] = temp;
temp = cCube[i + 6];
cCube[i + 6] = temp2;
cCube[i] = temp;
temp = cCube[i + 5];
cCube[i + 5] = cCube[i + 1];
temp2 = cCube[i + 7];
cCube[i + 7] = temp;
temp = cCube[i + 3];
cCube[i + 3] = temp2;
cCube[i + 1] = temp;
}
void F()
{
// Side cubelets
exchange(12, 9); exchange(15, 8); exchange(18, 7);
exchange(12, 46); exchange(15, 47); exchange(18, 48);
exchange(34, 46); exchange(31, 47); exchange(28, 48);
// Rotation
rotateCW(19);
}
void R()
{
// Side cubelets
exchange(43, 3); exchange(40, 6); exchange(37, 9);
exchange(21, 3); exchange(24, 6); exchange(27, 9);
exchange(21, 48); exchange(24, 51); exchange(27, 54);
// Rotation
rotateCW(28);
}
void U()
{
// Side cubelets
exchange(10, 19); exchange(11, 20); exchange(12, 21);
exchange(28, 19); exchange(29, 20); exchange(30, 21);
exchange(28, 37); exchange(29, 38); exchange(30, 39);
// Rotation
rotateCW(1);
}
void B()
{
// Side cubelets
exchange(10, 3); exchange(13, 2); exchange(16, 1);
exchange(36, 3); exchange(33, 2); exchange(30, 1);
exchange(36, 52); exchange(33, 53); exchange(30, 54);
// Rotation
rotateCW(37);
}
void L()
{
// Side cubelets
exchange(39, 52); exchange(42, 49); exchange(45, 46);
exchange(25, 52); exchange(22, 49); exchange(19, 46);
exchange(25, 7); exchange(22, 4); exchange(19, 1);
// Rotation
rotateCW(10);
}
void D()
{
// Side cubelets
exchange(16, 43); exchange(17, 44); exchange(18, 45);
exchange(34, 43); exchange(35, 44); exchange(36, 45);
exchange(34, 25); exchange(35, 26); exchange(36, 27);
// Rotation
rotateCW(46);
}
bool validate() {
for (int i = 0; i < 54; i += 9)
{
wchar_t temp = cCube[i];
for (int j = 1; j < 9; j++)
{
if (temp != cCube[i + j])
return false;;
}
}
return true;
}
void fillScreen(wchar_t* screen)
{
for (int x = 0; x < nScreenWidth; x++)
for (int y = 0; y < nScreenHeight; y++)
{
if (x >= 5 && x < 8)
{
if (y >= 9 && y < 12)
{
screen[y * nScreenWidth + x] = cCube[(y - 9 + 3) * 3 + (x - 5)];
}
}
else if (x >= 9 && x < 12)
{
if (y >= 5 && y < 8)
{
screen[y * nScreenWidth + x] = cCube[(y - 5) * 3 + (x - 9)];
}
else if (y >= 9 && y < 12)
{
screen[y * nScreenWidth + x] = cCube[(y - 5) * 3 + (x - 3)];
}
else if (y >= 13 && y < 16)
{
screen[y * nScreenWidth + x] = cCube[(y - 0) * 3 + (x - 3)];
}
}
else if (x >= 13 && x < 16)
{
if (y >= 9 && y < 12)
{
screen[y * nScreenWidth + x] = cCube[(y - 3) * 3 + (x - 4)];
}
}
else if (x >= 17 && x < 20)
{
if (y >= 9 && y < 12)
{
screen[y * nScreenWidth + x] = cCube[(y - 1) * 3 + (x - 5)];
}
}
else
screen[y * nScreenWidth + x] = L' ';
}
}
};
int main()
{
Rubik cube;
// Create screen buffer
wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
for (int i = 0; i < nScreenWidth * nScreenHeight; i++) screen[i] = L' ';
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
// Shuffle the cube
cube.F(); cube.F(); cube.F();
cube.fillScreen(screen);
// Write to screen buffer
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0, 0 }, &dwBytesWritten);
if (cube.validate())
return 1;
int nMoves[nMaxDepth] = { };
int nCurrent = 0;
bool bFoundSolution = false;
bool bSearchEnd = false;
while (!bFoundSolution)
{
int move = nMoves[nCurrent] / 6;
int rotation = nMoves[nCurrent] % 3;
switch (move)
{
case 0: cube.F(); break;
case 1: cube.R(); break;
case 2: cube.U(); break;
case 3: cube.B(); break;
case 4: cube.L(); break;
case 5: cube.D(); break;
}
if (cube.validate())
goto end;
if (nCurrent < nMaxDepth - 1)
{
nMoves[nCurrent]++;
nCurrent++;
}
else
{
if (nMoves[nCurrent] == 17)
{
nMoves[nCurrent] = 0;
nCurrent--;
}
else
{
}
}
}
while (!bFoundSolution && !bSearchEnd)
{
int move = nMoves[nCurrent] / 6;
int rotation = nMoves[nCurrent] % 3;
switch (move)
{
case 0: cube.F(); break;
case 1: cube.R(); break;
case 2: cube.U(); break;
case 3: cube.B(); break;
case 4: cube.L(); break;
case 5: cube.D(); break;
}
if (cube.validate())
goto end;
if (nCurrent < nMaxDepth - 1)
{
nMoves[nCurrent]++;
nCurrent++;
}
else
{
if (nMoves[nCurrent] == 17)
{
nMoves[nCurrent] = 0;
nCurrent--;
}
else
{
}
}
}
end: screen[0] = L'!';
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0, 0 }, &dwBytesWritten);
return 0;
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team