-1

我从这里得到了大部分代码,但我应该使用类。

我想做的是,如果我打电话cube.moveTopc(),我想让它继续工作cube.display()display()将输出一个展开的魔方,然后moveTopc()移动魔方的顶部。

class Rubiks
{
public:

char Top[10];
char Bottom[10];
char Front[10];
char Left[10];
char Back[10];
char Right[10];        

void display() { 

    char topColor = 'B';
    char bottomColor = 'G';
    char frontColor = 'W';
    char backColor = 'Y';
    char leftColor = 'O';
    char rightColor = 'R';
    std::fill (Top, Top + 10, topColor);
    std::fill (Bottom, Bottom + 10, bottomColor);
    std::fill (Front, Front + 10, frontColor);
    std::fill (Back, Back + 10, backColor);
    std::fill (Left, Left + 10, leftColor);
    std::fill (Right, Right + 10, rightColor);   

std::cout<<"                                     "<<Top[0]<<Top[1]<<Top[2]<<"\n";
std::cout<<"                                     "<<Top[3]<<Top[4]<<Top[5] << "\n";
std::cout<< "                                     " << Top[6]<< Top[7] << Top[8]    <<"\n\n";

std::cout<<"                                 " << Left[0] << Left[1] << Left[2];
std::cout<< " " << Front[0] << Front[1] << Front[2];
std::cout<<" " << Right[0] << Right[1] << Right[2] << "\n";

std::cout<< "                                 " << Left[3] << Left[4] << Left[5];
std::cout<< " " <<Front[3] <<Front[4] << Front[5];
std::cout<< " " <<Right[3] << Right[4] <<Right[5]<< "\n";

std::cout<< "                                 " << Left[6] <<Left[7] << Left[8];
std::cout<< " " << Front[6] << Front[7] << Front[8];
std::cout<< " " << Right[6] << Right[7] << Right[8]<<"\n\n";

std::cout<< "                                     " << Bottom[0] << Bottom[1] << Bottom[2] << "\n";
std::cout<< "                                     " << Bottom[3] <<Bottom[4] << Bottom[5] << "\n";
std::cout<< "                                     " << Bottom[6] << Bottom[7] << Bottom[8] << "\n\n";

 std::cout<< "                                     " << Back[6] << Back[7] << Back[8] << "\n";
std::cout<< "                                     " <<Back[3] << Back[4] << Back[5] << "\n";
std::cout<< "                                     " <<Back[0] << Back[1] << Back[2] <<                 "\n\n";

 }

void moveTopc()
{  
 char cornerSave, sideSave;
 char spotSave0, spotSave1, spotSave2;     
     cornerSave = Top[0];
     Top[0] = Top[6];
     Top[6] = Top[8];
     Top[8] = Top[2];
     Top[2] = cornerSave;

     sideSave = Top[1];
     Top[1] = Top[3];
     Top[3] = Top[7];
     Top[7] = Top[5];
     Top[5] = sideSave;

     spotSave0 = Front[0];
     spotSave1 = Front[1];
     spotSave2 = Front[2];
     Front[0] = Right[0];
     Front[1] = Right[1];
     Front[2] = Right[2];
     Right[0] = Back[2];
     Right[1] = Back[1];
     Right[2] = Back[0];
     Back[2] = Left[0];
     Back[1] = Left[1];
     Back[0] = Left[2];
     Left[0] = spotSave0;
     Left[1] = spotSave1;
     Left[2] = spotSave2;
 }   
4

1 回答 1

0

好的,我想我知道您正在尝试这样做。 将立方体的初始化转移到显示构造函数:

Rubiks::Rubiks()
{
char topColor = 'B';
    char bottomColor = 'G';
    char frontColor = 'W';
    char backColor = 'Y';
    char leftColor = 'O';
    char rightColor = 'R';
    std::fill (Top, Top + 10, topColor);
    std::fill (Bottom, Bottom + 10, bottomColor);
    std::fill (Front, Front + 10, frontColor);
    std::fill (Back, Back + 10, backColor);
    std::fill (Left, Left + 10, leftColor);
    std::fill (Right, Right + 10, rightColor); 
}

之后您可以调用 display() 并且输出将如您所愿(转换后立方体的状态

于 2013-02-24T05:27:05.830 回答