Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include <iostream>
- #include <string>
- #include <sstream>
- class Rubik {
- private:
- string Up[3][3];
- string Down[3][3];
- string Left[3][3];
- string Right[3][3];
- string Front[3][3];
- string Back[3][3];
- public:
- void RotateFrontCW() {
- string Temp[3][3], x, y, z;
- for (int i = 0; i < 3; i++) // rotate Front array to Temp array
- for (int j = 0; j < 3; j++)
- Temp[i][j] = Front[2 - j][i];
- for (int i = 0; i < 3; i++) // copy back Temp array to Front array
- for (int j = 0; j < 3; j++)
- Front[i][j] = Temp[i][j];
- // fix other sides
- x = Up[2][0];
- y = Up[2][1];
- z = Up[2][2];
- Up[2][0] = Left[2][2];
- Up[2][1] = Left[1][2];
- Up[2][2] = Left[0][2];
- Left[2][2] = Down[0][2];
- Left[1][2] = Down[0][1];
- Left[0][2] = Down[0][0];
- Down[0][2] = Right[0][0];
- Down[0][1] = Right[1][0];
- Down[0][0] = Right[2][0];
- Right[2][0] = z;
- Right[1][0] = y;
- Right[0][0] = x;
- }
- void SwapCW(string A[][3], const int & times) { // needed for rotating the cube
- string Temp[3][3];
- for (int i = times; i > 0; i--) {
- for (int i = 0; i < 3; i++) // rotate Front array to Temp array
- for (int j = 0; j < 3; j++)
- Temp[i][j] = A[2 - j][i];
- for (int i = 0; i < 3; i++) // copy back Temp array to Front array
- for (int j = 0; j < 3; j++)
- A[i][j] = Temp[i][j];
- }
- }
- void ArrayToArray(string A[][3], string B[][3]) { // A <-- B
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- A[i][j] = B[i][j];
- }
- void BringToFront(const string & which, const int & times) {
- string Temp[3][3];
- if (which == "up") {
- for (int i = times; i > 0; i--) {
- SwapCW(Back, 2);
- ArrayToArray(Temp, Front);
- ArrayToArray(Front, Up);
- ArrayToArray(Up, Back);
- ArrayToArray(Back, Down);
- ArrayToArray(Down, Temp);
- SwapCW(Left, 1);
- SwapCW(Right, 3);
- SwapCW(Back, 2);
- }
- }
- if (which == "left") {
- for (int i = times; i > 0; i--) {
- ArrayToArray(Temp, Front);
- ArrayToArray(Front, Left);
- ArrayToArray(Left, Back);
- ArrayToArray(Back, Right);
- ArrayToArray(Right, Temp);
- SwapCW(Down, 1);
- SwapCW(Up, 3);
- }
- }
- }
- void PrintFront() {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++)
- cout << Front[i][j] << " ";
- cout << endl;
- }
- }
- void FillArray(string A[][3], string fill) {
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- A[i][j] = fill + to_string(i * 3 + j + 1);
- }
- void F(const int & times) {
- for (int i = times; i > 0; i--) {
- RotateFrontCW();
- }
- }
- void B(const int & times) {
- for (int i = times; i > 0; i--) {
- BringToFront("left", 2);
- RotateFrontCW();
- BringToFront("left", 2);
- }
- }
- void L(const int & times) {
- for (int i = times; i > 0; i--) {
- BringToFront("left", 1);
- RotateFrontCW();
- BringToFront("left", 3);
- }
- }
- void R(const int & times) {
- for (int i = times; i > 0; i--) {
- BringToFront("left", 3);
- RotateFrontCW();
- BringToFront("left", 1);
- }
- }
- void U(const int & times) {
- for (int i = times; i > 0; i--) {
- BringToFront("up", 1);
- RotateFrontCW();
- BringToFront("up", 3);
- }
- }
- void D(const int & times) {
- for (int i = times; i > 0; i--) {
- BringToFront("up", 3);
- RotateFrontCW();
- BringToFront("up", 1);
- }
- }
- void ExecuteCommands(const string & cmdLine) {
- string cmd;
- int times;
- stringstream ss(cmdLine);
- while (!ss.eof()) {
- ss >> cmd;
- times = 1;
- if (cmd[1]) {
- if (cmd[1] == '2')
- times = 2;
- else if (cmd[1] == '\'')
- times = 3;
- }
- if (cmd[0] == 'F')
- F(times);
- if (cmd[0] == 'B')
- B(times);
- if (cmd[0] == 'L')
- L(times);
- if (cmd[0] == 'R')
- R(times);
- if (cmd[0] == 'U')
- U(times);
- if (cmd[0] == 'D')
- D(times);
- }
- }
- Rubik() {
- FillArray(Front, "r");
- FillArray(Back, "o");
- FillArray(Up, "y");
- FillArray(Down, "w");
- FillArray(Left, "b");
- FillArray(Right, "g");
- }
- };
- int main()
- {
- Rubik R;
- R.ExecuteCommands("U2 R' D2 R F L' U2 R");
- R.PrintFront();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement