Advertisement
Guest User

Untitled

a guest
Jun 17th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. class Rubik {
  7. private:
  8.     string Up[3][3];
  9.     string Down[3][3];
  10.  
  11.     string Left[3][3];
  12.     string Right[3][3];
  13.  
  14.     string Front[3][3];
  15.     string Back[3][3];
  16.  
  17. public:
  18.     void RotateFrontCW() {
  19.         string Temp[3][3], x, y, z;
  20.         for (int i = 0; i < 3; i++) // rotate Front array to Temp array
  21.         for (int j = 0; j < 3; j++)
  22.             Temp[i][j] = Front[2 - j][i];
  23.  
  24.         for (int i = 0; i < 3; i++) // copy back Temp array to Front array
  25.         for (int j = 0; j < 3; j++)
  26.             Front[i][j] = Temp[i][j];
  27.  
  28.         // fix other sides
  29.         x = Up[2][0];
  30.         y = Up[2][1];
  31.         z = Up[2][2];
  32.  
  33.         Up[2][0] = Left[2][2];
  34.         Up[2][1] = Left[1][2];
  35.         Up[2][2] = Left[0][2];
  36.  
  37.         Left[2][2] = Down[0][2];
  38.         Left[1][2] = Down[0][1];
  39.         Left[0][2] = Down[0][0];
  40.  
  41.         Down[0][2] = Right[0][0];
  42.         Down[0][1] = Right[1][0];
  43.         Down[0][0] = Right[2][0];
  44.  
  45.         Right[2][0] = z;
  46.         Right[1][0] = y;
  47.         Right[0][0] = x;
  48.     }
  49.  
  50.     void SwapCW(string A[][3], const int & times) { // needed for rotating the cube
  51.         string Temp[3][3];
  52.         for (int i = times; i > 0; i--) {
  53.             for (int i = 0; i < 3; i++) // rotate Front array to Temp array
  54.             for (int j = 0; j < 3; j++)
  55.                 Temp[i][j] = A[2 - j][i];
  56.  
  57.             for (int i = 0; i < 3; i++) // copy back Temp array to Front array
  58.             for (int j = 0; j < 3; j++)
  59.                 A[i][j] = Temp[i][j];
  60.         }
  61.     }
  62.  
  63.     void ArrayToArray(string A[][3], string B[][3]) { // A <-- B
  64.         for (int i = 0; i < 3; i++)
  65.         for (int j = 0; j < 3; j++)
  66.             A[i][j] = B[i][j];
  67.     }
  68.  
  69.     void BringToFront(const string & which, const int & times) {
  70.         string Temp[3][3];
  71.         if (which == "up") {
  72.             for (int i = times; i > 0; i--) {
  73.                 SwapCW(Back, 2);
  74.                 ArrayToArray(Temp, Front);
  75.                 ArrayToArray(Front, Up);
  76.                 ArrayToArray(Up, Back);
  77.                 ArrayToArray(Back, Down);
  78.                 ArrayToArray(Down, Temp);
  79.  
  80.                 SwapCW(Left, 1);
  81.                 SwapCW(Right, 3);
  82.                 SwapCW(Back, 2);
  83.             }
  84.         }
  85.  
  86.         if (which == "left") {
  87.             for (int i = times; i > 0; i--) {
  88.                 ArrayToArray(Temp, Front);
  89.                 ArrayToArray(Front, Left);
  90.                 ArrayToArray(Left, Back);
  91.                 ArrayToArray(Back, Right);
  92.                 ArrayToArray(Right, Temp);
  93.  
  94.                 SwapCW(Down, 1);
  95.                 SwapCW(Up, 3);
  96.             }
  97.         }
  98.     }
  99.  
  100.     void PrintFront() {
  101.         for (int i = 0; i < 3; i++) {
  102.             for (int j = 0; j < 3; j++)
  103.                 cout << Front[i][j] << " ";
  104.             cout << endl;
  105.         }
  106.     }
  107.  
  108.     void FillArray(string A[][3], string fill) {
  109.         for (int i = 0; i < 3; i++)
  110.         for (int j = 0; j < 3; j++)
  111.             A[i][j] = fill + to_string(i * 3 + j + 1);
  112.     }
  113.  
  114.     void F(const int & times) {
  115.         for (int i = times; i > 0; i--) {
  116.             RotateFrontCW();
  117.         }
  118.     }
  119.  
  120.     void B(const int & times) {
  121.         for (int i = times; i > 0; i--) {
  122.             BringToFront("left", 2);
  123.             RotateFrontCW();
  124.             BringToFront("left", 2);
  125.         }
  126.     }
  127.  
  128.     void L(const int & times) {
  129.         for (int i = times; i > 0; i--) {
  130.             BringToFront("left", 1);
  131.             RotateFrontCW();
  132.             BringToFront("left", 3);
  133.         }
  134.     }
  135.  
  136.     void R(const int & times) {
  137.         for (int i = times; i > 0; i--) {
  138.             BringToFront("left", 3);
  139.             RotateFrontCW();
  140.             BringToFront("left", 1);
  141.         }
  142.     }
  143.  
  144.     void U(const int & times) {
  145.         for (int i = times; i > 0; i--) {
  146.             BringToFront("up", 1);
  147.             RotateFrontCW();
  148.             BringToFront("up", 3);
  149.         }
  150.     }
  151.  
  152.     void D(const int & times) {
  153.         for (int i = times; i > 0; i--) {
  154.             BringToFront("up", 3);
  155.             RotateFrontCW();
  156.             BringToFront("up", 1);
  157.         }
  158.     }
  159.  
  160.     void ExecuteCommands(const string & cmdLine) {
  161.         string cmd;
  162.         int times;
  163.         stringstream ss(cmdLine);
  164.  
  165.         while (!ss.eof()) {
  166.             ss >> cmd;
  167.             times = 1;
  168.             if (cmd[1]) {
  169.                 if (cmd[1] == '2')
  170.                     times = 2;
  171.                 else if (cmd[1] == '\'')
  172.                     times = 3;
  173.             }
  174.             if (cmd[0] == 'F')
  175.                 F(times);
  176.             if (cmd[0] == 'B')
  177.                 B(times);
  178.             if (cmd[0] == 'L')
  179.                 L(times);
  180.             if (cmd[0] == 'R')
  181.                 R(times);
  182.             if (cmd[0] == 'U')
  183.                 U(times);
  184.             if (cmd[0] == 'D')
  185.                 D(times);
  186.         }
  187.     }
  188.  
  189.     Rubik() {
  190.         FillArray(Front, "r");
  191.         FillArray(Back, "o");
  192.         FillArray(Up, "y");
  193.         FillArray(Down, "w");
  194.         FillArray(Left, "b");
  195.         FillArray(Right, "g");
  196.     }
  197. };
  198.  
  199. int main()
  200. {
  201.     Rubik R;
  202.     R.ExecuteCommands("U2 R' D2 R F L' U2 R");
  203.     R.PrintFront();
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement