Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- * Version 2, December 2004
- *
- * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
- *
- * Work may-2013 piou http://whitehathacking.wordpress.com
- *
- * Everyone is permitted to copy and distribute verbatim or modified
- * copies of this license document, and changing it is allowed as long
- * as the name is changed.
- *
- * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- *
- * 0. You just DO WHAT THE FUCK YOU WANT TO.
- * 1. Just don't be an asshole and pretend you've done this (don't steal).
- * 3. No fucking warranties bitches.
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- enum COLOR {BLANCO,AZUL,AMARILLO,VERDE,NARANJA,ROJO};
- bool check(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3]);
- void apply(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], string move);
- void oneMove(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], char move);
- void oneMovei(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], char move);
- void showCube(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3]);
- int main() {
- string movement;
- char fileIn[40];
- char fileOut[40];
- int contador=0;
- int opt,opt2;
- fstream arIn;
- fstream arOut;
- COLOR U[3][3],F[3][3],D[3][3],B[3][3],R[3][3],L[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- U[i][j]=BLANCO;
- F[i][j]=AZUL;
- D[i][j]=AMARILLO;
- B[i][j]=VERDE;
- R[i][j]=NARANJA;
- L[i][j]=ROJO;
- }
- }
- while (1) {
- cout << endl << endl;
- cout << "Calculadora ordenes de subgrupos ciclicos del Cubo de Rubik (3x3x3):" << endl;
- cout << endl << "1.- Introducir movimiento por teclado" << endl;
- cout << "2.- Introducir movimiento por archivo" << endl;
- cout << "n.- Salir" << endl << endl;
- cin >> opt;
- if (opt == 1) {
- cout << "Notacion: " << endl;
- cout << "Rotaciones de 90º en el sentido de las agujas del reloj:" << endl;
- cout << "U: arriba" << endl << "F: frontal" << endl << "D: abajo" << endl << "B: atras" << endl << "R: derecha" << endl << "L: izquierda" << endl;
- cout << "Xi: movimiento contrario a las agujas del reloj: XXX=Xi" << endl;
- cout << "S para salir" << endl;
- cout << endl << endl;
- while (1) {
- contador=0;
- cin >> movement;
- if (movement[0] == 'S' || movement[0] == 's')
- break;
- do {
- contador++;
- apply(U,F,D,B,R,L,movement);
- //showCube(U,F,D,B,R,L);
- } while (!check(U,F,D,B,R,L));
- cout << "Orden: " << contador << endl;
- }
- }
- if (opt == 2) {
- cout << "Notacion: " << endl;
- cout << "Rotaciones de 90º en el sentido de las agujas del reloj:" << endl;
- cout << "U: arriba" << endl << "F: frontal" << endl << "D: abajo" << endl << "B: atras" << endl << "R: derecha" << endl << "L: izquierda" << endl;
- cout << "Xi: movimiento contrario a las agujas del reloj: XXX=Xi" << endl;
- cout << endl << "El archivo debe tener movimientos separados por lineas" << endl;
- cout << endl << endl;
- cin >> fileIn;
- arIn.open(fileIn);
- if (!arIn) {
- cout << "El archivo no se puede abrir" << endl;
- break;
- }
- cout << "Guardar resultados en archivo (1), o mostrar por pantalla (2): " << endl;
- cin >> opt2;
- if (opt2 == 1) {
- cout << "Nombre del archivo: ";
- cin >> fileOut;
- arOut.open(fileOut,ios::out);
- }
- vector<string> movements;
- string movExt;
- while (getline(arIn,movExt)) {
- movements.push_back(movExt);
- }
- cout << "WOW: " << movements.size() << endl;
- for (int i=0;i<movements.size();i++) {
- contador=0;
- do {
- contador++;
- apply(U,F,D,B,R,L,movements[i]);
- } while (!check(U,F,D,B,R,L));
- if (opt2 == 1) {
- arOut << contador << endl;
- }
- else {
- cout << contador << endl;
- }
- }
- if (opt2 == 1) {
- arOut.close();
- }
- arIn.close();
- }
- else
- break;
- }
- return 0;
- }
- void showCube(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3]) {
- cout << " " << U[0][0] << U[0][1] << U[0][2] << endl;
- cout << " " << U[1][0] << U[1][1] << U[1][2] << endl;
- cout << " " << U[2][0] << U[2][1] << U[2][2] << endl;
- cout << L[0][0] << L[0][1] << L[0][2] << F[0][0] << F[0][1] << F[0][2] << R[0][0] << R[0][1] << R[0][2] << B[0][0] << B[0][1] << B[0][2] << endl;
- cout << L[1][0] << L[1][1] << L[1][2] << F[1][0] << F[1][1] << F[1][2] << R[1][0] << R[1][1] << R[1][2] << B[1][0] << B[1][1] << B[1][2] << endl;
- cout << L[2][0] << L[2][1] << L[2][2] << F[2][0] << F[2][1] << F[2][2] << R[2][0] << R[2][1] << R[2][2] << B[2][0] << B[2][1] << B[2][2] << endl;
- cout << " " << D[0][0] << D[0][1] << D[0][2] << endl;
- cout << " " << D[1][0] << D[1][1] << D[1][2] << endl;
- cout << " " << D[2][0] << D[2][1] << D[2][2] << endl;
- }
- bool check(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3]) {
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- if (U[i][j]!=BLANCO)
- return false;
- if (F[i][j]!=AZUL)
- return false;
- if (D[i][j]!=AMARILLO)
- return false;
- if (B[i][j]!=VERDE)
- return false;
- if (R[i][j]!=NARANJA)
- return false;
- if (L[i][j]!=ROJO)
- return false;
- }
- }
- return true;
- }
- void apply(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], string move) {
- int length = move.size();
- for (int k=0;k<length;k++) {
- if (k!=length-1) {
- if (move[k+1]=='i') {
- oneMovei(U,F,D,B,R,L,move[k]);
- k++;
- }
- else {
- oneMove(U,F,D,B,R,L,move[k]);
- }
- }
- else
- oneMove(U,F,D,B,R,L,move[k]);
- }
- }
- void oneMove(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], char move) {
- switch (move) {
- case 'U':
- {
- COLOR auxU[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxU[i][j]=U[i][j];
- }
- }
- COLOR auxF[3] = {F[0][0],F[0][1],F[0][2]};
- COLOR auxR[3] = {R[0][0],R[0][1],R[0][2]};
- COLOR auxB[3] = {B[0][0],B[0][1],B[0][2]};
- COLOR auxL[3] = {L[0][0],L[0][1],L[0][2]};
- //Asignaciones:
- U[0][0] = auxU[2][0]; U[0][1] = auxU[1][0]; U[0][2] = auxU[0][0];
- U[1][0] = auxU[2][1]; U[1][2] = auxU[0][1];
- U[2][0] = auxU[2][2]; U[2][1] = auxU[1][2]; U[2][2] = auxU[0][2];
- F[0][0] = auxR[0]; F[0][1] = auxR[1]; F[0][2] = auxR[2];
- R[0][0] = auxB[0]; R[0][1] = auxB[1]; R[0][2] = auxB[2];
- B[0][0] = auxL[0]; B[0][1] = auxL[1]; B[0][2] = auxL[2];
- L[0][0] = auxF[0]; L[0][1] = auxF[1]; L[0][2] = auxF[2];
- break;
- }
- case 'F':
- {
- COLOR auxF[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxF[i][j]=F[i][j];
- }
- }
- COLOR auxU[3] = {U[2][0],U[2][1],U[2][2]};
- COLOR auxR[3] = {R[0][0],R[1][0],R[2][0]};
- COLOR auxD[3] = {D[0][0],D[0][1],D[0][2]};
- COLOR auxL[3] = {L[0][2],L[1][2],L[2][2]};
- //Asignaciones:
- F[0][0] = auxF[2][0]; F[0][1] = auxF[1][0]; F[0][2] = auxF[0][0];
- F[1][0] = auxF[2][1]; F[1][2] = auxF[0][1];
- F[2][0] = auxF[2][2]; F[2][1] = auxF[1][2]; F[2][2] = auxF[0][2];
- U[2][0] = auxL[2]; U[2][1] = auxL[1]; U[2][2] = auxL[0];
- L[0][2] = auxD[0]; L[1][2] = auxD[1]; L[2][2] = auxD[2];
- D[0][0] = auxR[2]; D[0][1] = auxR[1]; D[0][2] = auxR[0];
- R[0][0] = auxU[0]; R[1][0] = auxU[1]; R[2][0] = auxU[2];
- break;
- }
- case 'D':
- {
- COLOR auxD[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxD[i][j]=D[i][j];
- }
- }
- COLOR auxF[3] = {F[2][0],F[2][1],F[2][2]};
- COLOR auxR[3] = {R[2][0],R[2][1],R[2][2]};
- COLOR auxB[3] = {B[2][0],B[2][1],B[2][2]};
- COLOR auxL[3] = {L[2][0],L[2][1],L[2][2]};
- //Asignaciones:
- D[0][0] = auxD[2][0]; D[0][1] = auxD[1][0]; D[0][2] = auxD[0][0];
- D[1][0] = auxD[2][1]; D[1][2] = auxD[0][1];
- D[2][0] = auxD[2][2]; D[2][1] = auxD[1][2]; D[2][2] = auxD[0][2];
- F[2][0] = auxL[0]; F[2][1] = auxL[1]; F[2][2] = auxL[2];
- L[2][0] = auxB[0]; L[2][1] = auxB[1]; L[2][2] = auxB[2];
- B[2][0] = auxR[0]; B[2][1] = auxR[1]; B[2][2] = auxR[2];
- R[2][0] = auxF[0]; R[2][1] = auxF[1]; R[2][2] = auxF[2];
- break;
- }
- case 'B':
- {
- COLOR auxB[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxB[i][j]=B[i][j];
- }
- }
- COLOR auxU[3] = {U[0][0],U[0][1],U[0][2]};
- COLOR auxR[3] = {R[0][2],R[1][2],R[2][2]};
- COLOR auxD[3] = {D[2][0],D[2][1],D[2][2]};
- COLOR auxL[3] = {L[0][0],L[1][0],L[2][0]};
- //Asignaciones:
- B[0][0] = auxB[2][0]; B[0][1] = auxB[1][0]; B[0][2] = auxB[0][0];
- B[1][0] = auxB[2][1]; B[1][2] = auxB[0][1];
- B[2][0] = auxB[2][2]; B[2][1] = auxB[1][2]; B[2][2] = auxB[0][2];
- U[0][0] = auxR[0]; U[0][1] = auxR[1]; U[0][2] = auxR[2];
- R[0][2] = auxD[2]; R[1][2] = auxD[1]; R[2][2] = auxD[0];
- D[2][0] = auxL[0]; D[2][1] = auxL[1]; D[2][2] = auxL[2];
- L[0][0] = auxU[2]; L[1][0] = auxU[1]; L[2][0] = auxU[0];
- break;
- }
- case 'R':
- {
- COLOR auxR[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxR[i][j]=R[i][j];
- }
- }
- COLOR auxU[3] = {U[0][2],U[1][2],U[2][2]};
- COLOR auxB[3] = {B[0][0],B[1][0],B[2][0]};
- COLOR auxD[3] = {D[0][2],D[1][2],D[2][2]};
- COLOR auxF[3] = {F[0][2],F[1][2],F[2][2]};
- //Asignaciones:
- R[0][0] = auxR[2][0]; R[0][1] = auxR[1][0]; R[0][2] = auxR[0][0];
- R[1][0] = auxR[2][1]; R[1][2] = auxR[0][1];
- R[2][0] = auxR[2][2]; R[2][1] = auxR[1][2]; R[2][2] = auxR[0][2];
- U[0][2] = auxF[0]; U[1][2] = auxF[1]; U[2][2] = auxF[2];
- F[0][2] = auxD[0]; F[1][2] = auxD[1]; F[2][2] = auxD[2];
- D[0][2] = auxB[2]; D[1][2] = auxB[1]; D[2][2] = auxB[0];
- B[0][0] = auxU[2]; B[1][0] = auxU[1]; B[2][0] = auxU[0];
- break;
- }
- case 'L':
- {
- COLOR auxL[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxL[i][j]=L[i][j];
- }
- }
- COLOR auxU[3] = {U[0][0],U[1][0],U[2][0]};
- COLOR auxB[3] = {B[0][2],B[1][2],B[2][2]};
- COLOR auxD[3] = {D[0][0],D[1][0],D[2][0]};
- COLOR auxF[3] = {F[0][0],F[1][0],F[2][0]};
- //Asignaciones:
- L[0][0] = auxL[2][0]; L[0][1] = auxL[1][0]; L[0][2] = auxL[0][0];
- L[1][0] = auxL[2][1]; L[1][2] = auxL[0][1];
- L[2][0] = auxL[2][2]; L[2][1] = auxL[1][2]; L[2][2] = auxL[0][2];
- U[0][0] = auxB[2]; U[1][0] = auxB[1]; U[2][0] = auxB[0];
- F[0][0] = auxU[0]; F[1][0] = auxU[1]; F[2][0] = auxU[2];
- D[0][0] = auxF[0]; D[1][0] = auxF[1]; D[2][0] = auxF[2];
- B[0][2] = auxD[2]; B[1][2] = auxD[1]; B[2][2] = auxD[0];
- break;
- }
- }
- }
- void oneMovei(COLOR U[][3],COLOR F[][3],COLOR D[][3],COLOR B[][3],COLOR R[][3],COLOR L[][3], char move) {
- switch (move) {
- case 'U':
- {
- COLOR auxU[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxU[i][j]=U[i][j];
- }
- }
- COLOR auxF[3] = {F[0][0],F[0][1],F[0][2]};
- COLOR auxR[3] = {R[0][0],R[0][1],R[0][2]};
- COLOR auxB[3] = {B[0][0],B[0][1],B[0][2]};
- COLOR auxL[3] = {L[0][0],L[0][1],L[0][2]};
- //Asignaciones:
- U[2][0] = auxU[0][0]; U[1][0] = auxU[0][1]; U[0][0] = auxU[0][2];
- U[2][1] = auxU[1][0]; U[0][1] = auxU[1][2];
- U[2][2] = auxU[2][0]; U[1][2] = auxU[2][1]; U[0][2] = auxU[2][2];
- F[0][0] = auxL[0]; F[0][1] = auxL[1]; F[0][2] = auxL[2];
- R[0][0] = auxF[0]; R[0][1] = auxF[1]; R[0][2] = auxF[2];
- B[0][0] = auxR[0]; B[0][1] = auxR[1]; B[0][2] = auxR[2];
- L[0][0] = auxB[0]; L[0][1] = auxB[1]; L[0][2] = auxB[2];
- break;
- }
- case 'F':
- {
- COLOR auxF[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxF[i][j]=F[i][j];
- }
- }
- COLOR auxU[3] = {U[2][0],U[2][1],U[2][2]};
- COLOR auxR[3] = {R[0][0],R[1][0],R[2][0]};
- COLOR auxD[3] = {D[0][0],D[0][1],D[0][2]};
- COLOR auxL[3] = {L[0][2],L[1][2],L[2][2]};
- //Asignaciones:
- F[2][0] = auxF[0][0]; F[1][0] = auxF[0][1]; F[0][0] = auxF[0][2];
- F[2][1] = auxF[1][0]; F[0][1] = auxF[1][2];
- F[2][2] = auxF[2][0]; F[1][2] = auxF[2][1]; F[0][2] = auxF[2][2];
- U[2][0] = auxR[0]; U[2][1] = auxR[1]; U[2][2] = auxR[2];
- L[0][2] = auxU[2]; L[1][2] = auxU[1]; L[2][2] = auxU[0];
- D[0][0] = auxL[0]; D[0][1] = auxL[1]; D[0][2] = auxL[2];
- R[0][0] = auxD[2]; R[1][0] = auxD[1]; R[2][0] = auxD[0];
- break;
- }
- case 'D':
- {
- COLOR auxD[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxD[i][j]=D[i][j];
- }
- }
- COLOR auxF[3] = {F[2][0],F[2][1],F[2][2]};
- COLOR auxR[3] = {R[2][0],R[2][1],R[2][2]};
- COLOR auxB[3] = {B[2][0],B[2][1],B[2][2]};
- COLOR auxL[3] = {L[2][0],L[2][1],L[2][2]};
- //Asignaciones:
- D[2][0] = auxD[0][0]; D[1][0] = auxD[0][1]; D[0][0] = auxD[0][2];
- D[2][1] = auxD[1][0]; D[0][1] = auxD[1][2];
- D[2][2] = auxD[2][0]; D[1][2] = auxD[2][1]; D[0][2] = auxD[2][2];
- F[2][0] = auxR[0]; F[2][1] = auxR[1]; F[2][2] = auxR[2];
- L[2][0] = auxF[0]; L[2][1] = auxF[1]; L[2][2] = auxF[2];
- B[2][0] = auxL[0]; B[2][1] = auxL[1]; B[2][2] = auxL[2];
- R[2][0] = auxB[0]; R[2][1] = auxB[1]; R[2][2] = auxB[2];
- break;
- }
- case 'B':
- {
- COLOR auxB[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxB[i][j]=B[i][j];
- }
- }
- COLOR auxU[3] = {U[0][0],U[0][1],U[0][2]};
- COLOR auxR[3] = {R[0][2],R[1][2],R[2][2]};
- COLOR auxD[3] = {D[2][0],D[2][1],D[2][2]};
- COLOR auxL[3] = {L[0][0],L[1][0],L[2][0]};
- //Asignaciones:
- B[2][0] = auxB[0][0]; B[1][0] = auxB[0][1]; B[0][0] = auxB[0][2];
- B[2][1] = auxB[1][0]; B[0][1] = auxB[1][2];
- B[2][2] = auxB[2][0]; B[1][2] = auxB[2][1]; B[0][2] = auxB[2][2];
- U[0][0] = auxL[2]; U[0][1] = auxL[1]; U[0][2] = auxL[0];
- R[0][2] = auxU[0]; R[1][2] = auxU[1]; R[2][2] = auxU[2];
- D[2][0] = auxR[2]; D[2][1] = auxR[1]; D[2][2] = auxR[0];
- L[0][0] = auxD[0]; L[1][0] = auxD[1]; L[2][0] = auxD[2];
- break;
- }
- case 'R':
- {
- COLOR auxR[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxR[i][j]=R[i][j];
- }
- }
- COLOR auxU[3] = {U[0][2],U[1][2],U[2][2]};
- COLOR auxB[3] = {B[0][0],B[1][0],B[2][0]};
- COLOR auxD[3] = {D[0][2],D[1][2],D[2][2]};
- COLOR auxF[3] = {F[0][2],F[1][2],F[2][2]};
- //Asignaciones:
- R[2][0] = auxR[0][0]; R[1][0] = auxR[0][1]; R[0][0] = auxR[0][2];
- R[2][1] = auxR[1][0]; R[0][1] = auxR[1][2];
- R[2][2] = auxR[2][0]; R[1][2] = auxR[2][1]; R[0][2] = auxR[2][2];
- U[0][2] = auxB[2]; U[1][2] = auxB[1]; U[2][2] = auxB[0];
- F[0][2] = auxU[0]; F[1][2] = auxU[1]; F[2][2] = auxU[2];
- D[0][2] = auxF[0]; D[1][2] = auxF[1]; D[2][2] = auxF[2];
- B[0][0] = auxD[2]; B[1][0] = auxD[1]; B[2][0] = auxD[0];
- break;
- }
- case 'L':
- {
- COLOR auxL[3][3];
- for (int i=0;i<3;i++) {
- for (int j=0;j<3;j++) {
- auxL[i][j]=L[i][j];
- }
- }
- COLOR auxU[3] = {U[0][0],U[1][0],U[2][0]};
- COLOR auxB[3] = {B[0][2],B[1][2],B[2][2]};
- COLOR auxD[3] = {D[0][0],D[1][0],D[2][0]};
- COLOR auxF[3] = {F[0][0],F[1][0],F[2][0]};
- //Asignaciones:
- L[2][0] = auxL[0][0]; L[1][0] = auxL[0][1]; L[0][0] = auxL[0][2];
- L[2][1] = auxL[1][0]; L[0][1] = auxL[1][2];
- L[2][2] = auxL[2][0]; L[1][2] = auxL[2][1]; L[0][2] = auxL[2][2];
- U[0][0] = auxF[0]; U[1][0] = auxF[1]; U[2][0] = auxF[2];
- F[0][0] = auxD[0]; F[1][0] = auxD[1]; F[2][0] = auxD[2];
- D[0][0] = auxB[2]; D[1][0] = auxB[1]; D[2][0] = auxB[0];
- B[0][2] = auxU[2]; B[1][2] = auxU[1]; B[2][2] = auxU[0];
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement