ben1996123

10/10 rubiks cube simulator

Apr 15th, 2016
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include <iostream>
  2. #include "cube.h"
  3.  
  4. using namespace std;
  5.  
  6. cube::cube(face start_state[6]) {
  7.   int i;
  8.   if (start_state != NULL) {
  9.     for (i=0; i<6; i++) state[i] = start_state[i];
  10.   }
  11.  
  12.   return;
  13. }
  14.  
  15. void cube::printColor(char c) {
  16.   //lowercase
  17.   if (c > 'Z') c -= 'z' - 'Z';
  18.   if ((c < 'E' && c >= 'A') || c == '0') cout << "\x1b[37m" << "[" << c << "]";
  19.   else if ((c < 'I' && c >= 'A') || c == '1') cout << "\x1b[35m" << "[" << c << "]";
  20.   else if ((c < 'M' && c >= 'A') || c == '2') cout << "\x1b[32m" << "[" << c << "]";
  21.   else if ((c < 'Q' && c >= 'A') || c == '3') cout << "\x1b[31m" << "[" << c << "]";
  22.   else if ((c < 'U' && c >= 'A') || c == '4') cout << "\x1b[34m" << "[" << c << "]";
  23.   else cout << "\x1b[33m" << "[" << c << "]";
  24. }
  25.  
  26. void cube::printCube() {
  27.   //sorry this is confusing AF, but it works
  28.   int i;
  29.   int j;
  30.  
  31.   cout << "          ";
  32.   for (j=0; j<9; j++) {
  33.     printColor(*(state[4].CState[j]));
  34.     if (j%3 == 2) cout << endl << "          ";
  35.   }
  36.   cout << endl;
  37.  
  38.   for (i=0; i<3; i++) {
  39.     for (j=0; j<3; j++) printColor(*(state[1].BState[j + 3*i]));
  40.     cout << " ";
  41.     for (j=0; j<3; j++) printColor(*(state[0].AState[j + 3*i]));
  42.     cout << " ";
  43.     for (j=0; j<3; j++) printColor(*(state[3].DState[j + 3*i]));
  44.     cout << endl;
  45.   }
  46.  
  47.   cout << endl << "          ";
  48.   for (j=0; j<9; j++) {
  49.     printColor(*(state[2].AState[j]));
  50.     if (j%3 == 2) cout << endl << "          ";
  51.   }
  52.  
  53.   cout << endl << "          ";
  54.   for (j=0; j<9; j++) {
  55.     printColor(*(state[5].AState[j]));
  56.     if (j%3 == 2) cout << endl << "          ";
  57.   }
  58.  
  59.   cout << "\x1b[37m" << endl;
  60.  
  61.   return;
  62. }
  63.  
  64. void cube::vSlice(char layer) {
  65.   int i;
  66.   char buffer[9];
  67.  
  68.   //drop U into the buffer
  69.   for (i=layer; i<9; i+=3) buffer[i] = *(state[0].AState[i]);
  70.  
  71.   //move F to U
  72.   for (i=layer; i<9; i+=3) *(state[0].AState[i]) = *(state[2].AState[i]);
  73.  
  74.   //move D to F
  75.   for (i=layer; i<9; i+=3) *(state[2].AState[i]) = *(state[5].AState[i]);
  76.  
  77.   //move B to D
  78.   for (i=layer; i<9; i+=3) *(state[5].AState[i]) = *(state[4].CState[i]);
  79.  
  80.   //move F to B
  81.   for (i=layer; i<9; i+=3) *(state[4].CState[i]) = buffer[i];
  82.  
  83.   char rBuffer[9];
  84.   switch (layer) {
  85.     case 0:
  86.       for (i=0; i<9; i++) rBuffer[i] = *(state[1].DState[i]);
  87.       for (i=0; i<9; i++) *(state[1].AState[i]) = rBuffer[i];
  88.       break;
  89.     case 2:
  90.       for (i=0; i<9; i++) rBuffer[i] = *(state[3].BState[i]);
  91.       for (i=0; i<9; i++) *(state[3].AState[i]) = rBuffer[i];
  92.       break;
  93.   }
  94. }
  95.  
  96. void cube::R() {
  97.   vSlice(2);
  98.   return;
  99. }
  100.  
  101. void cube::r() {
  102.   vSlice(2);
  103.   vSlice(1);
  104.   return;
  105. }
  106.  
  107. void cube::Li() {
  108.   vSlice(0);
  109.   return;
  110. }
  111.  
  112. void cube::li() {
  113.   vSlice(0);
  114.   vSlice(1);
  115.   return;
  116. }
  117.  
  118. void cube::Mi() {
  119.   vSlice(1);
  120.   return;
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. #include <iostream>
  136. #include "face.h"
  137.  
  138. using namespace std;
  139.  
  140. int solved[54] = {
  141.   'a', 'A', 'b', 'D', '0', 'B', 'd', 'C', 'c',
  142.   'e', 'E', 'f', 'H', '1', 'F', 'h', 'G', 'g',
  143.   'i', 'I', 'j', 'L', '2', 'J', 'l', 'K', 'k',
  144.   'm', 'M', 'n', 'P', '3', 'N', 'p', 'O', 'o',
  145.   'q', 'Q', 'r', 'T', '4', 'R', 't', 'S', 's',
  146.   'u', 'U', 'v', 'X', '5', 'V', 'x', 'W', 'w'
  147. };
  148.  
  149. face::face(int start_state[9], char face) {
  150.   int i;
  151.  
  152.   //set the initial state
  153.   if (start_state == NULL) {
  154.     face = (face - '0')*9;
  155.     for (i=0; i<9; i++) state[i] = solved[face + i];
  156.   }
  157.   else {
  158.     for (i=0; i<9; i++) state[i] = start_state[i];
  159.   }
  160.  
  161.   //set the pointer arrays
  162.   for (i=0; i<9; i++) AState[i] = &(state[i]);
  163.  
  164.   //the rotation of one side and how it switches
  165.   //everything. Makes it easier to use fors
  166.   const char rotate[9] = {6, 3, 0, 7, 4, 1, 8, 5, 2};
  167.  
  168.   //rotate all of the sides using our rotate array
  169.   for (i=0; i<9; i++) BState[i] = AState[rotate[i]];
  170.   for (i=0; i<9; i++) CState[i] = BState[rotate[i]];
  171.   for (i=0; i<9; i++) DState[i] = CState[rotate[i]];
  172.  
  173.   return;
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. #include <iostream>
  187. #include "cube.h"
  188.  
  189. #define SUCCESS 0
  190. #define FAIL 1
  191.  
  192. #define U c.U();
  193. #define Ui c.Ui();
  194. #define D c.D();
  195. #define Di c.Di();
  196. #define R c.R();
  197. #define Ri c.Ri();
  198. #define L c.F();
  199. #define Li c.Fi();
  200. #define F c.F();
  201. #define Fi c.Fi();
  202. #define B c.B();
  203. #define Bi c.Bi();
  204. #define M c.M();
  205. #define Mi c.Mi();
  206.  
  207. using namespace std;
  208.  
  209. int main(int argc, char* argv[]) {
  210.   cube c;
  211.   c.printCube();
  212.   //T perm
  213.   R U Ri Ui Ri F R R Ui Ri Ui R U Ri Fi
  214.   cout << "-------------------" << endl << endl;
  215.   c.printCube();
  216.   //undo
  217.   R U Ri Ui Ri F R R Ui Ri Ui R U Ri Fi
  218.   cout << "-------------------" << endl << endl;
  219.   c.printCube();
  220.   //M perm
  221.   Mi Mi U Mi Mi U U Mi Mi U Mi Mi
  222.   cout << "-------------------" << endl << endl;
  223.   c.printCube();
  224.   //undo
  225.   Mi Mi U Mi Mi U U Mi Mi U Mi Mi
  226.   cout << "-------------------" << endl << endl;
  227.   c.printCube();
  228.   return SUCCESS;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment