Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 固定2D Rubik的旋转';s拼图_C++_Rubiks Cube - Fatal编程技术网

C++ 固定2D Rubik的旋转';s拼图

C++ 固定2D Rubik的旋转';s拼图,c++,rubiks-cube,C++,Rubiks Cube,我正在做一个模拟网格拼图的程序。网格就像是魔方的二维版本。以下是这个谜题的标准: 6 x 6网格 两张脸 用户可以旋转列和行 旋转180度 由两种颜色组成 注意:我用“*”星型字符表示黑色,用“o”表示白色 用户输入侧边(顶部、底部、左侧或右侧)以及要旋转的列数或行数,模拟将模拟移动。 请看一下网格: 当用户旋转时,颜色变为相反的颜色,并根据旋转改变位置 除了轮换,我几乎什么都做了。当用户输入边和行/列编号时,只有颜色反转,颜色的放置不正确 以下是旋转的一个示例: 你能帮我使旋转正常工作吗。

我正在做一个模拟网格拼图的程序。网格就像是魔方的二维版本。以下是这个谜题的标准:

  • 6 x 6网格
  • 两张脸
  • 用户可以旋转列和行
  • 旋转180度
  • 由两种颜色组成
注意:我用“*”星型字符表示黑色,用“o”表示白色

用户输入侧边(顶部、底部、左侧或右侧)以及要旋转的列数或行数,模拟将模拟移动。 请看一下网格:

当用户旋转时,颜色变为相反的颜色,并根据旋转改变位置

除了轮换,我几乎什么都做了。当用户输入边和行/列编号时,只有颜色反转,颜色的放置不正确

以下是旋转的一个示例:

你能帮我使旋转正常工作吗。 先谢谢你

这是我的班级代码

// Library Files
using namespace std;
# include <iostream>    // input and output

#ifndef G_R_I_D
#define G_R_I_D

class grid
{
public:
    int input();                                        // side and num
    int flipTop();
    int flipBottom();
    int flipRight();
    int flipLeft();
    void initBoard();                                   // Create blank board
    void printBoard();                                  // print board

private:
    char board [6][6];                                  // board
    int num;                                            // number of rows/ columns from side
};

/******************
*Member Definition*
******************/
int grid::input()
{
    cout << "# of Rows /Columns: ";                     // Indicates num of rows or columns
    cin >> num;
}

int grid::flipRight()
{
    num = 6 - num;                                     // Match input to correct array ex. if right 2, actual array is 4
    for(num; num < 6; num++)                           // Because bottom contains the y axis arrays 3, 4, 5, count up in sequence
    {
        for(int i = 0; i < 6; i++)
        {
            if (board[i][num] == 'o')
            {
                board[i][num] = '*';
            }


            else if (board[i][num] == '*')
            {
                board[i][num] = 'o';
            }
        }
    }
}

int grid::flipLeft()
{
    num = num - 1;                                      // If between arrays 0-2, subtract one from input because first array is 1
    for(num; num >= 0; num--)                           // Because bottom contains the y axis arrays 0, 1, 2, count down in sequence
    {
        for(int i = 0; i < 6; i++)
        {
            if (board[i][num] == 'o')
                board[i][num] = '*';

            else if (board[i][num] == '*')
                board[i][num] = 'o';
        }
    }
}

int grid::flipTop()
{
    num = num - 1;                                      // If between arrays 0-2, subtract one from input because first array is 1
    for(num; num >= 0; num--)                           // Because bottom contains the y axis arrays 0, 1, 2, count down in sequence
    {
        for(int i = 0; i < 6; i++)
            if (board[num][i] == 'o')
                board[num][i] = '*';

            else if (board[num][i] == '*')
                board[num][i] = 'o';
    }
}

int grid::flipBottom()
{
    num = 6 - num;                                       // Match input to correct array ex. if right 2, actual array is 4
    for(num; num < 6; num++)                             // Because bottom contains the y axis arrays 3, 4, 5, count up in sequence
    {
        for(int i = 0; i < 6; i++)
            if (board[num][i] == 'o')
                board[num][i] = '*';

            else if (board[num][i] == '*')
                board[num][i] = 'o';
    }
}

void grid::initBoard()                                // Goes through each 36 array starting with 0,0
{
    for(int y = 0; y < 6; y++)                          // columns
    {
        for (int x = 0; x < 6; x++)                     // rows
        {
            board[y][x] = 'o';                          // assign each array to the char 'o'
        }
    }
}

void grid::printBoard()
{
    for (int y = 0; y < 6; y++)                         // Defining y axis (wait until x axis arrays have printed to continue to next column)
    {
        for (int x = 0; x < 6; x++)                     // Defining x axis (once row is finished, proceed to next column)
        {
            cout << ' ' << board[y][x] << ' ';          // Place space between each character
        }
        cout << endl << endl;                           // New line, next row
    }
}

#endif // G_R_I_D


/*
+---+---+---+---+---+---+
|0,0|0,1|0,2|0,3|0,4|0,5|
+---+---+---+---+---+---+
|1,0|1,1|1,2|1,3|1,4|1,5|
+---+---+---+---+---+---+
|2,0|2,1|2,2|2,3|2,4|2,5|
+---+---+---+---+---+---+
|3,0|3,1|3,2|3,3|3,4|3,5|
+---+---+---+---+---+---+
|4,0|4,1|4,2|4,3|4,4|4,5|
+---+---+---+---+---+---+
|5,0|5,1|5,2|5,3|5,4|5,5|
+---+---+---+---+---+---+
*/
//库文件
使用名称空间std;
#包括//输入和输出
#如果你不知道
#定义G_R_I_D
类网格
{
公众:
int input();//side和num
int flipTop();
int flipBottom();
int flipRight();
int flipLeft();
void initBoard();//创建空白板
void printBoard();//打印板
私人:
字符板[6][6];//字符板
int num;//侧面的行/列数
};
/******************
*成员定义*
******************/
intgrid::input()
{
cout>num;
}
int grid::flipRight()
{
num=6-num;//将输入与正确的数组匹配,例如,如果正确为2,则实际数组为4
对于(num;num<6;num++)//因为底部包含y轴数组3、4、5,所以按顺序向上计数
{
对于(int i=0;i<6;i++)
{
如果(板[i][num]='o')
{
板[i][num]='*';
}
else if(板[i][num]='*')
{
板[i][num]=“o”;
}
}
}
}
int grid::flipLeft()
{
num=num-1;//如果在数组0-2之间,则从输入中减去一,因为第一个数组是1
对于(num;num>=0;num--)//因为底部包含y轴数组0、1、2,所以按顺序倒计时
{
对于(int i=0;i<6;i++)
{
如果(板[i][num]='o')
板[i][num]='*';
else if(板[i][num]='*')
板[i][num]=“o”;
}
}
}
int grid::flipTop()
{
num=num-1;//如果在数组0-2之间,则从输入中减去一,因为第一个数组是1
对于(num;num>=0;num--)//因为底部包含y轴数组0、1、2,所以按顺序倒计时
{
对于(int i=0;i<6;i++)
如果(板[num][i]='o')
板[num][i]='*';
else if(板[num][i]='*')
板[num][i]=“o”;
}
}
int grid::flipBottom()
{
num=6-num;//将输入与正确的数组匹配,例如,如果正确为2,则实际数组为4
对于(num;num<6;num++)//因为底部包含y轴数组3、4、5,所以按顺序向上计数
{
对于(int i=0;i<6;i++)
如果(板[num][i]='o')
板[num][i]='*';
else if(板[num][i]='*')
板[num][i]=“o”;
}
}
void grid::initBoard()//遍历从0,0开始的每个36数组
{
对于(int y=0;y<6;y++)//列
{
对于(int x=0;x<6;x++)//行
{
线路板[y][x]=“o”;//将每个数组分配给字符“o”
}
}
}
void grid::printBoard()
{
for(int y=0;y<6;y++)//定义y轴(等待x轴数组打印完成后,继续下一列)
{
for(int x=0;x<6;x++)//定义x轴(完成行后,转到下一列)
{

cout最简单的方法是认识到将数组旋转180度与在行方向和列方向分别翻转数组一次相同。因此,请执行以下步骤:

  • 获取用户从板阵列中选择的子阵列
  • 做倒转手术
  • 沿行方向翻转子阵列
  • 沿列方向翻转子阵列
  • 将子阵列复制回线路板阵列

  • 对于翻转操作,您可以使用临时数组。顺便说一句,我在这里称之为数组,但您可能希望查看std::array以更方便地处理数据。在那里,您可以使用迭代器和交换函数。

    在正确的旋转示例中,您不是实际旋转了顶部的3行而不是两行吗?否则,我不理解该示例
    // Library Files
    using namespace std;
    # include <iostream>
    # include "grid.h"
    
    // Start of main function
    int main()
    {
    
        // Variables
        char side;
        grid gridBoard;
        // Introduction
        cout << "Grid\n Instructions: This program is a simulation of the puzzle Grid. The Grid grid has four sides and each \n"
             << "side has three rows or columns. To twist a side, enter the side you want to turn and the number of rows or columns. \n"
             << "Sides:\n- (T)op \n- (B)ottom \n- (R)ight \n- (L)eft\n- (E)xit \nColumns/Rows: 1-3\n\n";
    
        // Initialize
        gridBoard.initBoard();
    
        //Rotations
        do
        {
            gridBoard.printBoard();
            cout << "Side: ";
            cin >> side;
            gridBoard.input();
            switch (toupper(side))
            {
            case 'T':
                gridBoard.flipTop();
                break;
            case 'B':
                gridBoard.flipBottom();
                break;
            case 'R':
                waffleBoard.flipRight();
                break;
            case 'L':
                gridBoard.flipLeft();
                break;
            case 'E':
                cout << "Simulation will exit.";
                break;
            default:
                cout << "Invalid input, please try again.\n\n";
                break;
            }
        }
        while (side != 'E');
    
        return 0;
    }